Commit edbe91b2 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

datalogger: allow logger to be reopened for simulation reset

Former-commit-id: a42e84b1
parent 548bbd93
......@@ -24,6 +24,8 @@
#include <map>
#include <iostream>
#include <fstream>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#include <dpsim/Definitions.h>
#include <dpsim/Scheduler.h>
......@@ -41,6 +43,7 @@ namespace DPsim {
String mName;
Bool mEnabled;
UInt mDownsampling;
fs::path mFilename;
std::map<String, CPS::AttributeBase::Ptr> mAttributes;
......@@ -54,9 +57,13 @@ namespace DPsim {
DataLogger(Bool enabled = true);
DataLogger(String name, Bool enabled = true, UInt downsampling = 1);
~DataLogger();
void open();
void close();
void reopen() {
close();
open();
}
void logPhasorNodeValues(Real time, const Matrix& data);
void logEMTNodeValues(Real time, const Matrix& data);
......
......@@ -19,8 +19,6 @@
*********************************************************************************/
#include <iomanip>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#include <dpsim/DataLogger.h>
#include <cps/Logger.h>
......@@ -41,26 +39,23 @@ DataLogger::DataLogger(String name, Bool enabled, UInt downsampling) :
if (!mEnabled)
return;
String filename = CPS::Logger::logDir() + "/" + name + ".csv";
mFilename = CPS::Logger::logDir() + "/" + name + ".csv";
fs::path p = filename;
if (mFilename.has_parent_path() && !fs::exists(mFilename.parent_path()))
fs::create_directory(mFilename.parent_path());
if (p.has_parent_path() && !fs::exists(p.parent_path()))
fs::create_directory(p.parent_path());
open();
}
mLogFile = std::ofstream(filename);
void DataLogger::open() {
mLogFile = std::ofstream(mFilename, std::ios_base::out|std::ios_base::trunc);
if (!mLogFile.is_open()) {
// TODO: replace by exception
std::cerr << "Cannot open log file " << filename << std::endl;
std::cerr << "Cannot open log file " << mFilename << std::endl;
mEnabled = false;
}
}
DataLogger::~DataLogger() {
if (mLogFile.is_open())
mLogFile.close();
}
void DataLogger::close() {
mLogFile.close();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment