From c394c09330760985d282cb866a06dea6294012aa Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Fri, 22 May 2020 13:43:46 +0200 Subject: first public release Signed-off-by: Toni Uhlig --- logging.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 logging.c (limited to 'logging.c') diff --git a/logging.c b/logging.c new file mode 100644 index 0000000..aadfe50 --- /dev/null +++ b/logging.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include + +#include "logging.h" + +#define LOGMSG_MAXLEN BUFSIZ +/* ANSI terminal color codes */ +#define RESET "\x1B[0m" +#define GRN "\x1B[32;1m" +#define YEL "\x1B[33;1m" +#define RED "\x1B[31;1;5m" +#define BLU "\x1B[34;1;1m" +#define CYA "\x1B[36;1;1m" +#define DEF RESET + +#ifdef DEBUG_BUILD +static log_priority lower_prio = LP_DEBUG; +#else +static log_priority lower_prio = NOTICE; +#endif + +void log_fmt_colored(log_priority prio, const char * fmt, ...) +{ + char out[LOGMSG_MAXLEN + 1]; + va_list arglist; + + if (prio < lower_prio) + return; + + assert(fmt); + va_start(arglist, fmt); + assert(vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0); + va_end(arglist); + + switch (prio) { + case LP_DEBUG: + printf("[" DEF "DEBUG" RESET "] %s\n", out); + break; + case NOTICE: + printf("[" GRN "NOTICE" RESET "] %s\n", out); + break; + case WARNING: + printf("[" YEL "WARNING" RESET "] %s\n", out); + break; + case ERROR: + printf("[" RED "ERROR" RESET "] %s\n", out); + break; + case EVENT: + printf("[" BLU "EVENT" RESET "] %s\n", out); + break; + case PROTO: + printf("[" CYA "PROTO" RESET "] %s\n", out); + break; + } +} -- cgit v1.2.3