Skip to content

Commit

Permalink
ensure log usable in static initialize stage
Browse files Browse the repository at this point in the history
  • Loading branch information
oathdruid committed Aug 7, 2024
1 parent 0d99ef3 commit 59d72c9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/babylon/logging/log_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ void LogStream::do_end() noexcept {}
DefaultLogStream::DefaultLogStream() noexcept
:
#if __clang__ || BABYLON_GCC_VERSION >= 50000
LogStream {*::std::cerr.rdbuf()} {
}
LogStream {*::std::cerr.rdbuf()}
#else // !__clang__ && BABYLON_GCC_VERSION < 50000
LogStream(*::std::cerr.rdbuf()) {
}
LogStream(*::std::cerr.rdbuf())
#endif // !__clang__ && BABYLON_GCC_VERSION < 50000
{
// Ensure std::cerr is initialized
::std::ios_base::Init();
rdbuf(::std::cerr.rdbuf());
}

::std::mutex& DefaultLogStream::mutex() noexcept {
static ::std::mutex mutex;
Expand Down Expand Up @@ -61,14 +64,17 @@ void DefaultLogStream::do_end() noexcept {
NullLogStream::NullLogStream() noexcept
:
#if __clang__ || BABYLON_GCC_VERSION >= 50000
LogStream {s_buffer} {
LogStream {buffer()} {
}
#else // !__clang__ && BABYLON_GCC_VERSION < 50000
LogStream(s_buffer) {
LogStream(buffer()) {
}
#endif // !__clang__ && BABYLON_GCC_VERSION < 50000

NullLogStream::Buffer NullLogStream::s_buffer;
NullLogStream::Buffer& NullLogStream::buffer() noexcept {
static Buffer static_buffer;
return static_buffer;
}
// NullLogStream end
////////////////////////////////////////////////////////////////////////////////

Expand Down
3 changes: 2 additions & 1 deletion src/babylon/logging/log_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ class NullLogStream : public LogStream {

private:
class Buffer;
static Buffer s_buffer;

static Buffer& buffer() noexcept;
};

////////////////////////////////////////////////////////////////////////////////
Expand Down
11 changes: 11 additions & 0 deletions test/logging/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ cc_test(
]
)

cc_test(
name = 'test_log_statically',
srcs = ['test_log_statically.cpp'],
copts = BABYLON_COPTS,
linkstatic = True,
deps = [
'//:logging_logger',
'@com_google_googletest//:gtest_main',
]
)

cc_test(
name = 'test_rolling_file_object',
srcs = ['test_rolling_file_object.cpp'],
Expand Down
9 changes: 9 additions & 0 deletions test/logging/test_log_statically.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "babylon/logging/logger.h"

// Do BABYLON_LOG at very begining of the program.
// Check whether log system is ensured to be ready in default state
__attribute__((init_priority(101))) static struct StaticallyInitialized {
StaticallyInitialized() {
BABYLON_LOG(INFO) << "log in static initialize stage is fine";
}
} statically_initialized_instance;

0 comments on commit 59d72c9

Please sign in to comment.