diff --git a/src/babylon/logging/log_severity.h b/src/babylon/logging/log_severity.h index e6d4454c..71926e19 100644 --- a/src/babylon/logging/log_severity.h +++ b/src/babylon/logging/log_severity.h @@ -18,9 +18,10 @@ class LogSeverity { inline constexpr LogSeverity() noexcept = default; inline constexpr LogSeverity(LogSeverity&&) noexcept = default; inline constexpr LogSeverity(const LogSeverity&) noexcept = default; - inline CONSTEXPR_SINCE_CXX14 LogSeverity& operator=(LogSeverity&&) noexcept = default; - inline CONSTEXPR_SINCE_CXX14 LogSeverity& operator=(const LogSeverity&) noexcept = + inline CONSTEXPR_SINCE_CXX14 LogSeverity& operator=(LogSeverity&&) noexcept = default; + inline CONSTEXPR_SINCE_CXX14 LogSeverity& operator=( + const LogSeverity&) noexcept = default; inline ~LogSeverity() noexcept = default; inline constexpr LogSeverity(int8_t value) noexcept; diff --git a/src/babylon/logging/logger.cpp b/src/babylon/logging/logger.cpp index 54f1e0c1..55433f33 100644 --- a/src/babylon/logging/logger.cpp +++ b/src/babylon/logging/logger.cpp @@ -172,6 +172,10 @@ Logger& LoggerManager::get_logger(StringView name) noexcept { return result.first->second; } +LoggerManager::LoggerManager() noexcept { + DefaultLoggerManagerInitializer::initialize(*this); +} + void LoggerManager::apply_to(StringView name, Logger& logger) noexcept { auto builder = find_nearest_builder(name); if (builder != nullptr) { @@ -199,4 +203,7 @@ LoggerBuilder* LoggerManager::find_nearest_builder(StringView name) noexcept { // LoggerManager end //////////////////////////////////////////////////////////////////////////////// +ABSL_ATTRIBUTE_WEAK void DefaultLoggerManagerInitializer::initialize( + LoggerManager&) noexcept {} + BABYLON_NAMESPACE_END diff --git a/src/babylon/logging/logger.h b/src/babylon/logging/logger.h index 0ce846b7..6a41d501 100644 --- a/src/babylon/logging/logger.h +++ b/src/babylon/logging/logger.h @@ -85,7 +85,7 @@ class LoggerManager final { Logger& get_logger(StringView name) noexcept; private: - LoggerManager() = default; + LoggerManager() noexcept; ~LoggerManager() noexcept = default; void apply_to(StringView name, Logger& logger) noexcept; @@ -102,6 +102,11 @@ class LoggerManager final { _builders; }; +class DefaultLoggerManagerInitializer { + public: + static void initialize(LoggerManager& manager) noexcept; +}; + // 通过&操作符将返回值转为void的工具类 // 辅助BABYLON_LOG宏实现条件日志 class Voidify { diff --git a/test/concurrent/test_bounded_queue_press_mpmc.cpp b/test/concurrent/test_bounded_queue_press_mpmc.cpp index 8e02ba29..4ff5622d 100644 --- a/test/concurrent/test_bounded_queue_press_mpmc.cpp +++ b/test/concurrent/test_bounded_queue_press_mpmc.cpp @@ -1,5 +1,5 @@ #include "babylon/concurrent/bounded_queue.h" -#include "babylon/logging/interface.h" +#include "babylon/logging/logger.h" #include "gtest/gtest.h" diff --git a/test/logging/BUILD b/test/logging/BUILD index 5c1d95c1..3b921b6d 100644 --- a/test/logging/BUILD +++ b/test/logging/BUILD @@ -25,11 +25,11 @@ cc_test( ) cc_test( - name = 'test_custom_default_provider', - srcs = ['test_custom_default_provider.cpp'], + name = 'test_statically_initialize', + srcs = ['test_statically_initialize.cpp'], copts = BABYLON_COPTS, deps = [ - '//:logging_interface', + '//:logging_logger', '@com_google_googletest//:gtest_main', ] ) diff --git a/test/logging/test_custom_default_provider.cpp b/test/logging/test_custom_default_provider.cpp deleted file mode 100644 index bf01f21a..00000000 --- a/test/logging/test_custom_default_provider.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "babylon/logging/interface.h" -#include "babylon/logging/logger.h" - -#include - -using ::babylon::LogInterface; -using ::babylon::LogStream; -using ::babylon::LogStreamProvider; -using ::babylon::StringView; - -struct MockLogStreamProvider : public LogStreamProvider { - virtual LogStream& stream(int severity, StringView file, - int line) noexcept override { - this->severity = severity; - this->file = file; - this->line = line; - return ls; - } - - ::std::stringstream ss; - LogStream ls {*ss.rdbuf()}; - StringView file; - int line {-1}; - int severity {-1}; -}; - -static MockLogStreamProvider provider; - -BABYLON_NAMESPACE_BEGIN -LogStreamProvider* LogInterface::_s_provider {&::provider}; -BABYLON_NAMESPACE_END - -TEST(LogInterfaceTest, default_log_to_custom__backend) { - auto file = __FILE__; - auto before_line = __LINE__; - BABYLON_LOG(INFO) << "this line should appear in provider"; - auto after_line = __LINE__; - ASSERT_NE(::std::string::npos, - provider.ss.str().find("this line should appear in provider")); - ASSERT_EQ(file, provider.file); - ASSERT_LT(before_line, provider.line); - ASSERT_GT(after_line, provider.line); - ASSERT_TRUE(LogInterface::SEVERITY_INFO == provider.severity); -} diff --git a/test/logging/test_statically_initialize.cpp b/test/logging/test_statically_initialize.cpp new file mode 100644 index 00000000..653de2e2 --- /dev/null +++ b/test/logging/test_statically_initialize.cpp @@ -0,0 +1,29 @@ +#include "babylon/logging/logger.h" + +#include + +using ::babylon::LogInterface; +using ::babylon::LogStream; +using ::babylon::LogStreamProvider; +using ::babylon::StringView; + +::std::stringbuf buffer; + +BABYLON_NAMESPACE_BEGIN +void DefaultLoggerManagerInitializer::initialize( + LoggerManager& manager) noexcept { + LoggerBuilder builder; + builder.set_log_stream_creator([] { + auto ptr = new ::babylon::LogStream(buffer); + return ::std::unique_ptr<::babylon::LogStream>(ptr); + }); + manager.set_root_builder(::std::move(builder)); + manager.apply(); +} +BABYLON_NAMESPACE_END + +TEST(DefaultLoggerManagerInitializer, custom_logger_manager_statically) { + BABYLON_LOG(INFO) << "this line should appear in provider"; + ASSERT_NE(::std::string::npos, + buffer.str().find("this line should appear in provider")); +}