summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Wasilczyk2017-12-18 08:30:17 -0600
committerTomasz Wasilczyk2017-12-20 12:59:46 -0600
commitc2516001021265aa0be719f02487117702ec3e71 (patch)
tree7ef02d77813fb1a934f9c133ca1dd467734a8a20
parent3459e5823e74777ea1d841c72bb5730170629213 (diff)
downloadplatform-system-core-c2516001021265aa0be719f02487117702ec3e71.tar.gz
platform-system-core-c2516001021265aa0be719f02487117702ec3e71.tar.xz
platform-system-core-c2516001021265aa0be719f02487117702ec3e71.zip
Use LOG_TAG instead of binary name as a tag.
If LOG_TAG was not defined, falling back to a default behaviour (using binary name). Bug: 35361699 Test: manual Change-Id: I209a6ebaf0df882f98642f6d1831766cb296c951
-rw-r--r--base/include/android-base/logging.h79
-rw-r--r--base/logging.cpp49
2 files changed, 82 insertions, 46 deletions
diff --git a/base/include/android-base/logging.h b/base/include/android-base/logging.h
index f93c69615..afff2c951 100644
--- a/base/include/android-base/logging.h
+++ b/base/include/android-base/logging.h
@@ -42,6 +42,10 @@
42// By default, output goes to logcat on Android and stderr on the host. 42// By default, output goes to logcat on Android and stderr on the host.
43// A process can use `SetLogger` to decide where all logging goes. 43// A process can use `SetLogger` to decide where all logging goes.
44// Implementations are provided for logcat, stderr, and dmesg. 44// Implementations are provided for logcat, stderr, and dmesg.
45//
46// By default, the process' name is used as the log tag.
47// Code can choose a specific log tag by defining LOG_TAG
48// before including this header.
45 49
46// This header also provides assertions: 50// This header also provides assertions:
47// 51//
@@ -63,6 +67,16 @@
63 67
64#include "android-base/macros.h" 68#include "android-base/macros.h"
65 69
70// Note: DO NOT USE DIRECTLY. Use LOG_TAG instead.
71#ifdef _LOG_TAG_INTERNAL
72#error "_LOG_TAG_INTERNAL must not be defined"
73#endif
74#ifdef LOG_TAG
75#define _LOG_TAG_INTERNAL LOG_TAG
76#else
77#define _LOG_TAG_INTERNAL nullptr
78#endif
79
66namespace android { 80namespace android {
67namespace base { 81namespace base {
68 82
@@ -201,10 +215,10 @@ struct LogAbortAfterFullExpr {
201 215
202// Get an ostream that can be used for logging at the given severity and to the 216// Get an ostream that can be used for logging at the given severity and to the
203// given destination. The same notes as for LOG_STREAM apply. 217// given destination. The same notes as for LOG_STREAM apply.
204#define LOG_STREAM_TO(dest, severity) \ 218#define LOG_STREAM_TO(dest, severity) \
205 ::android::base::LogMessage(__FILE__, __LINE__, \ 219 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest, \
206 ::android::base::dest, \ 220 SEVERITY_LAMBDA(severity), _LOG_TAG_INTERNAL, -1) \
207 SEVERITY_LAMBDA(severity), -1).stream() 221 .stream()
208 222
209// Logs a message to logcat on Android otherwise to stderr. If the severity is 223// Logs a message to logcat on Android otherwise to stderr. If the severity is
210// FATAL it also causes an abort. For example: 224// FATAL it also causes an abort. For example:
@@ -231,10 +245,10 @@ struct LogAbortAfterFullExpr {
231#define PLOG(severity) PLOG_TO(DEFAULT, severity) 245#define PLOG(severity) PLOG_TO(DEFAULT, severity)
232 246
233// Behaves like PLOG, but logs to the specified log ID. 247// Behaves like PLOG, but logs to the specified log ID.
234#define PLOG_TO(dest, severity) \ 248#define PLOG_TO(dest, severity) \
235 LOGGING_PREAMBLE(severity) && \ 249 LOGGING_PREAMBLE(severity) && \
236 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest, \ 250 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest, \
237 SEVERITY_LAMBDA(severity), errno) \ 251 SEVERITY_LAMBDA(severity), _LOG_TAG_INTERNAL, errno) \
238 .stream() 252 .stream()
239 253
240// Marker that code is yet to be implemented. 254// Marker that code is yet to be implemented.
@@ -247,23 +261,26 @@ struct LogAbortAfterFullExpr {
247// 261//
248// CHECK(false == true) results in a log message of 262// CHECK(false == true) results in a log message of
249// "Check failed: false == true". 263// "Check failed: false == true".
250#define CHECK(x) \ 264#define CHECK(x) \
251 LIKELY((x)) || ABORT_AFTER_LOG_FATAL_EXPR(false) || \ 265 LIKELY((x)) || ABORT_AFTER_LOG_FATAL_EXPR(false) || \
252 ::android::base::LogMessage( \ 266 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
253 __FILE__, __LINE__, ::android::base::DEFAULT, ::android::base::FATAL, \ 267 ::android::base::FATAL, _LOG_TAG_INTERNAL, -1) \
254 -1).stream() \ 268 .stream() \
255 << "Check failed: " #x << " " 269 << "Check failed: " #x << " "
256 270
271// clang-format off
257// Helper for CHECK_xx(x,y) macros. 272// Helper for CHECK_xx(x,y) macros.
258#define CHECK_OP(LHS, RHS, OP) \ 273#define CHECK_OP(LHS, RHS, OP) \
259 for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \ 274 for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \
260 UNLIKELY(!(_values.lhs OP _values.rhs)); \ 275 UNLIKELY(!(_values.lhs OP _values.rhs)); \
261 /* empty */) \ 276 /* empty */) \
262 ABORT_AFTER_LOG_FATAL \ 277 ABORT_AFTER_LOG_FATAL \
263 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \ 278 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
264 ::android::base::FATAL, -1).stream() \ 279 ::android::base::FATAL, _LOG_TAG_INTERNAL, -1) \
265 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \ 280 .stream() \
266 << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") " 281 << "Check failed: " << #LHS << " " << #OP << " " << #RHS << " (" #LHS "=" << _values.lhs \
282 << ", " #RHS "=" << _values.rhs << ") "
283// clang-format on
267 284
268// Check whether a condition holds between x and y, LOG(FATAL) if not. The value 285// Check whether a condition holds between x and y, LOG(FATAL) if not. The value
269// of the expressions x and y is evaluated once. Extra logging can be appended 286// of the expressions x and y is evaluated once. Extra logging can be appended
@@ -278,14 +295,17 @@ struct LogAbortAfterFullExpr {
278#define CHECK_GE(x, y) CHECK_OP(x, y, >= ) 295#define CHECK_GE(x, y) CHECK_OP(x, y, >= )
279#define CHECK_GT(x, y) CHECK_OP(x, y, > ) 296#define CHECK_GT(x, y) CHECK_OP(x, y, > )
280 297
298// clang-format off
281// Helper for CHECK_STRxx(s1,s2) macros. 299// Helper for CHECK_STRxx(s1,s2) macros.
282#define CHECK_STROP(s1, s2, sense) \ 300#define CHECK_STROP(s1, s2, sense) \
283 while (UNLIKELY((strcmp(s1, s2) == 0) != (sense))) \ 301 while (UNLIKELY((strcmp(s1, s2) == 0) != (sense))) \
284 ABORT_AFTER_LOG_FATAL \ 302 ABORT_AFTER_LOG_FATAL \
285 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \ 303 ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
286 ::android::base::FATAL, -1).stream() \ 304 ::android::base::FATAL, _LOG_TAG_INTERNAL, -1) \
305 .stream() \
287 << "Check failed: " << "\"" << (s1) << "\"" \ 306 << "Check failed: " << "\"" << (s1) << "\"" \
288 << ((sense) ? " == " : " != ") << "\"" << (s2) << "\"" 307 << ((sense) ? " == " : " != ") << "\"" << (s2) << "\""
308// clang-format on
289 309
290// Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not. 310// Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not.
291#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true) 311#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
@@ -400,8 +420,8 @@ class LogMessageData;
400// of a CHECK. The destructor will abort if the severity is FATAL. 420// of a CHECK. The destructor will abort if the severity is FATAL.
401class LogMessage { 421class LogMessage {
402 public: 422 public:
403 LogMessage(const char* file, unsigned int line, LogId id, 423 LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity, const char* tag,
404 LogSeverity severity, int error); 424 int error);
405 425
406 ~LogMessage(); 426 ~LogMessage();
407 427
@@ -410,12 +430,17 @@ class LogMessage {
410 std::ostream& stream(); 430 std::ostream& stream();
411 431
412 // The routine that performs the actual logging. 432 // The routine that performs the actual logging.
413 static void LogLine(const char* file, unsigned int line, LogId id, 433 static void LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
414 LogSeverity severity, const char* msg); 434 const char* tag, const char* msg);
415 435
416 private: 436 private:
417 const std::unique_ptr<LogMessageData> data_; 437 const std::unique_ptr<LogMessageData> data_;
418 438
439 // TODO(b/35361699): remove these symbols once all prebuilds stop using it.
440 LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity, int error);
441 static void LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
442 const char* msg);
443
419 DISALLOW_COPY_AND_ASSIGN(LogMessage); 444 DISALLOW_COPY_AND_ASSIGN(LogMessage);
420}; 445};
421 446
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
190void StderrLogger(LogId, LogSeverity severity, const char*, const char* file, 190void 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
212void DefaultAborter(const char* abort_message) { 212void 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.
345class LogMessageData { 345class 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
395LogMessage::LogMessage(const char* file, unsigned int line, LogId id, 398LogMessage::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
402LogMessage::LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity,
403 int error)
404 : LogMessage(file, line, id, severity, nullptr, error) {}
399 405
400LogMessage::~LogMessage() { 406LogMessage::~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
443void LogMessage::LogLine(const char* file, unsigned int line, LogId id, 449void 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
455void 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
449LogSeverity GetMinimumLogSeverity() { 460LogSeverity GetMinimumLogSeverity() {
450 return gMinimumLogSeverity; 461 return gMinimumLogSeverity;
451} 462}