]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
logging: refactor enabling of debug messages
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 2 Jul 2019 14:18:06 +0000 (16:18 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 4 Jul 2019 15:38:13 +0000 (17:38 +0200)
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
logging.c
logging.h
main.c

index 8736504ba67a22175f62a883802b06bd85a1dee4..fcaaf3ae82d749f2ddc525966be640da8dcff23d 100644 (file)
--- 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;
index d2296e02f48bac8f6a00c038116a67fe358e5605..b6a0489b32dabc8e403c0aedbf006f05843e90b5 100644 (file)
--- 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);
 }
 
 /* ================================================== */
index 67931dc735cbf9b69836b6ee2b7ec616b0208e38..b034e52a1cd987a9f3b1f9beecf6f14657c3fdb2 100644 (file)
--- 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 6ccf32ed6f347da380091c6b5904a12f128c5363..39ebf5d067480beca6b55a6af298ca2edcbdc105 100644 (file)
--- 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);