From: Bernie Innocenti Date: Fri, 25 Mar 2011 20:25:34 +0000 (-0400) Subject: Create a new log file every time on startup X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=2d09396ef3609269e9a42b40a84d53ebca434646;p=rmslog.git Create a new log file every time on startup --- diff --git a/rmslog/main.c b/rmslog/main.c index 206f421..95d988c 100644 --- a/rmslog/main.c +++ b/rmslog/main.c @@ -1,9 +1,6 @@ -/** - * \file - * - * \author Bernie Innocenti - * - * \brief Serial data logger for RMS +/* + * Copyleft 2011 Bernie Innocenti + * Serial data logger for RMS * * This application records all incoming data from an RS-232 serial port * to a file on a FAT-formatted SD card. It can be used to log kernel @@ -12,7 +9,8 @@ // Configuration #define FILE_NAME "log.txt" -#define FILE_OVERWRITE 1 +#define FILE_NAME_PATTERN "log%04d.txt" +#define FILE_OVERWRITE 0 #define FILE_PREALLOC_SIZE 102400 @@ -33,7 +31,8 @@ #include #include -#include +#include // sprintf() +#include // strcmp() // Our serial port @@ -162,24 +161,39 @@ int main(void) init(); size_t size; - char buf[256]; bool sync_pending = false; + char buf[256]; - /* create file */ - if(!fat_create_file(dd, FILE_NAME, &dir_entry)) + #if FILE_OVERWRITE { - #if FILE_OVERWRITE - fat_delete_file(fs, &dir_entry); - fat_create_file(dd, FILE_NAME, &dir_entry); - #endif + if(!fat_create_file(dd, FILE_NAME, &dir_entry)) + { + fat_delete_file(fs, &dir_entry); + fat_create_file(dd, FILE_NAME, &dir_entry); + } + + fd = open_file_in_dir(fs, dd, FILE_NAME); } + #else /* Generate a new filename */ + { + int n; + char filename[8+1+3+1]; + + for (n = 0; /**/; ++n) + { + sprintf(filename, FILE_NAME_PATTERN, n); + if (fat_create_file(dd, filename, &dir_entry)) + break; + } - fd = open_file_in_dir(fs, dd, FILE_NAME); + fd = open_file_in_dir(fs, dd, filename); + } + #endif - /* preallocate file */ #if FILE_PREALLOC_SIZE { LED_ON(); + memset(buf, ' ', sizeof(buf)); for (int i = 0; i < (int)(FILE_PREALLOC_SIZE / sizeof(buf)); ++i) fat_write_file(fd, (void *)buf, sizeof(buf)); @@ -189,7 +203,7 @@ int main(void) } #endif - while (1) + for (;;) { if ((size = ser_read(&ser, buf, sizeof(buf))) != 0) {