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

Commit 15899577 authored by David Duarte's avatar David Duarte Committed by Gerrit Code Review
Browse files

Merge changes Ic200323d,I492f44c4 into main

* changes:
  floss: Update syslog filtering for libbluetooth
  gd/os/log.h: Use liblog to control logging level
parents 97b4e56a a267553e
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -34,14 +34,6 @@ class InitFlags final {
    init_flags::load(std::move(rusted_flags));
  }

  inline static int GetLogLevelForTag(const std::string& tag) {
    return init_flags::get_log_level_for_tag(tag);
  }

  inline static int GetDefaultLogLevel() {
    return init_flags::get_default_log_level();
  }

  inline static bool IsDeviceIotConfigLoggingEnabled() {
    return init_flags::device_iot_config_logging_is_enabled();
  }
+0 −47
Original line number Diff line number Diff line
@@ -20,8 +20,6 @@

#include <gtest/gtest.h>

#include "os/log_tags.h"

using bluetooth::common::InitFlags;

TEST(InitFlagsTest, test_enable_btm_flush_discovery_queue_on_search_cancel) {
@@ -36,51 +34,6 @@ TEST(InitFlagsTest, test_leaudio_targeted_announcement_reconnection_mode) {
  ASSERT_TRUE(InitFlags::IsTargetedAnnouncementReconnectionMode());
}

TEST(InitFlagsTest, test_enable_debug_logging_for_all) {
  const char* input[] = {"INIT_default_log_level=5", nullptr};
  InitFlags::Load(input);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("foo"), LOG_TAG_DEBUG);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("bar"), LOG_TAG_DEBUG);
  ASSERT_EQ(InitFlags::GetDefaultLogLevel(), LOG_TAG_DEBUG);
}

TEST(InitFlagsTest, test_enable_debug_logging_for_tags) {
  const char* input[] = {"INIT_logging_debug_enabled_for_tags=foo,bar,hello", nullptr};
  InitFlags::Load(input);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("foo"), LOG_TAG_VERBOSE);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("bar"), LOG_TAG_VERBOSE);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("hello"), LOG_TAG_VERBOSE);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("Foo"), LOG_TAG_INFO);
  ASSERT_EQ(InitFlags::GetDefaultLogLevel(), LOG_TAG_INFO);
}

TEST(InitFlagsTest, test_disable_debug_logging_for_tags) {
  const char* input[] = {
      "INIT_logging_debug_disabled_for_tags=foo,bar,hello",
      "INIT_default_log_level_str=LOG_DEBUG",
      nullptr};
  InitFlags::Load(input);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("foo"), LOG_TAG_INFO);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("bar"), LOG_TAG_INFO);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("hello"), LOG_TAG_INFO);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("Foo"), LOG_TAG_DEBUG);
  ASSERT_EQ(InitFlags::GetDefaultLogLevel(), LOG_TAG_DEBUG);
}

TEST(InitFlagsTest, test_debug_logging_multiple_flags) {
  const char* input[] = {
      "INIT_logging_debug_enabled_for_tags=foo,hello",
      "INIT_logging_debug_disabled_for_tags=foo,bar",
      "INIT_default_log_level_str=LOG_WARN",
      nullptr};
  InitFlags::Load(input);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("foo"), LOG_TAG_INFO);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("bar"), LOG_TAG_INFO);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("hello"), LOG_TAG_VERBOSE);
  ASSERT_EQ(InitFlags::GetLogLevelForTag("Foo"), LOG_TAG_WARN);
  ASSERT_EQ(InitFlags::GetDefaultLogLevel(), LOG_TAG_WARN);
}

TEST(InitFlagsTest, test_device_iot_config_logging_is_enabled) {
  const char* input[] = {"INIT_device_iot_config_logging=true", nullptr};
  InitFlags::Load(input);
+16 −31
Original line number Diff line number Diff line
@@ -57,24 +57,21 @@ static_assert(LOG_TAG != nullptr, "LOG_TAG should never be NULL");
#include <log/log.h>
#include <log/log_event_list.h>

#if __has_include("src/init_flags.rs.h")

#include "common/init_flags.h"

#define LOG_VERBOSE_INT(fmt, args...)                                                     \
  do {                                                                                    \
    if (bluetooth::common::InitFlags::GetLogLevelForTag(LOG_TAG) >= LOG_TAG_VERBOSE) { \
    if (!__android_log_is_loggable(ANDROID_LOG_VERBOSE, LOG_TAG, ANDROID_LOG_INFO) &&     \
        !__android_log_is_loggable(ANDROID_LOG_VERBOSE, "bluetooth", ANDROID_LOG_INFO)) { \
      ALOGV(fmt, ##args);                                                                 \
    }                                                                                     \
  } while (false)

#define LOG_DEBUG_INT(fmt, args...)                                                     \
  do {                                                                                  \
    if (bluetooth::common::InitFlags::GetLogLevelForTag(LOG_TAG) >= LOG_TAG_DEBUG) { \
    if (!__android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO) &&     \
        !__android_log_is_loggable(ANDROID_LOG_DEBUG, "bluetooth", ANDROID_LOG_INFO)) { \
      ALOGD(fmt, ##args);                                                               \
    }                                                                                   \
  } while (false)
#endif /* __has_include("src/init_flags.rs.h") */

#define LOG_INFO_INT(fmt, args...) ALOGI(fmt, ##args)
#define LOG_WARN_INT(fmt, args...) ALOGW(fmt, ##args)
@@ -102,25 +99,13 @@ static_assert(LOG_TAG != nullptr, "LOG_TAG should never be NULL");
    abort();                                                                        \
  } while (false)
#elif defined(TARGET_FLOSS) /* end of defined (ANDROID_EMULATOR) */
#include "common/init_flags.h"
#include "os/syslog.h"

// Prefix the log with tag, file, line and function
#define LOGWRAPPER(tag, fmt, args...) \
  write_syslog(tag, "%s: " fmt, LOG_TAG, ##args)
#define LOGWRAPPER(tag, fmt, args...) write_syslog(tag, LOG_TAG, "%s: " fmt, LOG_TAG, ##args)

#define LOG_VERBOSE_INT(...)                                                           \
  do {                                                                                 \
    if (bluetooth::common::InitFlags::GetLogLevelForTag(LOG_TAG) >= LOG_TAG_VERBOSE) { \
      LOGWRAPPER(LOG_TAG_VERBOSE, __VA_ARGS__);                                        \
    }                                                                                  \
  } while (false)
#define LOG_DEBUG_INT(...)                                                           \
  do {                                                                               \
    if (bluetooth::common::InitFlags::GetLogLevelForTag(LOG_TAG) >= LOG_TAG_DEBUG) { \
      LOGWRAPPER(LOG_TAG_DEBUG, __VA_ARGS__);                                        \
    }                                                                                \
  } while (false)
#define LOG_VERBOSE_INT(...) LOGWRAPPER(LOG_TAG_VERBOSE, __VA_ARGS__)
#define LOG_DEBUG_INT(...) LOGWRAPPER(LOG_TAG_DEBUG, __VA_ARGS__)
#define LOG_INFO_INT(...) LOGWRAPPER(LOG_TAG_INFO, __VA_ARGS__)
#define LOG_WARN_INT(...) LOGWRAPPER(LOG_TAG_WARN, __VA_ARGS__)
#define LOG_ERROR_INT(...) LOGWRAPPER(LOG_TAG_ERROR, __VA_ARGS__)
+9 −8
Original line number Diff line number Diff line
@@ -15,14 +15,15 @@
 */
#pragma once

// TODO(b/305066880) - Deprecate once replaced with fmtlib implementation.
// These log levels may need to be mapped to system values. These values are
// used to control the log level via init flags.
// used to control the log level and should match
// `bluetooth::log_internal::Level`.
enum LogLevels {
  LOG_TAG_FATAL = 0,
  LOG_TAG_ERROR,
  LOG_TAG_WARN,
  LOG_TAG_NOTICE,
  LOG_TAG_INFO,
  LOG_TAG_DEBUG,
  LOG_TAG_VERBOSE
  LOG_TAG_VERBOSE = 2,
  LOG_TAG_DEBUG = 3,
  LOG_TAG_INFO = 4,
  LOG_TAG_WARN = 5,
  LOG_TAG_ERROR = 6,
  LOG_TAG_FATAL = 7,
};
+31 −13
Original line number Diff line number Diff line
@@ -23,23 +23,31 @@

#include "os/log_tags.h"

// TODO(b/305066880) - This implementation will replace this syslog
// implementation. Remove this file once everything is moved over to the new
// logging macros.
#include "bluetooth/log.h"

namespace bluetooth::log_internal {
extern Level GetLogLevelForTag(char const* tag);
}

namespace {
#define SYSLOG_IDENT "btadapterd"

const char kSyslogIdent[] = SYSLOG_IDENT;

// Map LOG_TAG_* to syslog mappings
const int kTagMap[] = {
    /*LOG_TAG_FATAL=*/LOG_CRIT,
    /*LOG_TAG_ERROR=*/LOG_ERR,
    /*LOG_TAG_WARN=*/LOG_WARNING,
    /*LOG_TAG_NOTICE=*/LOG_NOTICE,
    /*LOG_TAG_INFO=*/LOG_INFO,
    /*LOG_TAG_DEBUG=*/LOG_DEBUG,
const int kLevelMap[] = {
    /*LOG_TAG_VERBOSE=*/LOG_DEBUG,
    /*LOG_TAG_DEBUG=*/LOG_DEBUG,
    /*LOG_TAG_INFO=*/LOG_INFO,
    /*LOG_TAG_WARN=*/LOG_WARNING,
    /*LOG_TAG_ERROR=*/LOG_ERR,
    /*LOG_TAG_FATAL=*/LOG_CRIT,
};

static_assert(sizeof(kTagMap) / sizeof(kTagMap[0]) == LOG_TAG_VERBOSE + 1);
static_assert(sizeof(kLevelMap) / sizeof(kLevelMap[0]) == (LOG_TAG_FATAL - LOG_TAG_VERBOSE) + 1);

class SyslogWrapper {
 public:
@@ -55,18 +63,28 @@ class SyslogWrapper {
std::unique_ptr<SyslogWrapper> gSyslog;
}  // namespace

void write_syslog(int tag, const char* format, ...) {
void write_syslog(int level, const char* tag, const char* format, ...) {
  if (!gSyslog) {
    gSyslog = std::make_unique<SyslogWrapper>();
  }

  // I don't expect to see incorrect tags but making the check anyway so we
  // Filter out logs that don't meet level requirement.
  bluetooth::log_internal::Level current_level = bluetooth::log_internal::GetLogLevelForTag(tag);
  if (static_cast<bluetooth::log_internal::Level>(level) < current_level) {
    return;
  }

  // I don't expect to see incorrect levels but making the check anyway so we
  // don't go out of bounds in the array above.
  tag = tag <= LOG_TAG_VERBOSE ? tag : LOG_TAG_ERROR;
  int level = kTagMap[tag];
  if (level > LOG_TAG_FATAL) {
    level = LOG_TAG_ERROR;
  } else if (level < LOG_TAG_VERBOSE) {
    level = LOG_TAG_VERBOSE;
  }
  int syslog_level = kLevelMap[level - LOG_TAG_VERBOSE];

  va_list args;
  va_start(args, format);
  vsyslog(level, format, args);
  vsyslog(syslog_level, format, args);
  va_end(args);
}
Loading