Firmware
px4_log.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2015-2016 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
39 #pragma once
40 
41 #define _PX4_LOG_LEVEL_DEBUG 0
42 #define _PX4_LOG_LEVEL_INFO 1
43 #define _PX4_LOG_LEVEL_WARN 2
44 #define _PX4_LOG_LEVEL_ERROR 3
45 #define _PX4_LOG_LEVEL_PANIC 4
46 
47 // Used to silence unused variable warning
48 static inline void do_nothing(int level, ...)
49 {
50  (void)level;
51 }
52 
53 __BEGIN_DECLS
54 
58 __EXPORT extern void px4_log_initialize(void);
59 
60 __END_DECLS
61 
62 /****************************************************************************
63  * __px4_log_omit:
64  * Compile out the message
65  ****************************************************************************/
66 #define __px4_log_omit(level, FMT, ...) do_nothing(level, ##__VA_ARGS__)
67 
68 #if defined(__PX4_QURT)
69 #include "qurt_log.h"
70 /****************************************************************************
71  * Messages that should never be filtered or compiled out
72  ****************************************************************************/
73 #define PX4_INFO(FMT, ...) qurt_log(_PX4_LOG_LEVEL_INFO, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
74 #define PX4_INFO_RAW(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_INFO, FMT, ##__VA_ARGS__)
75 #define PX4_BACKTRACE()
76 
77 #if defined(TRACE_BUILD)
78 /****************************************************************************
79  * Extremely Verbose settings for a Trace build
80  ****************************************************************************/
81 #define PX4_PANIC(FMT, ...) qurt_log(_PX4_LOG_LEVEL_PANIC, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
82 #define PX4_ERR(FMT, ...) qurt_log(_PX4_LOG_LEVEL_ERROR, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
83 #define PX4_WARN(FMT, ...) qurt_log(_PX4_LOG_LEVEL_WARN, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
84 #define PX4_DEBUG(FMT, ...) qurt_log(_PX4_LOG_LEVEL_DEBUG, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
85 
86 #elif defined(DEBUG_BUILD)
87 /****************************************************************************
88  * Verbose settings for a Debug build
89  ****************************************************************************/
90 #define PX4_PANIC(FMT, ...) qurt_log(_PX4_LOG_LEVEL_PANIC, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
91 #define PX4_ERR(FMT, ...) qurt_log(_PX4_LOG_LEVEL_ERROR, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
92 #define PX4_WARN(FMT, ...) qurt_log(_PX4_LOG_LEVEL_WARN, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
93 #define PX4_DEBUG(FMT, ...) qurt_log(_PX4_LOG_LEVEL_DEBUG, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
94 
95 #elif defined(RELEASE_BUILD)
96 /****************************************************************************
97  * Non-verbose settings for a Release build to minimize strings in build
98  ****************************************************************************/
99 #define PX4_PANIC(FMT, ...) qurt_log(_PX4_LOG_LEVEL_PANIC, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
100 #define PX4_ERR(FMT, ...) qurt_log(_PX4_LOG_LEVEL_ERROR, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
101 #define PX4_WARN(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__)
102 #define PX4_DEBUG(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_DEBUG, FMT, ##__VA_ARGS__)
103 
104 #else
105 /****************************************************************************
106  * Medium verbose settings for a default build
107  ****************************************************************************/
108 #define PX4_PANIC(FMT, ...) qurt_log(_PX4_LOG_LEVEL_PANIC, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
109 #define PX4_ERR(FMT, ...) qurt_log(_PX4_LOG_LEVEL_ERROR, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
110 #define PX4_WARN(FMT, ...) qurt_log(_PX4_LOG_LEVEL_WARN, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
111 #define PX4_DEBUG(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_DEBUG, FMT, ##__VA_ARGS__)
112 
113 #endif
114 #define PX4_LOG_NAMED(name, FMT, ...) qurt_log( _PX4_LOG_LEVEL_INFO, __FILE__, __LINE__, "%s " FMT, name, ##__VA_ARGS__)
115 #define PX4_LOG_NAMED_COND(name, cond, FMT, ...) if( cond ) qurt_log( _PX4_LOG_LEVEL_INFO, __FILE__, __LINE__, "%s " FMT, name, ##__VA_ARGS__)
116 
117 #else
118 
119 #include <inttypes.h>
120 #include <stdint.h>
121 #include <sys/cdefs.h>
122 #include <stdio.h>
123 #include <stdarg.h>
124 
125 #include <px4_defines.h>
126 
127 __BEGIN_DECLS
128 
129 __EXPORT extern const char *__px4_log_level_str[_PX4_LOG_LEVEL_PANIC + 1];
130 __EXPORT extern const char *__px4_log_level_color[_PX4_LOG_LEVEL_PANIC + 1];
131 __EXPORT extern void px4_backtrace(void);
132 __EXPORT void px4_log_modulename(int level, const char *moduleName, const char *fmt, ...)
133 __attribute__((format(printf, 3, 4)));
134 __EXPORT void px4_log_raw(int level, const char *fmt, ...)
135 __attribute__((format(printf, 2, 3)));
136 
137 #if __GNUC__
138 // Allow empty format strings.
139 #pragma GCC diagnostic ignored "-Wformat-zero-length"
140 #endif
141 
142 __END_DECLS
143 
144 #define PX4_BACKTRACE() px4_backtrace()
145 
146 /****************************************************************************
147  * Implementation of log section formatting based on printf
148  *
149  * To write to a specific stream for each message type, open the streams and
150  * set __px4__log_startline to something like:
151  * printf(_px4_fd[level],
152  *
153  * Additional behavior can be added using "{\" for __px4__log_startline and
154  * "}" for __px4__log_endline and any other required setup or teardown steps
155  ****************************************************************************/
156 #define __px4__log_printcond(cond, ...) if (cond) printf(__VA_ARGS__)
157 #define __px4__log_printline(level, ...) printf(__VA_ARGS__)
158 
159 #define __px4__log_timestamp_fmt "%-10" PRIu64 " "
160 #define __px4__log_timestamp_arg ,hrt_absolute_time()
161 #define __px4__log_level_fmt "%-5s "
162 #define __px4__log_level_arg(level) ,__px4_log_level_str[level]
163 #define __px4__log_thread_fmt "%#X "
164 #define __px4__log_thread_arg ,(unsigned int)pthread_self()
165 #define __px4__log_modulename_fmt "%-10s "
166 #define __px4__log_modulename_pfmt "[%s] "
167 #define __px4__log_modulename_arg ,"[" MODULE_NAME "]"
168 
169 #define __px4__log_file_and_line_fmt " (file %s line %u)"
170 #define __px4__log_file_and_line_arg , __FILE__, __LINE__
171 #define __px4__log_end_fmt "\n"
172 
173 #define PX4_ANSI_COLOR_RED "\x1b[31m"
174 #define PX4_ANSI_COLOR_GREEN "\x1b[32m"
175 #define PX4_ANSI_COLOR_YELLOW "\x1b[33m"
176 #define PX4_ANSI_COLOR_BLUE "\x1b[34m"
177 #define PX4_ANSI_COLOR_MAGENTA "\x1b[35m"
178 #define PX4_ANSI_COLOR_CYAN "\x1b[36m"
179 #define PX4_ANSI_COLOR_GRAY "\x1B[37m"
180 #define PX4_ANSI_COLOR_RESET "\x1b[0m"
181 
182 #ifdef __PX4_POSIX
183 #define PX4_LOG_COLORIZED_OUTPUT //if defined and output is a tty, colorize the output according to the log level
184 #endif /* __PX4_POSIX */
185 
186 
187 /****************************************************************************
188  * Output format macros
189  * Use these to implement the code level macros below
190  ****************************************************************************/
191 
192 
193 /****************************************************************************
194  * __px4_log_named_cond:
195  * Convert a message in the form:
196  * PX4_LOG_COND(__dbg_enabled, "val is %d", val);
197  * to
198  * printf("%-5s val is %d\n", "LOG", val);
199  * if the first arg/condition is true.
200  ****************************************************************************/
201 #define __px4_log_named_cond(name, cond, FMT, ...) \
202  __px4__log_printcond(cond,\
203  "%s " \
204  FMT\
205  __px4__log_end_fmt \
206  ,name, ##__VA_ARGS__\
207  )
208 
209 /****************************************************************************
210  * __px4_log:
211  * Convert a message in the form:
212  * PX4_WARN("val is %d", val);
213  * to
214  * printf("%-5s val is %d\n", __px4_log_level_str[3], val);
215  ****************************************************************************/
216 #define __px4_log(level, FMT, ...) \
217  __px4__log_printline(level,\
218  __px4__log_level_fmt \
219  FMT\
220  __px4__log_end_fmt \
221  __px4__log_level_arg(level), ##__VA_ARGS__\
222  )
223 
224 /****************************************************************************
225  * __px4_log_modulename:
226  * Convert a message in the form:
227  * PX4_WARN("val is %d", val);
228  * to
229  * printf("%-5s [%s] val is %d\n", __px4_log_level_str[3],
230  * MODULENAME, val);
231  ****************************************************************************/
232 
233 #define __px4_log_modulename(level, fmt, ...) \
234  do { \
235  px4_log_modulename(level, MODULE_NAME, fmt, ##__VA_ARGS__); \
236  } while(0)
237 
238 /****************************************************************************
239  * __px4_log_raw:
240  * Convert a message in the form:
241  * PX4_INFO("val is %d", val);
242  * to
243  * printf("val is %d", val);
244  *
245  * This can be used for simple printfs with all the formatting control.
246  ****************************************************************************/
247 #define __px4_log_raw(level, fmt, ...) \
248  do { \
249  px4_log_raw(level, fmt, ##__VA_ARGS__); \
250  } while(0)
251 
252 
253 /****************************************************************************
254  * __px4_log_timestamp:
255  * Convert a message in the form:
256  * PX4_WARN("val is %d", val);
257  * to
258  * printf("%-5s %10lu val is %d\n", __px4_log_level_str[3],
259  * hrt_absolute_time(), val);
260  ****************************************************************************/
261 #define __px4_log_timestamp(level, FMT, ...) \
262  __px4__log_printline(level,\
263  __px4__log_level_fmt\
264  __px4__log_timestamp_fmt\
265  FMT\
266  __px4__log_end_fmt\
267  __px4__log_level_arg(level)\
268  __px4__log_timestamp_arg\
269  , ##__VA_ARGS__\
270  )
271 
272 /****************************************************************************
273  * __px4_log_timestamp_thread:
274  * Convert a message in the form:
275  * PX4_WARN("val is %d", val);
276  * to
277  * printf("%-5s %10lu %#X val is %d\n", __px4_log_level_str[3],
278  * hrt_absolute_time(), pthread_self(), val);
279  ****************************************************************************/
280 #define __px4_log_timestamp_thread(level, FMT, ...) \
281  __px4__log_printline(level,\
282  __px4__log_level_fmt\
283  __px4__log_timestamp_fmt\
284  __px4__log_thread_fmt\
285  FMT\
286  __px4__log_end_fmt\
287  __px4__log_level_arg(level)\
288  __px4__log_timestamp_arg\
289  __px4__log_thread_arg\
290  , ##__VA_ARGS__\
291  )
292 
293 /****************************************************************************
294  * __px4_log_file_and_line:
295  * Convert a message in the form:
296  * PX4_WARN("val is %d", val);
297  * to
298  * printf("%-5s val is %d (file %s line %u)\n",
299  * __px4_log_level_str[3], val, __FILE__, __LINE__);
300  ****************************************************************************/
301 #define __px4_log_file_and_line(level, FMT, ...) \
302  __px4__log_printline(level,\
303  __px4__log_level_fmt\
304  __px4__log_timestamp_fmt\
305  FMT\
306  __px4__log_file_and_line_fmt\
307  __px4__log_end_fmt\
308  __px4__log_level_arg(level)\
309  __px4__log_timestamp_arg\
310  , ##__VA_ARGS__\
311  __px4__log_file_and_line_arg\
312  )
313 
314 /****************************************************************************
315  * __px4_log_timestamp_file_and_line:
316  * Convert a message in the form:
317  * PX4_WARN("val is %d", val);
318  * to
319  * printf("%-5s %-10lu val is %d (file %s line %u)\n",
320  * __px4_log_level_str[3], hrt_absolute_time(),
321  * val, __FILE__, __LINE__);
322  ****************************************************************************/
323 #define __px4_log_timestamp_file_and_line(level, FMT, ...) \
324  __px4__log_printline(level,\
325  __px4__log_level_fmt\
326  __px4__log_timestamp_fmt\
327  FMT\
328  __px4__log_file_and_line_fmt\
329  __px4__log_end_fmt\
330  __px4__log_level_arg(level)\
331  __px4__log_timestamp_arg\
332  , ##__VA_ARGS__\
333  __px4__log_file_and_line_arg\
334  )
335 
336 /****************************************************************************
337  * __px4_log_thread_file_and_line:
338  * Convert a message in the form:
339  * PX4_WARN("val is %d", val);
340  * to
341  * printf("%-5s %#X val is %d (file %s line %u)\n",
342  * __px4_log_level_str[3], pthread_self(),
343  * val, __FILE__, __LINE__);
344  ****************************************************************************/
345 #define __px4_log_thread_file_and_line(level, FMT, ...) \
346  __px4__log_printline(level,\
347  __px4__log_level_fmt\
348  __px4__log_thread_fmt\
349  FMT\
350  __px4__log_file_and_line_fmt\
351  __px4__log_end_fmt\
352  __px4__log_level_arg(level)\
353  __px4__log_thread_arg\
354  , ##__VA_ARGS__\
355  __px4__log_file_and_line_arg\
356  )
357 
358 /****************************************************************************
359  * __px4_log_timestamp_thread_file_and_line:
360  * Convert a message in the form:
361  * PX4_WARN("val is %d", val);
362  * to
363  * printf("%-5s %-10lu %#X val is %d (file %s line %u)\n",
364  * __px4_log_level_str[3], hrt_absolute_time(),
365  * pthread_self(), val, __FILE__, __LINE__);
366  ****************************************************************************/
367 #define __px4_log_timestamp_thread_file_and_line(level, FMT, ...) \
368  __px4__log_printline(level,\
369  __px4__log_level_fmt\
370  __px4__log_timestamp_fmt\
371  __px4__log_thread_fmt\
372  FMT\
373  __px4__log_file_and_line_fmt\
374  __px4__log_end_fmt\
375  __px4__log_level_arg(level)\
376  __px4__log_timestamp_arg\
377  __px4__log_thread_arg\
378  , ##__VA_ARGS__\
379  __px4__log_file_and_line_arg\
380  )
381 
382 
383 /****************************************************************************
384  * Code level macros
385  * These are the log APIs that should be used by the code
386  ****************************************************************************/
387 
388 /****************************************************************************
389  * Messages that should never be filtered or compiled out
390  ****************************************************************************/
391 #define PX4_INFO(FMT, ...) __px4_log_modulename(_PX4_LOG_LEVEL_INFO, FMT, ##__VA_ARGS__)
392 
393 #ifdef __NUTTX
394 #define PX4_INFO_RAW printf
395 #else
396 #define PX4_INFO_RAW(FMT, ...) __px4_log_raw(_PX4_LOG_LEVEL_INFO, FMT, ##__VA_ARGS__)
397 #endif
398 
399 #if defined(TRACE_BUILD)
400 /****************************************************************************
401  * Extremely Verbose settings for a Trace build
402  ****************************************************************************/
403 #define PX4_PANIC(FMT, ...) __px4_log_timestamp_thread_file_and_line(_PX4_LOG_LEVEL_PANIC, FMT, ##__VA_ARGS__)
404 #define PX4_ERR(FMT, ...) __px4_log_timestamp_thread_file_and_line(_PX4_LOG_LEVEL_ERROR, FMT, ##__VA_ARGS__)
405 #define PX4_WARN(FMT, ...) __px4_log_timestamp_thread_file_and_line(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__)
406 #define PX4_DEBUG(FMT, ...) __px4_log_timestamp_thread(_PX4_LOG_LEVEL_DEBUG, FMT, ##__VA_ARGS__)
407 
408 #elif defined(DEBUG_BUILD)
409 /****************************************************************************
410  * Verbose settings for a Debug build
411  ****************************************************************************/
412 #define PX4_PANIC(FMT, ...) __px4_log_timestamp_file_and_line(_PX4_LOG_LEVEL_PANIC, FMT, ##__VA_ARGS__)
413 #define PX4_ERR(FMT, ...) __px4_log_timestamp_file_and_line(_PX4_LOG_LEVEL_ERROR, FMT, ##__VA_ARGS__)
414 #define PX4_WARN(FMT, ...) __px4_log_timestamp_file_and_line(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__)
415 #define PX4_DEBUG(FMT, ...) __px4_log_timestamp(_PX4_LOG_LEVEL_DEBUG, FMT, ##__VA_ARGS__)
416 
417 #elif defined(RELEASE_BUILD)
418 /****************************************************************************
419  * Non-verbose settings for a Release build to minimize strings in build
420  ****************************************************************************/
421 #define PX4_PANIC(FMT, ...) __px4_log_modulename(_PX4_LOG_LEVEL_PANIC, FMT, ##__VA_ARGS__)
422 #define PX4_ERR(FMT, ...) __px4_log_modulename(_PX4_LOG_LEVEL_ERROR, FMT, ##__VA_ARGS__)
423 #define PX4_WARN(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__)
424 #define PX4_DEBUG(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_DEBUG, FMT, ##__VA_ARGS__)
425 
426 #else
427 /****************************************************************************
428  * Medium verbose settings for a default build
429  ****************************************************************************/
430 #define PX4_PANIC(FMT, ...) __px4_log_modulename(_PX4_LOG_LEVEL_PANIC, FMT, ##__VA_ARGS__)
431 #define PX4_ERR(FMT, ...) __px4_log_modulename(_PX4_LOG_LEVEL_ERROR, FMT, ##__VA_ARGS__)
432 #define PX4_WARN(FMT, ...) __px4_log_modulename(_PX4_LOG_LEVEL_WARN, FMT, ##__VA_ARGS__)
433 #define PX4_DEBUG(FMT, ...) __px4_log_omit(_PX4_LOG_LEVEL_DEBUG, FMT, ##__VA_ARGS__)
434 
435 #endif
436 #define PX4_LOG_NAMED(name, FMT, ...) __px4_log_named_cond(name, true, FMT, ##__VA_ARGS__)
437 #define PX4_LOG_NAMED_COND(name, cond, FMT, ...) __px4_log_named_cond(name, cond, FMT, ##__VA_ARGS__)
438 #endif
Definition: crtp.h:57
Generally used magic defines.
Definition: I2C.hpp:51
__BEGIN_DECLS __EXPORT void px4_log_initialize(void)
initialize the orb logging.
Definition: px4_log.c:62