2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Revision:
31:20a95043adb0
Parent:
30:ed791f1f7f7d
Child:
32:eb673f6f5734
--- 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
 }