-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
227 lines (184 loc) · 5.68 KB
/
Logger.cpp
File metadata and controls
227 lines (184 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "Logger.h"
#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <chrono>
Logger::Logger()
{
//ctor
}
void Logger::SetOutputFilename(std::string inName){
WriteFilename = inName;
}
void Logger::SetOutputFiletype(std::string inType){
WriteFiletype = inType;
}
void Logger::SetDelimiter(std::string inDel){
WriteDelimiter = inDel;
}
void Logger::SetDescription(std::string inDesc){
WriteDescription = inDesc;
}
void Logger::AddData(uint64_t inTime, std::vector<double> inData){
//Assemble filename if not already done
if(CompositeFilename.compare("") == 0){
//Assemble composite filename
AssembleFilePath();
}
//Push time onto storage vector
Timestamp.push_back(inTime);
//Add new vector to 2D vector
Data.push_back(inData);
}
void Logger::AddLabels(std::vector<std::string> inLabels){
//Assemble filename if not already done
if(CompositeFilename.compare("") == 0){
//Assemble composite filename
AssembleFilePath();
}
Labels = inLabels;
}
bool Logger::WriteToFile(){
//Open file
std::ofstream outFile(CompositeFilename);
std::cout << "Writing data to file...";
//Write Description
if(WriteDescription.compare("No Description") == 0){
if(Desc_WriteDateTime){ //No description, only time
time_t _tm =time(NULL );
struct tm * curtime = localtime ( &_tm );
outFile << asctime(curtime) << "\n";
}
}else{
if(Desc_WriteDateTime){
time_t _tm =time(NULL );
struct tm * curtime = localtime ( &_tm );
//remove newline from asctime
std::string temp = asctime(curtime);
temp.erase(temp.size() - 1);
outFile << temp << " : " << WriteDescription << "\n";
}else{
outFile << WriteDescription << "\n";
}
}
//Write Labels with delimiters
outFile << "Time" << WriteDelimiter; //Manually write time label
for (int i = 0; i < Labels.size(); i++){
outFile << Labels[i];
if(i < Labels.size()-1){
outFile << WriteDelimiter; //write delimiter
}else{
outFile << "\n"; //write end of line
}
}
//Write 2d vector
for (int i = 0; i < Data.size(); i++){
//Write time to each line (if time value exists for line)
if(i < Timestamp.size()){
outFile << Timestamp[i] << WriteDelimiter;
}else{ //if time value does not exist, write a blank space so columns line up
outFile << WriteDelimiter;
}
for (int j = 0; j < Data[i].size(); j++){
outFile << Data[i][j];
if(j < Data[i].size()-1){
outFile << WriteDelimiter;
}else if( i == Data.size()-1 && j == Data[i].size()-1){
//Do nothing for last line
}else{
outFile << "\n";
}
}
}
//Close file
outFile.close();
std::cout << "Complete\n";
return true;
}
//Test functions
void Logger::test(){
std::cout << "in Test Mode\n";
SetOutputFilename("_test_Data");
SetOutputFiletype("csv");
SetDelimiter(",");
SetDescription("This is a test file");
AddLabels({"A","B","C","D","E","F","G","H","I","J"});
//Create 10x10 2d vector
int len_2dV = 10; //length of 2d vector
for (int i = 0; i < len_2dV; i++){
AddData(nanos(),testVector(10));
}
WriteToFile();
//Print test vector
//print2DVector(Data);
}
void Logger::print2DVector(std::vector<std::vector<double>> inVec){ //Print 2d vector
std::cout << "2D printed vector:\n";
for (int i = 0; i < inVec.size(); i++)
{
for (int j = 0; j < inVec[i].size(); j++)
{
std::cout << inVec[i][j] << ",";
}
std::cout << "\n";
}
std::cout << "END 2D printed vector\n";
}
std::vector<double> Logger::testVector(int inLen){ //Create test vector of length inLen
std::vector<double> outVect;
int rnum = rand() % 100 + 1;
for (int i = 0; i < inLen; i++){
outVect.push_back((double)(i+rnum)+0.1);
}
//Print out vector
std::cout << "Created Vector:";
for (int i=0; i<outVect.size(); ++i){
std::cout << outVect[i] << ' ';
}
std::cout << "\n";
return outVect;
}
///Private functions
int Logger::FileExists(std::string inFile,std::string inExt){
std::ifstream cFile;
std::ifstream dFile;
//Check zero cases
cFile.open(inFile+"."+inExt);
dFile.open(inFile+"0."+inExt);
if(!cFile && !dFile){
//Original file does not exist (or is not readable)
cFile.close();
dFile.close();
return 0;
}
//clear dFile, we do not need it anymore. Leave cFile open
dFile.close();
//counter
int cnt = 0;
//Cycle through files until we find first available name
while(cFile || cnt == 0){
cnt++;
cFile.close(); //Close first open file
std::string scnt = std::to_string(cnt);
cFile.open(inFile+scnt+"."+inExt);
}
cFile.close();
return cnt;
}
void Logger::AssembleFilePath(){
int fileNum = FileExists(WriteFilename,WriteFiletype);
if(fileNum == 0 && Filename_hideZero){
CompositeFilename = WriteFilename + "." + WriteFiletype;
}else{
CompositeFilename = WriteFilename + std::to_string(fileNum) + "." + WriteFiletype;
}
std::cout << "Log output filename (program directory): " << CompositeFilename << "\n";
}
uint64_t Logger::nanos()
{
uint64_t ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::
now().time_since_epoch()).count();
return ns;
}