2017-10-15 22:30:05 +08:00
|
|
|
/*
|
|
|
|
* 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 16:06:06 +08:00
|
|
|
* 2018-05-25 armink Add simple API, such as LOG_D, LOG_E
|
2017-10-15 22:30:05 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
2018-07-11 10:17:34 +08:00
|
|
|
* #define DBG_SECTION_NAME "MOD"
|
2018-02-11 13:56:30 +08:00
|
|
|
* #define DBG_ENABLE // enable debug macro
|
|
|
|
* #define DBG_LEVEL DBG_INFO
|
|
|
|
* #include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
|
2017-10-15 22:30:05 +08:00
|
|
|
*
|
|
|
|
* 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");
|
|
|
|
*
|
2018-05-25 16:06:06 +08:00
|
|
|
* 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
|
2018-02-11 13:56:30 +08:00
|
|
|
* #define DBG_COLOR
|
2017-10-15 22:30:05 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef RT_DBG_H__
|
|
|
|
#define RT_DBG_H__
|
|
|
|
|
|
|
|
#include <rtconfig.h>
|
|
|
|
|
|
|
|
/* DEBUG level */
|
2018-09-11 18:26:28 +08:00
|
|
|
#define DBG_ERROR 0
|
|
|
|
#define DBG_WARNING 1
|
|
|
|
#define DBG_INFO 2
|
|
|
|
#define DBG_LOG 3
|
2017-10-15 22:30:05 +08:00
|
|
|
|
|
|
|
#ifndef DBG_SECTION_NAME
|
2018-07-19 18:04:02 +08:00
|
|
|
#define DBG_SECTION_NAME "DBG"
|
2017-10-15 22:30:05 +08:00
|
|
|
#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
|
2018-07-11 10:17:34 +08:00
|
|
|
#define _DBG_COLOR(n) rt_kprintf("\033["#n"m")
|
|
|
|
#define _DBG_LOG_HDR(lvl_name, color_n) \
|
|
|
|
rt_kprintf("\033["#color_n"m["lvl_name"/"DBG_SECTION_NAME"] ")
|
2018-09-06 09:50:18 +08:00
|
|
|
#define _DBG_LOG_X_END \
|
|
|
|
rt_kprintf("\033[0m\n")
|
2017-10-15 22:30:05 +08:00
|
|
|
#else
|
|
|
|
#define _DBG_COLOR(n)
|
2018-07-11 10:17:34 +08:00
|
|
|
#define _DBG_LOG_HDR(lvl_name, color_n) \
|
|
|
|
rt_kprintf("["lvl_name"/"DBG_SECTION_NAME"] ")
|
2018-09-06 09:50:18 +08:00
|
|
|
#define _DBG_LOG_X_END \
|
|
|
|
rt_kprintf("\n")
|
2018-07-11 10:17:34 +08:00
|
|
|
#endif /* DBG_COLOR */
|
2017-10-15 22:30:05 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* static debug routine
|
|
|
|
*/
|
|
|
|
#define dbg_log(level, fmt, ...) \
|
2018-09-11 18:26:28 +08:00
|
|
|
if ((level) <= DBG_LEVEL) \
|
2017-10-15 22:30:05 +08:00
|
|
|
{ \
|
|
|
|
switch(level) \
|
|
|
|
{ \
|
2018-07-11 10:17:34 +08:00
|
|
|
case DBG_ERROR: _DBG_LOG_HDR("E", 31); break; \
|
|
|
|
case DBG_WARNING: _DBG_LOG_HDR("W", 33); break; \
|
|
|
|
case DBG_INFO: _DBG_LOG_HDR("I", 32); break; \
|
|
|
|
case DBG_LOG: _DBG_LOG_HDR("D", 0); break; \
|
2017-10-15 22:30:05 +08:00
|
|
|
default: break; \
|
|
|
|
} \
|
2018-07-11 10:17:34 +08:00
|
|
|
rt_kprintf(fmt, ##__VA_ARGS__); \
|
2017-10-15 22:30:05 +08:00
|
|
|
_DBG_COLOR(0); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define dbg_here \
|
2018-09-11 18:26:28 +08:00
|
|
|
if ((DBG_LEVEL) <= DBG_LOG){ \
|
2017-10-15 22:30:05 +08:00
|
|
|
rt_kprintf(DBG_SECTION_NAME " Here %s:%d\n", \
|
|
|
|
__FUNCTION__, __LINE__); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define dbg_enter \
|
2018-09-11 18:26:28 +08:00
|
|
|
if ((DBG_LEVEL) <= DBG_LOG){ \
|
2017-10-15 22:30:05 +08:00
|
|
|
_DBG_COLOR(32); \
|
|
|
|
rt_kprintf(DBG_SECTION_NAME " Enter %s\n", \
|
|
|
|
__FUNCTION__); \
|
|
|
|
_DBG_COLOR(0); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define dbg_exit \
|
2018-09-11 18:26:28 +08:00
|
|
|
if ((DBG_LEVEL) <= DBG_LOG){ \
|
2017-10-15 22:30:05 +08:00
|
|
|
_DBG_COLOR(32); \
|
|
|
|
rt_kprintf(DBG_SECTION_NAME " Exit %s:%d\n", \
|
|
|
|
__FUNCTION__); \
|
|
|
|
_DBG_COLOR(0); \
|
|
|
|
}
|
|
|
|
|
2018-09-06 09:50:18 +08:00
|
|
|
|
|
|
|
#define dbg_log_line(lvl, color_n, fmt, ...) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
_DBG_LOG_HDR(lvl, color_n); \
|
|
|
|
rt_kprintf(fmt, ##__VA_ARGS__); \
|
|
|
|
_DBG_LOG_X_END; \
|
|
|
|
} \
|
|
|
|
while (0)
|
2018-05-25 16:06:06 +08:00
|
|
|
|
2018-07-11 10:15:16 +08:00
|
|
|
#define dbg_raw(...) rt_kprintf(__VA_ARGS__);
|
|
|
|
|
2017-10-15 22:30:05 +08:00
|
|
|
#else
|
|
|
|
#define dbg_log(level, fmt, ...)
|
|
|
|
#define dbg_here
|
|
|
|
#define dbg_enter
|
|
|
|
#define dbg_exit
|
2018-09-06 09:50:18 +08:00
|
|
|
#define dbg_log_line(lvl, color_n, fmt, ...)
|
2018-07-11 10:15:16 +08:00
|
|
|
#define dbg_raw(...)
|
2018-09-06 09:50:18 +08:00
|
|
|
#endif /* DBG_ENABLE */
|
|
|
|
|
2018-09-11 18:26:28 +08:00
|
|
|
#if (DBG_LEVEL >= DBG_LOG)
|
2018-09-06 09:50:18 +08:00
|
|
|
#define LOG_D(fmt, ...) dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define LOG_D(...)
|
|
|
|
#endif
|
|
|
|
|
2018-09-11 18:26:28 +08:00
|
|
|
#if (DBG_LEVEL >= DBG_INFO)
|
2018-09-06 09:50:18 +08:00
|
|
|
#define LOG_I(fmt, ...) dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define LOG_I(...)
|
|
|
|
#endif
|
|
|
|
|
2018-09-11 18:26:28 +08:00
|
|
|
#if (DBG_LEVEL >= DBG_WARNING)
|
2018-09-06 09:50:18 +08:00
|
|
|
#define LOG_W(fmt, ...) dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define LOG_W(...)
|
|
|
|
#endif
|
|
|
|
|
2018-09-11 18:26:28 +08:00
|
|
|
#if (DBG_LEVEL >= DBG_ERROR)
|
2018-09-06 09:50:18 +08:00
|
|
|
#define LOG_E(fmt, ...) dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define LOG_E(...)
|
2017-10-15 22:30:05 +08:00
|
|
|
#endif
|
|
|
|
|
2018-07-11 10:15:16 +08:00
|
|
|
#define LOG_RAW(...) dbg_raw(__VA_ARGS__)
|
2017-10-15 22:30:05 +08:00
|
|
|
|
2018-05-25 16:06:06 +08:00
|
|
|
#endif /* RT_DBG_H__ */
|