summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsteven-na <noreply.github@stvnc.dev>2026-08-14 04:04:33 -0700
committersteven-na <noreply.github@stvnc.dev>2026-08-14 04:04:33 -0700
commit2f6e71222f347949786892803df431184fc61903 (patch)
treec2b012093d8882c52423d4515caa920c0645ec29 /src
parentabc88dd876c9aab0690074fc29802ca99060b7a7 (diff)
Add log_init
Diffstat (limited to 'src')
-rw-r--r--src/log.c17
-rw-r--r--src/log.h7
2 files changed, 18 insertions, 6 deletions
diff --git a/src/log.c b/src/log.c
index 2d7eaa2..03b81e9 100644
--- a/src/log.c
+++ b/src/log.c
@@ -8,6 +8,8 @@ static struct {
log_LockProc lock_proc;
LogLevelE lvl;
b32 quiet;
+ b32 inited;
+ FILE *log_default;
FILE *log_file;
} L;
@@ -29,6 +31,11 @@ void log_set_level(LogLevelE lvl) {
L.lvl = lvl;
}
+void log_init(FILE *out) {
+ L.inited = true;
+ L.log_default = out;
+}
+
void log_set_quiet(b32 quiet) {
L.quiet = quiet;
}
@@ -55,6 +62,8 @@ static inline void write_to_file(LogLevelE lvl, const char *src_file, u64 src_li
}
void log_log(LogLevelE lvl, const char *src_file, u64 src_line, const char *fmt, ...) {
+ if (!L.inited) return;
+
lock();
va_list va;
@@ -72,15 +81,15 @@ void log_log(LogLevelE lvl, const char *src_file, u64 src_line, const char *fmt,
time_str[strftime(time_str, sizeof(time_str), "%H:%M:%S", time)] = '\0';
#ifndef LOG_NO_COLOR
- fprintf(stdout, "%s %s%-5s\x1b[0m\x1b[90m%s:%lu:\x1b[0m ",
+ 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(stdout, "%s %-5s %s:%lu: ",
+ fprintf(L.log_default, "%s %-5s %s:%lu: ",
time_str, level_strings[L.lvl], src_file, src_line);
#endif /* ifndef LOG_NO_COLOR */
- vfprintf(stdout, fmt, va);
- fprintf(stdout, "\n");
+ vfprintf(L.log_default, fmt, va);
+ fprintf(L.log_default, "\n");
skip:
write_to_file(lvl, src_file, src_line, fmt, time, fva);
diff --git a/src/log.h b/src/log.h
index eedb182..287ee28 100644
--- a/src/log.h
+++ b/src/log.h
@@ -1,5 +1,4 @@
// Based on: https://github.com/rxi/log.c/
-
#pragma once
#include "common.h"
@@ -25,11 +24,15 @@ typedef enum {
#define log_error(...) log_log(LogLevelError, __FILE__, __LINE__, __VA_ARGS__)
#define log_fatal(...) log_log(LogLevelFatal, __FILE__, __LINE__, __VA_ARGS__)
-const char *log_get_level();
+// If this is not called, logging will have no effect.
+// Most likely pass stdout or stderr
+void log_init(FILE *out);
void log_set_level(LogLevelE lvl);
void log_set_quiet(b32 quiet);
void log_set_lock(log_LockProc proc);
void log_set_fp(FILE *log_file);
+const char *log_get_level();
+
void log_log(LogLevelE lvl, const char *src_file, u64 src_line, const char *fmt, ...);