From b328c8c348fce1da477a86abb520cce8342c7063 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 15 Nov 2022 15:05:36 +0100 Subject: [PATCH] logging: support context-specific severity Allow messages to have severity set to INFO or DEBUG depending on the context in which they are made to allow logging important changes made from chronyc or sourcefile, but not spam the system log if those changes are normally expected (e.g. specified in the config). --- cmdmon.c | 4 ++++ conf.c | 4 ++++ logging.c | 29 +++++++++++++++++++++++++++++ logging.h | 14 ++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/cmdmon.c b/cmdmon.c index e48a2fc..89ce191 100644 --- a/cmdmon.c +++ b/cmdmon.c @@ -1515,6 +1515,8 @@ read_from_cmd_socket(int sock_fd, int event, void *anything) } if (allowed) { + LOG_SetContext(LOGC_Command); + switch(rx_command) { case REQ_NULL: /* Do nothing */ @@ -1762,6 +1764,8 @@ read_from_cmd_socket(int sock_fd, int event, void *anything) tx_message.status = htons(STT_FAILED); break; } + + LOG_UnsetContext(LOGC_Command); } else { tx_message.status = htons(STT_UNAUTH); } diff --git a/conf.c b/conf.c index 8dd2743..12b1f1d 100644 --- a/conf.c +++ b/conf.c @@ -1704,6 +1704,8 @@ reload_source_dirs(void) new_ids = ARR_GetElements(ntp_source_ids); unresolved = 0; + LOG_SetContext(LOGC_SourceFile); + qsort(new_sources, new_size, sizeof (new_sources[0]), compare_sources); for (i = j = 0; i < prev_size || j < new_size; ) { @@ -1739,6 +1741,8 @@ reload_source_dirs(void) } } + LOG_UnsetContext(LOGC_SourceFile); + for (i = 0; i < prev_size; i++) Free(prev_sources[i].params.name); Free(prev_sources); diff --git a/logging.c b/logging.c index d858d2a..22c326c 100644 --- a/logging.c +++ b/logging.c @@ -39,6 +39,9 @@ /* This is used by DEBUG_LOG macro */ LOG_Severity log_min_severity = LOGS_INFO; +/* Current logging contexts */ +static LOG_Context log_contexts; + /* ================================================== */ /* Flag indicating we have initialised */ static int initialised = 0; @@ -72,6 +75,8 @@ void LOG_Initialise(void) { debug_prefix = Strdup(""); + log_contexts = 0; + initialised = 1; LOG_OpenFileLog(NULL); } @@ -237,6 +242,30 @@ LOG_GetMinSeverity(void) /* ================================================== */ +void +LOG_SetContext(LOG_Context context) +{ + log_contexts |= context; +} + +/* ================================================== */ + +void +LOG_UnsetContext(LOG_Context context) +{ + log_contexts &= ~context; +} + +/* ================================================== */ + +LOG_Severity +LOG_GetContextSeverity(LOG_Context contexts) +{ + return log_contexts & contexts ? LOGS_INFO : LOGS_DEBUG; +} + +/* ================================================== */ + void LOG_SetDebugPrefix(const char *prefix) { diff --git a/logging.h b/logging.h index 31bd462..ff2e30e 100644 --- a/logging.h +++ b/logging.h @@ -100,6 +100,20 @@ extern void LOG_SetMinSeverity(LOG_Severity severity); /* Get the minimum severity */ extern LOG_Severity LOG_GetMinSeverity(void); +/* Flags for info messages that should be logged only in specific contexts */ +typedef enum { + LOGC_Command = 1, + LOGC_SourceFile = 2, +} LOG_Context; + +/* Modify current contexts */ +extern void LOG_SetContext(LOG_Context context); +extern void LOG_UnsetContext(LOG_Context context); + +/* Get severity depending on the current active contexts: INFO if they contain + at least one of the specified contexts, DEBUG otherwise */ +extern LOG_Severity LOG_GetContextSeverity(LOG_Context contexts); + /* Set a prefix for debug messages */ extern void LOG_SetDebugPrefix(const char *prefix);