• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include <os/os.h>
22 #include <components/system.h>
23 #if CONFIG_STDIO_PRINTF
24 #include <stdio.h>
25 #endif
26 
27 #define BK_LOG_NONE    0 /*!< No log output */
28 #define BK_LOG_ERROR   1 /*!< Critical errors, software module can not recover on its own */
29 #define BK_LOG_WARN    2 /*!< Error conditions from which recovery measures have been taken */
30 #define BK_LOG_INFO    3 /*!< Information messages which describe normal flow of events */
31 #define BK_LOG_DEBUG   4 /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
32 #define BK_LOG_VERBOSE 5 /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
33 
34 #if CONFIG_STDIO_PRINTF
35 #define _OS_PRINTF printf
36 #else
37 #define _OS_PRINTF os_printf
38 #endif
39 
40 #if CFG_LEGACY_LOG
41 
42 #define BK_LOGE( tag, format, ... ) _OS_PRINTF(format, ##__VA_ARGS__)
43 #define BK_LOGW( tag, format, ... ) _OS_PRINTF(format, ##__VA_ARGS__)
44 #define BK_LOGI( tag, format, ... ) _OS_PRINTF(format, ##__VA_ARGS__)
45 #define BK_LOGD( tag, format, ... ) _OS_PRINTF(format, ##__VA_ARGS__)
46 #define BK_LOGV( tag, format, ... ) _OS_PRINTF(format, ##__VA_ARGS__)
47 
48 #else
49 
50 #if CFG_LOG_COLORS
51 #define LOG_COLOR_BLACK   "30"
52 #define LOG_COLOR_RED     "31"
53 #define LOG_COLOR_GREEN   "32"
54 #define LOG_COLOR_BROWN   "33"
55 #define LOG_COLOR_BLUE    "34"
56 #define LOG_COLOR_PURPLE  "35"
57 #define LOG_COLOR_CYAN    "36"
58 #define LOG_COLOR(COLOR)  "\033[0;" COLOR "m"
59 #define LOG_BOLD(COLOR)   "\033[1;" COLOR "m"
60 #define LOG_RESET_COLOR   "\033[0m"
61 #define LOG_COLOR_E       LOG_COLOR(LOG_COLOR_RED)
62 #define LOG_COLOR_W       LOG_COLOR(LOG_COLOR_BROWN)
63 #define LOG_COLOR_I       LOG_COLOR(LOG_COLOR_GREEN)
64 #define LOG_COLOR_D
65 #define LOG_COLOR_V
66 #else
67 #define LOG_COLOR_E
68 #define LOG_COLOR_W
69 #define LOG_COLOR_I
70 #define LOG_COLOR_D
71 #define LOG_COLOR_V
72 #define LOG_RESET_COLOR
73 #endif //CFG_LOG_COLORS
74 
75 #ifdef CFG_LOG_LEVEL
76 #define LOG_LEVEL         CFG_LOG_LEVEL
77 #else
78 #define LOG_LEVEL         BK_LOG_INFO
79 #endif
80 
81 #define BK_LOG_FORMAT(letter, format)  LOG_COLOR_ ## letter #letter " (%d) %s: " format  LOG_RESET_COLOR
82 
83 #if (LOG_LEVEL >= BK_LOG_ERROR)
84 #define BK_LOGE( tag, format, ... ) _OS_PRINTF(BK_LOG_FORMAT(E, format), rtos_get_time(), tag, ##__VA_ARGS__)
85 #else
86 #define BK_LOGE( tag, format, ... )
87 #endif
88 
89 #if (LOG_LEVEL >= BK_LOG_WARN)
90 #define BK_LOGW( tag, format, ... ) _OS_PRINTF(BK_LOG_FORMAT(W, format), rtos_get_time(), tag, ##__VA_ARGS__)
91 #else
92 #define BK_LOGW( tag, format, ... )
93 #endif
94 
95 #if (LOG_LEVEL >= BK_LOG_INFO)
96 #define BK_LOGI( tag, format, ... ) _OS_PRINTF(BK_LOG_FORMAT(I, format), rtos_get_time(), tag, ##__VA_ARGS__)
97 #else
98 #define BK_LOGI( tag, format, ... )
99 #endif
100 
101 #if (LOG_LEVEL >= BK_LOG_DEBUG)
102 #define BK_LOGD( tag, format, ... ) _OS_PRINTF(BK_LOG_FORMAT(D, format), rtos_get_time(), tag, ##__VA_ARGS__)
103 #else
104 #define BK_LOGD( tag, format, ... )
105 #endif
106 
107 #if (LOG_LEVEL >= BK_LOG_VERBOSE)
108 #define BK_LOGV( tag, format, ... ) _OS_PRINTF(BK_LOG_FORMAT(V, format), rtos_get_time(), tag, ##__VA_ARGS__)
109 #else
110 #define BK_LOGV( tag, format, ... )
111 #endif
112 
113 #endif // CFG_LEGACY_LOG
114 
115 #define BK_LOG_RAW(format, ...) _OS_PRINTF(format, ##__VA_ARGS__)
116 
117 #define BK_IP4_FORMAT "%d.%d.%d.%d"
118 #define BK_IP4_STR(_ip) ((_ip) & 0xFF), (((_ip) >> 8) & 0xFF), (((_ip) >> 16) & 0xFF), (((_ip) >> 24) & 0xFF)
119 #define BK_MAC_FORMAT "%02x:%02x:%02x:%02x:%02x:%02x"
120 #define BK_MAC_STR(_m) (_m)[0], (_m)[1], (_m)[2], (_m)[3], (_m)[4], (_m)[5]
121 #define BK_MAC_STR_INVERT(_m) (_m)[5], (_m)[4], (_m)[3], (_m)[2], (_m)[1], (_m)[0]
122 
123 void bk_mem_dump(const char* titile, uint32_t start, uint32_t len);
124 #define BK_MEM_DUMP(_title, _start, _len) bk_mem_dump((_title), (_start), (_len))
125 
126 #if CONFIG_SHELL_ASYNCLOG
127 
128 #include "components/shell_task.h"
129 
130 #undef BK_LOG_FORMAT
131 #undef BK_LOGE
132 #undef BK_LOGW
133 #undef BK_LOGI
134 #undef BK_LOGD
135 #undef BK_LOGV
136 
137 #define BK_LOG_FORMAT(letter, format)   #letter "(%d):" format
138 
139 #define BK_LOGE( tag, format, ... ) bk_printf_ex(BK_LOG_ERROR, tag, tag ":" BK_LOG_FORMAT(E, format), rtos_get_time(),  ##__VA_ARGS__)
140 #define BK_LOGW( tag, format, ... ) bk_printf_ex(BK_LOG_WARN, tag, tag ":" BK_LOG_FORMAT(W, format), rtos_get_time(),  ##__VA_ARGS__)
141 #define BK_LOGI( tag, format, ... ) bk_printf_ex(BK_LOG_INFO, tag, tag ":" BK_LOG_FORMAT(I, format), rtos_get_time(),  ##__VA_ARGS__)
142 #define BK_LOGD( tag, format, ... ) bk_printf_ex(BK_LOG_DEBUG, tag, tag ":" BK_LOG_FORMAT(D, format), rtos_get_time(),  ##__VA_ARGS__)
143 #define BK_LOGV( tag, format, ... ) bk_printf_ex(BK_LOG_VERBOSE, tag, tag ":" BK_LOG_FORMAT(V, format), rtos_get_time(), ##__VA_ARGS__)
144 
145 #define BK_SPY( spy_id, byte_array, byte_cnt )		shell_spy_out(spy_id, byte_array, byte_cnt)
146 
147 #define BK_TRACE(trace_id, ... )	shell_trace_out(trace_id, ##__VA_ARGS__)
148 
149 #define BK_LOG_FLUSH()				shell_log_flush()
150 
151 #else
152 
153 #define BK_SPY( spy_id, byte_array, byte_cnt )
154 #define BK_TRACE(trace_id, ... )
155 
156 #define BK_LOG_FLUSH()
157 
158 #endif
159 
160 #ifdef __cplusplus
161 }
162 #endif
163 
164