Skip to main content

Post chunks with Memfault CLI

In the following tutorial we will discuss how to use the Memfault SDK data export API to collect "chunks" from a device and then post them to the Memfault cloud for analysis.

Add call to memfault_data_export_dump_chunks#

The data export module uses the SDK data packetizer APIs to read the data which has been collected. It then base64 encode it and dump the result to a destination of your choosing.

First you will need to find a location in your project to trigger the export such as a code path that runs periodically or on-demand via a user triggered action such as a CLI command.

#include "memfault/core/data_export.h"
void some_periodic_task_or_cli_cmd(void) {
memfault_data_export_dump_chunks()
}
Optional: Override where data is exported to

By default, the data_export module will dump results to the console using the memfault_platform_log implementation that was added as part of the initial integration.

However, the location can be overriden so data can be published elsewhere (i.e directly to a file):

#include "memfault/core/data_export.h"
void memfault_data_export_base64_encoded_chunk(const char *chunk_str) {
user_export_write_string_to_file(chunk_str);
}

Grab Project Key from Memfault UI#

A Project key will be needed in order to communicate with Memfault's web services. Go to https://app.memfault.com/, navigate to the project you want to use and select 'Settings'→'General'. The 'Project Key' listed is what you will want to use.

Install the Python Memfault CLI Tool#

The Memfault Desktop CLI tool. tool is written in Python and published publicly in the Python Package Index (pypi).

To install it, make sure you have a recent version of Python 3.x installed and run:

$ pip3 install memfault-cli
$ memfault --help

Save device logs to file#

Start your console with your favorite terminal client. Let the system run for a bit, periodically dumping data to the console by calling memfault_data_export_dump_chunks.

You should see strings with the format: MC:BASE64_ENCODED_CHUNK: in the dump.

note

It's perfectly fine for other logs to be interleaved with the exported data.

For example:

shell> reboot
[00:00:01] INFO: System Booting!
[00:00:02] INFO: Memfault Build ID: d8d6a047282f025fffa29fa767100f310bc40f80
shell> trace
# CLI command making a call to `memfault_data_export_dump_chunks`
shell> export
[00:00:10] INFO: MC:SFQCpwIBAwEHalRFU1RTRVJJQUwKbXRlc3Qtc29mdHdhcmUJajEuMC4wLXRlcw==:

Save the resulting logs to a file such as logs.txt

Post chunks from logs with Memfault CLI#

Run the desktop Memfault CLI tool on your saved log file. The utility will parse the logs, extract the Memfault data, and post it here for processing!

$ memfault --project-key ${YOUR_PROJECT_KEY} post-chunk --encoding sdk_data_export logs.txt
Found 2 Chunks. Sending Data ...
Success