Skip to content

Simple data logging

The output data generated by the example files can easily be routed into log files for storing of the data. The following code sniplet shows what the user would have to do in principle to generate a log file, stored in the current working directory, on each example execution. The name of the log file is derived from the current time stamp at the time of execution. The code sniplet is valid for examples compiled for PC side (TARGET=PC, see above). If the example is run on the MCU, the data is provided via virtual COM port and the user can use any terminal program to access and store the data.

Note that the code snippet does not contain any exception handling, such as checking file overwrite or if fopen returns without error.

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    FILE *log_fd;
    char *logfile = malloc(28);
    time_t now;
    struct tm *tm;

    now = time(0);
    tm = localtime(&now);
    sprintf(logfile, "logfile_%04d%02d%02d_%02d%02d%02d.log", 
        tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
    log_fd = fopen(logfile, "w");

    ...

    while(CONDITION)
    {
        ...
        bmaXYZ_get_data(&data);
        fprintf(log_fd, "%d, %d, %d", data.x, data.y, data.z);
    }

    fclose(log_fd);
    return 0;
}