aboutsummaryrefslogtreecommitdiffstats
path: root/src/log.c
diff options
context:
space:
mode:
authorverdant <im@verdant.ee>2026-07-28 18:30:16 +0800
committerverdant <im@verdant.ee>2026-07-28 18:30:16 +0800
commit9bffcafa9ae0c38103a8c64b3482bb099e792869 (patch)
tree3cc18cabd99f9e300ca2539a9a48fb9c2d30e337 /src/log.c
parentcbdc8462b6e982d3137b74d61bed6f8977405b8a (diff)
downloadsf-9bffcafa9ae0c38103a8c64b3482bb099e792869.tar.gz
sf-9bffcafa9ae0c38103a8c64b3482bb099e792869.zip
Move source files to src/
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/log.c b/src/log.c
new file mode 100644
index 0000000..436307b
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,61 @@
+#include "log.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "err.h"
+
+FILE *log_file = NULL;
+
+void
+log_init(void)
+{
+ 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;
+ }
+}