diff options
Diffstat (limited to 'logging.c')
-rw-r--r-- | logging.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/logging.c b/logging.c new file mode 100644 index 0000000..2ad1704 --- /dev/null +++ b/logging.c @@ -0,0 +1,26 @@ +// +// Created by Keuin on 2021/12/28. +// + +#include "logging.h" +#include <stdio.h> + + +void *log_init(const char *filename) { + FILE *fp = fopen(filename, "a"); + return fp; +} + +void log_free(void *logger) { + fflush(logger); + fclose(logger); +} + +void log_print(void *logger, const char *level, time_t ts, const char *filename, int lineno, const char *msg) { + char timestr[32]; + strftime(timestr, 31, "%Y-%m-%d %H:%M:%S", localtime(&ts)); + if (fprintf(logger, "[%s][%s][%s][%d] %s\n", timestr, level, filename, lineno, msg)) + fflush(logger); + if (fprintf(stderr, "[%s][%s][%s][%d] %s\n", timestr, level, filename, lineno, msg)) + fflush(stderr); +} |