]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
logging: extend functionality
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 16 Jul 2020 11:16:15 +0000 (13:16 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 16 Jul 2020 11:24:59 +0000 (13:24 +0200)
Add a function to get the current minimum severity and a function to set
a global prefix for debug messages in order to identify messages from
helpers.

logging.c
logging.h

index 57f69453c143464fd2f92330f0107ecb8555c363..74d53c7ed27d4c548b666a8bd58dd9f29f9c5674 100644 (file)
--- a/logging.c
+++ b/logging.c
@@ -33,6 +33,7 @@
 
 #include "conf.h"
 #include "logging.h"
+#include "memory.h"
 #include "util.h"
 
 /* This is used by DEBUG_LOG macro */
@@ -61,12 +62,16 @@ static int n_filelogs = 0;
 
 static struct LogFile logfiles[MAX_FILELOGS];
 
+/* Global prefix for debug messages */
+static char *debug_prefix;
+
 /* ================================================== */
 /* Init function */
 
 void
 LOG_Initialise(void)
 {
+  debug_prefix = Strdup("");
   initialised = 1;
   LOG_OpenFileLog(NULL);
 }
@@ -85,6 +90,8 @@ LOG_Finalise(void)
 
   LOG_CycleLogFiles();
 
+  Free(debug_prefix);
+
   initialised = 0;
 }
 
@@ -132,6 +139,8 @@ void LOG_Message(LOG_Severity severity,
   time_t t;
   struct tm *tm;
 
+  assert(initialised);
+
   if (!system_log && file_log && severity >= log_min_severity) {
     /* Don't clutter up syslog with timestamps and internal debugging info */
     time(&t);
@@ -142,7 +151,7 @@ void LOG_Message(LOG_Severity severity,
     }
 #if DEBUG > 0
     if (log_min_severity <= LOGS_DEBUG)
-      fprintf(file_log, "%s:%d:(%s) ", filename, line_number, function_name);
+      fprintf(file_log, "%s%s:%d:(%s) ", debug_prefix, filename, line_number, function_name);
 #endif
   }
 
@@ -220,6 +229,23 @@ void LOG_SetMinSeverity(LOG_Severity severity)
 
 /* ================================================== */
 
+LOG_Severity
+LOG_GetMinSeverity(void)
+{
+  return log_min_severity;
+}
+
+/* ================================================== */
+
+void
+LOG_SetDebugPrefix(const char *prefix)
+{
+  Free(debug_prefix);
+  debug_prefix = Strdup(prefix);
+}
+
+/* ================================================== */
+
 void
 LOG_SetParentFd(int fd)
 {
index b034e52a1cd987a9f3b1f9beecf6f14657c3fdb2..31bd46258e528d150330eb3678a016b0e6af9bbb 100644 (file)
--- a/logging.h
+++ b/logging.h
@@ -97,6 +97,12 @@ extern void LOG_Message(LOG_Severity severity, const char *format, ...);
    prefixed with the filename, line number, and function name. */
 extern void LOG_SetMinSeverity(LOG_Severity severity);
 
+/* Get the minimum severity */
+extern LOG_Severity LOG_GetMinSeverity(void);
+
+/* Set a prefix for debug messages */
+extern void LOG_SetDebugPrefix(const char *prefix);
+
 /* Log messages to a file instead of stderr, or stderr again if NULL */
 extern void LOG_OpenFileLog(const char *log_file);