annotate projects/d-box/logger.cpp @ 233:18d03901f866 mergingClockSync

Turned gShouldStop into int
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 10 Apr 2016 03:20:52 +0200
parents 8a575ba3ab52
children
rev   line source
andrewm@0 1 /*
andrewm@0 2 * logger.cpp
andrewm@0 3 *
andrewm@0 4 * Created on: Aug 6, 2014
andrewm@0 5 * Author: VIctor Zappi and Andrew McPherson
andrewm@0 6 */
andrewm@0 7
andrewm@0 8 #include "logger.h"
andrewm@0 9
andrewm@0 10 // main extern vars
giuliomoro@233 11 extern int gShouldStop;
andrewm@0 12 extern int gVerbose;
andrewm@0 13
andrewm@0 14 // file nanme extern vars
andrewm@0 15 extern char gId;
andrewm@0 16 extern char gGroup;
andrewm@0 17
andrewm@0 18
andrewm@0 19 // logged extern vars
andrewm@0 20 extern int s0TouchNum;
andrewm@0 21 extern float s0Touches_[MAX_TOUCHES];
andrewm@0 22 extern float s0Size_[MAX_TOUCHES];
andrewm@0 23 extern int s0LastIndex;
andrewm@0 24
andrewm@0 25 extern int s1TouchNum;
andrewm@0 26 extern float s1Touches_[MAX_TOUCHES];
andrewm@0 27 extern float s1Size_[MAX_TOUCHES];
andrewm@0 28 extern int s1LastIndex;
andrewm@0 29
andrewm@0 30 extern int fsr;
andrewm@0 31
andrewm@0 32
andrewm@0 33
andrewm@0 34 string logPath = "/boot/uboot/instrumentLog";
andrewm@0 35 string logFileIncipit = "/datalog";
andrewm@0 36 string logFileName = "";
andrewm@0 37 ofstream logFile;
andrewm@0 38 timeval logTimeVal;
andrewm@0 39 unsigned long long logTimeOrig;
andrewm@0 40 int logCnt = 0; // counts how many lines so far
andrewm@0 41 int logCntSwap = 50; // how many log lines before closing and re-opening the file
andrewm@0 42
andrewm@0 43
andrewm@0 44 // create the log file, using incremental name convention
andrewm@0 45 int initLogLoop()
andrewm@0 46 {
andrewm@0 47 if(gVerbose==1)
andrewm@0 48 cout << "---------------->Init Log Thread" << endl;
andrewm@0 49
andrewm@0 50
andrewm@0 51 // transform chars into strings via stringstream objs
andrewm@0 52 stringstream id_ss, group_ss, freedom_ss;
andrewm@0 53 id_ss << gId;
andrewm@0 54 group_ss << gGroup;
andrewm@0 55
andrewm@0 56 int logNum = -1;
andrewm@0 57 int logMax = -1;
andrewm@0 58 int pathLen = logPath.length() + logFileIncipit.length() + 4; // + 4 is: "_", id, group, "_"
andrewm@0 59 glob_t globbuf;
andrewm@0 60
andrewm@0 61 // check how many log files are already there, and choose name according to this
andrewm@0 62 glob( (logPath + logFileIncipit + "*").c_str(), 0, NULL, &globbuf);
andrewm@0 63
andrewm@0 64 // cycle through all and find the highest index
andrewm@0 65 for(unsigned int i=0; i<globbuf.gl_pathc; i++)
andrewm@0 66 {
andrewm@0 67 // playing with 0-9 char digits, forming a number from 0 to 9999
andrewm@0 68 logNum = (globbuf.gl_pathv[i][pathLen]-48) * 1000; // 42 to 45 are the indices of the chars forming the file index
andrewm@0 69 logNum += (globbuf.gl_pathv[i][pathLen+1]-48) * 100;
andrewm@0 70 logNum += (globbuf.gl_pathv[i][pathLen+2]-48) * 10;
andrewm@0 71 logNum += globbuf.gl_pathv[i][pathLen+3]-48;
andrewm@0 72 if(logNum > logMax)
andrewm@0 73 logMax = logNum;
andrewm@0 74 }
andrewm@0 75 logNum = logMax + 1; // new index
andrewm@0 76
andrewm@0 77 globfree(&globbuf);
andrewm@0 78
andrewm@0 79 ostringstream numString;
andrewm@0 80 numString << setw (4) << setfill ('0') << logNum; // set integer with 4 figures
andrewm@0 81
andrewm@0 82 // here are the new names: PATH + DIR + INCIPIT + _ + id + group + freedom + _ + NUM (4figures) + _A.txt
andrewm@0 83 logFileName = logPath + logFileIncipit;
andrewm@0 84 logFileName += "_" + id_ss.str() + group_ss.str() + freedom_ss.str();
andrewm@0 85 logFileName += "_" + numString.str(); //static_cast<ostringstream*>( &(ostringstream() << logNum) )->str();
andrewm@0 86 logFileName += ".txt";
andrewm@0 87
andrewm@0 88
andrewm@0 89 // create new files
andrewm@0 90 FILE *fp_a = fopen(logFileName.c_str(), "wb");
andrewm@0 91 if(!fp_a)
andrewm@0 92 {
andrewm@0 93 dbox_printf("Cannot create files...\n");
andrewm@0 94 return 2;
andrewm@0 95 }
andrewm@0 96 fclose(fp_a);
andrewm@0 97
andrewm@0 98 // ready to append
andrewm@0 99 logFile.open(logFileName.c_str(), ios::out | ios::app);
andrewm@0 100
andrewm@0 101 dbox_printf("Logging on file %s\n", logFileName.c_str());
andrewm@0 102
andrewm@0 103 return 0;
andrewm@0 104 }
andrewm@0 105
andrewm@0 106
andrewm@0 107 void writeData(unsigned long long time)
andrewm@0 108 {
andrewm@0 109
andrewm@0 110 float fsr_ = ((float)(1799-fsr)/1799.0);
andrewm@0 111 logFile << time << "\t" // timestamp
andrewm@0 112 << s0TouchNum << "\t"; // sensor 0 touch count
andrewm@0 113 for(int i=0; i<MAX_TOUCHES; i++)
andrewm@0 114 logFile << s0Touches_[i] << "\t"; // sensor 0 touch pos x
andrewm@0 115 for(int i=0; i<MAX_TOUCHES; i++)
andrewm@0 116 logFile << s0Size_[i] << "\t"; // sensor 0 touch size
andrewm@0 117 logFile << s0LastIndex << "\t" // sensor 0 last index
andrewm@0 118 << fsr_ << "\t" // sensor 0 FSR pressure
andrewm@0 119 << s1TouchNum << "\t"; // sensor 1 touch count
andrewm@0 120 for(int i=0; i<MAX_TOUCHES; i++)
andrewm@0 121 logFile << s1Touches_[i] << "\t"; // sensor 1 touch pos x
andrewm@0 122 for(int i=0; i<MAX_TOUCHES; i++)
andrewm@0 123 logFile << s1Size_[i] << "\t"; // sensor 1 touch size
andrewm@0 124 logFile << s1LastIndex << "\t" // sensor 1 last index
andrewm@0 125 //... AND SO ON
andrewm@0 126 << "\n";
andrewm@0 127
andrewm@0 128 //dbox_printf("%d\n", s0LastIndex);
andrewm@0 129 //dbox_printf("s0TouchNum: %d\t s0Touches[0]: %f\t s0Size[0]: %f\t s0LastIndex: %d\n", s0TouchNum, s0Touches_[0], s0Size_[0], s0LastIndex);
andrewm@0 130
andrewm@0 131 }
andrewm@0 132
andrewm@0 133 void logData(unsigned long long time)
andrewm@0 134 {
andrewm@0 135 // if it's time to change write-file
andrewm@0 136 if(logCnt >= logCntSwap)
andrewm@0 137 {
andrewm@0 138 logFile.close(); // close file, dump stream
andrewm@0 139 logCnt = 0; // ready for another whole round
andrewm@0 140
andrewm@0 141 // open again, ready to append
andrewm@0 142 logFile.open(logFileName.c_str(), ios::out | ios::app);
andrewm@0 143 }
andrewm@0 144
andrewm@0 145 writeData(time);
andrewm@0 146
andrewm@0 147 logCnt++;
andrewm@0 148 }
andrewm@0 149
andrewm@0 150
andrewm@0 151
andrewm@0 152
andrewm@0 153 void *logLoop(void *)
andrewm@0 154 {
andrewm@0 155 set_realtime_priority(10);
andrewm@0 156
andrewm@0 157 if(gVerbose==1)
andrewm@0 158 dbox_printf("_________________Log Thread!\n");
andrewm@0 159
andrewm@0 160 // get time reference
andrewm@0 161 gettimeofday(&logTimeVal, NULL);
andrewm@0 162 logData(0);
andrewm@0 163
andrewm@0 164 logTimeOrig = logTimeVal.tv_usec;
andrewm@0 165 logTimeOrig *= 0.001; // from usec to msec
andrewm@0 166 logTimeOrig += logTimeVal.tv_sec*1000; // from sec to msec
andrewm@0 167
andrewm@0 168 usleep(5000);
andrewm@0 169
andrewm@0 170 while(!gShouldStop)
andrewm@0 171 {
andrewm@0 172 gettimeofday(&logTimeVal, NULL);
andrewm@0 173 unsigned long long currentTime = logTimeVal.tv_usec;
andrewm@0 174 currentTime *= 0.001; // from usec to msec
andrewm@0 175 currentTime += logTimeVal.tv_sec*1000; // from sec to msec
andrewm@0 176
andrewm@0 177 logData(currentTime-logTimeOrig);
andrewm@0 178
andrewm@0 179 usleep(5000);
andrewm@0 180 }
andrewm@0 181
andrewm@0 182 if(logFile!=NULL)
andrewm@0 183 logFile.close();
andrewm@0 184
andrewm@0 185 dbox_printf("log thread ended\n");
andrewm@0 186
andrewm@0 187 return (void *)0;
andrewm@0 188 }