blob: 5c80134341a63c0d7bdea0ec8371d99e2c268aaf (
plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <ctime>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "samples/slog.hpp"
/**
* @class CsvDumper
* @brief A CsvDumper class provides functionality for dumping the values in CSV files
*/
class CsvDumper {
std::ofstream file;
std::string filename;
bool canDump = true;
char delimiter = ';';
std::string generateFilename() {
std::stringstream filename;
filename << "dumpfile-";
filename << time(nullptr);
filename << ".csv";
return filename.str();
}
public:
/**
* @brief A constructor. Disables dumping in case dump file cannot be created
* @param enabled - True if dumping is enabled by default.
* @param name - name of file to dump to. File won't be created if first parameter is false.
*/
explicit CsvDumper(bool enabled = true, const std::string& name = "") : canDump(enabled) {
if (!canDump) {
return;
}
filename = (name == "" ? generateFilename() : name);
file.open(filename, std::ios::out);
if (!file) {
slog::warn << "Cannot create dump file! Disabling dump." << slog::endl;
canDump = false;
}
}
/**
* @brief Sets a delimiter to use in csv file
* @param c - Delimiter char
* @return
*/
void setDelimiter(char c) {
delimiter = c;
}
/**
* @brief Overloads operator to organize streaming values to file. Does nothing if dumping is
* disabled Adds delimiter at the end of value provided
* @param add - value to add to dump
* @return reference to same object
*/
template <class T>
CsvDumper& operator<<(const T& add) {
if (canDump) {
file << add << delimiter;
}
return *this;
}
/**
* @brief Finishes line in dump file. Does nothing if dumping is disabled
*/
void endLine() {
if (canDump) {
file << "\n";
}
}
/**
* @brief Gets information if dump is enabled.
* @return true if dump is enabled and file was successfully created
*/
bool dumpEnabled() {
return canDump;
}
/**
* @brief Gets name of a dump file
* @return name of a dump file
*/
std::string getFilename() const {
return filename;
}
};
|