diff options
author | Danny Holman <dholman@gymli.org> | 2023-01-04 00:33:15 -0600 |
---|---|---|
committer | Danny Holman <dholman@gymli.org> | 2023-01-04 00:33:15 -0600 |
commit | 760fada7269b083ce00a1f18a4fb1cbb9ab13dc7 (patch) | |
tree | 8c1f806f761abbc7c55245b82cb5338c2af084b7 /server/src | |
parent | be4f102b8aa16bb90ed7d8d51a84650987dfdb40 (diff) |
logging: add a close function
Add a close function to close the file handle pointing at the log file.
Signed-off-by: Danny Holman <dholman@gymli.org>
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/logging.c | 21 |
1 files changed, 17 insertions, 4 deletions
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 <logging.h> #include <stdio.h> #include <pthread.h> +#include <time.h> +#include <string.h> 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); +} |