summaryrefslogtreecommitdiff
path: root/include/rtdbg.h
blob: 4d9f760c01ff58e31fcf5d1c46e0c0cbd95c9a8e (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
147
/*
 * File      : rtdbg.h
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2016, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2016-11-12     Bernard      The first version
 * 2018-05-25     armink       Add simple API, such as LOG_D, LOG_E
 */

/*
 * The macro definitions for debug
 *
 * These macros are defined in static. If you want to use debug macro, you can
 * use as following code:
 *
 * In your C/C++ file, enable/disable DEBUG_ENABLE macro, and then include this
 * header file.
 *
 * #define DBG_SECTION_NAME    "[ MOD]"
 * #define DBG_ENABLE          // enable debug macro
 * #define DBG_LEVEL           DBG_INFO
 * #include <rtdbg.h>          // must after of DEBUG_ENABLE or some other options
 *
 * Then in your C/C++ file, you can use dbg_log macro to print out logs:
 * dbg_log(DBG_INFO, "this is a log!\n");
 *
 * Or if you want to using the simple API, you can
 * LOG_D("this is a debug log!");
 * LOG_E("this is a error log!");
 *
 * If you want to use different color for different kinds log, you can
 * #define DBG_COLOR
 */

#ifndef RT_DBG_H__
#define RT_DBG_H__

#include <rtconfig.h>

/* DEBUG level */
#define DBG_LOG             0
#define DBG_INFO            1
#define DBG_WARNING         2
#define DBG_ERROR           3

#ifndef DBG_SECTION_NAME
#define DBG_SECTION_NAME    "[ DBG]"
#endif

#ifdef DBG_ENABLE

#ifndef DBG_LEVEL
#define DBG_LEVEL         DBG_WARNING
#endif

/*
 * The color for terminal (foreground)
 * BLACK    30
 * RED      31
 * GREEN    32
 * YELLOW   33
 * BLUE     34
 * PURPLE   35
 * CYAN     36
 * WHITE    37
 */
#ifdef DBG_COLOR
#define _DBG_COLOR(n)       rt_kprintf("\033["#n"m")
#else
#define _DBG_COLOR(n)
#endif

/*
 * static debug routine
 */
#define dbg_log(level, fmt, ...)                            \
    if ((level) >= DBG_LEVEL)                               \
    {                                                       \
        switch(level)                                       \
        {                                                   \
            case DBG_ERROR:   _DBG_COLOR(31); break;        \
            case DBG_WARNING: _DBG_COLOR(33); break;        \
            case DBG_INFO:    _DBG_COLOR(32); break;        \
            default: break;                                 \
        }                                                   \
        rt_kprintf(DBG_SECTION_NAME fmt, ##__VA_ARGS__);    \
        _DBG_COLOR(0);                                      \
    }

#define dbg_here                                            \
    if ((DBG_LEVEL) >= DBG_LOG){                            \
        rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n",        \
            __FUNCTION__, __LINE__);                        \
    }

#define dbg_enter                                           \
    if ((DBG_LEVEL) >= DBG_LOG){                            \
        _DBG_COLOR(32);                                     \
        rt_kprintf(DBG_SECTION_NAME " Enter %s\n",          \
            __FUNCTION__);                                  \
        _DBG_COLOR(0);                                      \
    }

#define dbg_exit                                            \
    if ((DBG_LEVEL) >= DBG_LOG){                            \
        _DBG_COLOR(32);                                     \
        rt_kprintf(DBG_SECTION_NAME " Exit  %s:%d\n",       \
            __FUNCTION__);                                  \
        _DBG_COLOR(0);                                      \
    }

#define dbg_log_line(level, ...)                            \
    dbg_log(level, __VA_ARGS__);                            \
    if ((level) >= DBG_LEVEL) {                             \
        rt_kprintf("\n");                                   \
    }

#else
#define dbg_log(level, fmt, ...)
#define dbg_here
#define dbg_enter
#define dbg_exit
#define dbg_log_line(level, ...)
#endif

#define LOG_D(...)           dbg_log_line(DBG_LOG    , __VA_ARGS__)
#define LOG_I(...)           dbg_log_line(DBG_INFO   , __VA_ARGS__)
#define LOG_W(...)           dbg_log_line(DBG_WARNING, __VA_ARGS__)
#define LOG_E(...)           dbg_log_line(DBG_ERROR  , __VA_ARGS__)

#endif /* RT_DBG_H__ */