From 1227873b8810ed0f82d4b85a3c19c9562fda0b91 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 2 Jul 2019 16:18:06 +0200 Subject: [PATCH] logging: refactor enabling of debug messages Reorder the LOGS_Severity enum in order of severity and change the code to not log/print messages with severity below the specified minimum instead of having a separate debug level. --- client.c | 4 ++-- logging.c | 23 +++++++++-------------- logging.h | 22 ++++++++++------------ main.c | 2 +- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/client.c b/client.c index 8736504..fcaaf3a 100644 --- a/client.c +++ b/client.c @@ -77,7 +77,7 @@ static int csv_mode = 0; /* Log a message. This is a minimalistic replacement of the logging.c implementation to avoid linking with it and other modules. */ -int log_debug_enabled = 0; +LOG_Severity log_min_severity = LOGS_INFO; void LOG_Message(LOG_Severity severity, #if DEBUG > 0 @@ -3222,7 +3222,7 @@ main(int argc, char **argv) csv_mode = 1; break; case 'd': - log_debug_enabled = 1; + log_min_severity = LOGS_DEBUG; break; case 'h': hostnames = optarg; diff --git a/logging.c b/logging.c index d2296e0..b6a0489 100644 --- a/logging.c +++ b/logging.c @@ -34,7 +34,7 @@ #include "util.h" /* This is used by DEBUG_LOG macro */ -int log_debug_enabled = 0; +LOG_Severity log_min_severity = LOGS_INFO; /* ================================================== */ /* Flag indicating we have initialised */ @@ -45,10 +45,6 @@ static int system_log = 0; static int parent_fd = 0; -#define DEBUG_LEVEL_PRINT_FUNCTION 2 -#define DEBUG_LEVEL_PRINT_DEBUG 2 -static int debug_level = 0; - struct LogFile { const char *name; const char *banner; @@ -134,7 +130,7 @@ void LOG_Message(LOG_Severity severity, time_t t; struct tm *tm; - if (!system_log && file_log) { + if (!system_log && file_log && severity >= log_min_severity) { /* Don't clutter up syslog with timestamps and internal debugging info */ time(&t); tm = gmtime(&t); @@ -143,7 +139,7 @@ void LOG_Message(LOG_Severity severity, fprintf(file_log, "%s ", buf); } #if DEBUG > 0 - if (debug_level >= DEBUG_LEVEL_PRINT_FUNCTION) + if (log_min_severity <= LOGS_DEBUG) fprintf(file_log, "%s:%d:(%s) ", filename, line_number, function_name); #endif } @@ -157,10 +153,12 @@ void LOG_Message(LOG_Severity severity, case LOGS_INFO: case LOGS_WARN: case LOGS_ERR: - log_message(0, severity, buf); + if (severity >= log_min_severity) + log_message(0, severity, buf); break; case LOGS_FATAL: - log_message(1, severity, buf); + if (severity >= log_min_severity) + log_message(1, severity, buf); /* Send the message also to the foreground process if it is still running, or stderr if it is still open */ @@ -213,12 +211,9 @@ LOG_OpenSystemLog(void) /* ================================================== */ -void LOG_SetDebugLevel(int level) +void LOG_SetMinSeverity(LOG_Severity severity) { - debug_level = level; - if (level >= DEBUG_LEVEL_PRINT_DEBUG) { - log_debug_enabled = 1; - } + log_min_severity = CLAMP(LOGS_DEBUG, severity, LOGS_FATAL); } /* ================================================== */ diff --git a/logging.h b/logging.h index 67931dc..b034e52 100644 --- a/logging.h +++ b/logging.h @@ -31,9 +31,6 @@ #include "sysincl.h" -/* Flag indicating whether debug messages are logged */ -extern int log_debug_enabled; - /* Line logging macros. If the compiler is GNU C, we take advantage of being able to get the function name also. */ @@ -55,7 +52,7 @@ extern int log_debug_enabled; #define DEBUG_LOG(...) \ do { \ - if (DEBUG && log_debug_enabled) \ + if (DEBUG && log_min_severity == LOGS_DEBUG) \ LOG_MESSAGE(LOGS_DEBUG, __VA_ARGS__); \ } while (0) @@ -69,13 +66,16 @@ extern int log_debug_enabled; /* Definition of severity */ typedef enum { - LOGS_INFO, + LOGS_DEBUG = -1, + LOGS_INFO = 0, LOGS_WARN, LOGS_ERR, LOGS_FATAL, - LOGS_DEBUG } LOG_Severity; +/* Minimum severity of messages to be logged */ +extern LOG_Severity log_min_severity; + /* Init function */ extern void LOG_Initialise(void); @@ -92,12 +92,10 @@ FORMAT_ATTRIBUTE_PRINTF(2, 3) extern void LOG_Message(LOG_Severity severity, const char *format, ...); #endif -/* Set debug level: - 0, 1 - only non-debug messages are logged - 2 - debug messages are logged too, all messages are prefixed with - filename, line, and function name - */ -extern void LOG_SetDebugLevel(int level); +/* Set the minimum severity of a message to be logged or printed to terminal. + If the severity is LOGS_DEBUG and DEBUG is enabled, all messages will be + prefixed with the filename, line number, and function name. */ +extern void LOG_SetMinSeverity(LOG_Severity severity); /* Log messages to a file instead of stderr, or stderr again if NULL */ extern void LOG_OpenFileLog(const char *log_file); diff --git a/main.c b/main.c index 6ccf32e..39ebf5d 100644 --- a/main.c +++ b/main.c @@ -510,7 +510,7 @@ int main LOG_OpenSystemLog(); } - LOG_SetDebugLevel(debug); + LOG_SetMinSeverity(debug >= 2 ? LOGS_DEBUG : LOGS_INFO); LOG(LOGS_INFO, "chronyd version %s starting (%s)", CHRONY_VERSION, CHRONYD_FEATURES);