Log System#
Description#
Logging is very important for developing embedded products. This is especially true for connection-based wireless products because the use of breakpoints will probably result in dropped connections, whereas issues can be easily addressed by walking through the log.
This example introduces a simple implementation of logging on EFR32 based devices. The key point of the example code is to introduce a way to classify the log into different levels, where each level of log has its own identifier. Let’s start with 2 design sketches, which demonstrate outputting the log via RTT and VCOM(UART).
The images above show different levels of logging in different colors. This example introduces 5 logging levels, as shown below.
Type | color |
---|---|
Error (highest level) | Red |
Warning | Yellow |
Information | Blue |
Debug | Green |
Verbose (lowest level) | White |
Setting up#
To be able to test the logger module do as follows:
Create a new SoC-Empty example project in Simplicity Studio.
Copy the attached app.c, log.c, log.h files into your project (overwriting existing app.c).
LOG_PORT
, defined in log.h, can be set to SEGGER_JLINK_VIEWER or PORT_VCOM to determine if you want to send debug messages via DEBUG port or COM port (UART).SEGGER_JLINK_VIEWER – the log will be out from J-Link RTT.
PORT_VCOM
If
HAL_VCOM_ENABLE
is enabled in hal-config.h, the log will be out from VCOM via USB. This is the default configuration in a SOC - Empty project.If
HAL_VCOM_ENABLE
is disabled, the log will be out from the UART TX pin directly which can be accessed on the expansion header of the WSTK
If you use the COM port (UART) for logging, copy VCOM_RTT.h into your project. If you use SEGGER_JLINK_VIEWER, copy SEGGER_RTT*.* files into your project.
Define
LOG_LEVEL
in log.h. The definition ofLOG_LEVEL
determines which level of logging should be output to the terminal. As you can see from the table 1, error has the highest level while verbose has the lowest level. For example, If theLOG_LEVEL
is defined as information level, then error, warning and information log will be sent to terminal, the debug and verbose log which have lower level than information will not be sent to the terminal. See figure below, which shows the log as information level without modifying anything from the first figure, using RTT.Build and flash the project to your device
Usage#
On your PC open a terminal program and connect to the chosen port (e.g. you can use TeraTerm to connect via VCOM and RTT Viewer to connect via DEBUG port).
Reset your device and observe the log messages.
You may also open a Bluetooth connection to see more logs.
You can add new logs to your project. The following are 5 corresponding functions to send the log. The input parameter of these five functions is the same as standard printf();.
LOGE()
– Send ERROR log.LOGW()
– Send WARNING log.LOGI()
– Send INFORMATION log.LOGD()
– Send DEBUG log.LOGV()
– Send VERBOSE log.
Use these functions in your code to print different level logs.