This tutorial will cover how to enable the compact log feature in the Memfault Firmware SDK. When Compact logging is enabled, format strings will be replaced with an integer id at compile time. When the log is captured only the arguments and integer id will be serialized and sent to the Memfault cloud where the full format string will be recovered.
This has several key benefits:
- Reduces storage for logs on device
- Reduces required bandwidth to send logs to Memfault cloud
- Reduces CPU overhead for storing logs by offloading printf
Today, once enabled, compact logs will be used when capturing trace events with logs.
Let's take a look at a quick example:
With a formatted string we need to encode the string length as well as the entire string in our binary message taking up 41 bytes
where as with a compact log we need to encode a integer id and the arguments to be serialized which in this example takes a total of 5 bytes
So in this example we are able to reduce the storage occupied by 88%!
Add the following to your projects
Locate the linker script for your project (usually ends with
.ld) and add the
following to your linker script
Once enabled, if you see the following compiler error:
Compact logging uses a
GNU Compiler extension
which will remove the leading comma in
, ## __VA_ARGS__ if
empty. This particular extension has been implemented in most other modern
compilers including ARM Compiler 5, Clang & iccarm. All you need to do is make
sure you have enabled gnu extensions for your compiler of choice.
For GCC, this can be accomplished by adding
-std=gnu11 as an argument to your
compiler flag list
%p format is used and the argument is typed as a
char * or
char , it must be cast to a
void *. If it is not cast, a string will be
serialized instead and an incorrect pointer value will be displayed in the
Here's an example of correct usage: