Add priority string in logs (if applicable)

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2023-12-01 14:59:57 +01:00
parent 2db977612c
commit d01a62f053
5 changed files with 19 additions and 7 deletions
+4
View File
@@ -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"
+1
View File
@@ -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);
+1 -1
View File
@@ -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();
+11 -5
View File
@@ -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)
+2 -1
View File
@@ -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;