Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Spencer Low | 4759052 | 2015-05-19 22:12:06 -0700 | [diff] [blame] | 17 | #ifdef _WIN32 |
| 18 | #include <windows.h> |
| 19 | #endif |
| 20 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 21 | #include "base/logging.h" |
| 22 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 23 | #include <libgen.h> |
| 24 | |
| 25 | // For getprogname(3) or program_invocation_short_name. |
| 26 | #if defined(__ANDROID__) || defined(__APPLE__) |
| 27 | #include <stdlib.h> |
| 28 | #elif defined(__GLIBC__) |
| 29 | #include <errno.h> |
| 30 | #endif |
| 31 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 32 | #include <iostream> |
| 33 | #include <limits> |
| 34 | #include <sstream> |
| 35 | #include <string> |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 36 | #include <utility> |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 37 | #include <vector> |
| 38 | |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 39 | #ifndef _WIN32 |
| 40 | #include <mutex> |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #include "base/macros.h" |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 44 | #include "base/strings.h" |
Dan Albert | 4a6d4db | 2015-03-20 13:46:28 -0700 | [diff] [blame] | 45 | #include "cutils/threads.h" |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 46 | |
| 47 | // Headers for LogMessage::LogLine. |
| 48 | #ifdef __ANDROID__ |
| 49 | #include <android/set_abort_message.h> |
| 50 | #include "cutils/log.h" |
| 51 | #else |
| 52 | #include <sys/types.h> |
| 53 | #include <unistd.h> |
| 54 | #endif |
| 55 | |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 56 | namespace { |
| 57 | #ifndef _WIN32 |
| 58 | using std::mutex; |
| 59 | using std::lock_guard; |
| 60 | |
| 61 | #if defined(__GLIBC__) |
| 62 | const char* getprogname() { |
| 63 | return program_invocation_short_name; |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | #else |
| 68 | const char* getprogname() { |
| 69 | static bool first = true; |
| 70 | static char progname[MAX_PATH] = {}; |
| 71 | |
| 72 | if (first) { |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 73 | CHAR longname[MAX_PATH]; |
| 74 | DWORD nchars = GetModuleFileNameA(nullptr, longname, arraysize(longname)); |
| 75 | if ((nchars >= arraysize(longname)) || (nchars == 0)) { |
| 76 | // String truncation or some other error. |
| 77 | strcpy(progname, "<unknown>"); |
| 78 | } else { |
| 79 | strcpy(progname, basename(longname)); |
| 80 | } |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 81 | first = false; |
| 82 | } |
| 83 | |
| 84 | return progname; |
| 85 | } |
| 86 | |
| 87 | class mutex { |
| 88 | public: |
| 89 | mutex() { |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 90 | InitializeCriticalSection(&critical_section_); |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 91 | } |
| 92 | ~mutex() { |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 93 | DeleteCriticalSection(&critical_section_); |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void lock() { |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 97 | EnterCriticalSection(&critical_section_); |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void unlock() { |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 101 | LeaveCriticalSection(&critical_section_); |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private: |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 105 | CRITICAL_SECTION critical_section_; |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | template <typename LockT> |
| 109 | class lock_guard { |
| 110 | public: |
| 111 | explicit lock_guard(LockT& lock) : lock_(lock) { |
| 112 | lock_.lock(); |
| 113 | } |
| 114 | |
| 115 | ~lock_guard() { |
| 116 | lock_.unlock(); |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | LockT& lock_; |
| 121 | |
| 122 | DISALLOW_COPY_AND_ASSIGN(lock_guard); |
| 123 | }; |
| 124 | #endif |
| 125 | } // namespace |
| 126 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 127 | namespace android { |
| 128 | namespace base { |
| 129 | |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 130 | static mutex logging_lock; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 131 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 132 | #ifdef __ANDROID__ |
| 133 | static LogFunction gLogger = LogdLogger(); |
| 134 | #else |
| 135 | static LogFunction gLogger = StderrLogger; |
| 136 | #endif |
| 137 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 138 | static bool gInitialized = false; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 139 | static LogSeverity gMinimumLogSeverity = INFO; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 140 | static std::unique_ptr<std::string> gProgramInvocationName; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 141 | |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame^] | 142 | LogSeverity GetMinimumLogSeverity() { |
| 143 | return gMinimumLogSeverity; |
| 144 | } |
| 145 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 146 | static const char* ProgramInvocationName() { |
| 147 | if (gProgramInvocationName == nullptr) { |
| 148 | gProgramInvocationName.reset(new std::string(getprogname())); |
| 149 | } |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 150 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 151 | return gProgramInvocationName->c_str(); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 154 | void StderrLogger(LogId, LogSeverity severity, const char*, const char* file, |
| 155 | unsigned int line, const char* message) { |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 156 | static const char log_characters[] = "VDIWEF"; |
| 157 | static_assert(arraysize(log_characters) - 1 == FATAL + 1, |
| 158 | "Mismatch in size of log_characters and values in LogSeverity"); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 159 | char severity_char = log_characters[severity]; |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 160 | fprintf(stderr, "%s %c %5d %5d %s:%u] %s\n", ProgramInvocationName(), |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 161 | severity_char, getpid(), gettid(), file, line, message); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | #ifdef __ANDROID__ |
| 166 | LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) { |
| 167 | } |
| 168 | |
| 169 | static const android_LogPriority kLogSeverityToAndroidLogPriority[] = { |
| 170 | ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, |
| 171 | ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, |
| 172 | }; |
| 173 | static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1, |
| 174 | "Mismatch in size of kLogSeverityToAndroidLogPriority and values " |
| 175 | "in LogSeverity"); |
| 176 | |
| 177 | static const log_id kLogIdToAndroidLogId[] = { |
| 178 | LOG_ID_MAX, LOG_ID_MAIN, LOG_ID_SYSTEM, |
| 179 | }; |
| 180 | static_assert(arraysize(kLogIdToAndroidLogId) == SYSTEM + 1, |
| 181 | "Mismatch in size of kLogIdToAndroidLogId and values in LogId"); |
| 182 | |
| 183 | void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag, |
| 184 | const char* file, unsigned int line, |
| 185 | const char* message) { |
| 186 | int priority = kLogSeverityToAndroidLogPriority[severity]; |
| 187 | if (id == DEFAULT) { |
| 188 | id = default_log_id_; |
| 189 | } |
| 190 | |
| 191 | log_id lg_id = kLogIdToAndroidLogId[id]; |
| 192 | |
| 193 | if (priority == ANDROID_LOG_FATAL) { |
| 194 | __android_log_buf_print(lg_id, priority, tag, "%s:%u] %s", file, line, |
| 195 | message); |
| 196 | } else { |
| 197 | __android_log_buf_print(lg_id, priority, tag, "%s", message); |
| 198 | } |
| 199 | } |
| 200 | #endif |
| 201 | |
| 202 | void InitLogging(char* argv[], LogFunction&& logger) { |
| 203 | SetLogger(std::forward<LogFunction>(logger)); |
| 204 | InitLogging(argv); |
| 205 | } |
| 206 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 207 | void InitLogging(char* argv[]) { |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 208 | if (gInitialized) { |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 212 | gInitialized = true; |
| 213 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 214 | // Stash the command line for later use. We can use /proc/self/cmdline on |
| 215 | // Linux to recover this, but we don't have that luxury on the Mac, and there |
| 216 | // are a couple of argv[0] variants that are commonly used. |
| 217 | if (argv != nullptr) { |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 218 | gProgramInvocationName.reset(new std::string(basename(argv[0]))); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 219 | } |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 220 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 221 | const char* tags = getenv("ANDROID_LOG_TAGS"); |
| 222 | if (tags == nullptr) { |
| 223 | return; |
| 224 | } |
| 225 | |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 226 | std::vector<std::string> specs = Split(tags, " "); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 227 | for (size_t i = 0; i < specs.size(); ++i) { |
| 228 | // "tag-pattern:[vdiwefs]" |
| 229 | std::string spec(specs[i]); |
| 230 | if (spec.size() == 3 && StartsWith(spec, "*:")) { |
| 231 | switch (spec[2]) { |
| 232 | case 'v': |
| 233 | gMinimumLogSeverity = VERBOSE; |
| 234 | continue; |
| 235 | case 'd': |
| 236 | gMinimumLogSeverity = DEBUG; |
| 237 | continue; |
| 238 | case 'i': |
| 239 | gMinimumLogSeverity = INFO; |
| 240 | continue; |
| 241 | case 'w': |
| 242 | gMinimumLogSeverity = WARNING; |
| 243 | continue; |
| 244 | case 'e': |
| 245 | gMinimumLogSeverity = ERROR; |
| 246 | continue; |
| 247 | case 'f': |
| 248 | gMinimumLogSeverity = FATAL; |
| 249 | continue; |
| 250 | // liblog will even suppress FATAL if you say 's' for silent, but that's |
| 251 | // crazy! |
| 252 | case 's': |
| 253 | gMinimumLogSeverity = FATAL; |
| 254 | continue; |
| 255 | } |
| 256 | } |
| 257 | LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags |
| 258 | << ")"; |
| 259 | } |
| 260 | } |
| 261 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 262 | void SetLogger(LogFunction&& logger) { |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 263 | lock_guard<mutex> lock(logging_lock); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 264 | gLogger = std::move(logger); |
| 265 | } |
| 266 | |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 267 | // We can't use basename(3) because this code runs on the Mac, which doesn't |
| 268 | // have a non-modifying basename. |
| 269 | static const char* GetFileBasename(const char* file) { |
| 270 | const char* last_slash = strrchr(file, '/'); |
| 271 | return (last_slash == nullptr) ? file : last_slash + 1; |
| 272 | } |
| 273 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 274 | // This indirection greatly reduces the stack impact of having lots of |
| 275 | // checks/logging in a function. |
| 276 | class LogMessageData { |
| 277 | public: |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 278 | LogMessageData(const char* file, unsigned int line, LogId id, |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame^] | 279 | LogSeverity severity, int error) |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 280 | : file_(GetFileBasename(file)), |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 281 | line_number_(line), |
| 282 | id_(id), |
| 283 | severity_(severity), |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame^] | 284 | error_(error) { |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | const char* GetFile() const { |
| 288 | return file_; |
| 289 | } |
| 290 | |
| 291 | unsigned int GetLineNumber() const { |
| 292 | return line_number_; |
| 293 | } |
| 294 | |
| 295 | LogSeverity GetSeverity() const { |
| 296 | return severity_; |
| 297 | } |
| 298 | |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 299 | LogId GetId() const { |
| 300 | return id_; |
| 301 | } |
| 302 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 303 | int GetError() const { |
| 304 | return error_; |
| 305 | } |
| 306 | |
| 307 | std::ostream& GetBuffer() { |
| 308 | return buffer_; |
| 309 | } |
| 310 | |
| 311 | std::string ToString() const { |
| 312 | return buffer_.str(); |
| 313 | } |
| 314 | |
| 315 | private: |
| 316 | std::ostringstream buffer_; |
| 317 | const char* const file_; |
| 318 | const unsigned int line_number_; |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 319 | const LogId id_; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 320 | const LogSeverity severity_; |
| 321 | const int error_; |
| 322 | |
| 323 | DISALLOW_COPY_AND_ASSIGN(LogMessageData); |
| 324 | }; |
| 325 | |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 326 | LogMessage::LogMessage(const char* file, unsigned int line, LogId id, |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 327 | LogSeverity severity, int error) |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame^] | 328 | : data_(new LogMessageData(file, line, id, severity, error)) { |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | LogMessage::~LogMessage() { |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 332 | // Finish constructing the message. |
| 333 | if (data_->GetError() != -1) { |
| 334 | data_->GetBuffer() << ": " << strerror(data_->GetError()); |
| 335 | } |
| 336 | std::string msg(data_->ToString()); |
| 337 | |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame^] | 338 | { |
| 339 | // Do the actual logging with the lock held. |
| 340 | lock_guard<mutex> lock(logging_lock); |
| 341 | if (msg.find('\n') == std::string::npos) { |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 342 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame^] | 343 | data_->GetSeverity(), msg.c_str()); |
| 344 | } else { |
| 345 | msg += '\n'; |
| 346 | size_t i = 0; |
| 347 | while (i < msg.size()) { |
| 348 | size_t nl = msg.find('\n', i); |
| 349 | msg[nl] = '\0'; |
| 350 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), |
| 351 | data_->GetSeverity(), &msg[i]); |
| 352 | i = nl + 1; |
| 353 | } |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | // Abort if necessary. |
| 358 | if (data_->GetSeverity() == FATAL) { |
| 359 | #ifdef __ANDROID__ |
| 360 | android_set_abort_message(msg.c_str()); |
| 361 | #endif |
| 362 | abort(); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | std::ostream& LogMessage::stream() { |
| 367 | return data_->GetBuffer(); |
| 368 | } |
| 369 | |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 370 | void LogMessage::LogLine(const char* file, unsigned int line, LogId id, |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 371 | LogSeverity severity, const char* message) { |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 372 | const char* tag = ProgramInvocationName(); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 373 | gLogger(id, severity, tag, file, line, message); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 376 | ScopedLogSeverity::ScopedLogSeverity(LogSeverity level) { |
| 377 | old_ = gMinimumLogSeverity; |
| 378 | gMinimumLogSeverity = level; |
| 379 | } |
| 380 | |
| 381 | ScopedLogSeverity::~ScopedLogSeverity() { |
| 382 | gMinimumLogSeverity = old_; |
| 383 | } |
| 384 | |
| 385 | } // namespace base |
| 386 | } // namespace android |