[info] Logging on esp32 using ring_log

edmund-huber
Posts: 15
Joined: Wed Apr 12, 2017 8:30 pm

[info] Logging on esp32 using ring_log

Postby edmund-huber » 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:

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");


And then read all the entries out:

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");
}


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!

BuddyCasino
Posts: 215
Joined: Sun Jun 19, 2016 12:00 am

Re: [info] Logging on esp32 using ring_log

Postby BuddyCasino » Wed Jun 07, 2017 5:38 am

Thanks! Just yesterday I noticed your jsonex library, really nice stuff.

edmund-huber
Posts: 15
Joined: Wed Apr 12, 2017 8:30 pm

Re: [info] Logging on esp32 using ring_log

Postby edmund-huber » Wed Jun 07, 2017 6:49 am

Thank you, that means a lot to me. While jsonex pretty much works in its current state, I found it a little too handicapping in the end for anything more than very simple JSON. If you want to look at all items in a list in JSON for example, it's not really going to help you with that. I'm in the middle of a rewrite to make it SAX-like, while retaining the important features - no allocations, small code size.

Who is online

Users browsing this forum: No registered users and 1 guest