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.
This commit is contained in:
Miroslav Lichvar 2019-07-02 16:18:06 +02:00
parent d30e73d0d9
commit 1227873b88
4 changed files with 22 additions and 29 deletions

View file

@ -77,7 +77,7 @@ static int csv_mode = 0;
/* Log a message. This is a minimalistic replacement of the logging.c /* Log a message. This is a minimalistic replacement of the logging.c
implementation to avoid linking with it and other modules. */ 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, void LOG_Message(LOG_Severity severity,
#if DEBUG > 0 #if DEBUG > 0
@ -3222,7 +3222,7 @@ main(int argc, char **argv)
csv_mode = 1; csv_mode = 1;
break; break;
case 'd': case 'd':
log_debug_enabled = 1; log_min_severity = LOGS_DEBUG;
break; break;
case 'h': case 'h':
hostnames = optarg; hostnames = optarg;

View file

@ -34,7 +34,7 @@
#include "util.h" #include "util.h"
/* This is used by DEBUG_LOG macro */ /* This is used by DEBUG_LOG macro */
int log_debug_enabled = 0; LOG_Severity log_min_severity = LOGS_INFO;
/* ================================================== */ /* ================================================== */
/* Flag indicating we have initialised */ /* Flag indicating we have initialised */
@ -45,10 +45,6 @@ static int system_log = 0;
static int parent_fd = 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 { struct LogFile {
const char *name; const char *name;
const char *banner; const char *banner;
@ -134,7 +130,7 @@ void LOG_Message(LOG_Severity severity,
time_t t; time_t t;
struct tm *tm; 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 */ /* Don't clutter up syslog with timestamps and internal debugging info */
time(&t); time(&t);
tm = gmtime(&t); tm = gmtime(&t);
@ -143,7 +139,7 @@ void LOG_Message(LOG_Severity severity,
fprintf(file_log, "%s ", buf); fprintf(file_log, "%s ", buf);
} }
#if DEBUG > 0 #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); fprintf(file_log, "%s:%d:(%s) ", filename, line_number, function_name);
#endif #endif
} }
@ -157,9 +153,11 @@ void LOG_Message(LOG_Severity severity,
case LOGS_INFO: case LOGS_INFO:
case LOGS_WARN: case LOGS_WARN:
case LOGS_ERR: case LOGS_ERR:
if (severity >= log_min_severity)
log_message(0, severity, buf); log_message(0, severity, buf);
break; break;
case LOGS_FATAL: case LOGS_FATAL:
if (severity >= log_min_severity)
log_message(1, severity, buf); log_message(1, severity, buf);
/* Send the message also to the foreground process if it is /* Send the message also to the foreground process if it is
@ -213,12 +211,9 @@ LOG_OpenSystemLog(void)
/* ================================================== */ /* ================================================== */
void LOG_SetDebugLevel(int level) void LOG_SetMinSeverity(LOG_Severity severity)
{ {
debug_level = level; log_min_severity = CLAMP(LOGS_DEBUG, severity, LOGS_FATAL);
if (level >= DEBUG_LEVEL_PRINT_DEBUG) {
log_debug_enabled = 1;
}
} }
/* ================================================== */ /* ================================================== */

View file

@ -31,9 +31,6 @@
#include "sysincl.h" #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 /* Line logging macros. If the compiler is GNU C, we take advantage of
being able to get the function name also. */ being able to get the function name also. */
@ -55,7 +52,7 @@ extern int log_debug_enabled;
#define DEBUG_LOG(...) \ #define DEBUG_LOG(...) \
do { \ do { \
if (DEBUG && log_debug_enabled) \ if (DEBUG && log_min_severity == LOGS_DEBUG) \
LOG_MESSAGE(LOGS_DEBUG, __VA_ARGS__); \ LOG_MESSAGE(LOGS_DEBUG, __VA_ARGS__); \
} while (0) } while (0)
@ -69,13 +66,16 @@ extern int log_debug_enabled;
/* Definition of severity */ /* Definition of severity */
typedef enum { typedef enum {
LOGS_INFO, LOGS_DEBUG = -1,
LOGS_INFO = 0,
LOGS_WARN, LOGS_WARN,
LOGS_ERR, LOGS_ERR,
LOGS_FATAL, LOGS_FATAL,
LOGS_DEBUG
} LOG_Severity; } LOG_Severity;
/* Minimum severity of messages to be logged */
extern LOG_Severity log_min_severity;
/* Init function */ /* Init function */
extern void LOG_Initialise(void); extern void LOG_Initialise(void);
@ -92,12 +92,10 @@ FORMAT_ATTRIBUTE_PRINTF(2, 3)
extern void LOG_Message(LOG_Severity severity, const char *format, ...); extern void LOG_Message(LOG_Severity severity, const char *format, ...);
#endif #endif
/* Set debug level: /* Set the minimum severity of a message to be logged or printed to terminal.
0, 1 - only non-debug messages are logged If the severity is LOGS_DEBUG and DEBUG is enabled, all messages will be
2 - debug messages are logged too, all messages are prefixed with prefixed with the filename, line number, and function name. */
filename, line, and function name extern void LOG_SetMinSeverity(LOG_Severity severity);
*/
extern void LOG_SetDebugLevel(int level);
/* Log messages to a file instead of stderr, or stderr again if NULL */ /* Log messages to a file instead of stderr, or stderr again if NULL */
extern void LOG_OpenFileLog(const char *log_file); extern void LOG_OpenFileLog(const char *log_file);

2
main.c
View file

@ -510,7 +510,7 @@ int main
LOG_OpenSystemLog(); 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); LOG(LOGS_INFO, "chronyd version %s starting (%s)", CHRONY_VERSION, CHRONYD_FEATURES);