From be4f102b8aa16bb90ed7d8d51a84650987dfdb40 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Fri, 18 Nov 2022 09:56:13 -0600 Subject: server: add a logging framework Add a logging framework so the server can output to a single point. Signed-off-by: Danny Holman --- .gitignore | 4 ++-- server/include/logging.h | 18 +++++++++++++++++ server/src/logging.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 server/include/logging.h create mode 100644 server/src/logging.c diff --git a/.gitignore b/.gitignore index dcbc549..320e898 100644 --- a/.gitignore +++ b/.gitignore @@ -36,8 +36,8 @@ configure~ config.log config.status depcomp -include/config.h -include/stamp-h1 +config.h +stamp-h1 install-sh Makefile missing diff --git a/server/include/logging.h b/server/include/logging.h new file mode 100644 index 0000000..a964230 --- /dev/null +++ b/server/include/logging.h @@ -0,0 +1,18 @@ +#ifndef MRAT_LOGGING_H +#define MRAT_LOGGING_H + +#include +#include + +enum LOG_LEVEL { + LOG_SEVERE, + LOG_WARNING, + LOG_INFO, + LOG_DEBUG, +}; + +int init_logging(FILE *out_file); +int vlog_msg(int level, const char *fmt, va_list args); +int log_msg(int level, const char *fmt, ...); + +#endif diff --git a/server/src/logging.c b/server/src/logging.c new file mode 100644 index 0000000..9084baf --- /dev/null +++ b/server/src/logging.c @@ -0,0 +1,51 @@ +#include +#include +#include + +static const char *severe = "SEVERE"; +static const char *warn = "WARN"; +static const char *info = "INFO"; +static const char *debug = "DEBUG"; + +static FILE *out_stream; + +int init_logging(FILE *out_file) { + // TODO: maybe make this a bit more thread safe + out_stream = out_file; + return 0; +} + +int vlog_msg(int level, const char *fmt, va_list args) { + char output[1024]; + int ret = 0; + switch (level) { + case LOG_SEVERE: + snprintf(output, 1024, "%s: %s", severe, fmt); + ret = vfprintf(out_stream, output, args); + break; + case LOG_WARNING: + snprintf(output, 1024, "%s: %s", warn, fmt); + ret = vfprintf(out_stream, output, args); + break; + case LOG_INFO: + snprintf(output, 1024, "%s: %s", info, fmt); + ret = vfprintf(out_stream, output, args); + break; + case LOG_DEBUG: + snprintf(output, 1024, "%s: %s", debug, fmt); + ret = vfprintf(out_stream, output, args); + break; + } + return ret; +} + +int log_msg(int level, const char *fmt, ...) { + va_list args; + int done; + + va_start(args, fmt); + done = vlog_msg(level, fmt, args); + va_end(args); + + return done; +} -- cgit v1.2.3