13int tsLogger::initLogger()
16 logBuffer = (
char*) malloc(logBufferSize);
23 fopen_s(&(this->logFilePtr), fileName,
"w+");
24 setvbuf(logFilePtr, logBuffer, _IOLBF, logBufferSize);
27 fopen_s(&(this->logFilePtr), fileName,
"wb+");
32 dataVecLen = dataVector.size();
34 canAddPreamble =
true;
35 canAddDataHeader =
true;
36 isLogggingStart =
false;
43 logUnixTime = time(NULL);
45 localtime_s(&localTime, &logUnixTime);
47 snprintf(fileName,
fileNameLength,
"%s\\%s_%04d-%02d-%02d-%02d%02d%02d.csv",
50 localTime.tm_year+1900,
64 logUnixTime = time(NULL);
66 localtime_s(&localTime, &logUnixTime);
68 snprintf(fileName,
fileNameLength,
"%s\\%s_%04d-%02d-%02d-%02d%02d%02d.csv",
71 localTime.tm_year+1990,
77 fileType = logFileType;
85 logUnixTime = time(NULL);
95 logUnixTime = time(NULL);
97 fileType = logFileType;
105 logUnixTime = time(NULL);
107 fileType = logFileType;
108 logBufferSize = logBufferMaxSize;
115 strcpy_s(pathDestination, pathDestinationLength, this->fileName);
125 std::replace(appendString.begin(), appendString.end(),
'\n',
' ');
126 this->preamble.append(appendString);
127 this->preamble.push_back(
'\n');
128 this->numHeaderLines++;
135 if (canAddPreamble ==
true && this->canAddDataHeader ==
true && isLogggingStart ==
false) {
136 this->numHeaderLines++;
137 fprintf(this->logFilePtr,
"%05d,%lld\n%s", this->numHeaderLines, (
long long)this->logUnixTime, this->preamble.c_str());
138 canAddPreamble =
false;
144 if (this->canAddDataHeader ==
true && isLogggingStart ==
false) {
145 if (this->canAddPreamble ==
false) {
146 fseek(this->logFilePtr, 0, SEEK_SET);
147 this->numHeaderLines++;
148 fprintf(this->logFilePtr,
"%05d", this->numHeaderLines);
149 fseek(this->logFilePtr, 0, SEEK_END);
151 for (
auto it = std::begin(this->dataVector); it != std::end(this->dataVector); ) {
153 fprintf(this->logFilePtr,
"%s", it->dataName.c_str());
154 if (++it != this->dataVector.end()) {
155 fprintf(this->logFilePtr,
",");
158 fprintf(this->logFilePtr,
"\n");
162 this->canAddDataHeader =
false;
168 if (isLogggingStart ==
false) {
169 isLogggingStart =
true;
176 if (isLogggingStart !=
false) {
179 for (
auto it = std::begin(this->dataVector); it != std::end(this->dataVector); ) {
184 dataPtr = std::visit([](
auto&& arg) ->
smartDataPtr {
return arg;}, it->dataPtr);
185 std::visit([
this](
auto&& arg) {
186 using dataPtrType = std::decay_t<
decltype(arg)>;
189 if constexpr (std::is_same_v<dataPtrType, int*>)
190 fprintf(this->logFilePtr,
"%d", *arg);
192 else if constexpr (std::is_same_v<dataPtrType, unsigned int*>)
193 fprintf(this->logFilePtr,
"%u", *arg);
195 else if constexpr (std::is_same_v<dataPtrType, long*>)
196 fprintf(this->logFilePtr,
"%ld", *arg);
198 else if constexpr (std::is_same_v<dataPtrType, unsigned long*>)
199 fprintf(this->logFilePtr,
"%lu", *arg);
201 else if constexpr (std::is_same_v<dataPtrType, long long*>)
202 fprintf(this->logFilePtr,
"%lld", *arg);
204 else if constexpr (std::is_same_v<dataPtrType, unsigned long long*>)
205 fprintf(this->logFilePtr,
"%llu", *arg);
207 else if constexpr (std::is_same_v<dataPtrType, float*>)
208 fprintf(this->logFilePtr,
"%f", *arg);
210 else if constexpr (std::is_same_v<dataPtrType, double*>)
211 fprintf(this->logFilePtr,
"%f", *arg);
213 else if constexpr (std::is_same_v<dataPtrType, char*>)
214 fprintf(this->logFilePtr,
"%c", *arg);
216 else if constexpr (std::is_same_v<dataPtrType, unsigned char*>)
217 fprintf(this->logFilePtr,
"%hhu", *arg);
219 else if constexpr (std::is_same_v<dataPtrType, bool*>)
220 fprintf(this->logFilePtr,
"%d", *arg);
223 static_assert(always_false_v<dataPtrType>,
"tsLogger: FATAL ERROR: attempting to log unsupported data type!");
226 if (++it != this->dataVector.end()) {
227 fprintf(this->logFilePtr,
",");
230 fprintf(this->logFilePtr,
"\n");
248 isLogggingStart =
false;
void appendToPreamble(std::string appendString)
Appends a line of text to the preamble block of the log file. Replaces new line characters with space...
void startLogger()
Starts the logger and locks out the ability to add preambles, headers and new data points.
void getFilePath(char *pathDestination, size_t pathDestinationLength)
Allows the user to read the file path of the log file.
size_t getNumDataPoints()
Returns the total number of data points being tracked for logging by the tsLogger class object.
void addDataHeader()
Add the data header (consisting of datapoint names separated by commas) to the header file....
void addPreamble()
Adds the preamble to the log file-can only be added once and only be called before the addDataHeader ...
void logData()
Logs the data points being tracked as a line in the log file.
tsLogger()
Default constructor of the tsLogger class. Uses default values to initialize all object properties.
constexpr bool always_false_v
std::variant< int *, unsigned int *, long *, unsigned long *, long long *, unsigned long long *, float *, double *, char *, unsigned char *, bool * > smartDataPtr
This varriant datatype governs the supported data points that can be tracked.
enum _tsLogFileType tsLogFileType
The file-types supported by tsLogger. [BINARY support coming in the future].