comparison examples/10-Instruments/d-box/logger.cpp @ 464:8fcfbfb32aa0 prerelease

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