From 760fada7269b083ce00a1f18a4fb1cbb9ab13dc7 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Wed, 4 Jan 2023 00:33:15 -0600 Subject: logging: add a close function Add a close function to close the file handle pointing at the log file. Signed-off-by: Danny Holman --- server/src/logging.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'server/src') diff --git a/server/src/logging.c b/server/src/logging.c index 9084baf..66f1661 100644 --- a/server/src/logging.c +++ b/server/src/logging.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include static const char *severe = "SEVERE"; static const char *warn = "WARN"; @@ -16,26 +18,33 @@ int init_logging(FILE *out_file) { } int vlog_msg(int level, const char *fmt, va_list args) { + time_t rawtime; + time(&rawtime); + char timestr[1024]; + ctime_r(&rawtime, timestr); + timestr[strlen(timestr)-1] = '\0'; + char output[1024]; int ret = 0; switch (level) { case LOG_SEVERE: - snprintf(output, 1024, "%s: %s", severe, fmt); + snprintf(output, 1024, "%s %s: %s", timestr, severe, fmt); ret = vfprintf(out_stream, output, args); break; case LOG_WARNING: - snprintf(output, 1024, "%s: %s", warn, fmt); + snprintf(output, 1024, "%s %s: %s", timestr, warn, fmt); ret = vfprintf(out_stream, output, args); break; case LOG_INFO: - snprintf(output, 1024, "%s: %s", info, fmt); + snprintf(output, 1024, "%s %s: %s", timestr, info, fmt); ret = vfprintf(out_stream, output, args); break; case LOG_DEBUG: - snprintf(output, 1024, "%s: %s", debug, fmt); + snprintf(output, 1024, "%s %s: %s", timestr, debug, fmt); ret = vfprintf(out_stream, output, args); break; } + fflush(out_stream); return ret; } @@ -49,3 +58,7 @@ int log_msg(int level, const char *fmt, ...) { return done; } + +void close_logfile(void) { + fclose(out_stream); +} -- cgit v1.2.3