From c76ff89af389f2437432e422399af36473dbe4c8 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Tue, 25 Feb 2020 08:38:56 +0100 Subject: [PATCH] add va_list support to LV_LOG functions --- src/lv_misc/lv_log.c | 17 +++++++++++---- src/lv_misc/lv_log.h | 50 +++++++++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/lv_misc/lv_log.c b/src/lv_misc/lv_log.c index acbdfb732..7f2ca82db 100644 --- a/src/lv_misc/lv_log.c +++ b/src/lv_misc/lv_log.c @@ -9,6 +9,9 @@ #include "lv_log.h" #if LV_USE_LOG +#include +#include "lv_printf.h" + #if LV_LOG_PRINTF #include #endif @@ -54,17 +57,23 @@ void lv_log_register_print_cb(lv_log_print_g_cb_t print_cb) * @param level the level of log. (From `lv_log_level_t` enum) * @param file name of the file when the log added * @param line line number in the source code where the log added - * @param dsc description of the log + * @param format printf-like format string + * @param ... parameters for `format` */ -void lv_log_add(lv_log_level_t level, const char * file, int line, const char * dsc) +void lv_log_add(lv_log_level_t level, const char * file, int line, const char * format, ...) { if(level >= _LV_LOG_LEVEL_NUM) return; /*Invalid level*/ if(level >= LV_LOG_LEVEL) { + va_list args; + va_start(args, format); + char buf[256]; + lv_vsnprintf(buf, sizeof(buf), format, args); + va_end(args); #if LV_LOG_PRINTF - static const char * lvl_prefix[] = {"Trace", "Info", "Warn", "Error"}; - printf("%s: %s \t(%s #%d)\n", lvl_prefix[level], dsc, file, line); + static const char * lvl_prefix[] = {"Trace", "Info", "Warn", "Error", "User"}; + printf("%s: %s \t(%s #%d)\n", lvl_prefix[level], buf, file, line); #else if(custom_print_cb) custom_print_cb(level, file, line, dsc); #endif diff --git a/src/lv_misc/lv_log.h b/src/lv_misc/lv_log.h index d43db790b..b84aa2d7b 100644 --- a/src/lv_misc/lv_log.h +++ b/src/lv_misc/lv_log.h @@ -26,13 +26,15 @@ extern "C" { #define LV_LOG_LEVEL_INFO 1 /**< Log important events*/ #define LV_LOG_LEVEL_WARN 2 /**< Log if something unwanted happened but didn't caused problem*/ #define LV_LOG_LEVEL_ERROR 3 /**< Only critical issue, when the system may fail*/ -#define LV_LOG_LEVEL_NONE 4 /**< Do not log anything*/ -#define _LV_LOG_LEVEL_NUM 5 /**< Number of log levels */ +#define LV_LOG_LEVEL_USER 4 /**< Custom logs from the user*/ +#define LV_LOG_LEVEL_NONE 5 /**< Do not log anything*/ +#define _LV_LOG_LEVEL_NUM 6 /**< Number of log levels */ LV_EXPORT_CONST_INT(LV_LOG_LEVEL_TRACE); LV_EXPORT_CONST_INT(LV_LOG_LEVEL_INFO); LV_EXPORT_CONST_INT(LV_LOG_LEVEL_WARN); LV_EXPORT_CONST_INT(LV_LOG_LEVEL_ERROR); +LV_EXPORT_CONST_INT(LV_LOG_LEVEL_USER); LV_EXPORT_CONST_INT(LV_LOG_LEVEL_NONE); typedef int8_t lv_log_level_t; @@ -64,45 +66,55 @@ void lv_log_register_print_cb(lv_log_print_g_cb_t print_cb); * @param level the level of log. (From `lv_log_level_t` enum) * @param file name of the file when the log added * @param line line number in the source code where the log added - * @param dsc description of the log + * @param format printf-like format string + * @param ... parameters for `format` */ -void lv_log_add(lv_log_level_t level, const char * file, int line, const char * dsc); +void lv_log_add(lv_log_level_t level, const char * file, int line, const char * format, ...); /********************** * MACROS **********************/ #if LV_LOG_LEVEL <= LV_LOG_LEVEL_TRACE -#define LV_LOG_TRACE(dsc) lv_log_add(LV_LOG_LEVEL_TRACE, __FILE__, __LINE__, dsc); +#define LV_LOG_TRACE(...) lv_log_add(LV_LOG_LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__); #else -#define LV_LOG_TRACE(dsc) \ +#define LV_LOG_TRACE(...) \ { \ ; \ } #endif #if LV_LOG_LEVEL <= LV_LOG_LEVEL_INFO -#define LV_LOG_INFO(dsc) lv_log_add(LV_LOG_LEVEL_INFO, __FILE__, __LINE__, dsc); +#define LV_LOG_INFO(...) lv_log_add(LV_LOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__); #else -#define LV_LOG_INFO(dsc) \ +#define LV_LOG_INFO(...) \ { \ ; \ } #endif #if LV_LOG_LEVEL <= LV_LOG_LEVEL_WARN -#define LV_LOG_WARN(dsc) lv_log_add(LV_LOG_LEVEL_WARN, __FILE__, __LINE__, dsc); +#define LV_LOG_WARN(...) lv_log_add(LV_LOG_LEVEL_WARN, __FILE__, __LINE__, __VA_ARGS__); #else -#define LV_LOG_WARN(dsc) \ +#define LV_LOG_WARN(...) \ { \ ; \ } #endif #if LV_LOG_LEVEL <= LV_LOG_LEVEL_ERROR -#define LV_LOG_ERROR(dsc) lv_log_add(LV_LOG_LEVEL_ERROR, __FILE__, __LINE__, dsc); +#define LV_LOG_ERROR(...) lv_log_add(LV_LOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__); #else -#define LV_LOG_ERROR(dsc) \ +#define LV_LOG_ERROR(...) \ + { \ + ; \ + } +#endif + +#if LV_LOG_LEVEL <= LV_LOG_LEVEL_USER +#define LV_LOG_USER(...) lv_log_add(LV_LOG_LEVEL_USER, __FILE__, __LINE__, __VA_ARGS__); +#else +#define LV_LOG_USER(...) \ { \ ; \ } @@ -111,23 +123,27 @@ void lv_log_add(lv_log_level_t level, const char * file, int line, const char * #else /*LV_USE_LOG*/ /*Do nothing if `LV_USE_LOG 0`*/ -#define lv_log_add(level, file, line, dsc) \ +#define lv_log_add(level, file, line, ...) \ { \ ; \ } -#define LV_LOG_TRACE(dsc) \ +#define LV_LOG_TRACE(...) \ { \ ; \ } -#define LV_LOG_INFO(dsc) \ +#define LV_LOG_INFO(...) \ { \ ; \ } -#define LV_LOG_WARN(dsc) \ +#define LV_LOG_WARN(...) \ { \ ; \ } -#define LV_LOG_ERROR(dsc) \ +#define LV_LOG_ERROR(...) \ + { \ + ; \ + } +#define LV_LOG_USER(...) \ { \ ; \ }