|
px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
|
Output human readable debug info to a serial console.
File(s):
An old school debugging technique which still works well is to add debug print statements in the source code to track program flow and check for coding mistakes during development. Afterwards the debug print statements are removed from the final release build to avoid the significant code and delay overhead.
This module allows one to easily add formatted debug print macros to the source code and enable them by defining PX_LOG=1 or remove them from the build by defining PX_LOG=0 in the Makefile. There is no need to manually edit the source code afterwards for the release build.

The following macros can be used:
PX_LOG_CFG_LEVEL is used to specify which log messages are compiled into the source code. For example if PX_LOG_CFG_LEVEL is set to PX_LOG_LEVEL_INFO then all ERROR, WARNING and INFO messages will be included in the code but VERBOSE and DEBUG message will not. It does not affect PX_LOG_TRACE() or PX_LOG_ASSERT() macros.
Set PX_LOG_CFG_COLOR to 1 to indicate log message level with color on an ANSI/VT100 terminal emulator (for example Tera Term).
Example usage:
Observe that the compiler will automatically concatenate adjacent strings.
The log messages will report the file / module name and line number as well as the user format string. To save code space, the name must be declared at the top of the C file using PX_LOG_NAME(). This is the correct way to declare the name:
If PX_LOG_NAME() is not used, then the compiler will output the following error when log output is enabled (PX_LOG=1):
error: '_px_log_name' undeclared (first use in this function)
The log output can be redirected by using the PX_LOG_CFG_PUTCHAR macro, for example to stderr stream, a different uart or to a log file on an SD card.
Example:
The project wide PX_LOG_CFG_LEVEL setting in the Makefile or "px_log_cfg.h" can be overriden for a specific C file as follows:
The PX_LOG_CFG_LEVEL can easily be tested with the the PX_LOG_LEVEL_IS_E(), PX_LOG_LEVEL_IS_W(), PX_LOG_LEVEL_IS_I(), PX_LOG_LEVEL_IS_V() or PX_LOG_LEVEL_IS_D() macro to conditionally add code, for example:
A naming convention is used to start a function, variable or macro name with an underscore ('_') to indicate that it is used internally by this C module and should not be used directly, for example _px_log_log_error() or _px_log_name[].
Macros | |
| #define | PX_LOG 1 |
| Set flag to disable (PX_LOG=0) or enable (PX_LOG=1) log output. More... | |
| #define | PX_LOG_NAME(name) PX_ATTR_UNUSED static const char _px_log_name[] PX_ATTR_PGM = name; |
| Macro to declare a log name string once for each file to reduce code size. More... | |
| #define | PX_LOG_LEVEL_IS_E() (PX_LOG_CFG_LEVEL >= PX_LOG_LEVEL_ERROR) |
| Error level enabled? More... | |
| #define | PX_LOG_LEVEL_IS_W() (PX_LOG_CFG_LEVEL >= PX_LOG_LEVEL_WARNING) |
| Warning level enabled? More... | |
| #define | PX_LOG_LEVEL_IS_I() (PX_LOG_CFG_LEVEL >= PX_LOG_LEVEL_INFO) |
| Info level enabled? More... | |
| #define | PX_LOG_LEVEL_IS_D() (PX_LOG_CFG_LEVEL >= PX_LOG_LEVEL_DEBUG) |
| Debug level enabled? More... | |
| #define | PX_LOG_LEVEL_IS_V() (PX_LOG_CFG_LEVEL >= PX_LOG_LEVEL_VERBOSE) |
| Verbose level enabled? More... | |
| #define | PX_LOG_E(format, ...) |
| Macro to display a formatted ERROR message. More... | |
| #define | PX_LOG_W(format, ...) |
| Macro to display a formatted WARNING message. More... | |
| #define | PX_LOG_I(format, ...) |
| Macro to display a formatted INFO message. More... | |
| #define | PX_LOG_D(format, ...) |
| Macro to display a formatted DEBUG message. More... | |
| #define | PX_LOG_V(format, ...) |
| Macro to display a formatted VERBOSE message. More... | |
| #define | PX_LOG_ASSERT(expression) |
| Macro that will test an expression, and block indefinitely if false. More... | |
| #define | PX_LOG_TRACE(format, ...) _px_log_trace(PX_PGM_STR(format), ## __VA_ARGS__) |
| Macro to output a user format string if PX_LOG=1. More... | |
| #define | PX_LOG_TRACE_CHAR(c) _px_log_trace_char(c) |
| Macro to output a char if PX_LOG=1. More... | |
| #define | PX_LOG_TRACE_DATA(data, nr_of_bytes) _px_log_trace_data(data, nr_of_bytes) |
| Macro to output the content of a buffer if PX_LOG=1. More... | |
| #define | PX_LOG_TRACE_HEXDUMP(data, nr_of_bytes) _px_log_trace_hexdump(data, nr_of_bytes) |
| Macro to output the content of a buffer if PX_LOG=1. More... | |
| #define | PX_LOG_CFG_LEVEL PX_LOG_LEVEL_INFO |
| Set COMPILE TIME log output level. More... | |
| #define | PX_LOG_CFG_FILTER 0 |
| Disable (0) or Enable (1) run time log filter. More... | |
| #define | PX_LOG_CFG_COLOR 1 |
| Disable (0) or Enable (1) VT100 terminal color output. More... | |
| #define | PX_LOG_CFG_BUF_SIZE 64 |
| Debug output string buffer size. More... | |
Enumerations | |
| enum | px_log_level_t { PX_LOG_LEVEL_NONE = 0 , PX_LOG_LEVEL_ERROR = 1 , PX_LOG_LEVEL_WARNING = 2 , PX_LOG_LEVEL_DEBUG = 4 , PX_LOG_LEVEL_VERBOSE = 5 } |
| Log message level ordered in increasing verbosity. More... | |
Functions | |
| bool | px_log_filter (px_log_level_t level, const char *name) |
| Run time log filter. More... | |
| void | _px_log_log_error (const char *name, uint16_t line, const char *format,...) |
| Output error info: module name, line and variable argument user format string. More... | |
| void | _px_log_log_warning (const char *name, uint16_t line, const char *format,...) |
| Output warning info: module name, line and variable argument user format string. More... | |
| void | _px_log_log_info (const char *name, uint16_t line, const char *format,...) |
| Output info info: module name, line and variable argument user format string. More... | |
| void | _px_log_log_debug (const char *name, uint16_t line, const char *format,...) |
| Output debug info: module name, line and variable argument user format string. More... | |
| void | _px_log_log_verbose (const char *name, uint16_t line, const char *format,...) |
| Output verbose info: module name, line and variable argument user format string. More... | |
| void | _px_log_assert (const char *name, uint16_t line) PX_ATTR_NORETURN |
| Report that an assertion has failed and block indefinitely. More... | |
| void | _px_log_trace (const char *format,...) |
| Output a user format string. More... | |
| void | _px_log_trace_char (char c) |
| Output a character. More... | |
| void | _px_log_trace_data (const void *data, size_t nr_of_bytes) |
| Output the content of a buffer as an array of HEX values. More... | |
| void | _px_log_trace_hexdump (const void *data, size_t nr_of_bytes) |
| Output the content of a buffer as a spaced table of HEX values and ASCII characters. More... | |
| #define PX_LOG 1 |
| #define PX_LOG_NAME | ( | name | ) | PX_ATTR_UNUSED static const char _px_log_name[] PX_ATTR_PGM = name; |
| #define PX_LOG_LEVEL_IS_E | ( | ) | (PX_LOG_CFG_LEVEL >= PX_LOG_LEVEL_ERROR) |
| #define PX_LOG_LEVEL_IS_W | ( | ) | (PX_LOG_CFG_LEVEL >= PX_LOG_LEVEL_WARNING) |
| #define PX_LOG_LEVEL_IS_I | ( | ) | (PX_LOG_CFG_LEVEL >= PX_LOG_LEVEL_INFO) |
| #define PX_LOG_LEVEL_IS_D | ( | ) | (PX_LOG_CFG_LEVEL >= PX_LOG_LEVEL_DEBUG) |
| #define PX_LOG_LEVEL_IS_V | ( | ) | (PX_LOG_CFG_LEVEL >= PX_LOG_LEVEL_VERBOSE) |
| #define PX_LOG_E | ( | format, | |
| ... | |||
| ) |
Macro to display a formatted ERROR message.
| #define PX_LOG_W | ( | format, | |
| ... | |||
| ) |
Macro to display a formatted WARNING message.
| #define PX_LOG_I | ( | format, | |
| ... | |||
| ) |
Macro to display a formatted INFO message.
| #define PX_LOG_D | ( | format, | |
| ... | |||
| ) |
Macro to display a formatted DEBUG message.
| #define PX_LOG_V | ( | format, | |
| ... | |||
| ) |
Macro to display a formatted VERBOSE message.
| #define PX_LOG_ASSERT | ( | expression | ) |
Macro that will test an expression, and block indefinitely if false.
This macro will perform the test and if false, will output the filename and line number. The macro will then block indefinitely.
| [in] | expression | Expression that evaluates to a boolean value (true or false) |
| #define PX_LOG_TRACE | ( | format, | |
| ... | |||
| ) | _px_log_trace(PX_PGM_STR(format), ## __VA_ARGS__) |
| #define PX_LOG_TRACE_CHAR | ( | c | ) | _px_log_trace_char(c) |
| #define PX_LOG_TRACE_DATA | ( | data, | |
| nr_of_bytes | |||
| ) | _px_log_trace_data(data, nr_of_bytes) |
| #define PX_LOG_TRACE_HEXDUMP | ( | data, | |
| nr_of_bytes | |||
| ) | _px_log_trace_hexdump(data, nr_of_bytes) |
| #define PX_LOG_CFG_LEVEL PX_LOG_LEVEL_INFO |
Set COMPILE TIME log output level.
The levels are sorted in increasing levels of verbosity. For example if PX_LOG_CFG_LEVEL is set to PX_LOG_LEVEL_INFO then all ERROR, WARNING and INFO messages will be included in the code but DEBUG and VERBOSE messages will not.
Definition at line 49 of file px_log_cfg_template.h.
| #define PX_LOG_CFG_FILTER 0 |
Disable (0) or Enable (1) run time log filter.
Definition at line 53 of file px_log_cfg_template.h.
| #define PX_LOG_CFG_COLOR 1 |
Disable (0) or Enable (1) VT100 terminal color output.
Definition at line 56 of file px_log_cfg_template.h.
| #define PX_LOG_CFG_BUF_SIZE 64 |
Debug output string buffer size.
Definition at line 59 of file px_log_cfg_template.h.
| enum px_log_level_t |
Log message level ordered in increasing verbosity.
| bool px_log_filter | ( | px_log_level_t | level, |
| const char * | name | ||
| ) |
Run time log filter.
A customised version of this function can be declared to filter log output based on the level and / or name. Define PX_LOG_CFG_FILTER=1 to enable.
A weak version is declared in px_log.c that always returns false meaning that all log output is allowed (none is filtered).
| level | Log level |
| name | Module / file name |
| true | Log must be filtered |
| false | Log must be outputted |
| void _px_log_log_error | ( | const char * | name, |
| uint16_t | line, | ||
| const char * | format, | ||
| ... | |||
| ) |
Output error info: module name, line and variable argument user format string.
The line is automatically appended with a new line character ('\n'), except if the format string ends with a tab character ('\t'). The tab character at the end of the string is discarded.
| name | Module / file name |
| line | Line number |
| format | User format string |
| ... | Variable number of arguments |
| void _px_log_log_warning | ( | const char * | name, |
| uint16_t | line, | ||
| const char * | format, | ||
| ... | |||
| ) |
Output warning info: module name, line and variable argument user format string.
The line is automatically appended with a new line character ('\n'), except if the format string ends with a tab character ('\t'). The tab character at the end of the string is discarded.
| name | Module / file name |
| line | Line number |
| format | User format string |
| ... | Variable number of arguments |
| void _px_log_log_info | ( | const char * | name, |
| uint16_t | line, | ||
| const char * | format, | ||
| ... | |||
| ) |
Output info info: module name, line and variable argument user format string.
The line is automatically appended with a new line character ('\n'), except if the format string ends with a tab character ('\t'). The tab character at the end of the string is discarded.
| name | Module / file name |
| line | Line number |
| format | User format string |
| ... | Variable number of arguments |
| void _px_log_log_debug | ( | const char * | name, |
| uint16_t | line, | ||
| const char * | format, | ||
| ... | |||
| ) |
Output debug info: module name, line and variable argument user format string.
The line is automatically appended with a new line character ('\n'), except if the format string ends with a tab character ('\t'). The tab character at the end of the string is discarded.
| name | Module / file name |
| line | Line number |
| format | User format string |
| ... | Variable number of arguments |
| void _px_log_log_verbose | ( | const char * | name, |
| uint16_t | line, | ||
| const char * | format, | ||
| ... | |||
| ) |
Output verbose info: module name, line and variable argument user format string.
The line is automatically appended with a new line character ('\n'), except if the format string ends with a tab character ('\t'). The tab character at the end of the string is discarded.
| name | Module / file name |
| line | Line number |
| format | User format string |
| ... | Variable number of arguments |
| void _px_log_assert | ( | const char * | name, |
| uint16_t | line | ||
| ) |
| void _px_log_trace | ( | const char * | format, |
| ... | |||
| ) |
| void _px_log_trace_char | ( | char | c | ) |
| void _px_log_trace_data | ( | const void * | data, |
| size_t | nr_of_bytes | ||
| ) |
| void _px_log_trace_hexdump | ( | const void * | data, |
| size_t | nr_of_bytes | ||
| ) |