]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
logging: set debug level instead of on/off
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 8 Apr 2014 16:44:17 +0000 (18:44 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 9 Apr 2014 10:09:23 +0000 (12:09 +0200)
Prefix messages written to terminal with filename, line and function
name only with debug level 2 and higher.

logging.c
logging.h
main.c

index 077a60cfee03dd0243b57873ad23d78d8803ff6d..8719a469575f8c7a1a0ec14c04ff946fdc5f8ea7 100644 (file)
--- a/logging.c
+++ b/logging.c
@@ -42,7 +42,9 @@ static int system_log = 0;
 
 static int parent_fd = 0;
 
-static int log_debug = 0;
+#define DEBUG_LEVEL_PRINT_FUNCTION 2
+#define DEBUG_LEVEL_PRINT_DEBUG 2
+static int debug_level = 0;
 
 static time_t last_limited = 0;
 
@@ -146,18 +148,20 @@ void LOG_Message(LOG_Severity severity, LOG_Facility facility,
   time_t t;
   struct tm stm;
 
-  /* Don't write debug messages if not enabled */
-  if (!log_debug && severity == LOGS_DEBUG)
+  /* Don't write debug messages if debug level is too low */
+  if (debug_level < DEBUG_LEVEL_PRINT_DEBUG && severity == LOGS_DEBUG)
     return;
 
 #ifdef WINNT
 #else
   if (!system_log) {
-    /* Don't clutter up syslog with internal debugging info */
+    /* Don't clutter up syslog with timestamps and internal debugging info */
     time(&t);
     stm = *gmtime(&t);
     strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &stm);
-    fprintf(stderr, "%s %s:%d:(%s) ", buf, filename, line_number, function_name);
+    fprintf(stderr, "%s ", buf);
+    if (debug_level >= DEBUG_LEVEL_PRINT_FUNCTION)
+      fprintf(stderr, "%s:%d:(%s) ", filename, line_number, function_name);
   }
 #endif
 
@@ -209,9 +213,9 @@ LOG_OpenSystemLog(void)
 
 /* ================================================== */
 
-void LOG_EnableDebug(void)
+void LOG_SetDebugLevel(int level)
 {
-  log_debug = 1;
+  debug_level = level;
 }
 
 /* ================================================== */
index 2b247b4c6da34021e3579e5e0c36b4662cfe7b9b..fcadb7bbd268a45fe567fd4ac7fb575b7c66c2b9 100644 (file)
--- a/logging.h
+++ b/logging.h
@@ -102,8 +102,12 @@ extern void LOG_Message(LOG_Severity severity, LOG_Facility facility,
                         int line_number, const char *filename,
                         const char *function_name, const char *format, ...);
 
-/* Enable logging of debug messages */
-extern void LOG_EnableDebug(void);
+/* 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);
 
 /* Log messages to syslog instead of stderr */
 extern void LOG_OpenSystemLog(void);
diff --git a/main.c b/main.c
index 5ed3addafdeb03ea71b20d2c9a64b9f57385b9db..825c18bfd9998a4ed507b183c4cceac5aa0918c2 100644 (file)
--- a/main.c
+++ b/main.c
@@ -323,6 +323,7 @@ int main
   int do_init_rtc = 0, restarted = 0;
   int other_pid;
   int lock_memory = 0, sched_priority = 0;
+  int system_log = 1;
 
   LOG_Initialise();
 
@@ -361,6 +362,7 @@ int main
     } else if (!strcmp("-d", *argv)) {
       debug++;
       nofork = 1;
+      system_log = 0;
     } else if (!strcmp("-4", *argv)) {
       address_family = IPADDR_INET4;
     } else if (!strcmp("-6", *argv)) {
@@ -381,13 +383,11 @@ int main
     go_daemon();
   }
 
-  if (!debug) {
+  if (system_log) {
     LOG_OpenSystemLog();
   }
   
-  if (debug > 1) {
-    LOG_EnableDebug();
-  }
+  LOG_SetDebugLevel(debug);
   
   LOG(LOGS_INFO, LOGF_Main, "chronyd version %s starting", CHRONY_VERSION);