-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.cpp
66 lines (55 loc) · 1.71 KB
/
logger.cpp
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
#include <memory>
#include <string>
#include <iostream>
#include <cstdlib>
#include "logger.h"
#pragma warning(disable : 4996)
class ConsoleLogger :public ILogger
{
public:
template <typename... Args>
ConsoleLogger(Args&&... args) {}
virtual void save(double xBeadl, double xMT, double xBeadr, double xMol) override {
std::cout << xBeadl << " " << xMT << " " << xBeadr << " " << xMol << std::endl;
}
virtual ~ConsoleLogger() override {
}
};
class BinaryFileLogger :public ILogger
{
private:
FILE* const pFile;
int buffsize;
public:
template <typename... Args>
BinaryFileLogger(Args&&... args) {
throw std::logic_error{ "wrong BinaryFileLogger construct parameter types" };
}
BinaryFileLogger(std::string filenameTemplate, std::string filePath)
:pFile{ fopen((filePath + filenameTemplate + std::string{ "file.binary" }).c_str(), "wb") }
{
if (pFile == nullptr) {
throw std::runtime_error{ "the file was not created" };
}
//std::cout << "hello" << std::endl;
}
virtual void save(double xBeadl, double xMT, double xBeadr, double xMol) override {
//std::cout << xBeadl << " " << xMT << " " << xBeadr << " " << xMol << endl;
if (fwrite(&xBeadl, sizeof(double), 1, pFile) != 1) {
throw std::runtime_error{ "not all data was written to file" };
};
}
virtual ~BinaryFileLogger() override {
fclose(pFile);
}
};
template <typename... Args>
static std::shared_ptr <ILogger> createLogger(LoggerType type, Args&&... args) {
if (type == LoggerType::BINARY_FILE) {
return std::make_shared <BinaryFileLogger>(std::forward<Args>(args)...);
}
if (type == LoggerType::CONSOLE) {
return std::make_shared <ConsoleLogger>(std::forward<Args>(args)...);
}
throw std::logic_error{ "unknown logger type" };
}