Skip to content

Commit

Permalink
add statically initialize method for LoggerManager
Browse files Browse the repository at this point in the history
  • Loading branch information
oathdruid committed Aug 6, 2024
1 parent 90be52a commit 841c564
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 51 deletions.
5 changes: 3 additions & 2 deletions src/babylon/logging/log_severity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/babylon/logging/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
7 changes: 6 additions & 1 deletion src/babylon/logging/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -102,6 +102,11 @@ class LoggerManager final {
_builders;
};

class DefaultLoggerManagerInitializer {
public:
static void initialize(LoggerManager& manager) noexcept;
};

// 通过&操作符将返回值转为void的工具类
// 辅助BABYLON_LOG宏实现条件日志
class Voidify {
Expand Down
2 changes: 1 addition & 1 deletion test/concurrent/test_bounded_queue_press_mpmc.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "babylon/concurrent/bounded_queue.h"
#include "babylon/logging/interface.h"
#include "babylon/logging/logger.h"

#include "gtest/gtest.h"

Expand Down
6 changes: 3 additions & 3 deletions test/logging/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]
)
Expand Down
44 changes: 0 additions & 44 deletions test/logging/test_custom_default_provider.cpp

This file was deleted.

29 changes: 29 additions & 0 deletions test/logging/test_statically_initialize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "babylon/logging/logger.h"

#include <gtest/gtest.h>

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"));
}

0 comments on commit 841c564

Please sign in to comment.