diff options
Diffstat (limited to 'base/logging.cpp')
-rw-r--r-- | base/logging.cpp | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/base/logging.cpp b/base/logging.cpp index 75078e50e..0f2012a09 100644 --- a/base/logging.cpp +++ b/base/logging.cpp | |||
@@ -187,8 +187,8 @@ void KernelLogger(android::base::LogId, android::base::LogSeverity severity, | |||
187 | } | 187 | } |
188 | #endif | 188 | #endif |
189 | 189 | ||
190 | void StderrLogger(LogId, LogSeverity severity, const char*, const char* file, | 190 | void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file, unsigned int line, |
191 | unsigned int line, const char* message) { | 191 | const char* message) { |
192 | struct tm now; | 192 | struct tm now; |
193 | time_t t = time(nullptr); | 193 | time_t t = time(nullptr); |
194 | 194 | ||
@@ -205,8 +205,8 @@ void StderrLogger(LogId, LogSeverity severity, const char*, const char* file, | |||
205 | static_assert(arraysize(log_characters) - 1 == FATAL + 1, | 205 | static_assert(arraysize(log_characters) - 1 == FATAL + 1, |
206 | "Mismatch in size of log_characters and values in LogSeverity"); | 206 | "Mismatch in size of log_characters and values in LogSeverity"); |
207 | char severity_char = log_characters[severity]; | 207 | char severity_char = log_characters[severity]; |
208 | fprintf(stderr, "%s %c %s %5d %5d %s:%u] %s\n", ProgramInvocationName().c_str(), | 208 | fprintf(stderr, "%s %c %s %5d %5d %s:%u] %s\n", tag ? tag : "nullptr", severity_char, timestamp, |
209 | severity_char, timestamp, getpid(), GetThreadId(), file, line, message); | 209 | getpid(), GetThreadId(), file, line, message); |
210 | } | 210 | } |
211 | 211 | ||
212 | void DefaultAborter(const char* abort_message) { | 212 | void DefaultAborter(const char* abort_message) { |
@@ -344,14 +344,14 @@ static const char* GetFileBasename(const char* file) { | |||
344 | // checks/logging in a function. | 344 | // checks/logging in a function. |
345 | class LogMessageData { | 345 | class LogMessageData { |
346 | public: | 346 | public: |
347 | LogMessageData(const char* file, unsigned int line, LogId id, | 347 | LogMessageData(const char* file, unsigned int line, LogId id, LogSeverity severity, |
348 | LogSeverity severity, int error) | 348 | const char* tag, int error) |
349 | : file_(GetFileBasename(file)), | 349 | : file_(GetFileBasename(file)), |
350 | line_number_(line), | 350 | line_number_(line), |
351 | id_(id), | 351 | id_(id), |
352 | severity_(severity), | 352 | severity_(severity), |
353 | error_(error) { | 353 | tag_(tag), |
354 | } | 354 | error_(error) {} |
355 | 355 | ||
356 | const char* GetFile() const { | 356 | const char* GetFile() const { |
357 | return file_; | 357 | return file_; |
@@ -365,6 +365,8 @@ class LogMessageData { | |||
365 | return severity_; | 365 | return severity_; |
366 | } | 366 | } |
367 | 367 | ||
368 | const char* GetTag() const { return tag_; } | ||
369 | |||
368 | LogId GetId() const { | 370 | LogId GetId() const { |
369 | return id_; | 371 | return id_; |
370 | } | 372 | } |
@@ -387,15 +389,19 @@ class LogMessageData { | |||
387 | const unsigned int line_number_; | 389 | const unsigned int line_number_; |
388 | const LogId id_; | 390 | const LogId id_; |
389 | const LogSeverity severity_; | 391 | const LogSeverity severity_; |
392 | const char* const tag_; | ||
390 | const int error_; | 393 | const int error_; |
391 | 394 | ||
392 | DISALLOW_COPY_AND_ASSIGN(LogMessageData); | 395 | DISALLOW_COPY_AND_ASSIGN(LogMessageData); |
393 | }; | 396 | }; |
394 | 397 | ||
395 | LogMessage::LogMessage(const char* file, unsigned int line, LogId id, | 398 | LogMessage::LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity, |
396 | LogSeverity severity, int error) | 399 | const char* tag, int error) |
397 | : data_(new LogMessageData(file, line, id, severity, error)) { | 400 | : data_(new LogMessageData(file, line, id, severity, tag, error)) {} |
398 | } | 401 | |
402 | LogMessage::LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity, | ||
403 | int error) | ||
404 | : LogMessage(file, line, id, severity, nullptr, error) {} | ||
399 | 405 | ||
400 | LogMessage::~LogMessage() { | 406 | LogMessage::~LogMessage() { |
401 | // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM. | 407 | // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM. |
@@ -413,16 +419,16 @@ LogMessage::~LogMessage() { | |||
413 | // Do the actual logging with the lock held. | 419 | // Do the actual logging with the lock held. |
414 | std::lock_guard<std::mutex> lock(LoggingLock()); | 420 | std::lock_guard<std::mutex> lock(LoggingLock()); |
415 | if (msg.find('\n') == std::string::npos) { | 421 | if (msg.find('\n') == std::string::npos) { |
416 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), | 422 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(), |
417 | data_->GetSeverity(), msg.c_str()); | 423 | data_->GetTag(), msg.c_str()); |
418 | } else { | 424 | } else { |
419 | msg += '\n'; | 425 | msg += '\n'; |
420 | size_t i = 0; | 426 | size_t i = 0; |
421 | while (i < msg.size()) { | 427 | while (i < msg.size()) { |
422 | size_t nl = msg.find('\n', i); | 428 | size_t nl = msg.find('\n', i); |
423 | msg[nl] = '\0'; | 429 | msg[nl] = '\0'; |
424 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), | 430 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(), |
425 | data_->GetSeverity(), &msg[i]); | 431 | data_->GetTag(), &msg[i]); |
426 | // Undo the zero-termination so we can give the complete message to the aborter. | 432 | // Undo the zero-termination so we can give the complete message to the aborter. |
427 | msg[nl] = '\n'; | 433 | msg[nl] = '\n'; |
428 | i = nl + 1; | 434 | i = nl + 1; |
@@ -440,12 +446,17 @@ std::ostream& LogMessage::stream() { | |||
440 | return data_->GetBuffer(); | 446 | return data_->GetBuffer(); |
441 | } | 447 | } |
442 | 448 | ||
443 | void LogMessage::LogLine(const char* file, unsigned int line, LogId id, | 449 | void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity, |
444 | LogSeverity severity, const char* message) { | 450 | const char* tag, const char* message) { |
445 | const char* tag = ProgramInvocationName().c_str(); | 451 | if (tag == nullptr) tag = ProgramInvocationName().c_str(); |
446 | Logger()(id, severity, tag, file, line, message); | 452 | Logger()(id, severity, tag, file, line, message); |
447 | } | 453 | } |
448 | 454 | ||
455 | void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity, | ||
456 | const char* message) { | ||
457 | LogLine(file, line, id, severity, nullptr, message); | ||
458 | } | ||
459 | |||
449 | LogSeverity GetMinimumLogSeverity() { | 460 | LogSeverity GetMinimumLogSeverity() { |
450 | return gMinimumLogSeverity; | 461 | return gMinimumLogSeverity; |
451 | } | 462 | } |