diff --git a/lv_conf_templ.h b/lv_conf_templ.h index 1be9b24801..f657e5cba6 100644 --- a/lv_conf_templ.h +++ b/lv_conf_templ.h @@ -86,6 +86,16 @@ #define LV_ATTRIBUTE_TASK_HANDLER /* Define a custom attribute to `lv_task_handler` function */ #define LV_COMPILER_VLA_SUPPORTED 1 /* 1: Variable length array is supported*/ +/*Log settings*/ +#define USE_LV_LOG 1 /*Enable/disable the log module*/ +#if USE_LV_LOG +#define LV_LOG_INFO 0 /*1: Log a lot of runtime information*/ +#define LV_LOG_WARN 0 /*1: Log is something unexpected happens but succesfully handled*/ +#define LV_LOG_ERROR 1 /*1: Log critical error*/ +#define LV_LOG_USER 1 /*1: Log user defined/user level things */ +#define LV_LOG_PRINTF 0 /*1: Print the log with 'printf'; 0: user need to register a callback*/ +#endif /*USE_LV_LOG*/ + /*================ * THEME USAGE *================*/ diff --git a/lv_core/lv_obj.c b/lv_core/lv_obj.c index 9dbbfda08e..68fe510699 100644 --- a/lv_core/lv_obj.c +++ b/lv_core/lv_obj.c @@ -62,6 +62,10 @@ static lv_ll_t scr_ll; /*Linked list of screens*/ */ void lv_init(void) { +#if USE_LV_LOG + lv_log_add(LV_LOG_LEVEL_INFO, __FILE__, __LINE__, "lv_init called"); +#endif + /*Initialize the lv_misc modules*/ lv_mem_init(); lv_task_init(); @@ -101,6 +105,10 @@ void lv_init(void) /*Init the input device handling*/ lv_indev_init(); #endif + +#if USE_LV_LOG + lv_log_add(LV_LOG_LEVEL_INFO, __FILE__, __LINE__, "lv_init finished"); +#endif } /*-------------------- diff --git a/lv_core/lv_obj.h b/lv_core/lv_obj.h index f6a96dc23b..7735fbc89a 100644 --- a/lv_core/lv_obj.h +++ b/lv_core/lv_obj.h @@ -21,6 +21,7 @@ extern "C" { #include "../lv_misc/lv_mem.h" #include "../lv_misc/lv_ll.h" #include "../lv_misc/lv_color.h" +#include "../lv_misc/lv_log.h" /********************* * DEFINES diff --git a/lv_misc/lv_log.c b/lv_misc/lv_log.c new file mode 100644 index 0000000000..a796af47ac --- /dev/null +++ b/lv_misc/lv_log.c @@ -0,0 +1,80 @@ +/** + * @file lv_log.c + * + */ + +/********************* + * INCLUDES + *********************/ +#include "lv_log.h" +#if USE_LV_LOG + +#if LV_LOG_PRINTF +#include +#endif +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ + +/********************** + * STATIC VARIABLES + **********************/ +static void (*print_cb)(lv_log_level_t , const char *, uint32_t , const char *); + +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +/** + * Register custom print (or anything else) function to call when log is added + * @param f a function pointer: + * `void my_print (lv_log_level_t level, const char * file, uint32_t line, const char * dsc)` + */ +void lv_log_register_print(void f(lv_log_level_t , const char *, uint32_t , const char *)) +{ + print_cb = f; +} + +/** + * Add a log + * @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 + */ +void lv_log_add(lv_log_level_t level, const char * file, uint32_t line, const char * dsc) +{ + if(level >= _LV_LOG_LEVEL_NUM) return; /*Invalid level*/ + + if((level == LV_LOG_LEVEL_INFO && LV_LOG_INFO) || + (level == LV_LOG_LEVEL_WARN && LV_LOG_WARN) || + (level == LV_LOG_LEVEL_ERROR && LV_LOG_ERROR) || + (level == LV_LOG_LEVEL_USER && LV_LOG_USER)) + { + +#if LV_LOG_PRINTF + static const char * lvl_prefix[] = {"Info", "Warn", "Error", "User"}; + printf("%s %s:%d: %s\n", lvl_prefix[level], file, line, dsc); +#else + if(print_cb) print_cb(level, file, line, dsc); +#endif + } +} + +/********************** + * STATIC FUNCTIONS + **********************/ + +#endif /*USE_LV_LOG*/ diff --git a/lv_misc/lv_log.h b/lv_misc/lv_log.h new file mode 100644 index 0000000000..228ca6facd --- /dev/null +++ b/lv_misc/lv_log.h @@ -0,0 +1,74 @@ +/** + * @file lv_log.h + * + */ + +#ifndef LV_LOG_H +#define LV_LOG_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "../../lv_conf.h" +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/*Possible log level. For compatibility declare it independently from `USE_LV_LOG`*/ +typedef enum +{ + LV_LOG_LEVEL_INFO = 0, + LV_LOG_LEVEL_WARN, + LV_LOG_LEVEL_ERROR, + LV_LOG_LEVEL_USER, + _LV_LOG_LEVEL_NUM +}lv_log_level_t; + +#if USE_LV_LOG + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Register custom print (or anything else) function to call when log is added + * @param f a function pointer: + * `void my_print (lv_log_level_t level, const char * file, uint32_t line, const char * dsc)` + */ +void lv_log_register_print(void f(lv_log_level_t , const char *, uint32_t , const char *)); + +/** + * Add a log + * @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 + */ +void lv_log_add(lv_log_level_t level, const char * file, uint32_t line, const char * dsc); + +/********************** + * MACROS + **********************/ + +#else /*USE_LV_LOG*/ + +/*Do nothing if `USE_LV_LOG 0`*/ +#define lv_log_add(level, file, line, dsc) {;} + +#endif /*USE_LV_LOG*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_LOG_H*/