Skip to content

Commit 1d0463a

Browse files
committed
Simplify static initialization order fix
1 parent bc66bb4 commit 1d0463a

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

OpenSim/Common/Logger.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,30 @@ using namespace OpenSim;
4343
// completely initialized before that happens the first time.
4444

4545
// Initialize a logger
46-
static void initializeLogger(spdlog::logger& l, const char* pattern) {
47-
l.set_level(spdlog::level::info);
48-
l.flush_on(spdlog::level::info);
49-
l.set_pattern(pattern);
46+
static std::shared_ptr<spdlog::logger> initializeLogger(
47+
std::shared_ptr<spdlog::logger> l,
48+
const char* pattern)
49+
{
50+
l->set_level(spdlog::level::info);
51+
l->flush_on(spdlog::level::info);
52+
l->set_pattern(pattern);
53+
return l;
5054
}
5155

5256
// Return a reference to the static cout logger object, allocating and
5357
// initializing it on the first call.
5458
static spdlog::logger& coutLoggerInternal() {
55-
static std::shared_ptr<spdlog::logger> l = spdlog::stdout_color_mt("cout");
56-
static bool first = true;
57-
if (first) {
58-
initializeLogger(*l, "%v");
59-
first = false;
60-
}
61-
return *l;
59+
static std::shared_ptr<spdlog::logger> l =
60+
initializeLogger(spdlog::stdout_color_mt("cout"), "%v");
61+
return *l;
6262
}
6363

6464
// Return a reference to the static default logger object, allocating and
6565
// initializing it on the first call.
6666
static spdlog::logger& defaultLoggerInternal() {
67-
static std::shared_ptr<spdlog::logger> l = spdlog::default_logger();
68-
static bool first = true;
69-
if (first) {
70-
initializeLogger(*l, "[%l] %v");
71-
first = false;
72-
}
73-
return *l;
67+
static std::shared_ptr<spdlog::logger> l =
68+
initializeLogger(spdlog::default_logger(), "[%l] %v");
69+
return *l;
7470
}
7571

7672
// the file log sink (e.g. `opensim.log`) is lazily initialized.

0 commit comments

Comments
 (0)