2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
Logger.cpp@31:20a95043adb0, 2018-12-27 (annotated)
- Committer:
- shimniok
- Date:
- Thu Dec 27 00:42:35 2018 +0000
- Revision:
- 31:20a95043adb0
- Parent:
- 30:ed791f1f7f7d
- Child:
- 32:eb673f6f5734
implemented unique logfile naming
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 24:a7f92dfc5310 | 1 | #include "Logger.h" |
shimniok | 31:20a95043adb0 | 2 | #include <stdio.h> |
shimniok | 31:20a95043adb0 | 3 | #include <algorithm> |
shimniok | 31:20a95043adb0 | 4 | #include <vector> |
shimniok | 31:20a95043adb0 | 5 | #include <iterator> |
shimniok | 31:20a95043adb0 | 6 | #include <string> |
shimniok | 24:a7f92dfc5310 | 7 | |
shimniok | 31:20a95043adb0 | 8 | Logger::Logger() { |
shimniok | 31:20a95043adb0 | 9 | _file = NULL; |
shimniok | 29:cb2f55fbfe9c | 10 | _fp = NULL; |
shimniok | 29:cb2f55fbfe9c | 11 | } |
shimniok | 29:cb2f55fbfe9c | 12 | |
shimniok | 29:cb2f55fbfe9c | 13 | |
shimniok | 29:cb2f55fbfe9c | 14 | void Logger::start() { |
shimniok | 31:20a95043adb0 | 15 | if (_fp == NULL) { |
shimniok | 31:20a95043adb0 | 16 | DIR *d; |
shimniok | 31:20a95043adb0 | 17 | char file[24]; |
shimniok | 31:20a95043adb0 | 18 | std::vector<string> fl; |
shimniok | 31:20a95043adb0 | 19 | dirent *p; |
shimniok | 31:20a95043adb0 | 20 | |
shimniok | 31:20a95043adb0 | 21 | if ((d = opendir("/log")) != NULL) { |
shimniok | 31:20a95043adb0 | 22 | while ((p = readdir(d)) != NULL) { |
shimniok | 31:20a95043adb0 | 23 | fl.push_back(string(p->d_name)); |
shimniok | 31:20a95043adb0 | 24 | } |
shimniok | 31:20a95043adb0 | 25 | closedir(d); |
shimniok | 31:20a95043adb0 | 26 | |
shimniok | 31:20a95043adb0 | 27 | // Open a new unique log file of the form NNNN.csv |
shimniok | 31:20a95043adb0 | 28 | for (int i=0; i <= 9999; i++) { |
shimniok | 31:20a95043adb0 | 29 | sprintf(file, "%04d.CSV", i); // generate filename |
shimniok | 31:20a95043adb0 | 30 | string x(file); // create string version |
shimniok | 31:20a95043adb0 | 31 | // Is this filename NOT listed in the directory? |
shimniok | 31:20a95043adb0 | 32 | if (std::find(fl.begin(), fl.end(), x) == fl.end()) { |
shimniok | 31:20a95043adb0 | 33 | x.insert(0, "/log/"); // prepend the correct path |
shimniok | 31:20a95043adb0 | 34 | if ((_fp = fopen(x.c_str(), "w")) == NULL) { |
shimniok | 31:20a95043adb0 | 35 | fprintf(stdout, "%s: cannot open\n", file); |
shimniok | 31:20a95043adb0 | 36 | } else { |
shimniok | 31:20a95043adb0 | 37 | fprintf(stdout, "%s: opened\n", file); |
shimniok | 31:20a95043adb0 | 38 | } |
shimniok | 31:20a95043adb0 | 39 | break; |
shimniok | 31:20a95043adb0 | 40 | }// if |
shimniok | 31:20a95043adb0 | 41 | }// for |
shimniok | 31:20a95043adb0 | 42 | }// if |
shimniok | 31:20a95043adb0 | 43 | } // if |
shimniok | 29:cb2f55fbfe9c | 44 | } |
shimniok | 29:cb2f55fbfe9c | 45 | |
shimniok | 29:cb2f55fbfe9c | 46 | |
shimniok | 29:cb2f55fbfe9c | 47 | void Logger::stop() { |
shimniok | 29:cb2f55fbfe9c | 48 | if (_fp && fclose(_fp) != EOF) { |
shimniok | 29:cb2f55fbfe9c | 49 | _fp = NULL; |
shimniok | 29:cb2f55fbfe9c | 50 | } |
shimniok | 29:cb2f55fbfe9c | 51 | } |
shimniok | 29:cb2f55fbfe9c | 52 | |
shimniok | 29:cb2f55fbfe9c | 53 | |
shimniok | 29:cb2f55fbfe9c | 54 | bool Logger::enabled() { |
shimniok | 29:cb2f55fbfe9c | 55 | return (_fp != NULL); |
shimniok | 24:a7f92dfc5310 | 56 | } |
shimniok | 24:a7f92dfc5310 | 57 | |
shimniok | 24:a7f92dfc5310 | 58 | |
shimniok | 30:ed791f1f7f7d | 59 | void Logger::log_gps(GpsData d) { |
shimniok | 29:cb2f55fbfe9c | 60 | if (enabled()) { |
shimniok | 29:cb2f55fbfe9c | 61 | fprintf(_fp, "G,%llu,%3.9f,%3.9f,%3.1f,%2.1f,%2.1f,%d\n", |
shimniok | 30:ed791f1f7f7d | 62 | d.timestamp, |
shimniok | 30:ed791f1f7f7d | 63 | d.latitude, |
shimniok | 30:ed791f1f7f7d | 64 | d.longitude, |
shimniok | 30:ed791f1f7f7d | 65 | d.course, |
shimniok | 30:ed791f1f7f7d | 66 | d.speed, |
shimniok | 30:ed791f1f7f7d | 67 | d.hdop, |
shimniok | 30:ed791f1f7f7d | 68 | d.svcount |
shimniok | 29:cb2f55fbfe9c | 69 | ); |
shimniok | 29:cb2f55fbfe9c | 70 | } |
shimniok | 24:a7f92dfc5310 | 71 | } |
shimniok | 24:a7f92dfc5310 | 72 | |
shimniok | 24:a7f92dfc5310 | 73 | |
shimniok | 30:ed791f1f7f7d | 74 | void Logger::log_sensors(SensorData d) { |
shimniok | 30:ed791f1f7f7d | 75 | if (enabled()) { |
shimniok | 30:ed791f1f7f7d | 76 | fprintf(_fp, "S,%llu,%u,%d,%d,%d\n", |
shimniok | 30:ed791f1f7f7d | 77 | d.timestamp, |
shimniok | 30:ed791f1f7f7d | 78 | d.encoder, |
shimniok | 30:ed791f1f7f7d | 79 | d.gyro[0], |
shimniok | 30:ed791f1f7f7d | 80 | d.gyro[1], |
shimniok | 30:ed791f1f7f7d | 81 | d.gyro[2] |
shimniok | 30:ed791f1f7f7d | 82 | ); |
shimniok | 30:ed791f1f7f7d | 83 | } |
shimniok | 30:ed791f1f7f7d | 84 | } |
shimniok | 30:ed791f1f7f7d | 85 | |
shimniok | 30:ed791f1f7f7d | 86 | |
shimniok | 24:a7f92dfc5310 | 87 | void Logger::log_estimation() { |
shimniok | 29:cb2f55fbfe9c | 88 | if (enabled()) { |
shimniok | 24:a7f92dfc5310 | 89 | } |
shimniok | 24:a7f92dfc5310 | 90 | } |