1 # Serial printf style debugging
5 INAV offers a function to use serial `printf` style debugging.
7 This provides a simple and intuitive debugging facility. This facility is only available after the serial sub-system has been initialised, which should be adequate for all but the most hard-core debugging requirements.
9 In order to use this feature, the source file must include `common/log.h`.
13 It is necessary to set a serial port for serial logging using the function mask `FUNCTION_LOG`, 32768. For convenience this may be shared with MSP (mask 1), but no other function.
14 For example, on a VCP port.
17 serial 20 32769 115200 115200 0 115200
20 If the port is shared, it will be reused with extant baud rate settings; if the port is not shared it is opened at 921600 baud.
22 There are two run time settings that control the verbosity, the most verbose settings being:
26 Allowed values: ERROR, WARNING, INFO, VERBOSE, DEBUG
29 Allowed range: 0 - 4294967295
33 The use of level and topics is described in the following sections.
37 Log levels are defined in `src/main/common/log.h`, at the time of writing these include (in ascending order):
45 These are used at both compile time and run time.
47 At compile time, a maximum level may be defined. As of INAV 2.3, for F3 targets the maximum level is ERROR, for F4/F7 the maximum level is DEBUG.
49 At run time, the level defines the level that will be displayed, so for a F4 or F7 target that has compile time suport for all log levels, if the CLI sets
53 then only `ERROR`, `WARNING` and `INFO` levels will be output.
57 Log topics are defined in `src/main/common/log.h`, at the time of writing:
66 * LOG_TOPIC_TEMPERATURE
67 * LOG_TOPIC_POS_ESTIMATOR
71 Topics are stored as masks (SYSTEM=1 ... OSD=1024) and may be used to unconditionally display log messages.
73 If the CLI `log_topics` is non-zero, then all topics matching the mask will be displayed regardless of `log_level`. Setting `log_topics` to 4294967295 (all bits set) will display all log messages regardless of run time level (but still constrained by compile time settings), so F3 will still only display ERROR level messages.
77 A set of macros `LOG_ERROR()` (log error) through `LOG_DEBUG()` (log debug) may be used, subject to compile time log level constraints. These provide `printf` style logging for a given topic.
79 Note that the `topic` is specified without the `LOG_TOPIC_` prefix:
82 // LOG_DEBUG(topic, fmt, ...)
83 LOG_DEBUG(SYSTEM, "This is %s topic debug message, value %d", "system", 42);
86 It is also possible to dump a hex representation of arbitrary data, using functions named variously `LOG_BUFFER_` (`ERROR`) and `LOG_BUF_` (anything else, alas) e.g.:
89 // LOG_BUFFER_ERROR(topic, buf, size)
90 // LOG_BUF_DEBUG(topic, buf, size)
94 LOG_BUF_DEBUG(TEMPERATURE, &tstruct, sizeof(tstruct));
99 Log messages are transmitted through the `FUNCTION_LOG` serial port as MSP messages (`MSP_DEBUGMSG`). It is possible to use any serial terminal to display these messages, however it is advisable to use an application that understands `MSP_DEBUGMSG` in order to maintain readability (in a raw serial terminal the MSP message envelope may result in the display of strange characters). `MSP_DEBUGMSG` aware applications include:
101 * [dbg-tool](https://codeberg.org/stronnag/dbg-tool)
102 * [INAV Configurator](https://github.com/iNavFlight/inav-configurator)
103 * [mwp](https://github.com/stronnag/mwptools)
107 * [msp-tool](https://github.com/fiam/msp-tool) is obsolete and has limited OS support.
109 For example, with the final lines of `src/main/fc/fc_init.c` set to:
112 LOG_ERROR(SYSTEM, "Init is complete");
113 systemState |= SYSTEM_STATE_READY;
116 and the following CLI settings:
119 serial 20 32769 115200 115200 0 115200
120 set log_level = DEBUG
121 set log_topics = 4294967295
124 The output will be formatted as follows:
128 [dbg-tool] 12:46:49.909079 DBG: [ 3.967] Init is complete
130 # mwp (stderr log file)
131 2020-02-02T19:09:02+0000 DEBUG:[ 3.968] Init is complete
134 [DEBUG] [ 3.967] Init is complete
137 For the Configurator, debug messages are shown in the developer console log.
139 Note: The numeric value in square brackets is the FC uptime in seconds.