From 59b76fd601e5840043b9868ae2ca2f06b4b4e7a4 Mon Sep 17 00:00:00 2001 From: frosty Date: Tue, 11 Aug 2026 03:20:47 -0400 Subject: feat: complete logging overhaul --- src/l10n.c | 93 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 40 deletions(-) (limited to 'src/l10n.c') diff --git a/src/l10n.c b/src/l10n.c index 4d8ac30..43acff2 100644 --- a/src/l10n.c +++ b/src/l10n.c @@ -13,11 +13,15 @@ typedef enum { } IniSection; static char *trim(char *str) { - if (str == NULL) return NULL; - while (*str == ' ' || *str == '\t') str++; - if (*str == '\0') return str; + if (str == NULL) + return NULL; + while (*str == ' ' || *str == '\t') + str++; + if (*str == '\0') + return str; char *end = str + strlen(str) - 1; - while (end > str && (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n')) { + while (end > str && + (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n')) { *end = '\0'; end--; } @@ -35,10 +39,13 @@ static void unquote_value(char *value) { static int safe_grow_capacity(int current, int hard_max) { int new_cap; if (current == 0) { - new_cap = (hard_max < INITIAL_LOCALE_KEYS_CAPACITY) ? hard_max : INITIAL_LOCALE_KEYS_CAPACITY; + new_cap = (hard_max < INITIAL_LOCALE_KEYS_CAPACITY) + ? hard_max + : INITIAL_LOCALE_KEYS_CAPACITY; } else { new_cap = current * 2; - if (new_cap > hard_max) new_cap = hard_max; + if (new_cap > hard_max) + new_cap = hard_max; } return new_cap; } @@ -46,8 +53,8 @@ static int safe_grow_capacity(int current, int hard_max) { static int parse_ini_file(const char *path, Locale *locale) { FILE *fp = fopen(path, "r"); if (fp == NULL) { - fprintf(stderr, "[ERROR] parse_ini_file: Could not open '%s': %s\n", - path, strerror(errno)); + beaker_log("ERROR", "parse_ini_file: Could not open '%s': %s\n", path, + strerror(errno)); return -1; } @@ -67,7 +74,8 @@ static int parse_ini_file(const char *path, Locale *locale) { if (*trimmed == '[') { char *close = strchr(trimmed, ']'); - if (close == NULL) continue; + if (close == NULL) + continue; *close = '\0'; char *section_name = trimmed + 1; section_name = trim(section_name); @@ -83,7 +91,8 @@ static int parse_ini_file(const char *path, Locale *locale) { } char *eq = strchr(trimmed, '='); - if (eq == NULL) continue; + if (eq == NULL) + continue; *eq = '\0'; char *key = trim(trimmed); @@ -98,24 +107,28 @@ static int parse_ini_file(const char *path, Locale *locale) { strncpy(locale->meta.name, value, MAX_VALUE_LEN - 1); locale->meta.name[MAX_VALUE_LEN - 1] = '\0'; } else if (strcmp(key, "Direction") == 0) { - strncpy(locale->meta.direction, value, sizeof(locale->meta.direction) - 1); + strncpy(locale->meta.direction, value, + sizeof(locale->meta.direction) - 1); locale->meta.direction[sizeof(locale->meta.direction) - 1] = '\0'; } } else if (current_section == SECTION_KEYS) { if (locale->key_count >= locale->key_capacity) { - int new_cap = safe_grow_capacity(locale->key_capacity, MAX_LOCALE_KEYS_HARD); + int new_cap = + safe_grow_capacity(locale->key_capacity, MAX_LOCALE_KEYS_HARD); if (new_cap <= locale->key_count) { - fprintf(stderr, - "[WARNING] parse_ini_file: Hard key limit (%d) reached in '%s', " - "skipping key '%s'.\n", - MAX_LOCALE_KEYS_HARD, path, key); + beaker_log("WARN", + "parse_ini_file: Hard key limit (%d) reached in '%s', " + "skipping key '%s'.\n", + MAX_LOCALE_KEYS_HARD, path, key); continue; } - LocaleKV *new_keys = realloc(locale->keys, (size_t)new_cap * sizeof(LocaleKV)); + LocaleKV *new_keys = + realloc(locale->keys, (size_t)new_cap * sizeof(LocaleKV)); if (new_keys == NULL) { - fprintf(stderr, - "[ERROR] parse_ini_file: Memory allocation failed for keys in '%s'.\n", - path); + beaker_log( + "ERROR", + "parse_ini_file: Memory allocation failed for keys in '%s'.\n", + path); continue; } locale->keys = new_keys; @@ -133,9 +146,8 @@ static int parse_ini_file(const char *path, Locale *locale) { fclose(fp); if (locale->meta.id[0] == '\0') { - fprintf(stderr, - "[WARNING] parse_ini_file: No Id in [Meta] section of '%s'.\n", - path); + beaker_log("WARN", "parse_ini_file: No Id in [Meta] section of '%s'.\n", + path); return -1; } @@ -147,9 +159,9 @@ int beaker_load_locales(void) { DIR *dir = opendir(LOCALES_DIR); if (dir == NULL) { - fprintf(stderr, - "[ERROR] beaker_load_locales: Could not open directory '%s': %s\n", - LOCALES_DIR, strerror(errno)); + beaker_log("ERROR", + "beaker_load_locales: Could not open directory '%s': %s\n", + LOCALES_DIR, strerror(errno)); return 0; } @@ -165,16 +177,17 @@ int beaker_load_locales(void) { if (locale_count >= locale_capacity) { int new_cap = safe_grow_capacity(locale_capacity, MAX_LOCALES_HARD); if (new_cap <= locale_count) { - fprintf(stderr, - "[WARNING] beaker_load_locales: Hard locale limit (%d) reached, " - "skipping '%s'.\n", - MAX_LOCALES_HARD, name); + beaker_log("WARN", + "beaker_load_locales: Hard locale limit (%d) reached, " + "skipping '%s'.\n", + MAX_LOCALES_HARD, name); break; } Locale *new_locales = realloc(locales, (size_t)new_cap * sizeof(Locale)); if (new_locales == NULL) { - fprintf(stderr, - "[ERROR] beaker_load_locales: Memory allocation failed for locales.\n"); + beaker_log( + "ERROR", + "beaker_load_locales: Memory allocation failed for locales.\n"); break; } locales = new_locales; @@ -186,8 +199,8 @@ int beaker_load_locales(void) { Locale *locale = &locales[locale_count]; if (parse_ini_file(path, locale) == 0) { - fprintf(stderr, "[INFO] beaker_load_locales: Loaded locale '%s' from '%s'\n", - locale->meta.id, name); + beaker_log("DEBUG", "beaker_load_locales: Loaded locale '%s' from '%s'", + locale->meta.id, name); locale_count++; } else { free(locale->keys); @@ -203,8 +216,8 @@ int beaker_load_locales(void) { void beaker_set_locale(TemplateContext *ctx, const char *locale_id) { if (ctx == NULL || locale_id == NULL) { - fprintf(stderr, - "[ERROR] beaker_set_locale: Invalid NULL input (ctx or locale_id).\n"); + beaker_log("ERROR", + "beaker_set_locale: Invalid NULL input (ctx or locale_id).\n"); return; } @@ -216,10 +229,10 @@ void beaker_set_locale(TemplateContext *ctx, const char *locale_id) { context_set(ctx, "__locale_name", meta->name); context_set(ctx, "__locale_direction", meta->direction); } else { - fprintf(stderr, - "[WARNING] beaker_set_locale: Locale '%s' not found. Context vars " - "not set.\n", - locale_id); + beaker_log("WARN", + "beaker_set_locale: Locale '%s' not found. Context vars " + "not set.\n", + locale_id); } } -- cgit v1.3