-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCS-Logger.hpp
170 lines (133 loc) · 4.17 KB
/
DCS-Logger.hpp
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
#pragma once
#include <string>
#include <fstream>
#include <iostream>
#include <filesystem>
#include <algorithm>
#include <vector>
#include <chrono>
class Logger
{
public:
Logger::Logger() : totalTime(0.0), frequency(1.0){};
Logger::Logger(std::string logLocation, std::vector<std::string>, bool msMode_ = false, int frequency = 1); // logLocation is full file path of log
void Logger::AddToLogger(int addToLoggerInt); // Log a value of type int
void Logger::AddToLogger(double addToLoggerDouble); // Log a value of type double
void Logger::AddToLogger(std::string addToLoggerString); // Log a value of type std::string
void Logger::AddToLogger(bool addToLoggerBool); // Log a value of type bool
void Logger::SetCustomDelimiter(char newDelimiter); // Set a custom value delimiter
void Logger::BeginFrame(double dt, bool loggingOnOff = true); // loggingOnOff : false off, true on
void Logger::BeginFrame(bool loggingOnOff = true); // Initialize the frame this way for normal procedure, unless at very low framerate
void Logger::EndFrame(); // End the frame
void Logger::Close(); // Close the logger and free resources
private:
double totalTime;
std::chrono::steady_clock::time_point startTime;
std::ofstream fout;
std::vector<std::string> namesList;
long frames;
bool onoff;
bool msMode;
int frequency;
bool offFrame;
char delimiter;
};
inline Logger::Logger(std::string fileName, std::vector<std::string> namesList, bool msMode_, int frequency_)
{
offFrame = false;
this->delimiter = ',';
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
std::string timeString = buf;
std::replace(timeString.begin(), timeString.end(), ':', '-');
//debug_log("time stamp: %s", timeString.c_str());
std::string fullFileName = fileName + "." + timeString + ".csv";
fout.open(fullFileName, std::ios_base::out);
this->frequency = frequency_;
if (!fout.is_open()) printf("\nFile could not be written: %s", fullFileName.c_str());
this->namesList = namesList;
totalTime = 0.0;
this->msMode = msMode_;
if (this->msMode) startTime = std::chrono::steady_clock::now();
fout << "Time" << this->delimiter;
fout << "Frames" << this->delimiter;
for (int i = 0; i < this->namesList.size(); i++)
{
fout << namesList[i] << this->delimiter;
}
fout << "\n";
frames = 0;
}
inline void Logger::BeginFrame(double dt, bool logginOnOff)
{
offFrame = true;
totalTime += dt;
frames++;
onoff = logginOnOff;
if (!(frames % this->frequency))
offFrame = false;
if (!logginOnOff || offFrame == 0) return;
if (this->msMode)
{
fout << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - this->startTime).count() / 1000.0 << this->delimiter;
}
else
{
fout << totalTime << this->delimiter;
}
fout << frames << this->delimiter;
}
inline void Logger::BeginFrame(bool loggingOnOff)
{
offFrame = true;
frames++;
onoff = loggingOnOff;
if (!(frames % this->frequency))
{
offFrame = false;
}
if (!loggingOnOff || offFrame) return;
if (this->msMode)
{
fout << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - this->startTime).count() / 1000.0 << this->delimiter;
}
fout << frames << this->delimiter;
}
inline void Logger::AddToLogger(int addToLoggerInt)
{
if (!onoff || offFrame) return;
fout << addToLoggerInt << this->delimiter;
}
inline void Logger::AddToLogger(double addToLoggerDouble)
{
if (!onoff || offFrame) return;
fout << addToLoggerDouble << this->delimiter;
}
inline void Logger::AddToLogger(std::string addToLoggerString)
{
if (!onoff || offFrame) return;
fout << addToLoggerString << this->delimiter;
}
inline void Logger::AddToLogger(bool addToLoggerBool)
{
if (!onoff || offFrame) return;
fout << addToLoggerBool << this->delimiter;
}
inline void Logger::SetCustomDelimiter(char delimiter_)
{
this->delimiter = delimiter_;
}
inline void Logger::EndFrame()
{
if (!onoff || offFrame) return;
fout << "\n";
}
inline void Logger::Close()
{
if (fout.is_open()) fout.close();
}