CsvLogger.cpp
Go to the documentation of this file.
1 #include "mvsim/CsvLogger.h"
2 
4 {
5  file_ = std::make_shared<std::ofstream>(std::ofstream());
6 }
7 
9 void CSVLogger::addColumn(std::string name) { columns_[name] = 0.0; }
10 void CSVLogger::updateColumn(std::string name, double value)
11 {
12  columns_[name] = value;
13 }
14 
16 {
17  columns_type::iterator it;
18  for (it = columns_.begin(); it != columns_.end();)
19  {
20  *file_ << it->first;
21 
22  if (++it != columns_.end())
23  {
24  *file_ << ", ";
25  }
26  }
27 
28  *file_ << "\n"; // most CSV readers don't use \r\n
29 
30  return !!file_;
31 }
32 
34 {
35  if (!isRecording) return true;
36 
37  if (!isOpen()) clear();
38 
39  columns_type::iterator it;
40  for (it = columns_.begin(); it != columns_.end();)
41  {
42  *file_ << it->second;
43 
44  if (++it != columns_.end())
45  {
46  *file_ << ", ";
47  }
48  }
49 
50  *file_ << "\n";
51 
52  return !!file_;
53 }
54 
56 {
57  if (file_)
58  {
59  file_->open((std::string("session") + std::to_string(currentSession) +
60  std::string("-") + filepath_)
61  .c_str());
62  return isOpen();
63  }
64  return false;
65 }
66 
67 bool CSVLogger::isOpen() { return file_->is_open(); }
69 {
70  if (file_)
71  {
72  file_->close();
73  return !isOpen();
74  }
75  return false;
76 }
77 
79 {
80  if (isOpen()) close();
81 
82  if (open())
83  {
84  return writeHeader();
85  }
86 
87  return false;
88 }
89 
91 {
93  close();
94 }
std::string filepath_
Definition: CsvLogger.h:34
columns_type columns_
Definition: CsvLogger.h:32
bool isOpen()
Definition: CsvLogger.cpp:67
std::shared_ptr< std::ofstream > file_
Definition: CsvLogger.h:33
void updateColumn(std::string name, double value)
Definition: CsvLogger.cpp:10
bool clear()
Definition: CsvLogger.cpp:78
virtual ~CSVLogger()
Definition: CsvLogger.cpp:8
bool close()
Definition: CsvLogger.cpp:68
bool open()
Definition: CsvLogger.cpp:55
unsigned int currentSession
Definition: CsvLogger.h:36
bool isRecording
Definition: CsvLogger.h:35
bool writeHeader()
Definition: CsvLogger.cpp:15
bool writeRow()
Definition: CsvLogger.cpp:33
void newSession()
Definition: CsvLogger.cpp:90
void addColumn(std::string name)
Definition: CsvLogger.cpp:9


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19