aboutsummaryrefslogtreecommitdiff
path: root/src/misc/lv_log.c
blob: 3f3caec8ddb92ee676d50c9eaafe21f81993827a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
 * @file lv_log.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_log.h"
#if LV_USE_LOG

#include "../misc/lv_types.h"
#include "../stdlib/lv_sprintf.h"
#include "../stdlib/lv_mem.h"
#include "../stdlib/lv_string.h"
#include "../tick/lv_tick.h"
#include "../core/lv_global.h"

#if LV_LOG_PRINTF
    #include <stdio.h>
#endif

/*********************
 *      DEFINES
 *********************/
#if LV_LOG_USE_TIMESTAMP
    #define last_log_time LV_GLOBAL_DEFAULT()->log_last_log_time
#endif
#define custom_print_cb LV_GLOBAL_DEFAULT()->custom_log_print_cb

#if LV_LOG_USE_TIMESTAMP
    #define LOG_TIMESTAMP_FMT  "\t(%" LV_PRIu32 ".%03" LV_PRIu32 ", +%" LV_PRIu32 ")\t"
    #define LOG_TIMESTAMP_EXPR t / 1000, t % 1000, t - last_log_time,
#else
    #define LOG_TIMESTAMP_FMT
    #define LOG_TIMESTAMP_EXPR
#endif

#if LV_LOG_USE_FILE_LINE
    #define LOG_FILE_LINE_FMT " %s:%d"
    #define LOG_FILE_LINE_EXPR , &file[p], line
#else
    #define LOG_FILE_LINE_FMT
    #define LOG_FILE_LINE_EXPR
#endif

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_log_register_print_cb(lv_log_print_g_cb_t print_cb)
{
    custom_print_cb = print_cb;
}

void lv_log_add(lv_log_level_t level, const char * file, int line, const char * func, const char * format, ...)
{
    if(level >= LV_LOG_LEVEL_NUM) return; /*Invalid level*/

    if(level >= LV_LOG_LEVEL) {
        va_list args;
        va_start(args, format);

#if LV_LOG_USE_FILE_LINE
        /*Use only the file name not the path*/
        size_t p;
        for(p = lv_strlen(file); p > 0; p--) {
            if(file[p] == '/' || file[p] == '\\') {
                p++;    /*Skip the slash*/
                break;
            }
        }
#else
        LV_UNUSED(file);
        LV_UNUSED(line);
#endif

#if LV_LOG_USE_TIMESTAMP
        uint32_t t = lv_tick_get();
#endif
        static const char * lvl_prefix[] = {"Trace", "Info", "Warn", "Error", "User"};

#if LV_LOG_PRINTF
        printf("[%s]" LOG_TIMESTAMP_FMT " %s: ",
               lvl_prefix[level], LOG_TIMESTAMP_EXPR func);
        vprintf(format, args);
        printf(LOG_FILE_LINE_FMT "\n" LOG_FILE_LINE_EXPR);
        fflush(stdout);
#endif
        if(custom_print_cb) {
            char buf[512];
            char msg[256];
            lv_vsnprintf(msg, sizeof(msg), format, args);
            lv_snprintf(buf, sizeof(buf), "[%s]" LOG_TIMESTAMP_FMT " %s: %s" LOG_FILE_LINE_FMT "\n",
                        lvl_prefix[level], LOG_TIMESTAMP_EXPR func, msg LOG_FILE_LINE_EXPR);
            custom_print_cb(level, buf);
        }

#if LV_LOG_USE_TIMESTAMP
        last_log_time = t;
#endif
        va_end(args);
    }
}

void lv_log(const char * format, ...)
{
    if(LV_LOG_LEVEL >= LV_LOG_LEVEL_NONE) return; /* disable log */

    va_list args;
    va_start(args, format);

#if LV_LOG_PRINTF
    vprintf(format, args);
#else
    if(custom_print_cb) {
        char buf[512];
        lv_vsnprintf(buf, sizeof(buf), format, args);
        custom_print_cb(LV_LOG_LEVEL_USER, buf);
    }
#endif

    va_end(args);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

#endif /*LV_USE_LOG*/