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


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 19:36:40