Wirepas SDK
debug_log.h
Go to the documentation of this file.
1 /* Copyright 2019 Wirepas Ltd. All Rights Reserved.
2  *
3  * See file LICENSE.txt for full license details.
4  *
5  */
6 
13 #include <stdint.h>
14 #include <stdarg.h>
15 #include "api.h"
16 #include "uart_print.h"
17 
58 #ifdef APP_PRINTING
59 #define Print_Log(fmt, ...) UartPrint_printf(fmt, ##__VA_ARGS__)
60 #ifndef DEBUG_LOG_UART_BAUDRATE
61 #define DEBUG_LOG_UART_BAUDRATE 115200
62 #endif
63 #define LOG_INIT() UartPrint_init(DEBUG_LOG_UART_BAUDRATE)
64 #else
65 #define Print_Log(fmt, ...)
66 #define LOG_INIT()
67 #endif
68 
74 #ifndef DEBUG_LOG_MAX_LEVEL
75 /* By default only errors are displayed */
76 #define DEBUG_LOG_MAX_LEVEL LVL_ERROR
77 #endif
78 
82 #define LVL_DEBUG 4
83 #define LVL_INFO 3
84 #define LVL_WARNING 2
85 #define LVL_ERROR 1
86 #define LVL_NOLOG 0
87 
88 /* Do not modify; number in macro name must match the defined log levels
89  * above.
90  */
91 #define LVL_STRING_4 "D"
92 #define LVL_STRING_3 "I"
93 #define LVL_STRING_2 "W"
94 #define LVL_STRING_1 "E"
95 #define LVL_STRING_0 ""
96 
100 #define DEBUG_LVL_TO_STRING(level) LVL_STRING_##level
101 
105 #define FLUSH_DELAY_MS 45
106 
107 
108 #ifndef DEBUG_LOG_CUSTOM
109 # ifndef DEBUG_LOG_MODULE_NAME
110  /* Name of the module must be defined. */
111 # error "No module name set for logger"
112 #endif
113  /* Use "[Module name][Time] Log level: " log prefix by default. */
114 # define DEBUG_LOG_PRINT_MODULE_NAME
115 # define DEBUG_LOG_PRINT_TIME
116 # define DEBUG_LOG_PRINT_LEVEL
117 # undef DEBUG_LOG_PRINT_FUNCTION
118 # undef DEBUG_LOG_PRINT_LINE
119 #endif
120 
121 /* Module name string. */
122 #ifdef DEBUG_LOG_PRINT_MODULE_NAME
123 # define S_MOD_NAME_PREFIX "["DEBUG_LOG_MODULE_NAME"]"
124 #else
125 # define S_MOD_NAME_PREFIX
126 #endif
127 /* Timestamp string. */
128 #ifdef DEBUG_LOG_PRINT_TIME_HP
129 # define S_TIME_PREFIX "[%09u]"
130 # define S_TIME_SUFFIX , lib_time->getTimestampHp()
131 #elif defined(DEBUG_LOG_PRINT_TIME_MS)
132 # define S_TIME_PREFIX "[%09u]"
133 # define S_TIME_SUFFIX , (lib_time->getTimestampCoarse() * 125) >> 4
134 #elif defined(DEBUG_LOG_PRINT_TIME)
135 # define S_TIME_PREFIX "[%09u]"
136 # define S_TIME_SUFFIX , lib_time->getTimestampCoarse()
137 #else
138 # define S_TIME_PREFIX
139 # define S_TIME_SUFFIX
140 #endif
141 /* Log level string. */
142 #ifdef DEBUG_LOG_PRINT_LEVEL
143 # define S_LEVEL_PREFIX(level) " "DEBUG_LVL_TO_STRING(level)": "
144 #else
145 # define S_LEVEL_PREFIX(level)
146 #endif
147 /* Function string. */
148 #ifdef DEBUG_LOG_PRINT_FUNCTION
149 # define S_FUNCTION_PREFIX "func:%s, "
150 # define S_FUNCTION_SUFFIX , __FUNCTION__
151 #else
152 # define S_FUNCTION_PREFIX
153 # define S_FUNCTION_SUFFIX
154 #endif
155 /* Line string. */
156 #ifdef DEBUG_LOG_PRINT_LINE
157 # define S_LINE_PREFIX "line: %03d, "
158 # define S_LINE_SUFFIX , __LINE__
159 #else
160 # define S_LINE_PREFIX
161 # define S_LINE_SUFFIX
162 #endif
163 
164 
175 #define LOG(level, fmt, ...) \
176 { \
177  ((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL) ? \
178  Print_Log( \
179  S_MOD_NAME_PREFIX S_TIME_PREFIX S_LEVEL_PREFIX(level) \
180  S_FUNCTION_PREFIX S_LINE_PREFIX \
181  fmt"\n" S_TIME_SUFFIX S_FUNCTION_SUFFIX S_LINE_SUFFIX \
182  , ##__VA_ARGS__) : \
183  (void)NULL; \
184 }
185 
196 #define LOG_BUFFER(level, buffer, size) \
197 { \
198  if(((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL)) { \
199  for (uint8_t i = 0; i < size; i++) \
200  { \
201  Print_Log("%02X ", buffer[i]); \
202  if ((i & 0xF) == 0xF && i != (uint8_t)(size-1)) \
203  { \
204  Print_Log("\n"); \
205  } \
206  } \
207  Print_Log("\n"); \
208  } \
209 }
210 
216 #define LOG_FLUSH(level) \
217 { \
218  if(((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL)) { \
219  app_lib_time_timestamp_hp_t end; \
220  end = lib_time->addUsToHpTimestamp(lib_time->getTimestampHp(), \
221  FLUSH_DELAY_MS * 1000); \
222  while (lib_time->isHpTimestampBefore(lib_time->getTimestampHp(),end)); \
223  } \
224 }
uart_print.h
api.h