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