1 /**
2  * @file lv_log.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_log.h"
10 #if LV_USE_LOG
11 
12 #include <stdarg.h>
13 #include <string.h>
14 #include "lv_printf.h"
15 
16 #if LV_LOG_PRINTF
17     #include <stdio.h>
18 #endif
19 
20 /*********************
21  *      DEFINES
22  *********************/
23 
24 /**********************
25  *      TYPEDEFS
26  **********************/
27 
28 /**********************
29  *  STATIC PROTOTYPES
30  **********************/
31 
32 /**********************
33  *  STATIC VARIABLES
34  **********************/
35 static lv_log_print_g_cb_t custom_print_cb;
36 
37 /**********************
38  *      MACROS
39  **********************/
40 
41 /**********************
42  *   GLOBAL FUNCTIONS
43  **********************/
44 
45 /**
46  * Register custom print/write function to call when a log is added.
47  * It can format its "File path", "Line number" and "Description" as required
48  * and send the formatted log message to a console or serial port.
49  * @param print_cb a function pointer to print a log
50  */
lv_log_register_print_cb(lv_log_print_g_cb_t print_cb)51 void lv_log_register_print_cb(lv_log_print_g_cb_t print_cb)
52 {
53     custom_print_cb = print_cb;
54 }
55 
56 
57 /**
58  * Add a log
59  * @param level the level of log. (From `lv_log_level_t` enum)
60  * @param file name of the file when the log added
61  * @param line line number in the source code where the log added
62  * @param func name of the function when the log added
63  * @param format printf-like format string
64  * @param ... parameters for `format`
65  */
_lv_log_add(lv_log_level_t level,const char * file,int line,const char * func,const char * format,...)66 void _lv_log_add(lv_log_level_t level, const char * file, int line, const char * func, const char * format, ...)
67 {
68     if(level >= _LV_LOG_LEVEL_NUM) return; /*Invalid level*/
69 
70     if(level >= LV_LOG_LEVEL) {
71         va_list args;
72         va_start(args, format);
73         char buf[256];
74         lv_vsnprintf(buf, sizeof(buf), format, args);
75         va_end(args);
76 
77 #if LV_LOG_PRINTF
78         /*Use only the file name not the path*/
79         size_t p;
80         for(p = strlen(file); p > 0; p--) {
81             if(file[p] == '/' || file[p] == '\\') {
82                 p++;    /*Skip the slash*/
83                 break;
84             }
85         }
86 
87         static const char * lvl_prefix[] = {"Trace", "Info", "Warn", "Error", "User"};
88         printf("%s: %s \t(%s #%d %s())\n", lvl_prefix[level], buf, &file[p], line, func);
89 #else
90         if(custom_print_cb) custom_print_cb(level, file, line, func, buf);
91 #endif
92     }
93 }
94 
95 /**********************
96  *   STATIC FUNCTIONS
97  **********************/
98 
99 #endif /*LV_USE_LOG*/
100