2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Files at this revision

API Documentation at this revision

Comitter:
shimniok
Date:
Thu Dec 27 00:42:35 2018 +0000
Parent:
30:ed791f1f7f7d
Child:
32:eb673f6f5734
Commit message:
implemented unique logfile naming

Changed in this revision

Logger.cpp Show annotated file Show diff for this revision Revisions of this file
Logger.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Logger.cpp	Wed Dec 26 19:34:17 2018 +0000
+++ b/Logger.cpp	Thu Dec 27 00:42:35 2018 +0000
@@ -1,13 +1,46 @@
 #include "Logger.h"
+#include <stdio.h>
+#include <algorithm>
+#include <vector>
+#include <iterator>
+#include <string>
 
-Logger::Logger(const char *file) {
-    _file = file;
+Logger::Logger() {
+    _file = NULL;
     _fp = NULL;
 }
 
 
 void Logger::start() {
-    if (_fp == NULL) _fp = fopen(_file, "a");
+    if (_fp == NULL) {
+        DIR *d;
+        char file[24];
+        std::vector<string> fl;
+        dirent *p;
+        
+        if ((d = opendir("/log")) != NULL) {
+            while ((p = readdir(d)) != NULL) {
+                fl.push_back(string(p->d_name));
+            }
+            closedir(d);
+            
+            // Open a new unique log file of the form NNNN.csv
+            for (int i=0; i <= 9999; i++) {
+                sprintf(file, "%04d.CSV", i);   // generate filename
+                string x(file);                 // create string version
+                // Is this filename NOT listed in the directory?
+                if (std::find(fl.begin(), fl.end(), x) == fl.end()) {
+                    x.insert(0, "/log/"); // prepend the correct path
+                    if ((_fp = fopen(x.c_str(), "w")) == NULL) {
+                        fprintf(stdout, "%s: cannot open\n", file);
+                    } else {
+                        fprintf(stdout, "%s: opened\n", file);
+                    }
+                    break;
+                }// if
+            }// for
+        }// if
+    } // if
 }
 
 
--- a/Logger.h	Wed Dec 26 19:34:17 2018 +0000
+++ b/Logger.h	Thu Dec 27 00:42:35 2018 +0000
@@ -6,7 +6,7 @@
 
 class Logger {
 public:
-    Logger(const char *file);
+    Logger();
     void start();
     void stop();
     bool enabled();
@@ -15,7 +15,7 @@
     void log_estimation();
 
 private:
-    const char *_file;
+    char *_file;
     FILE *_fp;
 };
 
--- a/main.cpp	Wed Dec 26 19:34:17 2018 +0000
+++ b/main.cpp	Thu Dec 27 00:42:35 2018 +0000
@@ -48,7 +48,7 @@
 ///////////////////////////////////////////////////////////////////////////////
 // Logging
 EventQueue logQueue(16 * EVENTS_EVENT_SIZE);
-Logger logger("/log/test.csv");
+Logger logger;
 
 ///////////////////////////////////////////////////////////////////////////////
 // Updater