未验证 提交 5aeb8dce 编写于 作者: B Bernard Xiong 提交者: GitHub

Merge pull request #1938 from armink/add_ulog

Add ulog compatible with rtdbg.
...@@ -95,7 +95,7 @@ void vsyslog(int priority, const char *format, va_list args) ...@@ -95,7 +95,7 @@ void vsyslog(int priority, const char *format, va_list args)
priority |= local_facility; priority |= local_facility;
} }
ulog_voutput(priority, local_ident, format, args); ulog_voutput(priority, local_ident, RT_TRUE, format, args);
} }
/** /**
...@@ -169,7 +169,7 @@ static const char *get_month_str(uint8_t month) ...@@ -169,7 +169,7 @@ static const char *get_month_str(uint8_t month)
} }
} }
RT_WEAK rt_size_t syslog_formater(char *log_buf, int level, const char *tag, const char *format, va_list args) RT_WEAK rt_size_t syslog_formater(char *log_buf, int level, const char *tag, rt_bool_t newline, const char *format, va_list args)
{ {
extern size_t ulog_strcpy(size_t cur_len, char *dst, const char *src); extern size_t ulog_strcpy(size_t cur_len, char *dst, const char *src);
...@@ -252,7 +252,10 @@ RT_WEAK rt_size_t syslog_formater(char *log_buf, int level, const char *tag, con ...@@ -252,7 +252,10 @@ RT_WEAK rt_size_t syslog_formater(char *log_buf, int level, const char *tag, con
} }
/* package newline sign */ /* package newline sign */
if (newline)
{
log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN); log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN);
}
return log_len; return log_len;
} }
......
...@@ -218,7 +218,8 @@ static char *get_log_buf(void) ...@@ -218,7 +218,8 @@ static char *get_log_buf(void)
} }
} }
RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *tag, const char *format, va_list args) RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *tag, rt_bool_t newline,
const char *format, va_list args)
{ {
rt_size_t log_len = 0, newline_len = rt_strlen(ULOG_NEWLINE_SIGN); rt_size_t log_len = 0, newline_len = rt_strlen(ULOG_NEWLINE_SIGN);
int fmt_result; int fmt_result;
...@@ -340,7 +341,10 @@ RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta ...@@ -340,7 +341,10 @@ RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta
} }
/* package newline sign */ /* package newline sign */
if (newline)
{
log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN); log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN);
}
#ifdef ULOG_USING_COLOR #ifdef ULOG_USING_COLOR
/* add CSI end sign */ /* add CSI end sign */
...@@ -449,10 +453,11 @@ static void do_output(rt_uint32_t level, const char *tag, rt_bool_t is_raw, cons ...@@ -449,10 +453,11 @@ static void do_output(rt_uint32_t level, const char *tag, rt_bool_t is_raw, cons
* *
* @param level level * @param level level
* @param tag tag * @param tag tag
* @param newline has_newline
* @param format output format * @param format output format
* @param args variable argument list * @param args variable argument list
*/ */
void ulog_voutput(rt_uint32_t level, const char *tag, const char *format, va_list args) void ulog_voutput(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, va_list args)
{ {
char *log_buf = NULL; char *log_buf = NULL;
rt_size_t log_len = 0; rt_size_t log_len = 0;
...@@ -499,10 +504,10 @@ void ulog_voutput(rt_uint32_t level, const char *tag, const char *format, va_lis ...@@ -499,10 +504,10 @@ void ulog_voutput(rt_uint32_t level, const char *tag, const char *format, va_lis
output_lock(); output_lock();
#ifndef ULOG_USING_SYSLOG #ifndef ULOG_USING_SYSLOG
log_len = ulog_formater(log_buf, level, tag, format, args); log_len = ulog_formater(log_buf, level, tag, newline, format, args);
#else #else
extern rt_size_t syslog_formater(char *log_buf, rt_uint8_t level, const char *tag, const char *format, va_list args); extern rt_size_t syslog_formater(char *log_buf, rt_uint8_t level, const char *tag, rt_bool_t newline, const char *format, va_list args);
log_len = syslog_formater(log_buf, level, tag, format, args); log_len = syslog_formater(log_buf, level, tag, newline, format, args);
#endif /* ULOG_USING_SYSLOG */ #endif /* ULOG_USING_SYSLOG */
#ifdef ULOG_USING_FILTER #ifdef ULOG_USING_FILTER
...@@ -532,17 +537,18 @@ void ulog_voutput(rt_uint32_t level, const char *tag, const char *format, va_lis ...@@ -532,17 +537,18 @@ void ulog_voutput(rt_uint32_t level, const char *tag, const char *format, va_lis
* *
* @param level level * @param level level
* @param tag tag * @param tag tag
* @param newline has newline
* @param format output format * @param format output format
* @param ... args * @param ... args
*/ */
void ulog_output(rt_uint32_t level, const char *tag, const char *format, ...) void ulog_output(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, ...)
{ {
va_list args; va_list args;
/* args point to the first variable parameter */ /* args point to the first variable parameter */
va_start(args, format); va_start(args, format);
ulog_voutput(level, tag, format, args); ulog_voutput(level, tag, newline, format, args);
va_end(args); va_end(args);
} }
......
...@@ -84,8 +84,8 @@ void ulog_hexdump(const char *name, rt_size_t width, rt_uint8_t *buf, rt_size_t ...@@ -84,8 +84,8 @@ void ulog_hexdump(const char *name, rt_size_t width, rt_uint8_t *buf, rt_size_t
/* /*
* Another log output API. This API is difficult to use than LOG_X API. * Another log output API. This API is difficult to use than LOG_X API.
*/ */
void ulog_voutput(rt_uint32_t level, const char *tag, const char *format, va_list args); void ulog_voutput(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, va_list args);
void ulog_output(rt_uint32_t level, const char *tag, const char *format, ...); void ulog_output(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, ...);
void ulog_raw(const char *format, ...); void ulog_raw(const char *format, ...);
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -40,10 +40,16 @@ extern "C" { ...@@ -40,10 +40,16 @@ extern "C" {
#undef DBG_WARNING #undef DBG_WARNING
#undef DBG_INFO #undef DBG_INFO
#undef DBG_LOG #undef DBG_LOG
#undef dbg_log
#define DBG_ERROR LOG_LVL_ERROR #define DBG_ERROR LOG_LVL_ERROR
#define DBG_WARNING LOG_LVL_WARNING #define DBG_WARNING LOG_LVL_WARNING
#define DBG_INFO LOG_LVL_INFO #define DBG_INFO LOG_LVL_INFO
#define DBG_LOG LOG_LVL_DBG #define DBG_LOG LOG_LVL_DBG
#define dbg_log(level, ...) \
if ((level) <= DBG_LEVEL) \
{ \
ulog_output(level, LOG_TAG, RT_FALSE, __VA_ARGS__);\
}
#if !defined(LOG_TAG) #if !defined(LOG_TAG)
/* compatible for rtdbg */ /* compatible for rtdbg */
...@@ -64,25 +70,25 @@ extern "C" { ...@@ -64,25 +70,25 @@ extern "C" {
#endif /* !defined(LOG_LVL) */ #endif /* !defined(LOG_LVL) */
#if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
#define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, __VA_ARGS__) #define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__)
#else #else
#define ulog_d(TAG, ...) #define ulog_d(TAG, ...)
#endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */ #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
#if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) #if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO)
#define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, __VA_ARGS__) #define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__)
#else #else
#define ulog_i(TAG, ...) #define ulog_i(TAG, ...)
#endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */ #endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */
#if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) #if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING)
#define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, __VA_ARGS__) #define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__)
#else #else
#define ulog_w(TAG, ...) #define ulog_w(TAG, ...)
#endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */ #endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */
#if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) #if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR)
#define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, __VA_ARGS__) #define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__)
#else #else
#define ulog_e(TAG, ...) #define ulog_e(TAG, ...)
#endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */ #endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */
...@@ -92,7 +98,7 @@ extern "C" { ...@@ -92,7 +98,7 @@ extern "C" {
#define ULOG_ASSERT(EXPR) \ #define ULOG_ASSERT(EXPR) \
if (!(EXPR)) \ if (!(EXPR)) \
{ \ { \
ulog_output(LOG_LVL_ASSERT, LOG_TAG, "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \ ulog_output(LOG_LVL_ASSERT, LOG_TAG, RT_TRUE, "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \
ulog_flush(); \ ulog_flush(); \
while (1); \ while (1); \
} }
......
...@@ -23,10 +23,7 @@ ...@@ -23,10 +23,7 @@
* #define DBG_LEVEL DBG_INFO * #define DBG_LEVEL DBG_INFO
* #include <rtdbg.h> // must after of DEBUG_ENABLE or some other options * #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: * Then in your C/C++ file, you can use LOG_X 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_D("this is a debug log!");
* LOG_E("this is a error log!"); * LOG_E("this is a error log!");
* *
...@@ -39,6 +36,11 @@ ...@@ -39,6 +36,11 @@
#include <rtconfig.h> #include <rtconfig.h>
#if defined(RT_USING_ULOG) && defined(DBG_ENABLE)
/* using ulog compatible with rtdbg */
#include <ulog.h>
#else
/* DEBUG level */ /* DEBUG level */
#define DBG_ERROR 0 #define DBG_ERROR 0
#define DBG_WARNING 1 #define DBG_WARNING 1
...@@ -82,6 +84,8 @@ ...@@ -82,6 +84,8 @@
/* /*
* static debug routine * static debug routine
* NOTE: This is a NOT RECOMMENDED API. Please using LOG_X API.
* It will be DISCARDED later. Because it will take up more resources.
*/ */
#define dbg_log(level, fmt, ...) \ #define dbg_log(level, fmt, ...) \
if ((level) <= DBG_LEVEL) \ if ((level) <= DBG_LEVEL) \
...@@ -167,4 +171,6 @@ ...@@ -167,4 +171,6 @@
#define LOG_RAW(...) dbg_raw(__VA_ARGS__) #define LOG_RAW(...) dbg_raw(__VA_ARGS__)
#endif /* defined(RT_USING_ULOG) && define(DBG_ENABLE) */
#endif /* RT_DBG_H__ */ #endif /* RT_DBG_H__ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册