@@ -52,19 +52,20 @@ static bool other_init = []() {
52
52
return true ;
53
53
}();
54
54
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.
59
61
static std::shared_ptr<spdlog::sinks::basic_file_sink_mt> m_filesink = nullptr ;
60
62
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
64
65
static bool filesink_auto_initialization_disabled = false ;
65
66
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
68
69
static bool initFileLoggingAsNeeded () {
69
70
#ifdef OPENSIM_DISABLE_LOG_FILE
70
71
// software builders may want to statically ensure that automatic file logging
@@ -213,6 +214,14 @@ bool Logger::shouldLog(Level level) {
213
214
}
214
215
215
216
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
+
216
225
if (m_filesink) {
217
226
default_logger->warn (" Already logging to file '{}'; log file not added. Call "
218
227
" removeFileSink() first." , m_filesink->filename ());
@@ -235,12 +244,16 @@ void Logger::addFileSink(const std::string& filepath) {
235
244
}
236
245
237
246
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
+
238
256
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 ;
244
257
return ;
245
258
}
246
259
0 commit comments