Create a new log file every time on startup
authorBernie Innocenti <bernie@codewiz.org>
Fri, 25 Mar 2011 20:25:34 +0000 (16:25 -0400)
committerBernie Innocenti <bernie@codewiz.org>
Fri, 25 Mar 2011 22:48:04 +0000 (18:48 -0400)
rmslog/main.c

index 206f421c9a887e33c32aa8ee951e003ee0624a7c..95d988ce0016d8e8a5bd0570aa644f2427584be9 100644 (file)
@@ -1,9 +1,6 @@
-/**
- * \file
- *
- * \author Bernie Innocenti <bernie@codewiz.org>
- *
- * \brief Serial data logger for RMS
+/*
+ * Copyleft 2011 Bernie Innocenti <bernie@codewiz.org>
+ * 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 <drv/timer.h>
 #include <kern/proc.h>
 
-#include <string.h>
+#include <stdio.h>  // sprintf()
+#include <string.h> // 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)
                {