Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "util/base/logging_raw.h"
     18 
     19 #include <stdio.h>
     20 #include <string>
     21 
     22 // NOTE: this file contains two implementations: one for Android, one for all
     23 // other cases.  We always build exactly one implementation.
     24 #if defined(__ANDROID__)
     25 
     26 // Compiled as part of Android.
     27 #include <android/log.h>
     28 
     29 namespace libtextclassifier {
     30 namespace logging {
     31 
     32 namespace {
     33 // Converts LogSeverity to level for __android_log_write.
     34 int GetAndroidLogLevel(LogSeverity severity) {
     35   switch (severity) {
     36     case FATAL:
     37       return ANDROID_LOG_FATAL;
     38     case ERROR:
     39       return ANDROID_LOG_ERROR;
     40     case WARNING:
     41       return ANDROID_LOG_WARN;
     42     case INFO:
     43       return ANDROID_LOG_INFO;
     44     default:
     45       return ANDROID_LOG_DEBUG;
     46   }
     47 }
     48 }  // namespace
     49 
     50 void LowLevelLogging(LogSeverity severity, const std::string& tag,
     51                      const std::string& message) {
     52   const int android_log_level = GetAndroidLogLevel(severity);
     53 #if !defined(TC_DEBUG_LOGGING)
     54   if (android_log_level != ANDROID_LOG_ERROR &&
     55       android_log_level != ANDROID_LOG_FATAL) {
     56     return;
     57   }
     58 #endif
     59   __android_log_write(android_log_level, tag.c_str(), message.c_str());
     60 }
     61 
     62 }  // namespace logging
     63 }  // namespace libtextclassifier
     64 
     65 #else  // if defined(__ANDROID__)
     66 
     67 // Not on Android: implement LowLevelLogging to print to stderr (see below).
     68 namespace libtextclassifier {
     69 namespace logging {
     70 
     71 namespace {
     72 // Converts LogSeverity to human-readable text.
     73 const char *LogSeverityToString(LogSeverity severity) {
     74   switch (severity) {
     75     case INFO:
     76       return "INFO";
     77     case WARNING:
     78       return "WARNING";
     79     case ERROR:
     80       return "ERROR";
     81     case FATAL:
     82       return "FATAL";
     83     default:
     84       return "UNKNOWN";
     85   }
     86 }
     87 }  // namespace
     88 
     89 void LowLevelLogging(LogSeverity severity, const std::string &tag,
     90                      const std::string &message) {
     91   fprintf(stderr, "[%s] %s : %s\n", LogSeverityToString(severity), tag.c_str(),
     92           message.c_str());
     93   fflush(stderr);
     94 }
     95 
     96 }  // namespace logging
     97 }  // namespace libtextclassifier
     98 
     99 #endif  // if defined(__ANDROID__)
    100