From d01a62f0539d72d43b3dbb1d9ff03cbb1bfbbe1e Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 1 Dec 2023 14:59:57 +0100 Subject: [PATCH] Add priority string in logs (if applicable) Signed-off-by: DL6ER --- src/api/docs/content/specs/logs.yaml | 4 ++++ src/api/logs.c | 1 + src/dnsmasq_interface.c | 2 +- src/log.c | 16 +++++++++++----- src/log.h | 3 ++- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/api/docs/content/specs/logs.yaml b/src/api/docs/content/specs/logs.yaml index e65a8c58..b816f20c 100644 --- a/src/api/docs/content/specs/logs.yaml +++ b/src/api/docs/content/specs/logs.yaml @@ -119,6 +119,10 @@ components: message: type: string description: Log line content + prio: + type: string + nullable: true + description: Log line priority (if available) example: - timestamp: 1611729969.0 message: "started, version pi-hole-2.84 cachesize 10000" diff --git a/src/api/logs.c b/src/api/logs.c index e6678b8a..35f3bb3e 100644 --- a/src/api/logs.c +++ b/src/api/logs.c @@ -68,6 +68,7 @@ int api_logs(struct ftl_conn *api) cJSON *entry = JSON_NEW_OBJECT(); JSON_ADD_NUMBER_TO_OBJECT(entry, "timestamp", fifo_log->logs[api->opts.which].timestamp[i]); JSON_REF_STR_IN_OBJECT(entry, "message", fifo_log->logs[api->opts.which].message[i]); + JSON_REF_STR_IN_OBJECT(entry, "prio", fifo_log->logs[api->opts.which].prio[i]); JSON_ADD_ITEM_TO_ARRAY(log, entry); } JSON_ADD_ITEM_TO_OBJECT(json, "log", log); diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index d880db64..071e73d2 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -3403,7 +3403,7 @@ void FTL_dnsmasq_log(const char *payload, const int length) lock_shm(); // Add to FIFO buffer - add_to_fifo_buffer(FIFO_DNSMASQ, payload, length); + add_to_fifo_buffer(FIFO_DNSMASQ, payload, NULL, length); // Unlock SHM unlock_shm(); diff --git a/src/log.c b/src/log.c index 3b5902d1..94cd61f0 100644 --- a/src/log.c +++ b/src/log.c @@ -274,6 +274,8 @@ void __attribute__ ((format (gnu_printf, 3, 4))) _FTL_log(const int priority, co const int mpid = main_pid(); // Get the process ID of the main FTL process const int tid = gettid(); // Get the thread ID of the calling process + const char *prio = priostr(priority, flag); + // There are four cases we have to differentiate here: if(pid == tid) if(is_fork(mpid, pid)) @@ -295,7 +297,7 @@ void __attribute__ ((format (gnu_printf, 3, 4))) _FTL_log(const int priority, co { // Only print time/ID string when not in direct user interaction (CLI mode) if(!cli_mode) - printf("%s [%s] %s: ", timestring, idstr, priostr(priority, flag)); + printf("%s [%s] %s: ", timestring, idstr, prio); va_start(args, format); vprintf(format, args); va_end(args); @@ -310,7 +312,7 @@ void __attribute__ ((format (gnu_printf, 3, 4))) _FTL_log(const int priority, co va_start(args, format); const size_t len = vsnprintf(buffer, MAX_MSG_FIFO, format, args) + 1u; /* include zero-terminator */ va_end(args); - add_to_fifo_buffer(FIFO_FTL, buffer, len > MAX_MSG_FIFO ? MAX_MSG_FIFO : len); + add_to_fifo_buffer(FIFO_FTL, buffer, prio, len > MAX_MSG_FIFO ? MAX_MSG_FIFO : len); if(config.files.log.ftl.v.s != NULL) { @@ -321,7 +323,7 @@ void __attribute__ ((format (gnu_printf, 3, 4))) _FTL_log(const int priority, co if(logfile != NULL) { // Prepend message with identification string and priority - fprintf(logfile, "%s [%s] %s: ", timestring, idstr, priostr(priority, flag)); + fprintf(logfile, "%s [%s] %s: ", timestring, idstr, prio); // Log message va_start(args, format); @@ -361,7 +363,7 @@ void __attribute__ ((format (gnu_printf, 1, 2))) log_web(const char *format, ... va_start(args, format); const size_t len = vsnprintf(buffer, MAX_MSG_FIFO, format, args) + 1u; /* include zero-terminator */ va_end(args); - add_to_fifo_buffer(FIFO_WEBSERVER, buffer, len > MAX_MSG_FIFO ? MAX_MSG_FIFO : len); + add_to_fifo_buffer(FIFO_WEBSERVER, buffer, NULL, len > MAX_MSG_FIFO ? MAX_MSG_FIFO : len); // Get human-readable time get_timestr(timestring, now, true, false); @@ -721,7 +723,7 @@ void dnsmasq_diagnosis_warning(char *message) logg_warn_dnsmasq_message(skipStr("warning: ", message)); } -void add_to_fifo_buffer(const enum fifo_logs which, const char *payload, const size_t length) +void add_to_fifo_buffer(const enum fifo_logs which, const char *payload, const char *prio, const size_t length) { const double now = double_time(); @@ -735,6 +737,7 @@ void add_to_fifo_buffer(const enum fifo_logs which, const char *payload, const s // Log is full, move everything one slot forward to make space for a new record at the end // This pruges the oldest message from the list (it is overwritten by the second message) memmove(&fifo_log->logs[which].message[0][0], &fifo_log->logs[which].message[1][0], (LOG_SIZE - 1u) * MAX_MSG_FIFO); + memmove(&fifo_log->logs[which].prio[0], &fifo_log->logs[which].prio[1], (LOG_SIZE - 1u) * sizeof(fifo_log->logs[which].prio[0])); memmove(&fifo_log->logs[which].timestamp[0], &fifo_log->logs[which].timestamp[1], (LOG_SIZE - 1u) * sizeof(fifo_log->logs[which].timestamp[0])); idx = LOG_SIZE - 1u; } @@ -758,6 +761,9 @@ void add_to_fifo_buffer(const enum fifo_logs which, const char *payload, const s // Set timestamp fifo_log->logs[which].timestamp[idx] = now; + + // Set prio (if available) + fifo_log->logs[which].prio[idx] = prio; } bool flush_dnsmasq_log(void) diff --git a/src/log.h b/src/log.h index 5c5bf888..fd5f58f0 100644 --- a/src/log.h +++ b/src/log.h @@ -90,7 +90,7 @@ const char *short_path(const char *full_path) __attribute__ ((pure)); // Defaults to 512 [512 * 256 above = use 128 KB of memory for the log] #define LOG_SIZE 515u -void add_to_fifo_buffer(const enum fifo_logs which, const char *payload, const size_t length); +void add_to_fifo_buffer(const enum fifo_logs which, const char *payload, const char *prio, const size_t length); bool flush_dnsmasq_log(void); @@ -99,6 +99,7 @@ typedef struct { unsigned int next_id; double timestamp[LOG_SIZE]; char message[LOG_SIZE][MAX_MSG_FIFO]; + const char *prio[LOG_SIZE]; } logs[FIFO_MAX]; } fifologData;