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

Commit bbaf43e2 authored by Henri Chataing's avatar Henri Chataing
Browse files

system/log: Implement log::fatal_if helper

Will replace all kinds of assert macros currently in
use in the stack.

Test: atest libbluetooth_log_test
Bug: 305066880
Flag: EXEMPT, log change
Change-Id: Ie0fe8cc8d2c8cc110b2610f2eb41ed8eb1db3450
parent dd94bd05
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -146,6 +146,23 @@ verbose(fmt::format_string<T...>, T&&...) -> verbose<T...>;

#endif  // GCC / C++20

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()) {
    if (cond) {
      vlog(log_internal::kFatal, LOG_TAG, file_name, line, function_name,
           static_cast<fmt::string_view>(fmt),
           fmt::make_format_args(format_replace(args)...));
    }
  }
};

template <typename... T>
fatal_if(fmt::format_string<T...>, T&&...) -> fatal_if<T...>;

}  // namespace bluetooth::log

namespace fmt {
+12 −3
Original line number Diff line number Diff line
@@ -118,13 +118,22 @@ TEST(BluetoothLoggerTest, error) {
               "TestBody: error test");
}

TEST(BluetoothLoggerTest, fatal_if) {
  androidLogMessage.reset();

  log::fatal_if(false, "fatal_if test false");

  ASSERT_DEATH(
      { log::fatal_if(true, "fatal_if test true"); }, "fatal_if test true");
}

TEST(BluetoothLoggerTest, null_string_parameter) {
  androidLogMessage.reset();

  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:125 "
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:134 "
               "TestBody: input: (nullptr)");

  androidLogMessage.reset();
@@ -132,7 +141,7 @@ TEST(BluetoothLoggerTest, null_string_parameter) {
  char* null_str = nullptr;
  log::info("input: {}", null_str);
  EXPECT_STREQ(androidLogMessage->message,
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:133 "
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:142 "
               "TestBody: input: (nullptr)");

  androidLogMessage.reset();
@@ -140,6 +149,6 @@ TEST(BluetoothLoggerTest, null_string_parameter) {
  char const* nonnull_str = "hello world";
  log::info("input: {}", nonnull_str);
  EXPECT_STREQ(androidLogMessage->message,
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:141 "
               "packages/modules/Bluetooth/system/log/src/vlog_test.cc:150 "
               "TestBody: input: hello world");
}