Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_LogUtils.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Sep 1, 2022
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7
8#pragma once
9
13constexpr uint32_t fileNameHash(const char* str, size_t strLen) {
14 uint32_t hash = 5381;
15 for (int32_t i = strLen - 1; i >= 0; --i) {
16 if (str[i] == '/') {
17 return hash;
18 }
19 hash = ((hash << 5) + hash) + str[i]; // hash * 33 + c
20 }
21 return hash;
22}
23
24void cs_log_start(size_t msgSize, uart_msg_log_header_t& header);
25void cs_log_arg(const uint8_t* const valPtr, size_t valSize);
27
29 uint32_t fileNameHash,
30 uint32_t lineNumber,
31 uint8_t logLevel,
32 bool addNewLine,
33 bool reverse,
34 const uint8_t* const ptr,
35 size_t size,
36 ElementType elementType,
37 size_t elementSize);
38
39template <typename T>
40void cs_log_add_arg_size(size_t& size, uint8_t& numArgs, T val) {
41 size += sizeof(uart_msg_log_arg_header_t) + sizeof(T);
42 ++numArgs;
43}
44
45template <>
46void cs_log_add_arg_size(size_t& size, uint8_t& numArgs, char* str);
47
48template <>
49void cs_log_add_arg_size(size_t& size, uint8_t& numArgs, const char* str);
50
51template <typename T>
52void cs_log_arg(T val) {
53 const uint8_t* const valPtr = reinterpret_cast<const uint8_t* const>(&val);
54 cs_log_arg(valPtr, sizeof(T));
55}
56
57template <>
58void cs_log_arg(char* str);
59
60template <>
61void cs_log_arg(const char* str);
62
63// Uses the fold expression, a handy way to replace a recursive call.
64template <class... Args>
66 uint32_t fileNameHash, uint32_t lineNumber, uint8_t logLevel, bool addNewLine, const Args&... args) {
69 header.header.lineNumber = lineNumber;
70 header.header.logLevel = logLevel;
71 header.header.flags.newLine = addNewLine;
72 header.numArgs = 0;
73
74 // Get number of arguments and size of all arguments.
75 size_t totalSize = sizeof(header);
76 (cs_log_add_arg_size(totalSize, header.numArgs, args), ...);
77
78 // Write the header.
79 cs_log_start(totalSize, header);
80
81 // Write each argument.
82 (cs_log_arg(args), ...);
83
84 // Finalize the uart msg.
85 cs_log_end();
86}
87
88// This function takes an unused argument "fmt".
89// This fmt ends up in the .ii file (preprocessed .cpp file), and is then used to gather all log strings.
90// To make it easier for the compiler to optimize out the fmt string, we only call cs_log_args without the fmt arg here.
91template <class... Args>
93 uint32_t fileNameHash,
94 uint32_t lineNumber,
95 uint8_t logLevel,
96 bool addNewLine,
97 const char* fmt,
98 const Args&... args) {
99 cs_log_args_no_fmt(fileNameHash, lineNumber, logLevel, addNewLine, args...);
100}
101
102// This function takes unused format arguments.
103// These formats ends up in the .ii file (preprocessed .cpp file), and is then used to gather all log formats.
104// To make it easier for the compiler to optimize out the format strings, we only call cs_log_array_no_fmt without the
105// format args here.
106inline void cs_log_array(
107 uint32_t fileNameHash,
108 uint32_t lineNumber,
109 uint8_t logLevel,
110 bool addNewLine,
111 bool reverse,
112 const uint8_t* const ptr,
113 size_t size,
114 ElementType elementType,
115 size_t elementSize,
116 const char* startFormat,
117 const char* endFormat,
118 const char* seperationFormat,
119 const char* elementFormat) {
120 cs_log_array_no_fmt(fileNameHash, lineNumber, logLevel, addNewLine, reverse, ptr, size, elementType, elementSize);
121}
122
123inline void cs_log_array(
124 uint32_t fileNameHash,
125 uint32_t lineNumber,
126 uint8_t logLevel,
127 bool addNewLine,
128 bool reverse,
129 const uint8_t* const ptr,
130 size_t size,
131 const char* startFormat,
132 const char* endFormat,
133 const char* seperationFormat,
134 const char* elementFormat = "%3u") {
137 lineNumber,
138 logLevel,
139 addNewLine,
140 reverse,
141 ptr,
142 size,
144 sizeof(ptr[0]),
145 startFormat,
146 endFormat,
147 seperationFormat,
148 elementFormat);
149}
150
151inline void cs_log_array(
152 uint32_t fileNameHash,
153 uint32_t lineNumber,
154 uint8_t logLevel,
155 bool addNewLine,
156 bool reverse,
157 const uint16_t* const ptr,
158 size_t size,
159 const char* startFormat,
160 const char* endFormat,
161 const char* seperationFormat,
162 const char* elementFormat = "%5u") {
165 lineNumber,
166 logLevel,
167 addNewLine,
168 reverse,
169 reinterpret_cast<const uint8_t*>(ptr),
170 size,
172 sizeof(ptr[0]),
173 startFormat,
174 endFormat,
175 seperationFormat,
176 elementFormat);
177}
178
179inline void cs_log_array(
180 uint32_t fileNameHash,
181 uint32_t lineNumber,
182 uint8_t logLevel,
183 bool addNewLine,
184 bool reverse,
185 const uint32_t* const ptr,
186 size_t size,
187 const char* startFormat,
188 const char* endFormat,
189 const char* seperationFormat,
190 const char* elementFormat = "%10u") {
193 lineNumber,
194 logLevel,
195 addNewLine,
196 reverse,
197 reinterpret_cast<const uint8_t*>(ptr),
198 size,
200 sizeof(ptr[0]),
201 startFormat,
202 endFormat,
203 seperationFormat,
204 elementFormat);
205}
206
207inline void cs_log_array(
208 uint32_t fileNameHash,
209 uint32_t lineNumber,
210 uint8_t logLevel,
211 bool addNewLine,
212 bool reverse,
213 const uint64_t* const ptr,
214 size_t size,
215 const char* startFormat,
216 const char* endFormat,
217 const char* seperationFormat,
218 const char* elementFormat = "%20u") {
221 lineNumber,
222 logLevel,
223 addNewLine,
224 reverse,
225 reinterpret_cast<const uint8_t*>(ptr),
226 size,
228 sizeof(ptr[0]),
229 startFormat,
230 endFormat,
231 seperationFormat,
232 elementFormat);
233}
234
235inline void cs_log_array(
236 uint32_t fileNameHash,
237 uint32_t lineNumber,
238 uint8_t logLevel,
239 bool addNewLine,
240 bool reverse,
241 const int8_t* const ptr,
242 size_t size,
243 const char* startFormat,
244 const char* endFormat,
245 const char* seperationFormat,
246 const char* elementFormat = "%3i") {
249 lineNumber,
250 logLevel,
251 addNewLine,
252 reverse,
253 reinterpret_cast<const uint8_t*>(ptr),
254 size,
256 sizeof(ptr[0]),
257 startFormat,
258 endFormat,
259 seperationFormat,
260 elementFormat);
261}
262
263inline void cs_log_array(
264 uint32_t fileNameHash,
265 uint32_t lineNumber,
266 uint8_t logLevel,
267 bool addNewLine,
268 bool reverse,
269 const int16_t* const ptr,
270 size_t size,
271 const char* startFormat,
272 const char* endFormat,
273 const char* seperationFormat,
274 const char* elementFormat = "%5i") {
277 lineNumber,
278 logLevel,
279 addNewLine,
280 reverse,
281 reinterpret_cast<const uint8_t*>(ptr),
282 size,
284 sizeof(ptr[0]),
285 startFormat,
286 endFormat,
287 seperationFormat,
288 elementFormat);
289}
290
291inline void cs_log_array(
292 uint32_t fileNameHash,
293 uint32_t lineNumber,
294 uint8_t logLevel,
295 bool addNewLine,
296 bool reverse,
297 const int32_t* const ptr,
298 size_t size,
299 const char* startFormat,
300 const char* endFormat,
301 const char* seperationFormat,
302 const char* elementFormat = "%10i") {
305 lineNumber,
306 logLevel,
307 addNewLine,
308 reverse,
309 reinterpret_cast<const uint8_t*>(ptr),
310 size,
312 sizeof(ptr[0]),
313 startFormat,
314 endFormat,
315 seperationFormat,
316 elementFormat);
317}
318
319inline void cs_log_array(
320 uint32_t fileNameHash,
321 uint32_t lineNumber,
322 uint8_t logLevel,
323 bool addNewLine,
324 bool reverse,
325 const int64_t* const ptr,
326 size_t size,
327 const char* startFormat,
328 const char* endFormat,
329 const char* seperationFormat,
330 const char* elementFormat = "%20i") {
333 lineNumber,
334 logLevel,
335 addNewLine,
336 reverse,
337 reinterpret_cast<const uint8_t*>(ptr),
338 size,
340 sizeof(ptr[0]),
341 startFormat,
342 endFormat,
343 seperationFormat,
344 elementFormat);
345}
346
347inline void cs_log_array(
348 uint32_t fileNameHash,
349 uint32_t lineNumber,
350 uint8_t logLevel,
351 bool addNewLine,
352 bool reverse,
353 const float* const ptr,
354 size_t size,
355 const char* startFormat,
356 const char* endFormat,
357 const char* seperationFormat,
358 const char* elementFormat = "%f.") {
361 lineNumber,
362 logLevel,
363 addNewLine,
364 reverse,
365 reinterpret_cast<const uint8_t*>(ptr),
366 size,
368 sizeof(ptr[0]),
369 startFormat,
370 endFormat,
371 seperationFormat,
372 elementFormat);
373}
374
375inline void cs_log_array(
376 uint32_t fileNameHash,
377 uint32_t lineNumber,
378 uint8_t logLevel,
379 bool addNewLine,
380 bool reverse,
381 const double* const ptr,
382 size_t size,
383 const char* startFormat,
384 const char* endFormat,
385 const char* seperationFormat,
386 const char* elementFormat = "%f.") {
389 lineNumber,
390 logLevel,
391 addNewLine,
392 reverse,
393 reinterpret_cast<const uint8_t*>(ptr),
394 size,
396 sizeof(ptr[0]),
397 startFormat,
398 endFormat,
399 seperationFormat,
400 elementFormat);
401}
void cs_log_array(uint32_t fileNameHash, uint32_t lineNumber, uint8_t logLevel, bool addNewLine, bool reverse, const uint8_t *const ptr, size_t size, ElementType elementType, size_t elementSize, const char *startFormat, const char *endFormat, const char *seperationFormat, const char *elementFormat)
Definition: cs_LogUtils.h:106
void cs_log_array_no_fmt(uint32_t fileNameHash, uint32_t lineNumber, uint8_t logLevel, bool addNewLine, bool reverse, const uint8_t *const ptr, size_t size, ElementType elementType, size_t elementSize)
constexpr uint32_t fileNameHash(const char *str, size_t strLen)
Returns the 32 bits DJB2 hash of the reversed file name, up to the first '/'.
Definition: cs_LogUtils.h:13
void cs_log_args_no_fmt(uint32_t fileNameHash, uint32_t lineNumber, uint8_t logLevel, bool addNewLine, const Args &... args)
Definition: cs_LogUtils.h:65
void cs_log_start(size_t msgSize, uart_msg_log_header_t &header)
void cs_log_arg(const uint8_t *const valPtr, size_t valSize)
void cs_log_args(uint32_t fileNameHash, uint32_t lineNumber, uint8_t logLevel, bool addNewLine, const char *fmt, const Args &... args)
Definition: cs_LogUtils.h:92
void cs_log_add_arg_size(size_t &size, uint8_t &numArgs, T val)
Definition: cs_LogUtils.h:40
void cs_log_end()
ElementType
Definition: cs_UartMsgTypes.h:98
@ ELEMENT_TYPE_UNSIGNED_INTEGER
Definition: cs_UartMsgTypes.h:100
@ ELEMENT_TYPE_FLOAT
Definition: cs_UartMsgTypes.h:101
@ ELEMENT_TYPE_SIGNED_INTEGER
Definition: cs_UartMsgTypes.h:99
Definition: cs_UartMsgTypes.h:93
bool newLine
Definition: cs_UartMsgTypes.h:82
uint16_t lineNumber
Definition: cs_UartMsgTypes.h:79
uint8_t logLevel
Definition: cs_UartMsgTypes.h:80
struct uart_msg_log_common_header_t::@34 flags
uint32_t fileNameHash
Definition: cs_UartMsgTypes.h:78
Definition: cs_UartMsgTypes.h:87
uart_msg_log_common_header_t header
Definition: cs_UartMsgTypes.h:88
uint8_t numArgs
Definition: cs_UartMsgTypes.h:89