1 /* 2 * Copyright (c) 2006-2022, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2018-08-25 armink the first version 9 */ 10 11 #ifndef _ULOG_DEF_H_ 12 #define _ULOG_DEF_H_ 13 14 #include <rtdef.h> 15 16 #ifdef __cplusplus 17 extern "C" { 18 #endif 19 20 /* logger level, the number is compatible for syslog */ 21 #define LOG_LVL_ASSERT 0 22 #define LOG_LVL_ERROR 3 23 #define LOG_LVL_WARNING 4 24 #define LOG_LVL_INFO 6 25 #define LOG_LVL_DBG 7 26 27 /* the output silent level and all level for filter setting */ 28 #ifndef ULOG_USING_SYSLOG 29 #define LOG_FILTER_LVL_SILENT 0 30 #define LOG_FILTER_LVL_ALL 7 31 #else 32 #define LOG_FILTER_LVL_SILENT 1 33 #define LOG_FILTER_LVL_ALL 255 34 #endif /* ULOG_USING_SYSLOG */ 35 36 /* compatible for rtdbg */ 37 #undef LOG_D 38 #undef LOG_I 39 #undef LOG_W 40 #undef LOG_E 41 #undef LOG_RAW 42 #undef DBG_ERROR 43 #undef DBG_WARNING 44 #undef DBG_INFO 45 #undef DBG_LOG 46 #define DBG_ERROR LOG_LVL_ERROR 47 #define DBG_WARNING LOG_LVL_WARNING 48 #define DBG_INFO LOG_LVL_INFO 49 #define DBG_LOG LOG_LVL_DBG 50 51 #if !defined(LOG_TAG) 52 /* compatible for rtdbg */ 53 #if defined(DBG_TAG) 54 #define LOG_TAG DBG_TAG 55 #elif defined(DBG_SECTION_NAME) 56 #define LOG_TAG DBG_SECTION_NAME 57 #else 58 #define LOG_TAG "NO_TAG" 59 #endif 60 #endif /* !defined(LOG_TAG) */ 61 62 #if !defined(LOG_LVL) 63 /* compatible for rtdbg */ 64 #if defined(DBG_LVL) 65 #define LOG_LVL DBG_LVL 66 #elif defined(DBG_LEVEL) 67 #define LOG_LVL DBG_LEVEL 68 #else 69 #define LOG_LVL LOG_LVL_DBG 70 #endif 71 #endif /* !defined(LOG_LVL) */ 72 73 #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) 74 #define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__) 75 #else 76 #define ulog_d(TAG, ...) 77 #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */ 78 79 #if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) 80 #define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__) 81 #else 82 #define ulog_i(TAG, ...) 83 #endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */ 84 85 #if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) 86 #define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__) 87 #else 88 #define ulog_w(TAG, ...) 89 #endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */ 90 91 #if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) 92 #define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__) 93 #else 94 #define ulog_e(TAG, ...) 95 #endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */ 96 97 #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) 98 #define ulog_hex(TAG, width, buf, size) ulog_hexdump(TAG, width, buf, size) 99 #else 100 #define ulog_hex(TAG, width, buf, size) 101 #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */ 102 103 /* assert for developer. */ 104 #ifdef ULOG_ASSERT_ENABLE 105 #define ULOG_ASSERT(EXPR) \ 106 if (!(EXPR)) \ 107 { \ 108 ulog_output(LOG_LVL_ASSERT, LOG_TAG, RT_TRUE, "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \ 109 ulog_flush(); \ 110 while (1); \ 111 } 112 #else 113 #define ULOG_ASSERT(EXPR) 114 #endif 115 116 /* ASSERT API definition */ 117 #if !defined(ASSERT) 118 #define ASSERT ULOG_ASSERT 119 #endif 120 121 /* compatible for elog */ 122 #undef assert 123 #undef log_e 124 #undef log_w 125 #undef log_i 126 #undef log_d 127 #undef log_v 128 #undef ELOG_LVL_ASSERT 129 #undef ELOG_LVL_ERROR 130 #undef ELOG_LVL_WARN 131 #undef ELOG_LVL_INFO 132 #undef ELOG_LVL_DEBUG 133 #undef ELOG_LVL_VERBOSE 134 #define assert ASSERT 135 #define log_e LOG_E 136 #define log_w LOG_W 137 #define log_i LOG_I 138 #define log_d LOG_D 139 #define log_v LOG_D 140 #define log_raw LOG_RAW 141 #define log_hex LOG_HEX 142 #define ELOG_LVL_ASSERT LOG_LVL_ASSERT 143 #define ELOG_LVL_ERROR LOG_LVL_ERROR 144 #define ELOG_LVL_WARN LOG_LVL_WARNING 145 #define ELOG_LVL_INFO LOG_LVL_INFO 146 #define ELOG_LVL_DEBUG LOG_LVL_DBG 147 #define ELOG_LVL_VERBOSE LOG_LVL_DBG 148 149 /* setting static output log level */ 150 #ifndef ULOG_OUTPUT_LVL 151 #define ULOG_OUTPUT_LVL LOG_LVL_DBG 152 #endif 153 154 /* buffer size for every line's log */ 155 #ifndef ULOG_LINE_BUF_SIZE 156 #define ULOG_LINE_BUF_SIZE 128 157 #endif 158 159 /* output filter's tag max length */ 160 #ifndef ULOG_FILTER_TAG_MAX_LEN 161 #define ULOG_FILTER_TAG_MAX_LEN 23 162 #endif 163 164 /* output filter's keyword max length */ 165 #ifndef ULOG_FILTER_KW_MAX_LEN 166 #define ULOG_FILTER_KW_MAX_LEN 15 167 #endif 168 169 #ifndef ULOG_NEWLINE_SIGN 170 #define ULOG_NEWLINE_SIGN "\r\n" 171 #endif 172 173 #define ULOG_FRAME_MAGIC 0x10 174 175 /* tag's level filter */ 176 struct ulog_tag_lvl_filter 177 { 178 char tag[ULOG_FILTER_TAG_MAX_LEN + 1]; 179 rt_uint32_t level; 180 rt_slist_t list; 181 }; 182 typedef struct ulog_tag_lvl_filter *ulog_tag_lvl_filter_t; 183 184 struct ulog_frame 185 { 186 /* magic word is 0x10 ('lo') */ 187 rt_uint32_t magic:8; 188 rt_uint32_t is_raw:1; 189 rt_uint32_t log_len:23; 190 rt_uint32_t level; 191 const char *log; 192 const char *tag; 193 }; 194 typedef struct ulog_frame *ulog_frame_t; 195 196 struct ulog_backend 197 { 198 char name[RT_NAME_MAX]; 199 rt_bool_t support_color; 200 rt_uint32_t out_level; 201 void (*init) (struct ulog_backend *backend); 202 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); 203 void (*flush) (struct ulog_backend *backend); 204 void (*deinit)(struct ulog_backend *backend); 205 /* The filter will be call before output. It will return TRUE when the filter condition is math. */ 206 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); 207 rt_slist_t list; 208 }; 209 typedef struct ulog_backend *ulog_backend_t; 210 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); 211 212 #ifdef __cplusplus 213 } 214 #endif 215 216 #endif /* _ULOG_DEF_H_ */ 217