1 /*
2  * Copyright (C) 2018 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 "lang_id/common/lite_base/compact-logging-raw.h"
18 
19 #include <stdio.h>
20 
21 #include <string>
22 
23 // NOTE: this file contains two implementations: one for Android, one for all
24 // other cases.  We always build exactly one implementation.
25 #if defined(__ANDROID__)
26 
27 // Compiled as part of Android.
28 #include <android/log.h>
29 
30 namespace libtextclassifier3 {
31 namespace mobile {
32 namespace internal_logging {
33 
34 namespace {
35 // Converts LogSeverity to level for __android_log_write.
GetAndroidLogLevel(LogSeverity severity)36 int GetAndroidLogLevel(LogSeverity severity) {
37   switch (severity) {
38     case FATAL:
39       return ANDROID_LOG_FATAL;
40     case ERROR:
41       return ANDROID_LOG_ERROR;
42     case WARNING:
43       return ANDROID_LOG_WARN;
44     case INFO:
45       return ANDROID_LOG_INFO;
46     default:
47       return ANDROID_LOG_DEBUG;
48   }
49 }
50 }  // namespace
51 
LowLevelLogging(LogSeverity severity,const std::string & tag,const std::string & message)52 void LowLevelLogging(LogSeverity severity, const std::string &tag,
53                      const std::string &message) {
54   const int android_log_level = GetAndroidLogLevel(severity);
55 #if !defined(SAFTM_DEBUG_LOGGING)
56   if (android_log_level != ANDROID_LOG_ERROR &&
57       android_log_level != ANDROID_LOG_FATAL) {
58     return;
59   }
60 #endif
61   __android_log_write(android_log_level, tag.c_str(), message.c_str());
62 }
63 
64 }  // namespace internal_logging
65 }  // namespace mobile
66 }  // namespace nlp_saft
67 
68 #else  // if defined(__ANDROID__)
69 
70 // Not on Android: implement LowLevelLogging to print to stderr (see below).
71 namespace libtextclassifier3 {
72 namespace mobile {
73 namespace internal_logging {
74 
75 namespace {
76 // Converts LogSeverity to human-readable text.
LogSeverityToString(LogSeverity severity)77 const char *LogSeverityToString(LogSeverity severity) {
78   switch (severity) {
79     case INFO:
80       return "INFO";
81     case WARNING:
82       return "WARNING";
83     case ERROR:
84       return "ERROR";
85     case FATAL:
86       return "FATAL";
87     default:
88       return "UNKNOWN";
89   }
90 }
91 }  // namespace
92 
LowLevelLogging(LogSeverity severity,const std::string & tag,const std::string & message)93 void LowLevelLogging(LogSeverity severity, const std::string &tag,
94                      const std::string &message) {
95   fprintf(stderr, "[%s] %s : %s\n", LogSeverityToString(severity), tag.c_str(),
96           message.c_str());
97   fflush(stderr);
98 }
99 
100 }  // namespace internal_logging
101 }  // namespace mobile
102 }  // namespace nlp_saft
103 
104 #endif  // if defined(__ANDROID__)
105