CsvLogger.cpp
Go to the documentation of this file.
00001 #include "mvsim/CsvLogger.h"
00002 
00003 CSVLogger::CSVLogger()
00004 {
00005         m_file = std::make_shared<std::ofstream>(std::ofstream());
00006 }
00007 
00008 CSVLogger::~CSVLogger() { close(); }
00009 void CSVLogger::addColumn(std::string name) { m_columns[name] = 0.0; }
00010 void CSVLogger::updateColumn(std::string name, double value)
00011 {
00012         m_columns[name] = value;
00013 }
00014 
00015 bool CSVLogger::writeHeader()
00016 {
00017         columns_type::iterator it;
00018         for (it = m_columns.begin(); it != m_columns.end();)
00019         {
00020                 *m_file << it->first;
00021 
00022                 if (++it != m_columns.end())
00023                 {
00024                         *m_file << ", ";
00025                 }
00026         }
00027 
00028         *m_file << "\n";  // most CSV readers don't use \r\n
00029 
00030         return !!m_file;
00031 }
00032 
00033 bool CSVLogger::writeRow()
00034 {
00035         if (!isRecording) return true;
00036 
00037         if (!isOpen()) clear();
00038 
00039         columns_type::iterator it;
00040         for (it = m_columns.begin(); it != m_columns.end();)
00041         {
00042                 *m_file << it->second;
00043 
00044                 if (++it != m_columns.end())
00045                 {
00046                         *m_file << ", ";
00047                 }
00048         }
00049 
00050         *m_file << "\n";
00051 
00052         return !!m_file;
00053 }
00054 
00055 bool CSVLogger::open()
00056 {
00057         if (m_file)
00058         {
00059                 m_file->open(
00060                         (std::string("session") + std::to_string(currentSession) +
00061                          std::string("-") + m_filepath)
00062                                 .c_str());
00063                 return isOpen();
00064         }
00065         return false;
00066 }
00067 
00068 bool CSVLogger::isOpen() { return m_file->is_open(); }
00069 bool CSVLogger::close()
00070 {
00071         if (m_file)
00072         {
00073                 m_file->close();
00074                 return !isOpen();
00075         }
00076         return false;
00077 }
00078 
00079 bool CSVLogger::clear()
00080 {
00081         if (isOpen()) close();
00082 
00083         if (open())
00084         {
00085                 return writeHeader();
00086         }
00087 
00088         return false;
00089 }
00090 
00091 void CSVLogger::newSession()
00092 {
00093         currentSession++;
00094         close();
00095 }


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 22:08:35