-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_file_logger.cpp
More file actions
43 lines (35 loc) · 1.2 KB
/
Copy pathbinary_file_logger.cpp
File metadata and controls
43 lines (35 loc) · 1.2 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
#pragma once
#include <iostream>
#include <fstream>
#include "configuration.h"
#include "binary_file_logger.h"
BinaryFileLogger::BinaryFileLogger(LoggerParameters loggerParams, double(SystemState::* loggedField), std::string coordinateName) :
_file{ loggerParams.filepath + loggerParams.name + "_results_" + coordinateName + ".binary", std::ios::binary },
//_file{ loggerParams.filepath + loggerParams.name + "_results_" + coordinateName + ".dat", std::ofstream::out },
_loggedField{ loggedField }
{
if (!_file) {
throw std::runtime_error{ "the file was not created" };
}
_buffer.reserve(_buffsize);
}
BinaryFileLogger::~BinaryFileLogger() {
flush();
}
void BinaryFileLogger::save(const SystemState* systemState) {
_buffer.push_back(systemState->*_loggedField);
if (_buffer.size() == _buffsize) {
flush();
}
}
void BinaryFileLogger::flush() {
if (_buffer.empty()) {
return;
}
_file.write(reinterpret_cast<const char*>(_buffer.data()), _buffer.size() * sizeof(double));
//_file.write(reinterpret_cast<const char*>(_buffer.data()), _buffer.size() * sizeof(double));
if (!_file.good()) {
throw std::runtime_error{ "not all data was written to file" };
};
_buffer.clear();
}