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 "../misc/lv_types.h"
13 #include "../stdlib/lv_sprintf.h"
14 #include "../stdlib/lv_mem.h"
15 #include "../stdlib/lv_string.h"
16 #include "../tick/lv_tick.h"
17 #include "../core/lv_global.h"
18 
19 #if LV_LOG_PRINTF
20     #include <stdio.h>
21 #endif
22 
23 /*********************
24  *      DEFINES
25  *********************/
26 #if LV_LOG_USE_TIMESTAMP
27     #define last_log_time LV_GLOBAL_DEFAULT()->log_last_log_time
28 #endif
29 #define custom_print_cb LV_GLOBAL_DEFAULT()->custom_log_print_cb
30 
31 #if LV_LOG_USE_TIMESTAMP
32     #define LOG_TIMESTAMP_FMT  "\t(%" LV_PRIu32 ".%03" LV_PRIu32 ", +%" LV_PRIu32 ")\t"
33     #define LOG_TIMESTAMP_EXPR t / 1000, t % 1000, t - last_log_time,
34 #else
35     #define LOG_TIMESTAMP_FMT
36     #define LOG_TIMESTAMP_EXPR
37 #endif
38 
39 #if LV_LOG_USE_FILE_LINE
40     #define LOG_FILE_LINE_FMT " %s:%d"
41     #define LOG_FILE_LINE_EXPR , &file[p], line
42 #else
43     #define LOG_FILE_LINE_FMT
44     #define LOG_FILE_LINE_EXPR
45 #endif
46 
47 /**********************
48  *      TYPEDEFS
49  **********************/
50 
51 /**********************
52  *  STATIC PROTOTYPES
53  **********************/
54 
55 /**********************
56  *  STATIC VARIABLES
57  **********************/
58 
59 /**********************
60  *      MACROS
61  **********************/
62 
63 /**********************
64  *   GLOBAL FUNCTIONS
65  **********************/
66 
lv_log_register_print_cb(lv_log_print_g_cb_t print_cb)67 void lv_log_register_print_cb(lv_log_print_g_cb_t print_cb)
68 {
69     custom_print_cb = print_cb;
70 }
71 
lv_log_add(lv_log_level_t level,const char * file,int line,const char * func,const char * format,...)72 void lv_log_add(lv_log_level_t level, const char * file, int line, const char * func, const char * format, ...)
73 {
74     if(level >= LV_LOG_LEVEL_NUM) return; /*Invalid level*/
75 
76     if(level >= LV_LOG_LEVEL) {
77         va_list args;
78         va_start(args, format);
79 
80 #if LV_LOG_USE_FILE_LINE
81         /*Use only the file name not the path*/
82         size_t p;
83         for(p = lv_strlen(file); p > 0; p--) {
84             if(file[p] == '/' || file[p] == '\\') {
85                 p++;    /*Skip the slash*/
86                 break;
87             }
88         }
89 #else
90         LV_UNUSED(file);
91         LV_UNUSED(line);
92 #endif
93 
94 #if LV_LOG_USE_TIMESTAMP
95         uint32_t t = lv_tick_get();
96 #endif
97         static const char * lvl_prefix[] = {"Trace", "Info", "Warn", "Error", "User"};
98 
99 #if LV_LOG_PRINTF
100         printf("[%s]" LOG_TIMESTAMP_FMT " %s: ",
101                lvl_prefix[level], LOG_TIMESTAMP_EXPR func);
102         vprintf(format, args);
103         printf(LOG_FILE_LINE_FMT "\n" LOG_FILE_LINE_EXPR);
104         fflush(stdout);
105 #endif
106         if(custom_print_cb) {
107             char buf[512];
108             char msg[256];
109             lv_vsnprintf(msg, sizeof(msg), format, args);
110             lv_snprintf(buf, sizeof(buf), "[%s]" LOG_TIMESTAMP_FMT " %s: %s" LOG_FILE_LINE_FMT "\n",
111                         lvl_prefix[level], LOG_TIMESTAMP_EXPR func, msg LOG_FILE_LINE_EXPR);
112             custom_print_cb(level, buf);
113         }
114 
115 #if LV_LOG_USE_TIMESTAMP
116         last_log_time = t;
117 #endif
118         va_end(args);
119     }
120 }
121 
lv_log(const char * format,...)122 void lv_log(const char * format, ...)
123 {
124     if(LV_LOG_LEVEL >= LV_LOG_LEVEL_NONE) return; /* disable log */
125 
126     va_list args;
127     va_start(args, format);
128 
129 #if LV_LOG_PRINTF
130     vprintf(format, args);
131 #else
132     if(custom_print_cb) {
133         char buf[512];
134         lv_vsnprintf(buf, sizeof(buf), format, args);
135         custom_print_cb(LV_LOG_LEVEL_USER, buf);
136     }
137 #endif
138 
139     va_end(args);
140 }
141 
142 /**********************
143  *   STATIC FUNCTIONS
144  **********************/
145 
146 #endif /*LV_USE_LOG*/
147