aboutsummaryrefslogtreecommitdiffstats
path: root/log.c
diff options
context:
space:
mode:
authorverdant <im@verdant.ee>2026-07-21 18:53:18 +0800
committerverdant <im@verdant.ee>2026-07-21 18:53:18 +0800
commit3e4e10c1c1203d605aeceafa8f15e6a3a4aa8033 (patch)
treedc3042da7d09e82b7e77ae23c5c76e9f2738b3ab /log.c
parent1c7cbcc6f8b775f846277379df23ce6c47fb1321 (diff)
downloadsf-3e4e10c1c1203d605aeceafa8f15e6a3a4aa8033.tar.gz
sf-3e4e10c1c1203d605aeceafa8f15e6a3a4aa8033.zip
Add logging system
Diffstat (limited to 'log.c')
-rw-r--r--log.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/log.c b/log.c
new file mode 100644
index 0000000..305a666
--- /dev/null
+++ b/log.c
@@ -0,0 +1,57 @@
+#include "log.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "err.h"
+#include "utils.h"
+
+FILE* log_file = NULL;
+
+void log_init(void) {
+ if (is_debug()) {
+ log_file = fopen("./log", "a");
+ } else {
+ log_file = fopen(LOG_FILEPATH, "a");
+ }
+
+ if (!log_file) {
+ die("Faild to open log file");
+ }
+}
+
+void log_write(log_level level, const char* fmt, ...) {
+ char* level_str;
+
+ if (level == LOG_INFO) level_str = "INFO";
+ if (level == LOG_WARN) level_str = "WARN";
+ if (level == LOG_ERR) level_str = "ERR";
+
+ time_t rawtime;
+ struct tm* timeinfo;
+ char time_str[32];
+
+ time(&rawtime);
+ timeinfo = localtime(&rawtime);
+ strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", timeinfo);
+
+ fprintf(log_file, "[%s] [%s] ", time_str, level_str);
+
+ va_list ap;
+ va_start(ap, fmt);
+ vfprintf(log_file, fmt, ap);
+ va_end(ap);
+
+ fprintf(log_file, "\n");
+ fflush(log_file);
+}
+
+void log_end(FILE* log_file) {
+ if (log_file) {
+ fclose(log_file);
+ log_file = NULL;
+ }
+}