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

Commit 0391a879 authored by Tom Cherry's avatar Tom Cherry
Browse files

Move minimum log priority from libbase to liblog

See the previous commit moving SetLogger and SetAborter to liblog for
motivation.

This creates more harmony between the two mechanisms in libbase and
liblog for checking loggability.
Currently:
1) libbase filters all messages based on its minimum log priority. For
   example, if minimum log priority in libbase remained at its
   default, but a tag was specifically opted into DEBUG logs via
   log.tag.<tag>, libbase would not print this log.
2) liblog ignores libbase's minimum log priority.  For example if a
   process called SetMinimumLogPriority(WARNING) but used a library
   that logged via liblog's ALOGI macro, that log would still be
   printed even though the process intends on filtering out those INFO
   messages.

With this change:
1) If both a minimum log priority and a priority through log.tag.<tag>
   are set, then the lower of the two values is used.
2) If only one or the other is set, then that value is used.  This
   fixes the two issues described above.
3) If neither of these values are set, then the default of using INFO
   is unchanged.

Bug: 116329414
Bug: 119867234
Test: libbase and liblog minimum log priority tests
Change-Id: Icb49b30b9d93bf797470e23730ae9e537931bb6c
parent 349b0c43
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ enum LogSeverity {
  INFO,
  WARNING,
  ERROR,
  FATAL_WITHOUT_ABORT,
  FATAL_WITHOUT_ABORT,  // For loggability tests, this is considered identical to FATAL.
  FATAL,
};

@@ -212,7 +212,7 @@ struct LogAbortAfterFullExpr {

// Defines whether the given severity will be logged or silently swallowed.
#define WOULD_LOG(severity)                                                              \
  (UNLIKELY((SEVERITY_LAMBDA(severity)) >= ::android::base::GetMinimumLogSeverity()) || \
  (UNLIKELY(::android::base::ShouldLog(SEVERITY_LAMBDA(severity), _LOG_TAG_INTERNAL)) || \
   MUST_LOG_MESSAGE(severity))

// Get an ostream that can be used for logging at the given severity and to the default
@@ -444,6 +444,9 @@ LogSeverity GetMinimumLogSeverity();
// Set the minimum severity level for logging, returning the old severity.
LogSeverity SetMinimumLogSeverity(LogSeverity new_severity);

// Return whether or not a log message with the associated tag should be logged.
bool ShouldLog(LogSeverity severity, const char* tag);

// Allows to temporarily change the minimum severity level for logging.
class ScopedLogSeverity {
 public:
+4 −0
Original line number Diff line number Diff line
@@ -48,6 +48,8 @@ const std::optional<LibLogFunctions>& GetLibLogFunctions() {
    DLSYM(__android_log_set_aborter)
    DLSYM(__android_log_call_aborter)
    DLSYM(__android_log_default_aborter)
    DLSYM(__android_log_set_minimum_priority);
    DLSYM(__android_log_get_minimum_priority);
#undef DLSYM

    return real_liblog_functions;
@@ -68,6 +70,8 @@ const std::optional<LibLogFunctions>& GetLibLogFunctions() {
        .__android_log_set_aborter = __android_log_set_aborter,
        .__android_log_call_aborter = __android_log_call_aborter,
        .__android_log_default_aborter = __android_log_default_aborter,
        .__android_log_set_minimum_priority = __android_log_set_minimum_priority,
        .__android_log_get_minimum_priority = __android_log_get_minimum_priority,
    };
  }();
  return liblog_functions;
+2 −0
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ struct LibLogFunctions {
  void (*__android_log_set_aborter)(__android_aborter_function aborter);
  void (*__android_log_call_aborter)(const char* abort_message);
  void (*__android_log_default_aborter)(const char* abort_message);
  int (*__android_log_set_minimum_priority)(int priority);
  int (*__android_log_get_minimum_priority)();
};

const std::optional<LibLogFunctions>& GetLibLogFunctions();
+39 −10
Original line number Diff line number Diff line
@@ -152,6 +152,8 @@ static int LogIdTolog_id_t(LogId log_id) {

static LogSeverity PriorityToLogSeverity(int priority) {
  switch (priority) {
    case ANDROID_LOG_DEFAULT:
      return INFO;
    case ANDROID_LOG_VERBOSE:
      return VERBOSE;
    case ANDROID_LOG_DEBUG:
@@ -233,6 +235,8 @@ void SetDefaultTag(const std::string& tag) {
}

static bool gInitialized = false;

// Only used for Q fallback.
static LogSeverity gMinimumLogSeverity = INFO;

#if defined(__linux__)
@@ -381,27 +385,27 @@ void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) {
    if (spec.size() == 3 && StartsWith(spec, "*:")) {
      switch (spec[2]) {
        case 'v':
          gMinimumLogSeverity = VERBOSE;
          SetMinimumLogSeverity(VERBOSE);
          continue;
        case 'd':
          gMinimumLogSeverity = DEBUG;
          SetMinimumLogSeverity(DEBUG);
          continue;
        case 'i':
          gMinimumLogSeverity = INFO;
          SetMinimumLogSeverity(INFO);
          continue;
        case 'w':
          gMinimumLogSeverity = WARNING;
          SetMinimumLogSeverity(WARNING);
          continue;
        case 'e':
          gMinimumLogSeverity = ERROR;
          SetMinimumLogSeverity(ERROR);
          continue;
        case 'f':
          gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
          SetMinimumLogSeverity(FATAL_WITHOUT_ABORT);
          continue;
        // liblog will even suppress FATAL if you say 's' for silent, but that's
        // crazy!
        case 's':
          gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
          SetMinimumLogSeverity(FATAL_WITHOUT_ABORT);
          continue;
      }
    }
@@ -595,14 +599,39 @@ void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity severi
}

LogSeverity GetMinimumLogSeverity() {
  static auto& liblog_functions = GetLibLogFunctions();
  if (liblog_functions) {
    return PriorityToLogSeverity(liblog_functions->__android_log_get_minimum_priority());
  } else {
    return gMinimumLogSeverity;
  }
}

bool ShouldLog(LogSeverity severity, const char* tag) {
  static auto& liblog_functions = GetLibLogFunctions();
  // Even though we're not using the R liblog functions in this function, if we're running on Q,
  // we need to fall back to using gMinimumLogSeverity, since __android_log_is_loggable() will not
  // take into consideration the value from SetMinimumLogSeverity().
  if (liblog_functions) {
    // TODO: It is safe to pass nullptr for tag, but it will be better to use the default log tag.
    int priority = LogSeverityToPriority(severity);
    return __android_log_is_loggable(priority, tag, ANDROID_LOG_INFO);
  } else {
    return severity >= gMinimumLogSeverity;
  }
}

LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) {
  static auto& liblog_functions = GetLibLogFunctions();
  if (liblog_functions) {
    auto priority = LogSeverityToPriority(new_severity);
    return PriorityToLogSeverity(liblog_functions->__android_log_set_minimum_priority(priority));
  } else {
    LogSeverity old_severity = gMinimumLogSeverity;
    gMinimumLogSeverity = new_severity;
    return old_severity;
  }
}

ScopedLogSeverity::ScopedLogSeverity(LogSeverity new_severity) {
  old_ = SetMinimumLogSeverity(new_severity);
+0 −16
Original line number Diff line number Diff line
@@ -140,10 +140,6 @@ TEST(logging, WOULD_LOG_FATAL) {
  CHECK_WOULD_LOG_ENABLED(FATAL);
}

TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_disabled) {
  CHECK_WOULD_LOG_DISABLED(FATAL_WITHOUT_ABORT);
}

TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_enabled) {
  CHECK_WOULD_LOG_ENABLED(FATAL_WITHOUT_ABORT);
}
@@ -266,10 +262,6 @@ static void CheckMessage(CapturedStderr& cap, android::base::LogSeverity severit
    CheckMessage(cap2, android::base::severity, "foobar"); \
  } \

TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_disabled) {
  CHECK_LOG_STREAM_DISABLED(FATAL_WITHOUT_ABORT);
}

TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_enabled) {
  ASSERT_NO_FATAL_FAILURE(CHECK_LOG_STREAM_ENABLED(FATAL_WITHOUT_ABORT));
}
@@ -352,10 +344,6 @@ TEST(logging, LOG_FATAL) {
  ASSERT_DEATH({SuppressAbortUI(); LOG(::android::base::FATAL) << "foobar";}, "foobar");
}

TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) {
  CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT);
}

TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) {
  ASSERT_NO_FATAL_FAILURE(CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT));
}
@@ -508,10 +496,6 @@ TEST(logging, PLOG_FATAL) {
  ASSERT_DEATH({SuppressAbortUI(); PLOG(::android::base::FATAL) << "foobar";}, "foobar");
}

TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) {
  CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT);
}

TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) {
  ASSERT_NO_FATAL_FAILURE(CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT));
}
Loading