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

Commit d2a63095 authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge "C Logging: Strip android repo location prefix" into main

parents e8660f50 8543ceef
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -21,6 +21,9 @@

namespace bluetooth::log_internal {

static constexpr std::string_view kAndroidRepoLocation =
    "packages/modules/Bluetooth/";

static constexpr size_t kBufferSize = 1024;

void vlog(Level level, char const* tag, source_location location,
@@ -31,15 +34,21 @@ void vlog(Level level, char const* tag, source_location location,
    return;
  }

  // Strip prefix of file_name to remove kAndroidRepoLocation if present
  const char* file_name = location.file_name;
  if (strncmp(kAndroidRepoLocation.data(), location.file_name,
              kAndroidRepoLocation.size()) == 0) {
    file_name = location.file_name + kAndroidRepoLocation.size();
  }

  // Format to stack buffer.
  // liblog uses a different default depending on the execution context
  // (host or device); the file and line are not systematically included.
  // 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),
                 "{}:{} {}: ", location.file_name, location.line,
                 location.function_name);
  fmt::format_to(std::back_insert_iterator(buffer), "{}:{} {}: ", file_name,
                 location.line, location.function_name);
  fmt::vformat_to(std::back_insert_iterator(buffer), fmt, vargs);

  // Send message to liblog.
+8 −16
Original line number Diff line number Diff line
@@ -54,8 +54,7 @@ TEST(BluetoothLogTest, verbose) {
  EXPECT_EQ(androidLogMessage->file, nullptr);
  EXPECT_EQ(androidLogMessage->line, 0);
  EXPECT_STREQ(androidLogMessage->message,
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:49 "
               "TestBody: verbose test");
               "system/log/src/vlog_test.cc:49 TestBody: verbose test");
}

TEST(BluetoothLogTest, debug) {
@@ -69,8 +68,7 @@ TEST(BluetoothLogTest, debug) {
  EXPECT_STREQ(androidLogMessage->file, nullptr);
  EXPECT_EQ(androidLogMessage->line, 0);
  EXPECT_STREQ(androidLogMessage->message,
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:64 "
               "TestBody: debug test");
               "system/log/src/vlog_test.cc:63 TestBody: debug test");
}

TEST(BluetoothLogTest, info) {
@@ -84,8 +82,7 @@ TEST(BluetoothLogTest, info) {
  EXPECT_STREQ(androidLogMessage->file, nullptr);
  EXPECT_EQ(androidLogMessage->line, 0);
  EXPECT_STREQ(androidLogMessage->message,
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:79 "
               "TestBody: info test");
               "system/log/src/vlog_test.cc:77 TestBody: info test");
}

TEST(BluetoothLogTest, warn) {
@@ -99,8 +96,7 @@ TEST(BluetoothLogTest, warn) {
  EXPECT_STREQ(androidLogMessage->file, nullptr);
  EXPECT_EQ(androidLogMessage->line, 0);
  EXPECT_STREQ(androidLogMessage->message,
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:94 "
               "TestBody: warn test");
               "system/log/src/vlog_test.cc:91 TestBody: warn test");
}

TEST(BluetoothLogTest, error) {
@@ -114,8 +110,7 @@ TEST(BluetoothLogTest, error) {
  EXPECT_STREQ(androidLogMessage->file, nullptr);
  EXPECT_EQ(androidLogMessage->line, 0);
  EXPECT_STREQ(androidLogMessage->message,
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:109 "
               "TestBody: error test");
               "system/log/src/vlog_test.cc:105 TestBody: error test");
}

TEST(BluetoothLogDeathTest, fatal) {
@@ -166,22 +161,19 @@ TEST(BluetoothLogTest, null_string_parameter) {
  char const* const_null_str = nullptr;
  log::info("input: {}", const_null_str);
  EXPECT_STREQ(androidLogMessage->message,
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:167 "
               "TestBody: input: (nullptr)");
               "system/log/src/vlog_test.cc:162 TestBody: input: (nullptr)");

  androidLogMessage.reset();

  char* null_str = nullptr;
  log::info("input: {}", null_str);
  EXPECT_STREQ(androidLogMessage->message,
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:175 "
               "TestBody: input: (nullptr)");
               "system/log/src/vlog_test.cc:169 TestBody: input: (nullptr)");

  androidLogMessage.reset();

  char const* nonnull_str = "hello world";
  log::info("input: {}", nonnull_str);
  EXPECT_STREQ(androidLogMessage->message,
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:183 "
               "TestBody: input: hello world");
               "system/log/src/vlog_test.cc:176 TestBody: input: hello world");
}