Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c2516001 authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk
Browse files

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
parent 3459e582
Loading
Loading
Loading
Loading
+52 −27
Original line number Original line Diff line number Diff line
@@ -42,6 +42,10 @@
// By default, output goes to logcat on Android and stderr on the host.
// By default, output goes to logcat on Android and stderr on the host.
// A process can use `SetLogger` to decide where all logging goes.
// A process can use `SetLogger` to decide where all logging goes.
// Implementations are provided for logcat, stderr, and dmesg.
// Implementations are provided for logcat, stderr, and dmesg.
//
// By default, the process' name is used as the log tag.
// Code can choose a specific log tag by defining LOG_TAG
// before including this header.


// This header also provides assertions:
// This header also provides assertions:
//
//
@@ -63,6 +67,16 @@


#include "android-base/macros.h"
#include "android-base/macros.h"


// Note: DO NOT USE DIRECTLY. Use LOG_TAG instead.
#ifdef _LOG_TAG_INTERNAL
#error "_LOG_TAG_INTERNAL must not be defined"
#endif
#ifdef LOG_TAG
#define _LOG_TAG_INTERNAL LOG_TAG
#else
#define _LOG_TAG_INTERNAL nullptr
#endif

namespace android {
namespace android {
namespace base {
namespace base {


@@ -202,9 +216,9 @@ struct LogAbortAfterFullExpr {
// Get an ostream that can be used for logging at the given severity and to the
// Get an ostream that can be used for logging at the given severity and to the
// given destination. The same notes as for LOG_STREAM apply.
// given destination. The same notes as for LOG_STREAM apply.
#define LOG_STREAM_TO(dest, severity)                                           \
#define LOG_STREAM_TO(dest, severity)                                           \
  ::android::base::LogMessage(__FILE__, __LINE__,                       \
  ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest,        \
                              ::android::base::dest,                    \
                              SEVERITY_LAMBDA(severity), _LOG_TAG_INTERNAL, -1) \
                              SEVERITY_LAMBDA(severity), -1).stream()
      .stream()


// Logs a message to logcat on Android otherwise to stderr. If the severity is
// Logs a message to logcat on Android otherwise to stderr. If the severity is
// FATAL it also causes an abort. For example:
// FATAL it also causes an abort. For example:
@@ -234,7 +248,7 @@ struct LogAbortAfterFullExpr {
#define PLOG_TO(dest, severity)                                                        \
#define PLOG_TO(dest, severity)                                                        \
  LOGGING_PREAMBLE(severity) &&                                                        \
  LOGGING_PREAMBLE(severity) &&                                                        \
      ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest,           \
      ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest,           \
                                  SEVERITY_LAMBDA(severity), errno)          \
                                  SEVERITY_LAMBDA(severity), _LOG_TAG_INTERNAL, errno) \
          .stream()
          .stream()


// Marker that code is yet to be implemented.
// Marker that code is yet to be implemented.
@@ -249,11 +263,12 @@ struct LogAbortAfterFullExpr {
//       "Check failed: false == true".
//       "Check failed: false == true".
#define CHECK(x)                                                                 \
#define CHECK(x)                                                                 \
  LIKELY((x)) || ABORT_AFTER_LOG_FATAL_EXPR(false) ||                            \
  LIKELY((x)) || ABORT_AFTER_LOG_FATAL_EXPR(false) ||                            \
      ::android::base::LogMessage(                                              \
      ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT,  \
          __FILE__, __LINE__, ::android::base::DEFAULT, ::android::base::FATAL, \
                                  ::android::base::FATAL, _LOG_TAG_INTERNAL, -1) \
          -1).stream()                                                          \
              .stream()                                                          \
          << "Check failed: " #x << " "
          << "Check failed: " #x << " "


// clang-format off
// Helper for CHECK_xx(x,y) macros.
// Helper for CHECK_xx(x,y) macros.
#define CHECK_OP(LHS, RHS, OP)                                                                 \
#define CHECK_OP(LHS, RHS, OP)                                                                 \
  for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS);                           \
  for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS);                           \
@@ -261,9 +276,11 @@ struct LogAbortAfterFullExpr {
       /* empty */)                                                                            \
       /* empty */)                                                                            \
  ABORT_AFTER_LOG_FATAL                                                                        \
  ABORT_AFTER_LOG_FATAL                                                                        \
  ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT,                    \
  ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT,                    \
                              ::android::base::FATAL, -1).stream()          \
                              ::android::base::FATAL, _LOG_TAG_INTERNAL, -1)                   \
      << "Check failed: " << #LHS << " " << #OP << " " << #RHS              \
          .stream()                                                                            \
      << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
      << "Check failed: " << #LHS << " " << #OP << " " << #RHS << " (" #LHS "=" << _values.lhs \
      << ", " #RHS "=" << _values.rhs << ") "
// clang-format on


// Check whether a condition holds between x and y, LOG(FATAL) if not. The value
// Check whether a condition holds between x and y, LOG(FATAL) if not. The value
// of the expressions x and y is evaluated once. Extra logging can be appended
// of the expressions x and y is evaluated once. Extra logging can be appended
@@ -278,14 +295,17 @@ struct LogAbortAfterFullExpr {
#define CHECK_GE(x, y) CHECK_OP(x, y, >= )
#define CHECK_GE(x, y) CHECK_OP(x, y, >= )
#define CHECK_GT(x, y) CHECK_OP(x, y, > )
#define CHECK_GT(x, y) CHECK_OP(x, y, > )


// clang-format off
// Helper for CHECK_STRxx(s1,s2) macros.
// Helper for CHECK_STRxx(s1,s2) macros.
#define CHECK_STROP(s1, s2, sense)                                             \
#define CHECK_STROP(s1, s2, sense)                                             \
  while (UNLIKELY((strcmp(s1, s2) == 0) != (sense)))                           \
  while (UNLIKELY((strcmp(s1, s2) == 0) != (sense)))                           \
    ABORT_AFTER_LOG_FATAL                                                      \
    ABORT_AFTER_LOG_FATAL                                                      \
    ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT,  \
    ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT,  \
                                ::android::base::FATAL, -1).stream()           \
                                ::android::base::FATAL, _LOG_TAG_INTERNAL, -1) \
        .stream()                                                              \
        << "Check failed: " << "\"" << (s1) << "\""                            \
        << "Check failed: " << "\"" << (s1) << "\""                            \
        << ((sense) ? " == " : " != ") << "\"" << (s2) << "\""
        << ((sense) ? " == " : " != ") << "\"" << (s2) << "\""
// clang-format on


// Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not.
// Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not.
#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
@@ -400,8 +420,8 @@ class LogMessageData;
// of a CHECK. The destructor will abort if the severity is FATAL.
// of a CHECK. The destructor will abort if the severity is FATAL.
class LogMessage {
class LogMessage {
 public:
 public:
  LogMessage(const char* file, unsigned int line, LogId id,
  LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity, const char* tag,
             LogSeverity severity, int error);
             int error);


  ~LogMessage();
  ~LogMessage();


@@ -410,12 +430,17 @@ class LogMessage {
  std::ostream& stream();
  std::ostream& stream();


  // The routine that performs the actual logging.
  // The routine that performs the actual logging.
  static void LogLine(const char* file, unsigned int line, LogId id,
  static void LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
                      LogSeverity severity, const char* msg);
                      const char* tag, const char* msg);


 private:
 private:
  const std::unique_ptr<LogMessageData> data_;
  const std::unique_ptr<LogMessageData> data_;


  // TODO(b/35361699): remove these symbols once all prebuilds stop using it.
  LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity, int error);
  static void LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
                      const char* msg);

  DISALLOW_COPY_AND_ASSIGN(LogMessage);
  DISALLOW_COPY_AND_ASSIGN(LogMessage);
};
};


+30 −19
Original line number Original line Diff line number Diff line
@@ -187,8 +187,8 @@ void KernelLogger(android::base::LogId, android::base::LogSeverity severity,
}
}
#endif
#endif


void StderrLogger(LogId, LogSeverity severity, const char*, const char* file,
void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file, unsigned int line,
                  unsigned int line, const char* message) {
                  const char* message) {
  struct tm now;
  struct tm now;
  time_t t = time(nullptr);
  time_t t = time(nullptr);


@@ -205,8 +205,8 @@ void StderrLogger(LogId, LogSeverity severity, const char*, const char* file,
  static_assert(arraysize(log_characters) - 1 == FATAL + 1,
  static_assert(arraysize(log_characters) - 1 == FATAL + 1,
                "Mismatch in size of log_characters and values in LogSeverity");
                "Mismatch in size of log_characters and values in LogSeverity");
  char severity_char = log_characters[severity];
  char severity_char = log_characters[severity];
  fprintf(stderr, "%s %c %s %5d %5d %s:%u] %s\n", ProgramInvocationName().c_str(),
  fprintf(stderr, "%s %c %s %5d %5d %s:%u] %s\n", tag ? tag : "nullptr", severity_char, timestamp,
          severity_char, timestamp, getpid(), GetThreadId(), file, line, message);
          getpid(), GetThreadId(), file, line, message);
}
}


void DefaultAborter(const char* abort_message) {
void DefaultAborter(const char* abort_message) {
@@ -344,14 +344,14 @@ static const char* GetFileBasename(const char* file) {
// checks/logging in a function.
// checks/logging in a function.
class LogMessageData {
class LogMessageData {
 public:
 public:
  LogMessageData(const char* file, unsigned int line, LogId id,
  LogMessageData(const char* file, unsigned int line, LogId id, LogSeverity severity,
                 LogSeverity severity, int error)
                 const char* tag, int error)
      : file_(GetFileBasename(file)),
      : file_(GetFileBasename(file)),
        line_number_(line),
        line_number_(line),
        id_(id),
        id_(id),
        severity_(severity),
        severity_(severity),
        error_(error) {
        tag_(tag),
  }
        error_(error) {}


  const char* GetFile() const {
  const char* GetFile() const {
    return file_;
    return file_;
@@ -365,6 +365,8 @@ class LogMessageData {
    return severity_;
    return severity_;
  }
  }


  const char* GetTag() const { return tag_; }

  LogId GetId() const {
  LogId GetId() const {
    return id_;
    return id_;
  }
  }
@@ -387,15 +389,19 @@ class LogMessageData {
  const unsigned int line_number_;
  const unsigned int line_number_;
  const LogId id_;
  const LogId id_;
  const LogSeverity severity_;
  const LogSeverity severity_;
  const char* const tag_;
  const int error_;
  const int error_;


  DISALLOW_COPY_AND_ASSIGN(LogMessageData);
  DISALLOW_COPY_AND_ASSIGN(LogMessageData);
};
};


LogMessage::LogMessage(const char* file, unsigned int line, LogId id,
LogMessage::LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity,
                       LogSeverity severity, int error)
                       const char* tag, int error)
    : data_(new LogMessageData(file, line, id, severity, error)) {
    : data_(new LogMessageData(file, line, id, severity, tag, error)) {}
}

LogMessage::LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity,
                       int error)
    : LogMessage(file, line, id, severity, nullptr, error) {}


LogMessage::~LogMessage() {
LogMessage::~LogMessage() {
  // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM.
  // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM.
@@ -413,16 +419,16 @@ LogMessage::~LogMessage() {
    // Do the actual logging with the lock held.
    // Do the actual logging with the lock held.
    std::lock_guard<std::mutex> lock(LoggingLock());
    std::lock_guard<std::mutex> lock(LoggingLock());
    if (msg.find('\n') == std::string::npos) {
    if (msg.find('\n') == std::string::npos) {
      LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(),
      LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(),
              data_->GetSeverity(), msg.c_str());
              data_->GetTag(), msg.c_str());
    } else {
    } else {
      msg += '\n';
      msg += '\n';
      size_t i = 0;
      size_t i = 0;
      while (i < msg.size()) {
      while (i < msg.size()) {
        size_t nl = msg.find('\n', i);
        size_t nl = msg.find('\n', i);
        msg[nl] = '\0';
        msg[nl] = '\0';
        LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(),
        LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(),
                data_->GetSeverity(), &msg[i]);
                data_->GetTag(), &msg[i]);
        // Undo the zero-termination so we can give the complete message to the aborter.
        // Undo the zero-termination so we can give the complete message to the aborter.
        msg[nl] = '\n';
        msg[nl] = '\n';
        i = nl + 1;
        i = nl + 1;
@@ -440,12 +446,17 @@ std::ostream& LogMessage::stream() {
  return data_->GetBuffer();
  return data_->GetBuffer();
}
}


void LogMessage::LogLine(const char* file, unsigned int line, LogId id,
void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
                         LogSeverity severity, const char* message) {
                         const char* tag, const char* message) {
  const char* tag = ProgramInvocationName().c_str();
  if (tag == nullptr) tag = ProgramInvocationName().c_str();
  Logger()(id, severity, tag, file, line, message);
  Logger()(id, severity, tag, file, line, message);
}
}


void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
                         const char* message) {
  LogLine(file, line, id, severity, nullptr, message);
}

LogSeverity GetMinimumLogSeverity() {
LogSeverity GetMinimumLogSeverity() {
    return gMinimumLogSeverity;
    return gMinimumLogSeverity;
}
}