Skip to content

Commit ca94b20

Browse files
committed
Swift: auto-flush logs on errors
1 parent 2904aa8 commit ca94b20

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

swift/logging/SwiftLogging.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ BINLOG_ADAPT_ENUM(codeql::Log::Level, trace, debug, info, warning, error, critic
1010

1111
namespace codeql {
1212

13+
bool Log::initialized{false};
14+
1315
namespace {
1416
using LevelRule = std::pair<std::regex, Log::Level>;
1517
using LevelRules = std::vector<LevelRule>;
@@ -149,6 +151,7 @@ void Log::configure() {
149151
}
150152
LOG_INFO("Logging configured (binary: {}, text: {}, console: {})", binary.level, text.level,
151153
console.level);
154+
initialized = true;
152155
flushImpl();
153156
}
154157

swift/logging/SwiftLogging.h

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@
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__)
@@ -49,10 +52,10 @@
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

Comments
 (0)