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

Commit a267553e authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi
Browse files

floss: Update syslog filtering for libbluetooth

Add functions to set the default log level and log level by tag to
replicate the log filtering functionality previously implemented via
init flags.

Bug: 290841136
Bug: 305066880
Test: ./build.py; mmm packages/modules/Bluetooth
Flag: Exempt, logging change
Change-Id: Ic200323daac41eaffb5fc2a40a36bb22a5f3ac2f
parent 3494365d
Loading
Loading
Loading
Loading
+0 −2
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) {
+1 −2
Original line number Diff line number Diff line
@@ -102,8 +102,7 @@ static_assert(LOG_TAG != nullptr, "LOG_TAG should never be NULL");
#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(...) LOGWRAPPER(LOG_TAG_VERBOSE, __VA_ARGS__)
#define LOG_DEBUG_INT(...) LOGWRAPPER(LOG_TAG_DEBUG, __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);
}
+1 −1
Original line number Diff line number Diff line
@@ -26,4 +26,4 @@
/**
 * Write log to syslog.
 */
void write_syslog(int tag, const char* format, ...);
void write_syslog(int level, const char* tag, const char* format, ...);
Loading