]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
logging: support context-specific severity
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 15 Nov 2022 14:05:36 +0000 (15:05 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 16 Nov 2022 15:57:49 +0000 (16:57 +0100)
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
conf.c
logging.c
logging.h

index e48a2fc167c3851545d326e7c4388f979373a4ce..89ce191cfac60b5b8fd15be0ca5e9bbdb4a9f5f6 100644 (file)
--- 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 8dd27436a99624893fe58eaff1a40ec43dd8d707..12b1f1d4a7a4c5d71f3bbaaf557892408bc01eb2 100644 (file)
--- 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);
index d858d2adcbbf2bc87c5cececcac208e4b0326c2a..22c326c0e7f68dd5aba99728e09a9a458dc18dda 100644 (file)
--- 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)
 {
index 31bd46258e528d150330eb3678a016b0e6af9bbb..ff2e30e7c68b3f5575fc4a8da1b7812afb08411e 100644 (file)
--- 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);