1 /*
2 * Copyright 2017 Google LLC
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 "fcp/base/monitoring.h"
18
19 #include <stdlib.h> /* for abort() */
20
21 #include <string>
22
23 #ifndef FCP_BAREMETAL
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #ifdef __ANDROID__
28 #include <android/log.h>
29 #endif
30
31 #include <cstring>
32
33 #include "absl/strings/str_format.h"
34 #endif // FCP_BAREMETAL
35
36 #include "fcp/base/base_name.h"
37
38 namespace fcp {
39
40 namespace internal {
41
42 namespace {
43 #ifdef __ANDROID__
44 constexpr char kAndroidLogTag[] = "fcp";
45
AndroidLogLevel(LogSeverity severity)46 int AndroidLogLevel(LogSeverity severity) {
47 switch (severity) {
48 case LogSeverity::kFatal:
49 return ANDROID_LOG_FATAL;
50 case LogSeverity::kError:
51 return ANDROID_LOG_ERROR;
52 case LogSeverity::kWarning:
53 return ANDROID_LOG_WARN;
54 default:
55 return ANDROID_LOG_INFO;
56 }
57 }
58 #endif
59
60 } // namespace
61
62 // Provides safe static initialization of the default global logger instance.
63 // TODO(team): Improve the logger registration mechanism.
GetGlobalLogger()64 Logger*& GetGlobalLogger() {
65 static Logger* global_logger = new Logger();
66 return global_logger;
67 }
68
logger()69 Logger* logger() { return GetGlobalLogger(); }
set_logger(Logger * logger)70 void set_logger(Logger* logger) { GetGlobalLogger() = logger; }
71
Log(const char * file,int line,LogSeverity severity,const char * message)72 void Logger::Log(const char* file, int line, LogSeverity severity,
73 const char* message) {
74 #ifndef FCP_BAREMETAL
75 auto base_file_name = BaseName(file);
76 #ifdef __ANDROID__
77 bool log_to_logcat = true;
78 #ifdef NDEBUG
79 // We don't log INFO logs on Android if this is a production build, since
80 // they're too verbose. We can't just log them at ANDROID_LOG_VERBOSE either,
81 // since then they'd still show up in the logcat unless we first check
82 // __android_log_is_loggable, but that function isn't available until Android
83 // API level 30. So to keep things simple we only log warnings or above,
84 // unless this is a debug build.
85 log_to_logcat = severity != LogSeverity::kInfo;
86 #endif // NDEBUG
87 if (log_to_logcat) {
88 int level = AndroidLogLevel(severity);
89 __android_log_print(level, kAndroidLogTag, "%c %s:%d %s\n",
90 absl::LogSeverityName(severity)[0],
91 base_file_name.c_str(), line, message);
92 }
93 #endif // __ANDROID__
94 // Note that on Android we print both to logcat *and* stderr. This allows
95 // tests to use ASSERT_DEATH to test for fatal error messages, among other
96 // uses.
97 absl::FPrintF(stderr, "%c %s:%d %s\n", absl::LogSeverityName(severity)[0],
98 base_file_name, line, message);
99 #endif // FCP_BAREMETAL
100 }
101
StatusBuilder(StatusCode code,const char * file,int line)102 StatusBuilder::StatusBuilder(StatusCode code, const char* file, int line)
103 : file_(file), line_(line), code_(code), message_() {}
104
StatusBuilder(StatusBuilder const & other)105 StatusBuilder::StatusBuilder(StatusBuilder const& other)
106 : file_(other.file_),
107 line_(other.line_),
108 code_(other.code_),
109 message_(other.message_.str()) {}
110
operator Status()111 StatusBuilder::operator Status() {
112 auto message_str = message_.str();
113 if (code_ != OK) {
114 StringStream status_message;
115 status_message << "(at " << BaseName(file_) << ":" << line_ << message_str;
116 message_str = status_message.str();
117 if (log_severity_ != kNoLog) {
118 StringStream log_message;
119 log_message << "[" << code_ << "] " << message_str;
120 logger()->Log(file_, line_, log_severity_, log_message.str().c_str());
121 if (log_severity_ == LogSeverity::kFatal) {
122 abort();
123 }
124 }
125 }
126 return Status(code_, message_str);
127 }
128
129 } // namespace internal
130
131 } // namespace fcp
132