[info] Logging on esp32 using ring_log
Posted: Tue Jun 06, 2017 7:26 pm
We're all familiar with logging to Flash. You want to keep persistent logs to know how something failed, or to see patterns of behavior, etc.
But of course you can't log forever, unless you first figure out a way to get rid of old logs.
I wrote a library called ring_log (GitHub) to deal with the log storage problem.
ring_log writes a ring buffer to Flash, so that you always have the N most recent log entries. Where N is decided by how much space you allow each ring_log to use.
Here's an example (copied from example.c (GitHub)) of how to use it. First, we'll start up ring_log and write a few entries:
And then read all the entries out:
Here's an example ESP-IDF project using ring_log. (See examples/ring_log.)
I put a lot of care into this library, knowing that I would be using it for my projects.
* It has a pretty brutal test suite. I wouldn't feel confident using it, or sharing it, otherwise.
* It's written pedantic C99 with all the warnings enabled.
* It should be quite resistant to unexpected power-offs, but there is an issue to improve that.
I hope you find it useful!
But of course you can't log forever, unless you first figure out a way to get rid of old logs.
I wrote a library called ring_log (GitHub) to deal with the log storage problem.
ring_log writes a ring buffer to Flash, so that you always have the N most recent log entries. Where N is decided by how much space you allow each ring_log to use.
Here's an example (copied from example.c (GitHub)) of how to use it. First, we'll start up ring_log and write a few entries:
Code: Select all
if (!ring_log_init()) {
puts("ring_log_init failed");
exit(1);
}
ring_log_write_tail("log_a", "one", 3);
ring_log_write_tail_complete("log_a");
ring_log_write_tail("log_a", "two", 3);
ring_log_write_tail_complete("log_a");
ring_log_write_tail("log_a", "three", 5);
ring_log_write_tail_complete("log_a");
Code: Select all
while (ring_log_has_unread("log_a")) {
int read_now;
char buffer[8];
size_t read_total = 0;
printf("entry: ");
while ((read_now = ring_log_read_head("log_a", &buffer, sizeof(buffer), &read_total))) {
printf("%.*s", read_now, (char *)&buffer);
};
puts("");
ring_log_read_head_success("log_a");
}
I put a lot of care into this library, knowing that I would be using it for my projects.
* It has a pretty brutal test suite. I wouldn't feel confident using it, or sharing it, otherwise.
* It's written pedantic C99 with all the warnings enabled.
* It should be quite resistant to unexpected power-offs, but there is an issue to improve that.
I hope you find it useful!