WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
SimLogger.h
Go to the documentation of this file.
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16/*
17Logger header file
18
19Author: Alex Reynolds
20*/
21#include "configuration.h"
22
23#if CLOCKWERK_ALLOW_VECTOR
24#if CLOCKWERK_ALLOW_STD_STRING
25
26#ifndef LOGGING_SIM_LOGGER_H
27#define LOGGING_SIM_LOGGER_H
28
29#include <vector>
30#include <array>
31#include <string>
32
35#include "core/Matrix.hpp"
36
37namespace clockwerk {
38
39 enum vector_select_e {
40 BOOL,
41 INT,
42 UINT,
43 FLOAT,
44 DOUBLE,
45 STRING
46 };
47
48 class Executive;
49
58
60
67
71 class SimLogger : public GraphTreeObject {
72 public:
75 SimLogger(GraphTreeObject &parent, const std::string &filename="output.csv", unsigned int buffer_size=1);
76 virtual ~SimLogger() {close();}
77
79 unsigned int bufferSize() {return _buffer_size;}
80 int bufferSize(unsigned int buffer_size);
81
83 std::string filename() {return _filename;}
84 int filename(const std::string &name);
85
87 std::string outDir() {return _output_directory;}
88 int outDir(const std::string &directory);
89
93 virtual int setup(const std::string &file_name) {return NO_ERROR;}
94
102 int addParameter(GraphTreeObject &parameter, const std::string &param_name,
103 bool recursive = false);
104
112 int addParameter(const std::string &parameter, const std::string &param_name,
113 bool recursive = false);
114
122 virtual int lockForLogging();
123
127 virtual int log();
128
131 virtual int close();
132
133 int writeHeaders(const bool &val) {return writeHeaders((uint64)val);}
134 int writeHeaders(const int8 &val) {return writeHeaders((int64)val);}
135 int writeHeaders(const int16 &val) {return writeHeaders((int64)val);}
136 int writeHeaders(const int32 &val) {return writeHeaders((int64)val);}
137 int writeHeaders(const int64 &val) {_group_idx.push_back(-1); _headers.push_back(_current_name); return NO_ERROR;}
138 int writeHeaders(const uint8 &val) {return writeHeaders((uint64)val);}
139 int writeHeaders(const uint16 &val) {return writeHeaders((uint64)val);}
140 int writeHeaders(const uint32 &val) {return writeHeaders((uint64)val);}
141 int writeHeaders(const uint64 &val) {_group_idx.push_back(-1); _headers.push_back(_current_name); return NO_ERROR;}
142 int writeHeaders(const double &val) {_group_idx.push_back(-1); _headers.push_back(_current_name); return NO_ERROR;}
143 int writeHeaders(const GraphTreeObject &val);
144 int writeHeaders(void* val) {_group_idx.push_back(-1); _headers.push_back(_current_name); return NO_ERROR;}
145 int writeHeaders(const std::string &val) {_group_idx.push_back(-1); _headers.push_back(_current_name); return NO_ERROR;}
146
147 template <typename T> int writeHeaders(const std::vector<T> &val);
148 template <typename T, long unsigned int N> int writeHeaders(const std::array<T, N> &val);
149
150 template <unsigned int R, unsigned int C> int writeHeaders(const Matrix<R, C> &val);
151
157 int writeToBuffer(const bool &val);
158 int writeToBuffer(const int8 &val) {return writeToBuffer((int64)val);}
159 int writeToBuffer(const int16 &val) {return writeToBuffer((int64)val);}
160 int writeToBuffer(const int32 &val) {return writeToBuffer((int64)val);}
161 int writeToBuffer(const int64 &val);
162 int writeToBuffer(const uint8 &val) {return writeToBuffer((uint64)val);}
163 int writeToBuffer(const uint16 &val) {return writeToBuffer((uint64)val);}
164 int writeToBuffer(const uint32 &val) {return writeToBuffer((uint64)val);}
165 int writeToBuffer(const uint64 &val);
166 int writeToBuffer(const float &val);
167 int writeToBuffer(const double &val);
168 int writeToBuffer(const std::string &val);
169 int writeToBuffer(void* val);
170 int writeToBuffer(GraphTreeObject &val);
171
175 template <typename T> int writeToBuffer(const std::vector<T> &val);
176 template <typename T, long unsigned int N> int writeToBuffer(const std::array<T, N> &val);
177
178 template <unsigned int R, unsigned int C> int writeToBuffer(const Matrix<R, C> &val);
179
180 protected:
182 void _fillBuffers();
183
185 virtual int _createSetupFile() {return NO_ERROR;}
186
188 virtual int _writeToFile() {return NO_ERROR;}
189
191 std::vector<GraphTreeObject*> _logged_nodes;
192 std::vector<std::string> _log_names;
193 std::string _current_name;
194
196 std::string _filename;
197 std::string _output_directory;
198
208 unsigned int _buffer_size = 10;
209 unsigned int _buffer_write_idx = 0;
210 unsigned int _buffer_read_idx = 0;
211 std::vector<std::vector<bool>> _bool_vector;
212 std::vector<std::vector<int64>> _int_vector;
213 std::vector<std::vector<uint64>> _uint_vector;
214 std::vector<std::vector<float>> _float_vector;
215 std::vector<std::vector<double>> _double_vector;
216 std::vector<std::vector<std::string>> _string_vector;
217
219 bool _locked;
220 unsigned int _seq_idx = 0;
221 std::vector<std::string> _headers;
222 std::vector<unsigned int> _sequence;
223 std::vector<vector_select_e> _type;
224
230 struct Group {
231 long unsigned int size;
232 std::string group_name;
233 };
234 std::vector<int> _group_idx;
235 std::vector<Group> _groups;
236 };
237
238
239 template <typename T>
240 int SimLogger::writeToBuffer(const std::vector<T> &val) {
241 int err;
243 for(unsigned int i = 0; i < val.size(); i++) {
244 err = writeToBuffer(val[i]);
245 if(err) {
246 return err;
247 }
248 }
249 return NO_ERROR;
250 }
251
252 template <typename T, long unsigned int N>
253 int SimLogger::writeToBuffer(const std::array<T, N> &val) {
254 int err;
256 for(unsigned int i = 0; i < N; i++) {
257 err = writeToBuffer(val[i]);
258 if(err) {
259 return err;
260 }
261 }
262 return NO_ERROR;
263 }
264
265 template <unsigned int R, unsigned int C>
266 int SimLogger::writeToBuffer(const Matrix<R, C> &val) {
267 int err;
268 unsigned int i, j;
269 for(i = 0; i < R; i++) {
270 for(j = 0; j < C; j++) {
271 err = writeToBuffer(val.values[i][j]);
272 if(err) {
273 return err;
274 }
275 }
276 }
277 return NO_ERROR;
278 }
279
280 template <typename T>
281 int SimLogger::writeHeaders(const std::vector<T> &val) {
284 _groups.push_back({val.size(), _current_name});
285
286 for(unsigned int i = 0; i < val.size(); i++) {
287 _headers.push_back(_current_name + "_" + std::to_string(i));
288 _group_idx.push_back(_groups.size() - 1);
289 }
290 return NO_ERROR;
291 }
292
293 template <typename T, long unsigned int N>
294 int SimLogger::writeHeaders(const std::array<T, N> &val) {
297 _groups.push_back({val.size(), _current_name});
298
299 for(unsigned int i = 0; i < N; i++) {
300 _headers.push_back(_current_name + "_" + std::to_string(i));
301 _group_idx.push_back(_groups.size() - 1);
302 }
303 return NO_ERROR;
304 }
305
306 template <unsigned int R, unsigned int C>
307 int SimLogger::writeHeaders(const Matrix<R, C> &val) {
310 _groups.push_back({R*C, _current_name});
311
312 unsigned int i, j;
313 for(i = 0; i < R; i++) {
314 for(j = 0; j < C; j++) {
315 if(C == 1) {
317 _headers.push_back(_current_name + "_" + std::to_string(i));
318 _group_idx.push_back(_groups.size() - 1);
319 } else {
321 _headers.push_back(_current_name + "_" + std::to_string(i) + "_" + std::to_string(j));
322 _group_idx.push_back(_groups.size() - 1);
323 }
324 }
325 }
326 return NO_ERROR;
327 }
328
329}
330
331#endif
332#endif
333#endif
Base class for object organization.
Definition GraphTreeObject.h:98
Matrix math implementation.
Definition Matrix.hpp:55
#define NO_ERROR
Error code in the case where matrix math executed successfully.
Definition clockwerkerrors.h:34
Definition CircularBuffer.hpp:28
half log(half arg)
Definition half.hpp:3050
@ STRING
Definition Connection.h:40