log: prefer once_flag over an atomic<bool>
This commit is contained in:
parent
e8f23a349e
commit
f178159fba
@ -47,7 +47,7 @@ cruft::log::needs_break (level_t level)
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// mingw#xxx: MinGW doesn't have once_flag so we just use an atomic bool.
|
// mingw#xxx: MinGW doesn't have once_flag so we just use an atomic bool.
|
||||||
// Pray that multiple threads don't run into an issue here.
|
// Pray that multiple threads don't run into an issue here.
|
||||||
static std::atomic<bool> s_sink_init;
|
static std::once_flag s_sink_init;
|
||||||
static std::unique_ptr<cruft::log::sink::base> s_default_sink;
|
static std::unique_ptr<cruft::log::sink::base> s_default_sink;
|
||||||
|
|
||||||
|
|
||||||
@ -64,7 +64,7 @@ cruft::log::default_sink (std::unique_ptr<sink::base> value)
|
|||||||
cruft::log::sink::base&
|
cruft::log::sink::base&
|
||||||
cruft::log::default_sink ()
|
cruft::log::default_sink ()
|
||||||
{
|
{
|
||||||
if (unlikely (!s_sink_init)) {
|
std::call_once (s_sink_init, [&] () {
|
||||||
static char const *DEFAULT_SINK = "CONSOLE";
|
static char const *DEFAULT_SINK = "CONSOLE";
|
||||||
char const *log_sink = getenv ("CRUFT_LOG_SINK") ?: DEFAULT_SINK;
|
char const *log_sink = getenv ("CRUFT_LOG_SINK") ?: DEFAULT_SINK;
|
||||||
|
|
||||||
@ -76,9 +76,7 @@ cruft::log::default_sink ()
|
|||||||
std::clog << "Unknown logging sink. Falling back to console.\n";
|
std::clog << "Unknown logging sink. Falling back to console.\n";
|
||||||
default_sink (std::make_unique<sink::console> (PACKAGE_NAME));
|
default_sink (std::make_unique<sink::console> (PACKAGE_NAME));
|
||||||
}
|
}
|
||||||
|
});
|
||||||
s_sink_init = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return *s_default_sink;
|
return *s_default_sink;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user