Skip to content

Commit

Permalink
Use absl::NoDestructor for global log sinks.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 583235586
Change-Id: Ia472b8d6530fd829fed1c07558e152975d9b24ac
  • Loading branch information
Abseil Team authored and copybara-github committed Nov 17, 2023
1 parent 524ebb7 commit 8197f8f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions absl/log/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ absl_cc_library(
absl::log_entry
absl::log_severity
absl::log_sink
absl::no_destructor
absl::raw_logging_internal
absl::synchronization
absl::span
Expand Down
1 change: 1 addition & 0 deletions absl/log/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ cc_library(
"//absl/base:config",
"//absl/base:core_headers",
"//absl/base:log_severity",
"//absl/base:no_destructor",
"//absl/base:raw_logging_internal",
"//absl/cleanup",
"//absl/log:globals",
Expand Down
16 changes: 8 additions & 8 deletions absl/log/internal/log_sink_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "absl/base/config.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/base/log_severity.h"
#include "absl/base/no_destructor.h"
#include "absl/base/thread_annotations.h"
#include "absl/cleanup/cleanup.h"
#include "absl/log/globals.h"
Expand Down Expand Up @@ -168,17 +169,16 @@ class GlobalLogSinkSet final {
#if defined(__myriad2__) || defined(__Fuchsia__)
// myriad2 and Fuchsia do not log to stderr by default.
#else
static StderrLogSink* stderr_log_sink = new StderrLogSink;
AddLogSink(stderr_log_sink);
static absl::NoDestructor<StderrLogSink> stderr_log_sink;
AddLogSink(stderr_log_sink.get());
#endif
#ifdef __ANDROID__
static AndroidLogSink* android_log_sink = new AndroidLogSink;
AddLogSink(android_log_sink);
static absl::NoDestructor<AndroidLogSink> android_log_sink;
AddLogSink(android_log_sink.get());
#endif
#if defined(_WIN32)
static WindowsDebuggerLogSink* debugger_log_sink =
new WindowsDebuggerLogSink;
AddLogSink(debugger_log_sink);
static absl::NoDestructor<WindowsDebuggerLogSink> debugger_log_sink;
AddLogSink(debugger_log_sink.get());
#endif // !defined(_WIN32)
}

Expand Down Expand Up @@ -268,7 +268,7 @@ class GlobalLogSinkSet final {

// Returns reference to the global LogSinks set.
GlobalLogSinkSet& GlobalSinks() {
static GlobalLogSinkSet* global_sinks = new GlobalLogSinkSet;
static absl::NoDestructor<GlobalLogSinkSet> global_sinks;
return *global_sinks;
}

Expand Down

0 comments on commit 8197f8f

Please sign in to comment.