From 04d33973f4a54a0c971ec920bf6e8b5470faefcb Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Thu, 23 May 2019 13:58:16 +0200 Subject: [PATCH] sys/log: add colored logging module --- makefiles/pseudomodules.inc.mk | 1 + sys/log/Makefile.include | 4 ++ sys/log/log_color/log_module.h | 123 +++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 sys/log/log_color/log_module.h diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index d110900ff1..d82af66745 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -44,6 +44,7 @@ PSEUDOMODULES += l2filter_whitelist PSEUDOMODULES += lis2dh12_spi PSEUDOMODULES += log PSEUDOMODULES += log_printfnoformat +PSEUDOMODULES += log_color PSEUDOMODULES += lora PSEUDOMODULES += mpu_stack_guard PSEUDOMODULES += nanocoap_% diff --git a/sys/log/Makefile.include b/sys/log/Makefile.include index 05720548df..530c972f11 100644 --- a/sys/log/Makefile.include +++ b/sys/log/Makefile.include @@ -1,3 +1,7 @@ ifneq (,$(filter log_printfnoformat,$(USEMODULE))) USEMODULE_INCLUDES += $(RIOTBASE)/sys/log/log_printfnoformat endif + +ifneq (,$(filter log_color,$(USEMODULE))) + USEMODULE_INCLUDES += $(RIOTBASE)/sys/log/log_color +endif diff --git a/sys/log/log_color/log_module.h b/sys/log/log_color/log_module.h new file mode 100644 index 0000000000..4e0942923b --- /dev/null +++ b/sys/log/log_color/log_module.h @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2019 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @defgroup sys_log_color Colored log module + * @ingroup sys + * @brief This module implements a logging module with colored output + * @{ + * + * @file + * @brief log_module header + * + * @author Alexandre Abadie + */ + +#ifndef LOG_MODULE_H +#define LOG_MODULE_H + +#include +#include +#include + +#include "log.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Default ANSI color escape code for error logs + * + * Default is bold red + */ +#ifndef LOG_ERROR_ANSI_COLOR_CODE +#define LOG_ERROR_ANSI_COLOR_CODE ("\033[1;31m") +#endif + +/** + * @brief Default ANSI color escape code for warning logs + * + * Default is bold yellow + */ +#ifndef LOG_WARNING_ANSI_COLOR_CODE +#define LOG_WARNING_ANSI_COLOR_CODE ("\033[1;33m") +#endif + +/** + * @brief Default ANSI color escape code for info logs + * + * Default is bold white + */ +#ifndef LOG_INFO_ANSI_COLOR_CODE +#define LOG_INFO_ANSI_COLOR_CODE ("\033[1m") +#endif + +/** + * @brief Default ANSI color escape code for debug logs + * + * Default is green + */ +#ifndef LOG_DEBUG_ANSI_COLOR_CODE +#define LOG_DEBUG_ANSI_COLOR_CODE ("\033[0;32m") +#endif + +/** + * @brief ANSI color escape code used for resetting color + */ +#define LOG_RESET_ANSI_COLOR_CODE ("\033[0m") + +/** + * @brief ANSI color escape codes array + * + * Internal use only + */ +static const char * const _ansi_codes[] = +{ + [LOG_ERROR] = LOG_ERROR_ANSI_COLOR_CODE, + [LOG_WARNING] = LOG_WARNING_ANSI_COLOR_CODE, + [LOG_INFO] = LOG_INFO_ANSI_COLOR_CODE, + [LOG_DEBUG] = LOG_DEBUG_ANSI_COLOR_CODE, +}; + +/** + * @brief log_write overridden function for colored output + * + * @param[in] level Logging level + * @param[in] format String format to print + */ +static inline void log_write(unsigned level, const char *format, ...) +{ + assert(level > 0); + + printf("%s", _ansi_codes[level]); + va_list args; + va_start(args, format); + /* Temporarily disable clang format-nonliteral warning */ +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wformat-nonliteral" +#endif /* clang */ + vprintf(format, args); +#ifdef __clang__ +#pragma clang diagnostic pop +#endif /* clang */ + va_end(args); + printf(LOG_RESET_ANSI_COLOR_CODE); + +#ifdef MODULE_NEWLIB + /* no fflush on msp430 */ + fflush(stdout); +#endif +} + +#ifdef __cplusplus +} +#endif +/**@}*/ +#endif /* LOG_MODULE_H */