2018-10-30 09:00:30 +08:00
|
|
|
/*
|
2022-03-21 14:06:17 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2018-10-30 09:00:30 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2018-08-25 armink the first version
|
|
|
|
*/
|
2018-11-01 14:51:50 +08:00
|
|
|
|
2018-10-30 09:00:30 +08:00
|
|
|
#ifndef _ULOG_DEF_H_
|
|
|
|
#define _ULOG_DEF_H_
|
|
|
|
|
2021-10-08 10:03:01 +08:00
|
|
|
#include <rtdef.h>
|
|
|
|
|
2018-10-30 09:00:30 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* logger level, the number is compatible for syslog */
|
|
|
|
#define LOG_LVL_ASSERT 0
|
|
|
|
#define LOG_LVL_ERROR 3
|
|
|
|
#define LOG_LVL_WARNING 4
|
|
|
|
#define LOG_LVL_INFO 6
|
|
|
|
#define LOG_LVL_DBG 7
|
|
|
|
|
|
|
|
/* the output silent level and all level for filter setting */
|
|
|
|
#ifndef ULOG_USING_SYSLOG
|
|
|
|
#define LOG_FILTER_LVL_SILENT 0
|
|
|
|
#define LOG_FILTER_LVL_ALL 7
|
|
|
|
#else
|
|
|
|
#define LOG_FILTER_LVL_SILENT 1
|
|
|
|
#define LOG_FILTER_LVL_ALL 255
|
|
|
|
#endif /* ULOG_USING_SYSLOG */
|
|
|
|
|
|
|
|
/* compatible for rtdbg */
|
|
|
|
#undef LOG_D
|
|
|
|
#undef LOG_I
|
|
|
|
#undef LOG_W
|
|
|
|
#undef LOG_E
|
|
|
|
#undef LOG_RAW
|
|
|
|
#undef DBG_ERROR
|
|
|
|
#undef DBG_WARNING
|
|
|
|
#undef DBG_INFO
|
|
|
|
#undef DBG_LOG
|
2018-10-31 10:54:12 +08:00
|
|
|
#undef dbg_log
|
2018-10-30 09:00:30 +08:00
|
|
|
#define DBG_ERROR LOG_LVL_ERROR
|
|
|
|
#define DBG_WARNING LOG_LVL_WARNING
|
|
|
|
#define DBG_INFO LOG_LVL_INFO
|
|
|
|
#define DBG_LOG LOG_LVL_DBG
|
2018-10-31 10:54:12 +08:00
|
|
|
#define dbg_log(level, ...) \
|
2019-03-06 17:07:15 +08:00
|
|
|
if ((level) <= LOG_LVL) \
|
2018-10-31 10:54:12 +08:00
|
|
|
{ \
|
|
|
|
ulog_output(level, LOG_TAG, RT_FALSE, __VA_ARGS__);\
|
|
|
|
}
|
2018-10-30 09:00:30 +08:00
|
|
|
|
|
|
|
#if !defined(LOG_TAG)
|
|
|
|
/* compatible for rtdbg */
|
2019-04-12 10:18:57 +08:00
|
|
|
#if defined(DBG_TAG)
|
|
|
|
#define LOG_TAG DBG_TAG
|
|
|
|
#elif defined(DBG_SECTION_NAME)
|
2018-10-30 09:00:30 +08:00
|
|
|
#define LOG_TAG DBG_SECTION_NAME
|
|
|
|
#else
|
|
|
|
#define LOG_TAG "NO_TAG"
|
|
|
|
#endif
|
|
|
|
#endif /* !defined(LOG_TAG) */
|
|
|
|
|
|
|
|
#if !defined(LOG_LVL)
|
|
|
|
/* compatible for rtdbg */
|
2019-04-12 10:18:57 +08:00
|
|
|
#if defined(DBG_LVL)
|
|
|
|
#define LOG_LVL DBG_LVL
|
|
|
|
#elif defined(DBG_LEVEL)
|
2018-11-01 14:51:50 +08:00
|
|
|
#define LOG_LVL DBG_LEVEL
|
2018-10-30 09:00:30 +08:00
|
|
|
#else
|
2018-11-01 14:51:50 +08:00
|
|
|
#define LOG_LVL LOG_LVL_DBG
|
2018-10-30 09:00:30 +08:00
|
|
|
#endif
|
|
|
|
#endif /* !defined(LOG_LVL) */
|
|
|
|
|
|
|
|
#if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
|
2018-10-31 10:54:12 +08:00
|
|
|
#define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__)
|
2018-10-30 09:00:30 +08:00
|
|
|
#else
|
|
|
|
#define ulog_d(TAG, ...)
|
|
|
|
#endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
|
|
|
|
|
|
|
|
#if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO)
|
2018-10-31 10:54:12 +08:00
|
|
|
#define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__)
|
2018-10-30 09:00:30 +08:00
|
|
|
#else
|
|
|
|
#define ulog_i(TAG, ...)
|
|
|
|
#endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */
|
|
|
|
|
|
|
|
#if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING)
|
2018-10-31 10:54:12 +08:00
|
|
|
#define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__)
|
2018-10-30 09:00:30 +08:00
|
|
|
#else
|
|
|
|
#define ulog_w(TAG, ...)
|
|
|
|
#endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */
|
|
|
|
|
|
|
|
#if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR)
|
2018-10-31 10:54:12 +08:00
|
|
|
#define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__)
|
2018-10-30 09:00:30 +08:00
|
|
|
#else
|
|
|
|
#define ulog_e(TAG, ...)
|
|
|
|
#endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */
|
|
|
|
|
2018-12-04 09:09:58 +08:00
|
|
|
#if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
|
|
|
|
#define ulog_hex(TAG, width, buf, size) ulog_hexdump(TAG, width, buf, size)
|
|
|
|
#else
|
|
|
|
#define ulog_hex(TAG, width, buf, size)
|
2021-03-08 18:19:04 +08:00
|
|
|
#endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
|
|
|
|
|
2018-10-30 09:00:30 +08:00
|
|
|
/* assert for developer. */
|
|
|
|
#ifdef ULOG_ASSERT_ENABLE
|
|
|
|
#define ULOG_ASSERT(EXPR) \
|
|
|
|
if (!(EXPR)) \
|
|
|
|
{ \
|
2018-10-31 10:54:12 +08:00
|
|
|
ulog_output(LOG_LVL_ASSERT, LOG_TAG, RT_TRUE, "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \
|
2018-10-30 09:00:30 +08:00
|
|
|
ulog_flush(); \
|
|
|
|
while (1); \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define ULOG_ASSERT(EXPR)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ASSERT API definition */
|
|
|
|
#if !defined(ASSERT)
|
|
|
|
#define ASSERT ULOG_ASSERT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* compatible for elog */
|
|
|
|
#undef assert
|
|
|
|
#undef log_e
|
|
|
|
#undef log_w
|
|
|
|
#undef log_i
|
|
|
|
#undef log_d
|
|
|
|
#undef log_v
|
|
|
|
#undef ELOG_LVL_ASSERT
|
|
|
|
#undef ELOG_LVL_ERROR
|
|
|
|
#undef ELOG_LVL_WARN
|
|
|
|
#undef ELOG_LVL_INFO
|
|
|
|
#undef ELOG_LVL_DEBUG
|
|
|
|
#undef ELOG_LVL_VERBOSE
|
|
|
|
#define assert ASSERT
|
|
|
|
#define log_e LOG_E
|
|
|
|
#define log_w LOG_W
|
|
|
|
#define log_i LOG_I
|
|
|
|
#define log_d LOG_D
|
|
|
|
#define log_v LOG_D
|
|
|
|
#define log_raw LOG_RAW
|
2021-03-08 18:19:04 +08:00
|
|
|
#define log_hex LOG_HEX
|
2018-10-30 09:00:30 +08:00
|
|
|
#define ELOG_LVL_ASSERT LOG_LVL_ASSERT
|
|
|
|
#define ELOG_LVL_ERROR LOG_LVL_ERROR
|
|
|
|
#define ELOG_LVL_WARN LOG_LVL_WARNING
|
|
|
|
#define ELOG_LVL_INFO LOG_LVL_INFO
|
|
|
|
#define ELOG_LVL_DEBUG LOG_LVL_DBG
|
|
|
|
#define ELOG_LVL_VERBOSE LOG_LVL_DBG
|
|
|
|
|
|
|
|
/* setting static output log level */
|
|
|
|
#ifndef ULOG_OUTPUT_LVL
|
|
|
|
#define ULOG_OUTPUT_LVL LOG_LVL_DBG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* buffer size for every line's log */
|
|
|
|
#ifndef ULOG_LINE_BUF_SIZE
|
|
|
|
#define ULOG_LINE_BUF_SIZE 128
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* output filter's tag max length */
|
|
|
|
#ifndef ULOG_FILTER_TAG_MAX_LEN
|
|
|
|
#define ULOG_FILTER_TAG_MAX_LEN 23
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* output filter's keyword max length */
|
|
|
|
#ifndef ULOG_FILTER_KW_MAX_LEN
|
|
|
|
#define ULOG_FILTER_KW_MAX_LEN 15
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ULOG_NEWLINE_SIGN
|
|
|
|
#define ULOG_NEWLINE_SIGN "\r\n"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ULOG_FRAME_MAGIC 0x10
|
|
|
|
|
2018-11-08 11:09:35 +08:00
|
|
|
/* tag's level filter */
|
|
|
|
struct ulog_tag_lvl_filter
|
|
|
|
{
|
|
|
|
char tag[ULOG_FILTER_TAG_MAX_LEN + 1];
|
|
|
|
rt_uint32_t level;
|
|
|
|
rt_slist_t list;
|
|
|
|
};
|
|
|
|
typedef struct ulog_tag_lvl_filter *ulog_tag_lvl_filter_t;
|
|
|
|
|
2018-10-30 09:00:30 +08:00
|
|
|
struct ulog_frame
|
|
|
|
{
|
|
|
|
/* magic word is 0x10 ('lo') */
|
|
|
|
rt_uint32_t magic:8;
|
|
|
|
rt_uint32_t is_raw:1;
|
|
|
|
rt_uint32_t log_len:23;
|
|
|
|
rt_uint32_t level;
|
|
|
|
const char *log;
|
|
|
|
const char *tag;
|
|
|
|
};
|
|
|
|
typedef struct ulog_frame *ulog_frame_t;
|
|
|
|
|
|
|
|
struct ulog_backend
|
|
|
|
{
|
|
|
|
char name[RT_NAME_MAX];
|
|
|
|
rt_bool_t support_color;
|
2021-01-20 11:20:30 +08:00
|
|
|
rt_uint32_t out_level;
|
2018-10-30 09:00:30 +08:00
|
|
|
void (*init) (struct ulog_backend *backend);
|
2022-03-21 14:06:17 +08:00
|
|
|
void (*output)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, rt_size_t len);
|
2018-10-30 09:00:30 +08:00
|
|
|
void (*flush) (struct ulog_backend *backend);
|
|
|
|
void (*deinit)(struct ulog_backend *backend);
|
2021-12-03 12:04:20 +08:00
|
|
|
/* The filter will be call before output. It will return TRUE when the filter condition is math. */
|
2022-03-21 14:06:17 +08:00
|
|
|
rt_bool_t (*filter)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, rt_size_t len);
|
2018-10-30 09:00:30 +08:00
|
|
|
rt_slist_t list;
|
|
|
|
};
|
|
|
|
typedef struct ulog_backend *ulog_backend_t;
|
2022-03-21 14:06:17 +08:00
|
|
|
typedef rt_bool_t (*ulog_backend_filter_t)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, rt_size_t len);
|
2018-10-30 09:00:30 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* _ULOG_DEF_H_ */
|