summaryrefslogtreecommitdiff
path: root/src/log.c
diff options
context:
space:
mode:
authorsteven-na <noreply.github@stvnc.dev>2026-08-22 21:52:29 -0700
committersteven-na <noreply.github@stvnc.dev>2026-08-22 21:52:29 -0700
commitfd475232ee12089f817f237cc755fe4444ec483b (patch)
tree08bd147def56285c5420e852d0e3ff5dab7931e4 /src/log.c
parentfee3f00c6d8a855b4372a0367e0b3e9dcea76699 (diff)
Fixed va_list bug and strng_dup off by one
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c45
1 files changed, 25 insertions, 20 deletions
diff --git a/src/log.c b/src/log.c
index 670fbb5..c67b8b2 100644
--- a/src/log.c
+++ b/src/log.c
@@ -54,7 +54,7 @@ static inline void unlock() { if (L.lock_proc) L.lock_proc(false); }
static inline void write_to_file(LogLevelE lvl, const char *src_file, u64 src_line, const char *fmt, struct tm* time, va_list va) {
if (!L.log_file) { return; }
char time_str[64];
- time_str[strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", time)] = '\0';
+ strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", time);
fprintf(L.log_file, "%s [%-5s] %s:%lu ", time_str, level_strings[lvl], src_file, src_line);
vfprintf(L.log_file, fmt, va);
@@ -66,32 +66,37 @@ void log_log(LogLevelE lvl, const char *src_file, u64 src_line, const char *fmt,
lock();
- va_list va;
- va_list fva;
- va_start(va, fmt);
- va_copy(fva, va);
- va_end(va);
-
time_t t = time(NULL);
- struct tm* time = localtime(&t);
+ struct tm tm_info;
+ struct tm* time_ptr = localtime_r(&t, &tm_info);
- if (L.quiet || lvl < L.lvl) { goto skip; }
+ bool should_log_to_console = !(L.quiet || lvl < L.lvl);
- char time_str[16];
- time_str[strftime(time_str, sizeof(time_str), "%H:%M:%S", time)] = '\0';
+ va_list va;
+ va_start(va, fmt);
+
+ if (should_log_to_console) {
+ char time_str[16];
+ strftime(time_str, sizeof(time_str), "%H:%M:%S", time_ptr);
#ifndef LOG_NO_COLOR
- fprintf(L.log_default, "%s %s[%-5s] \x1b[0m\x1b[90m%s:%lu:\x1b[0m ",
- time_str, level_colors[lvl], level_strings[lvl], src_file, src_line);
+ fprintf(L.log_default, "%s %s[%-5s] \x1b[0m\x1b[90m%s:%lu:\x1b[0m ",
+ time_str, level_colors[lvl], level_strings[lvl], src_file, src_line);
#else
- fprintf(L.log_default, "%s %-5s %s:%lu: ",
- time_str, level_strings[L.lvl], src_file, src_line);
-#endif /* ifndef LOG_NO_COLOR */
+ fprintf(L.log_default, "%s %-5s %s:%lu: ",
+ time_str, level_strings[L.lvl], src_file, src_line);
+#endif
+
+ vfprintf(L.log_default, fmt, va);
+ fprintf(L.log_default, "\n");
+ }
+
+ va_list fva;
+ va_copy(fva, va);
+ va_end(va);
- vfprintf(L.log_default, fmt, va);
- fprintf(L.log_default, "\n");
+ write_to_file(lvl, src_file, src_line, fmt, time_ptr, fva);
+ va_end(fva);
-skip:
- write_to_file(lvl, src_file, src_line, fmt, time, fva);
unlock();
}