tsLogger
A C++17 data logger for timeseries and snapshot data
Loading...
Searching...
No Matches
tsLogger.h
Go to the documentation of this file.
1#pragma once
2#ifndef _TSLOGGER_H
3#define _TSLOGGER_H
4#include <cstdio>
5#include <string>
6#include <iostream>
7#include <vector>
8#include <variant>
9#include <algorithm>
10#include <ctime>
11
12#define defaultFilePath "C:\\data"
13#define defaultFileName "tsDataLog"
14#define defaultDataName "datapoint"
15#define fileNameLength 400
16#define defLogBufferSize 1000
17#define defHeaderBufSize 1000
18#define defDataBufSize 100
19
23typedef enum _tsLogFileType
24{
28
32using smartDataPtr = std::variant <int*, unsigned int*, \
33 long*, unsigned long*, \
34 long long*, unsigned long long*, \
35 float*, double*, \
36 char*, unsigned char*, \
37 bool* >;
38
43 std::string dataName; // data name
44 smartDataPtr dataPtr; // variant pointer to data
45};
46
50class tsLogger {
51 // File properties
52 time_t logUnixTime;
53 char fileName[fileNameLength];
54 tsLogFileType fileType;
55 FILE* logFilePtr;
56
57
58 // Buffers
59 size_t logBufferSize;
60 char* logBuffer;
61 std::string preamble; // stores info like number of preamble+header lines, time of file creation
62 std::string tsHeader;
63 std::vector<tsDataPointer> dataVector;
64
65 // Logger object parameters
66 unsigned numHeaderLines;
67 size_t dataVecLen;
68 bool canAddPreamble, canAddDataHeader, isLogggingStart;
69
70 // Private member functions
71
76 int initLogger();
77
78 // Public member functions
79public:
80 // tsLogger Constructors
81
86 tsLogger();
87
93 tsLogger(tsLogFileType logFileType);
94
100 tsLogger(const char* logFileName);
101
108 tsLogger(const char* logFileName, tsLogFileType logFileType);
109
117 tsLogger(const char* logFileName, tsLogFileType logFileType, size_t logBufferMaxSize);
118
119 // Functions to read logger state
120
126 void getFilePath(char* pathDestination, size_t pathDestinationLength);
127
132 size_t getNumDataPoints();
133
134 // Functions/templates to modify logger state
135
143 template <typename T> unsigned trackDataPoint(T* dataPointer, const std::string& dataName)
144 {
145 tsDataPointer newData;
146 if (canAddDataHeader != true) {
147 return -1; // data cannot be added after the data header has been written to file.
148 }
149 if (isLogggingStart != false) {
150 return -2; // data can't be added after logging has started.
151 }
152 if (dataPointer != nullptr) {
153 if (dataName.empty()) {
154 newData.dataName = defaultDataName + std::to_string(this->dataVecLen);
155 }
156 else {
157 newData.dataName = dataName;
158 // sanity check name so there is no trouble when eventually inserting names in the CSV header
159 std::replace(newData.dataName.begin(), newData.dataName.end(), '\n', ' '); // replace all new line characters with spaces
160 std::replace(newData.dataName.begin(), newData.dataName.end(), ',' , ' '); // replace all comma characters with spaces
161 std::replace(newData.dataName.begin(), newData.dataName.end(), '\r', ' '); // replace all carriage return characters with spaces
162 std::replace(newData.dataName.begin(), newData.dataName.end(), '\0', ' '); // replace all EOF characters with spaces
163 }
164 newData.dataPtr = dataPointer;
165
166 this->dataVector.push_back(newData);
167 this->dataVecLen = this->dataVector.size();
168 }
169 return this->dataVecLen;
170 }
171
177 void appendToPreamble(std::string appendString);
178
182 void addPreamble();
183
188 void addDataHeader();
189
190 // Functions to log data
191
195 void startLogger();
196
200 void logData();
201
202 // tsLogger Destructor
203 ~tsLogger();
204};
205
206#endif // !_TSLOGGER_H
207
The core class of the library is the tsLogger class. Multiple simultaneous class objects are supporte...
Definition: tsLogger.h:50
void appendToPreamble(std::string appendString)
Appends a line of text to the preamble block of the log file. Replaces new line characters with space...
Definition: tsLogger.cpp:123
unsigned trackDataPoint(T *dataPointer, const std::string &dataName)
Template member function that appends the data point to the list of tracked data.
Definition: tsLogger.h:143
void startLogger()
Starts the logger and locks out the ability to add preambles, headers and new data points.
Definition: tsLogger.cpp:166
void getFilePath(char *pathDestination, size_t pathDestinationLength)
Allows the user to read the file path of the log file.
Definition: tsLogger.cpp:113
size_t getNumDataPoints()
Returns the total number of data points being tracked for logging by the tsLogger class object.
Definition: tsLogger.cpp:118
void addDataHeader()
Add the data header (consisting of datapoint names separated by commas) to the header file....
Definition: tsLogger.cpp:142
void addPreamble()
Adds the preamble to the log file-can only be added once and only be called before the addDataHeader ...
Definition: tsLogger.cpp:131
void logData()
Logs the data points being tracked as a line in the log file.
Definition: tsLogger.cpp:173
tsLogger()
Default constructor of the tsLogger class. Uses default values to initialize all object properties.
Definition: tsLogger.cpp:41
Data pointer structure used to track datapoints, with datapoint name and pointer to data.
Definition: tsLogger.h:42
smartDataPtr dataPtr
Definition: tsLogger.h:44
std::string dataName
Definition: tsLogger.h:43
_tsLogFileType
The file-types supported by tsLogger. [BINARY support coming in the future].
Definition: tsLogger.h:24
@ _TSLOGGER_CSV
Definition: tsLogger.h:25
@ _TSLOGGER_BINARY
Definition: tsLogger.h:26
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.
Definition: tsLogger.h:37
#define fileNameLength
Definition: tsLogger.h:15
enum _tsLogFileType tsLogFileType
The file-types supported by tsLogger. [BINARY support coming in the future].
#define defaultDataName
Definition: tsLogger.h:14