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)
132 # define S_TIME_PREFIX "[%09u]"
133 # define S_TIME_SUFFIX , lib_time->getTimestampCoarse()
134 #else
135 # define S_TIME_PREFIX
136 # define S_TIME_SUFFIX
137 #endif
138 /* Log level string. */
139 #ifdef DEBUG_LOG_PRINT_LEVEL
140 # define S_LEVEL_PREFIX(level) " "DEBUG_LVL_TO_STRING(level)": "
141 #else
142 # define S_LEVEL_PREFIX(level)
143 #endif
144 /* Function string. */
145 #ifdef DEBUG_LOG_PRINT_FUNCTION
146 # define S_FUNCTION_PREFIX "func:%s, "
147 # define S_FUNCTION_SUFFIX , __FUNCTION__
148 #else
149 # define S_FUNCTION_PREFIX
150 # define S_FUNCTION_SUFFIX
151 #endif
152 /* Line string. */
153 #ifdef DEBUG_LOG_PRINT_LINE
154 # define S_LINE_PREFIX "line: %03d, "
155 # define S_LINE_SUFFIX , __LINE__
156 #else
157 # define S_LINE_PREFIX
158 # define S_LINE_SUFFIX
159 #endif
160 
161 
172 #define LOG(level, fmt, ...) \
173 { \
174  ((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL) ? \
175  Print_Log( \
176  S_MOD_NAME_PREFIX S_TIME_PREFIX S_LEVEL_PREFIX(level) \
177  S_FUNCTION_PREFIX S_LINE_PREFIX \
178  fmt"\n" S_TIME_SUFFIX S_FUNCTION_SUFFIX S_LINE_SUFFIX \
179  , ##__VA_ARGS__) : \
180  (void)NULL; \
181 }
182 
193 #define LOG_BUFFER(level, buffer, size) \
194 { \
195  if(((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL)) { \
196  for (uint8_t i = 0; i < size; i++) \
197  { \
198  Print_Log("%02X ", buffer[i]); \
199  if ((i & 0xF) == 0xF && i != (uint8_t)(size-1)) \
200  { \
201  Print_Log("\n"); \
202  } \
203  } \
204  Print_Log("\n"); \
205  } \
206 }
207 
213 #define LOG_FLUSH(level) \
214 { \
215  if(((uint8_t) level) <= ((uint8_t) DEBUG_LOG_MAX_LEVEL)) { \
216  app_lib_time_timestamp_hp_t end; \
217  end = lib_time->addUsToHpTimestamp(lib_time->getTimestampHp(), \
218  FLUSH_DELAY_MS * 1000); \
219  while (lib_time->isHpTimestampBefore(lib_time->getTimestampHp(),end)); \
220  } \
221 }
uart_print.h
api.h