3232
3333// only do the actual logging if the picked up `Logger` instance is configured to handle the
3434// provided log level. `LEVEL` must be a compile-time constant. `logger()` is evaluated once
35- #define LOG_WITH_LEVEL_AND_CATEGORY (LEVEL, CATEGORY, ...) \
36- do { \
37- constexpr codeql::Log::Level _level = codeql::Log::Level::LEVEL; \
38- codeql::Logger& _logger = logger (); \
39- if (_level >= _logger.level ()) { \
40- BINLOG_CREATE_SOURCE_AND_EVENT (_logger.writer (), _level, CATEGORY, binlog::clockNow (), \
41- __VA_ARGS__); \
42- } \
35+ #define LOG_WITH_LEVEL_AND_CATEGORY (LEVEL, CATEGORY, ...) \
36+ do { \
37+ constexpr codeql::Log::Level _level = ::codeql::Log::Level::LEVEL; \
38+ ::codeql::Logger& _logger = logger (); \
39+ if (_level >= _logger.level ()) { \
40+ BINLOG_CREATE_SOURCE_AND_EVENT (_logger.writer (), _level, CATEGORY, ::binlog::clockNow (), \
41+ __VA_ARGS__); \
42+ } \
43+ if (_level >= ::codeql::Log::Level::error) { \
44+ ::codeql::Log::flush (); \
45+ } \
4346 } while (false )
4447
4548#define LOG_WITH_LEVEL (LEVEL, ...) LOG_WITH_LEVEL_AND_CATEGORY(LEVEL, , __VA_ARGS__)
4952#define DIAGNOSE_CRITICAL (ID, ...) DIAGNOSE_WITH_LEVEL(critical, ID, __VA_ARGS__)
5053#define DIAGNOSE_ERROR (ID, ...) DIAGNOSE_WITH_LEVEL(error, ID, __VA_ARGS__)
5154
52- #define DIAGNOSE_WITH_LEVEL (LEVEL, ID, ...) \
53- do { \
54- codeql::SwiftDiagnosticsSource::ensureRegistered<&codeql_diagnostics::ID>(); \
55- LOG_WITH_LEVEL_AND_CATEGORY (LEVEL, ID, __VA_ARGS__); \
55+ #define DIAGNOSE_WITH_LEVEL (LEVEL, ID, ...) \
56+ do { \
57+ :: codeql::SwiftDiagnosticsSource::ensureRegistered<&:: codeql_diagnostics::ID>(); \
58+ LOG_WITH_LEVEL_AND_CATEGORY (LEVEL, ID, __VA_ARGS__); \
5659 } while (false )
5760
5861// avoid calling into binlog's original macros
@@ -111,7 +114,11 @@ class Log {
111114 };
112115
113116 // Flush logs to the designated outputs
114- static void flush () { instance ().flushImpl (); }
117+ static void flush () {
118+ if (initialized) {
119+ instance ().flushImpl ();
120+ }
121+ }
115122
116123 // create `Logger` configuration, used internally by `Logger`'s constructor
117124 static LoggerConfiguration getLoggerConfiguration (std::string_view name) {
@@ -120,6 +127,7 @@ class Log {
120127
121128 private:
122129 static constexpr const char * format = " %u %S [%n] %m (%G:%L)\n " ;
130+ static bool initialized;
123131
124132 Log () { configure (); }
125133
0 commit comments