From 568caa0501a99b6e18055fa937efff5fa8bf6c04 Mon Sep 17 00:00:00 2001 From: Alexander Egorenkov Date: Thu, 8 Apr 2021 13:06:33 +0200 Subject: [PATCH] libutil: Introduce multi-level message logging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The goal of util_log is to provide a facility for a multi-level message logging on stderr. This allows to selectively enable/disable log messages via a log level which can be adjusted at runtime. Signed-off-by: Alexander Egorenkov Reviewed-by: Jan Hoeppner Reviewed-by: Philipp Rudo Signed-off-by: Jan Höppner --- include/lib/util_log.h | 28 +++++++++++++++++ libutil/Makefile | 1 + libutil/util_log.c | 70 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 include/lib/util_log.h create mode 100644 libutil/util_log.c diff --git a/include/lib/util_log.h b/include/lib/util_log.h new file mode 100644 index 00000000..b08fb32d --- /dev/null +++ b/include/lib/util_log.h @@ -0,0 +1,28 @@ +/** + * @defgroup util_log_h util_file: Multi-level message logging interface + * @{ + * @brief Multi-level message logging + * + * Copyright IBM Corp. 2021 + * + * s390-tools is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIB_UTIL_LOG_H +#define LIB_UTIL_LOG_H + +enum util_log_level { + UTIL_LOG_ERROR, + UTIL_LOG_WARN, + UTIL_LOG_INFO, + UTIL_LOG_DEBUG, + UTIL_LOG_TRACE, + UTIL_LOG_NUM_LEVELS /* Must be the last one. */ +}; + +void util_log_set_level(int log_level); + +void util_log_print(int log_level, const char *fmt, ...); + +#endif /** LIB_UTIL_LOG_H @} */ diff --git a/libutil/Makefile b/libutil/Makefile index 8c36d4ef..71fed661 100644 --- a/libutil/Makefile +++ b/libutil/Makefile @@ -22,6 +22,7 @@ objects = util_base.o \ util_file.o \ util_libc.o \ util_list.o \ + util_log.o \ util_opt.o \ util_panic.o \ util_part.o \ diff --git a/libutil/util_log.c b/libutil/util_log.c new file mode 100644 index 00000000..6299b482 --- /dev/null +++ b/libutil/util_log.c @@ -0,0 +1,70 @@ +/* + * Multi-level message logging + * + * Copyright IBM Corp. 2021 + * + * s390-tools is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include +#include + +#include "lib/util_log.h" + +static int current_log_level = UTIL_LOG_INFO; + +static const char *log_level_to_str(int log_level) +{ + switch (log_level) { + case UTIL_LOG_ERROR: + return "ERROR"; + case UTIL_LOG_WARN: + return "WARN"; + case UTIL_LOG_INFO: + return "INFO"; + case UTIL_LOG_DEBUG: + return "DEBUG"; + case UTIL_LOG_TRACE: + return "TRACE"; + default: + return "UNKNW"; + } +} + +static void log_helper(FILE *fp, int log_level, const char *fmt, va_list args) +{ + if (current_log_level < log_level) + return; + + fprintf(fp, "%5s: ", log_level_to_str(log_level)); + vfprintf(fp, fmt, args); +} + +/** + * Changes the current log level + * + * @param[in] log_level A new log level to be set as the current one + */ +void util_log_set_level(int log_level) +{ + current_log_level = log_level; +} + +/** + * Outputs the given message on stderr + * + * The given message is printed only if the current log level is >= than the given one. + * + * @param[in] log_level Log level starting from which the given message shall be printed + * @param[in] fmt Format string for generation of the log message + * @param[in] ... Parameters for format string + */ +void util_log_print(int log_level, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + log_helper(stderr, log_level, fmt, args); + va_end(args); +}