Skip to content

Commit

Permalink
Expose IsGoogleLoggingInitialized() in public API.
Browse files Browse the repository at this point in the history
Usually library does not have control of the process lifespan.
Without this function, it is impossible to init/shutdown reliably.
It has been one of the major pain points for years when using glog in libraries.

AFAIK 3 workarounds have been used previously:
1. Init without checking. This causes compatiblity issues with other libs using glog.
2. Also provide a init function in library's API. This makes API complicated and stateful, especially for libs that does not mean to stay for the entire life of process.
3. Steal the utility function in internal namespace. Does not work with msvc (due to missing dllexport) or `gcc -fvisibility=hidden`.

None of them are perfect, except for the last hack that usually works well on Linux.
0.5.0 changes default visibility to hidden and it does not work anymore.

Resolve #125
  • Loading branch information
xkszltl committed May 13, 2021
1 parent d5c04ee commit 5e593fd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,8 @@ GOOGLE_GLOG_DLL_DECL void InitGoogleLogging(const char* argv0,
void* prefix_callback_data = NULL);
#endif

GOOGLE_GLOG_DLL_DECL bool IsGoogleLoggingInitialized();

// Shutdown google's logging library.
GOOGLE_GLOG_DLL_DECL void ShutdownGoogleLogging();

Expand Down
4 changes: 4 additions & 0 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2568,6 +2568,10 @@ void InitGoogleLogging(const char* argv0,
}
#endif

bool IsGoogleLoggingInitialized() {
return glog_internal_namespace_::IsGoogleLoggingInitialized();
}

void ShutdownGoogleLogging() {
glog_internal_namespace_::ShutdownGoogleLoggingUtilities();
LogDestination::DeleteLogDestinations();
Expand Down
6 changes: 6 additions & 0 deletions src/logging_custom_prefix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,15 @@ int main(int argc, char **argv) {
LogWithLevels(0, 0, 0, 0); // simulate "before global c-tors"
const string early_stderr = GetCapturedTestStderr();

EXPECT_FALSE(::google::IsGoogleLoggingInitialized());

// Setting a custom prefix generator (it will use the default format so that
// the golden outputs can be reused):
string prefix_attacher_data = "good data";
InitGoogleLogging(argv[0], &PrefixAttacher, static_cast<void*>(&prefix_attacher_data));

EXPECT_TRUE(::google::IsGoogleLoggingInitialized());

RunSpecifiedBenchmarks();

FLAGS_logtostderr = true;
Expand Down Expand Up @@ -992,8 +996,10 @@ static void TestCustomLoggerDeletionOnShutdown() {
base::SetLogger(GLOG_INFO,
new RecordDeletionLogger(&custom_logger_deleted,
base::GetLogger(GLOG_INFO)));
EXPECT_TRUE(::google::IsGoogleLoggingInitialized());
ShutdownGoogleLogging();
EXPECT_TRUE(custom_logger_deleted);
EXPECT_FALSE(::google::IsGoogleLoggingInitialized());
}

_START_GOOGLE_NAMESPACE_
Expand Down
6 changes: 6 additions & 0 deletions src/logging_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,12 @@ int main(int argc, char **argv) {
LogWithLevels(0, 0, 0, 0); // simulate "before global c-tors"
const string early_stderr = GetCapturedTestStderr();

EXPECT_FALSE(::google::IsGoogleLoggingInitialized());

InitGoogleLogging(argv[0]);

EXPECT_TRUE(::google::IsGoogleLoggingInitialized());

RunSpecifiedBenchmarks();

FLAGS_logtostderr = true;
Expand Down Expand Up @@ -965,8 +969,10 @@ static void TestCustomLoggerDeletionOnShutdown() {
base::SetLogger(GLOG_INFO,
new RecordDeletionLogger(&custom_logger_deleted,
base::GetLogger(GLOG_INFO)));
EXPECT_TRUE(::google::IsGoogleLoggingInitialized());
ShutdownGoogleLogging();
EXPECT_TRUE(custom_logger_deleted);
EXPECT_FALSE(::google::IsGoogleLoggingInitialized());
}

_START_GOOGLE_NAMESPACE_
Expand Down

0 comments on commit 5e593fd

Please sign in to comment.