Skip to content

Commit 42a3be8

Browse files
committed
Clarified comments in Logger.cpp and ensured implementation disables auto-init more aggressively
1 parent cf6fc71 commit 42a3be8

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

OpenSim/Common/Logger.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,20 @@ static bool other_init = []() {
5252
return true;
5353
}();
5454

55-
// the file log sink (e.g. `opensim.log`) is *not* necessarily initialized
56-
// during static initialization time. It is only initialized when the first log
57-
// message is about to be written. Users *may* disable this functionality
58-
// before the first log message is written.
55+
// the file log sink (e.g. `opensim.log`) is lazily initialized - potentially
56+
// during static initialization time.
57+
//
58+
// it is only initialized when the first log message is about to be written.
59+
// Users *may* disable this functionality before the first log message is
60+
// written.
5961
static std::shared_ptr<spdlog::sinks::basic_file_sink_mt> m_filesink = nullptr;
6062

61-
// if a user calls `Logger::removeFileSink` before m_filesink is initialized, a
62-
// flag should be set to ensure that the "first use" initialization does not
63-
// subsequently happen.
63+
// if a user manually calls `Logger::(remove|add)FileSink`, auto-initialization
64+
// should be entirely disabled
6465
static bool filesink_auto_initialization_disabled = false;
6566

66-
// when filesink initialization *may* happen, ensure it only happens exactly
67-
// once globally by wrapping it in a function-local static.
67+
// when auto-initialization of the file log *may* happen, ensure that it can
68+
// only happen exactly once globally
6869
static bool initFileLoggingAsNeeded() {
6970
#ifdef OPENSIM_DISABLE_LOG_FILE
7071
// software builders may want to statically ensure that automatic file logging
@@ -213,6 +214,14 @@ bool Logger::shouldLog(Level level) {
213214
}
214215

215216
void Logger::addFileSink(const std::string& filepath) {
217+
// this method is either called by the file log auto-initializer, which
218+
// should now be disabled, or by downstream code trying to manually specify
219+
// a file sink
220+
//
221+
// downstream callers would find it quite surprising if the auto-initializer
222+
// runs *after* they manually specify a log, so just disable it
223+
filesink_auto_initialization_disabled = true;
224+
216225
if (m_filesink) {
217226
default_logger->warn("Already logging to file '{}'; log file not added. Call "
218227
"removeFileSink() first.", m_filesink->filename());
@@ -235,12 +244,16 @@ void Logger::addFileSink(const std::string& filepath) {
235244
}
236245

237246
void Logger::removeFileSink() {
247+
// if this method is called, then we are probably at a point in the
248+
// application's lifetime where automatic log allocation is going to cause
249+
// confusion.
250+
//
251+
// callers will be surpised if, after calling this method, auto
252+
// initialization happens afterwards and the log file still exists - even
253+
// if they called it to remove some manually-specified log
254+
filesink_auto_initialization_disabled = true;
255+
238256
if (m_filesink == nullptr) {
239-
// the user called `removeFileSink` before any messages passed through
240-
// the logger (which would initialize it) and before calling
241-
// `addFileSink` themselves (which would also initialize it), so they
242-
// *probably* want to completely disable automatic initialization.
243-
filesink_auto_initialization_disabled = true;
244257
return;
245258
}
246259

0 commit comments

Comments
 (0)