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

Commit 0cac2866 authored by Henri Chataing's avatar Henri Chataing
Browse files

system/log: Implement source_location helper

Gather file_name, function_name, line to make
passing these parameters around easier

Test: atest libbluetooth_log_test
Bug: 305066880
Flag: EXEMPT, log change
Change-Id: I9325f2cbd2ce104ec8afaa64cc9e087c0735eddb
parent bbaf43e2
Loading
Loading
Loading
Loading
+21 −11
Original line number Diff line number Diff line
@@ -38,11 +38,24 @@ enum Level {
  kFatal = 7,
};

/// Information about the location a log is printed from.
/// Passing this parameter by default value will fill in
/// the correct information.
struct source_location {
  source_location(char const* file_name = __builtin_FILE(),
                  int line = __builtin_LINE(),
                  char const* function_name = __builtin_FUNCTION())
      : line(line), file_name(file_name), function_name(function_name) {}

  int line;
  char const* file_name;
  char const* function_name;
};

/// Write a single log line.
/// The implementation of this function is dependent on the backend.
void vlog(Level level, char const* tag, char const* file_name, int line,
          char const* function_name, fmt::string_view fmt,
          fmt::format_args vargs);
void vlog(Level level, char const* tag, source_location location,
          fmt::string_view fmt, fmt::format_args vargs);

/// Capture invalid parameter values that would cause runtime
/// formatting errors.
@@ -70,10 +83,8 @@ char*& format_replace(char*& arg) {
template <Level level, typename... T>
struct log {
  log(fmt::format_string<T...> fmt, T&&... args,
      char const* file_name = __builtin_FILE(), int line = __builtin_LINE(),
      char const* function_name = __builtin_FUNCTION()) {
    vlog(level, LOG_TAG, file_name, line, function_name,
         static_cast<fmt::string_view>(fmt),
      source_location location = source_location()) {
    vlog(level, LOG_TAG, location, static_cast<fmt::string_view>(fmt),
         fmt::make_format_args(format_replace(args)...));
  }
};
@@ -149,11 +160,10 @@ verbose(fmt::format_string<T...>, T&&...) -> verbose<T...>;
template <typename... T>
struct fatal_if {
  fatal_if(bool cond, fmt::format_string<T...> fmt, T&&... args,
           char const* file_name = __builtin_FILE(),
           int line = __builtin_LINE(),
           char const* function_name = __builtin_FUNCTION()) {
           log_internal::source_location location =
               log_internal::source_location()) {
    if (cond) {
      vlog(log_internal::kFatal, LOG_TAG, file_name, line, function_name,
      vlog(log_internal::kFatal, LOG_TAG, location,
           static_cast<fmt::string_view>(fmt),
           fmt::make_format_args(format_replace(args)...));
    }
+5 −5
Original line number Diff line number Diff line
@@ -23,9 +23,8 @@ namespace bluetooth::log_internal {

static constexpr size_t kBufferSize = 1024;

void vlog(Level level, char const* tag, char const* file_name, int line,
          char const* function_name, fmt::string_view fmt,
          fmt::format_args vargs) {
void vlog(Level level, char const* tag, source_location location,
          fmt::string_view fmt, fmt::format_args vargs) {
  // Check if log is enabled.
  if (!__android_log_is_loggable(level, tag, ANDROID_LOG_INFO) &&
      !__android_log_is_loggable(level, "bluetooth", ANDROID_LOG_INFO)) {
@@ -38,8 +37,9 @@ void vlog(Level level, char const* tag, char const* file_name, int line,
  // In order to have consistent logs we include it manually in the log
  // message.
  truncating_buffer<kBufferSize> buffer;
  fmt::format_to(std::back_insert_iterator(buffer), "{}:{} {}: ", file_name,
                 line, function_name);
  fmt::format_to(std::back_insert_iterator(buffer),
                 "{}:{} {}: ", location.file_name, location.line,
                 location.function_name);
  fmt::vformat_to(std::back_insert_iterator(buffer), fmt, vargs);

  // Send message to liblog.
+3 −4
Original line number Diff line number Diff line
@@ -48,9 +48,8 @@ Level GetDefaultLogLevel() { return gDefaultLogLevel; }
// Default value for $MaxMessageSize for rsyslog.
static constexpr size_t kBufferSize = 8192;

void vlog(Level level, char const* tag, char const* file_name, int line,
          char const* function_name, fmt::string_view fmt,
          fmt::format_args vargs) {
void vlog(Level level, char const* tag, source_location location,
          fmt::string_view fmt, fmt::format_args vargs) {
  // Filter out logs that don't meet level requirement.
  Level current_level = GetLogLevelForTag(tag);
  if (level < current_level) {
@@ -84,7 +83,7 @@ void vlog(Level level, char const* tag, char const* file_name, int line,

  // Format file, line.
  fmt::format_to(std::back_insert_iterator(buffer), "{} {}:{} {}: ", tag,
                 file_name, line, function_name);
                 location.file_name, location.line, location.function_name);

  // Format message.
  fmt::vformat_to(std::back_insert_iterator(buffer), fmt, vargs);