From a1d0f9e7d4ac83dc1b8428af35f62bcc05167130 Mon Sep 17 00:00:00 2001 From: jasper-clarke Date: Mon, 11 Nov 2024 19:27:07 +1100 Subject: [PATCH] feat: customizable logging level and cleanup of old logs --- config.example.toml | 1 + configurer.c | 18 ++++++++++++++++-- configurer.h | 1 + util.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/config.example.toml b/config.example.toml index a6a4156..d50c1ee 100644 --- a/config.example.toml +++ b/config.example.toml @@ -1,4 +1,5 @@ startup_progs = ["kitty", "nitrogen --restore", "picom --daemon"] +log_level = "info" workspaces = [ "web", "code", diff --git a/configurer.c b/configurer.c index 0dd6d43..6322510 100644 --- a/configurer.c +++ b/configurer.c @@ -19,6 +19,7 @@ Config cfg = { .focusNewWindows = 1, .moveCursorWithFocus = 1, .refreshRate = 60, + .logLevel = "info", }; static const struct { @@ -115,7 +116,7 @@ void parse_keybinding(const char *key_str, toml_table_t *binding_table) { return; } - LOG_INFO("Action: %s", action.u.s); + LOG_DEBUG("Action: %s", action.u.s); // Create the keybinding Keybinding *kb = &cfg.keybindings[cfg.keybindingCount]; @@ -141,7 +142,7 @@ void parse_keybinding(const char *key_str, toml_table_t *binding_table) { free(action.u.s); cfg.keybindingCount++; - LOG_INFO("Added keybinding: %s -> %s", key_str, kb->description); + LOG_DEBUG("Added keybinding: %s -> %s", key_str, kb->description); } void load_keybindings(toml_table_t *conf) { @@ -466,6 +467,19 @@ int load_config(const char *config_path) { } } + toml_datum_t log_level = toml_string_in(conf, "log_level"); + if (log_level.ok) { + // If log_level matches any avaliable log level, use it + if (strcmp(log_level.u.s, "debug") == 0 || + strcmp(log_level.u.s, "info") == 0 || + strcmp(log_level.u.s, "warning") == 0) { + cfg.logLevel = strdup(log_level.u.s); + free(log_level.u.s); + } else { + LOG_WARN("Invalid log level: %s", log_level.u.s); + } + } + load_keybindings(conf); load_startup_programs(conf); load_workspaces(conf); diff --git a/configurer.h b/configurer.h index 6414ab3..77541c1 100644 --- a/configurer.h +++ b/configurer.h @@ -36,6 +36,7 @@ typedef struct { // General StartupProgram *startup_progs; int startup_prog_count; + char *logLevel; } Config; // Global configuration instance diff --git a/util.c b/util.c index 3162414..a82b900 100644 --- a/util.c +++ b/util.c @@ -4,7 +4,9 @@ #include #include #include +#include +#include "configurer.h" #include "util.h" // ANSI color codes for different log levels @@ -53,7 +55,17 @@ static void init_log_file() { } char log_path[512]; + char old_log_path[512]; snprintf(log_path, sizeof(log_path), "%s/.atlaslogs", home); + snprintf(old_log_path, sizeof(old_log_path), "%s/.atlaslogsold", home); + + // If the log file exists, rename it to .atlaslogsold + if (access(log_path, F_OK) == 0) { + // Remove old backup if it exists + unlink(old_log_path); + // Rename current log to old + rename(log_path, old_log_path); + } log_file = fopen(log_path, "a"); if (log_file == NULL) { @@ -77,6 +89,22 @@ void log_message(LogLevel level, const char *file, int line, const char *fmt, if (level < current_log_level) return; + // Convert between string and log levels + LogLevel levelFrom; + if (strcmp(cfg.logLevel, "debug") == 0) { + levelFrom = LOG_DEBUG; + } else if (strcmp(cfg.logLevel, "info") == 0) { + levelFrom = LOG_INFO; + } else if (strcmp(cfg.logLevel, "warning") == 0) { + levelFrom = LOG_WARNING; + } else { + levelFrom = LOG_INFO; + return; + } + + if (level < levelFrom) + return; + // Initialize log file if not already done if (log_file == NULL) { init_log_file();