Skip to content

Commit

Permalink
feat: customizable logging level and cleanup of old logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jasper-clarke committed Nov 11, 2024
1 parent 3cda707 commit a1d0f9e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
startup_progs = ["kitty", "nitrogen --restore", "picom --daemon"]
log_level = "info"
workspaces = [
"web",
"code",
Expand Down
18 changes: 16 additions & 2 deletions configurer.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Config cfg = {
.focusNewWindows = 1,
.moveCursorWithFocus = 1,
.refreshRate = 60,
.logLevel = "info",
};

static const struct {
Expand Down Expand Up @@ -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];
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions configurer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef struct {
// General
StartupProgram *startup_progs;
int startup_prog_count;
char *logLevel;
} Config;

// Global configuration instance
Expand Down
28 changes: 28 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "configurer.h"
#include "util.h"

// ANSI color codes for different log levels
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down

0 comments on commit a1d0f9e

Please sign in to comment.