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 encodes it and dumps 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 overridden by implementing the memfault_data_export_base64_encoded_chunk() function 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 the Settings page for your project in the Memfault app and grab the 'Project Key' listed.

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:

# CLI command recording a reboot reason, then rebooting the system
shell> test_reboot
MFLT:[INFO] GNU Build ID: 3c92e3313fe1c9d00ac8687ce966d5f527a05ed3
MFLT:[INFO] S/N: freertos-example
MFLT:[INFO] SW type: qemu-app
MFLT:[INFO] SW version: 1.0.0
MFLT:[INFO] HW version: qemu-mps2-an385
MFLT:[INFO] Memfault Initialized!
# CLI command capturing a trace event
shell> test_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 for processing!

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