From bc41d39f27c7467970b751ed3ebea28b00a8a0b3 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Thu, 22 Aug 2024 20:58:03 -0500 Subject: core: add a basic logging framework Add a basic framework for logging messages to the console or to a file. Signed-off-by: Danny Holman --- .gitignore | 4 ++++ include/rune_logging.h | 16 ++++++++++++++++ src/logging.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 include/rune_logging.h create mode 100644 src/logging.c diff --git a/.gitignore b/.gitignore index 78e4e10..29cd635 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ *.lzo *.o *.o.* +*.out *.patch *.so *.tar @@ -32,3 +33,6 @@ cscope.* ncscope.* tags TAGS + +# Output directory +bin/ diff --git a/include/rune_logging.h b/include/rune_logging.h new file mode 100644 index 0000000..a0b9e29 --- /dev/null +++ b/include/rune_logging.h @@ -0,0 +1,16 @@ +#ifndef RUNE_LOGGING_H +#define RUNE_LOGGING_H + +#include + +enum log_level { + LOG_FATAL, + LOG_ERROR, + LOG_WARN, + LOG_INFO, + LOG_DEBUG +}; + +void log_output(int level, const char *fmt, ...); + +#endif diff --git a/src/logging.c b/src/logging.c new file mode 100644 index 0000000..e755710 --- /dev/null +++ b/src/logging.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +#define LSTR_FATAL "[FATAL]" +#define LSTR_ERROR "[ERROR]" +#define LSTR_WARN "[WARNING]" +#define LSTR_INFO "[INFO]" +#define LSTR_DEBUG "[DEBUG]" + +void log_output(int level, const char *fmt, ...) { + char out[8192]; + memset(out, 0, sizeof(out)); + + va_list arg_ptr; + va_start(arg_ptr, fmt); + vsnprintf(out, 8192, fmt, arg_ptr); + va_end(arg_ptr); + + char *lvl_str; + switch (level) { + case LOG_FATAL: + lvl_str = LSTR_FATAL; + break; + case LOG_ERROR: + lvl_str = LSTR_ERROR; + break; + case LOG_WARN: + lvl_str = LSTR_WARN; + break; + case LOG_INFO: + lvl_str = LSTR_INFO; + break; + case LOG_DEBUG: + lvl_str = LSTR_DEBUG; + break; + } + printf("%s %s\n", lvl_str, out); +} -- cgit v1.2.3