Skip to content

Commit

Permalink
Allow debug logging from multiple threads (#107)
Browse files Browse the repository at this point in the history
Co-authored-by: Mattias Axelsson <[email protected]>
  • Loading branch information
github-actions[bot] and killenheladagen authored Apr 15, 2024
1 parent fa51b37 commit 285af5f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/dockerdwrapperwithcompose.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ int main(int argc, char** argv) {
return EX_SOFTWARE;
}

log_settings.debug = is_app_log_level_debug(app_state.param_handle);
log_debug_set(is_app_log_level_debug(app_state.param_handle));

init_signals();

Expand All @@ -801,7 +801,7 @@ int main(int argc, char** argv) {

main_loop_run();

log_settings.debug = is_app_log_level_debug(app_state.param_handle);
log_debug_set(is_app_log_level_debug(app_state.param_handle));

stop_dockerd();
}
Expand Down
19 changes: 12 additions & 7 deletions app/log.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "log.h"
#include <glib.h>
#include <stdio.h>
#include <syslog.h>

static volatile int debug_log_enabled; // Accessed using g_atomic_int_get/set only

static int log_level_to_syslog_priority(GLogLevelFlags log_level) {
if (log_level == G_LOG_LEVEL_NON_FATAL_ERROR)
log_level = G_LOG_LEVEL_ERROR;
Expand Down Expand Up @@ -44,24 +45,24 @@ static const char* log_level_to_string(GLogLevelFlags log_level) {
}
}

static bool log_threshold_met(GLogLevelFlags log_level, const struct log_settings* settings) {
return settings->debug || (log_level & ~G_LOG_LEVEL_DEBUG);
static bool log_threshold_met(GLogLevelFlags log_level) {
return g_atomic_int_get(&debug_log_enabled) || (log_level & ~G_LOG_LEVEL_DEBUG);
}

static void log_to_syslog(__attribute__((unused)) const char* log_domain,
GLogLevelFlags log_level,
const char* message,
gpointer settings_void_ptr) {
if (log_threshold_met(log_level, settings_void_ptr))
__attribute__((unused)) gpointer settings_void_ptr) {
if (log_threshold_met(log_level))
syslog(log_level_to_syslog_priority(log_level), "%s", message);
}

// Timestamp format and log level have been chosen to match that of dockerd
static void log_to_stdout(__attribute__((unused)) const char* log_domain,
GLogLevelFlags log_level,
const char* message,
gpointer settings_void_ptr) {
if (log_threshold_met(log_level, settings_void_ptr)) {
__attribute__((unused)) gpointer settings_void_ptr) {
if (log_threshold_met(log_level)) {
GDateTime* now = g_date_time_new_now_local();
g_autofree char* now_text = g_date_time_format(now, "%Y-%m-%dT%T.%f000%:z");
g_date_time_unref(now);
Expand All @@ -78,3 +79,7 @@ void log_init(struct log_settings* settings) {
settings->destination == log_dest_syslog ? log_to_syslog : log_to_stdout,
settings);
}

void log_debug_set(bool enabled) {
g_atomic_int_set(&debug_log_enabled, enabled);
}
4 changes: 3 additions & 1 deletion app/log.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once
#include <glib.h>
#include <stdbool.h>

enum log_destination { log_dest_stdout, log_dest_syslog };

struct log_settings {
enum log_destination destination;
bool debug;
};

// Set up g_log to log to either stdout or syslog.
Expand All @@ -15,6 +15,8 @@ struct log_settings {
// the struct must live until the process exits.
void log_init(struct log_settings* settings);

void log_debug_set(bool enabled);

// Replacement for G_LOG_LEVEL_ERROR, which is fatal.
#define G_LOG_LEVEL_NON_FATAL_ERROR (1 << G_LOG_LEVEL_USER_SHIFT)

Expand Down

0 comments on commit 285af5f

Please sign in to comment.