From 94d7588c2045cbe76ec0287a5e92f1b4123d996f Mon Sep 17 00:00:00 2001 From: Keuin Date: Tue, 28 Dec 2021 05:37:54 +0800 Subject: Initial version. --- logging.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 logging.c (limited to 'logging.c') 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 + + +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); +} -- cgit v1.2.3