diff --git a/include/utilities/FileStreamHandler.hpp b/include/utilities/FileStreamHandler.hpp index 1e4108fbaa..04dd901934 100644 --- a/include/utilities/FileStreamHandler.hpp +++ b/include/utilities/FileStreamHandler.hpp @@ -8,13 +8,26 @@ namespace utils class FileStreamHandler : public StreamHandler { public: - FileStreamHandler(const char* path) : StreamHandler() + FileStreamHandler(const char* path) : StreamHandler(), path(path) { - auto stream = std::make_shared(); - stream->open(path, std::ios::trunc); + stream = std::make_shared(); + stream->open(path, std::ios::trunc); //clear any existing data + stream->close(); //avoid too many open files output_stream = stream; } virtual ~FileStreamHandler(){} + + template std::ostream& operator<<(const DataType& val) + { + if( !stream->is_open() ) + stream->open(path, std::ios_base::app); + put(val); + stream->close(); + return *output_stream; + } + private: + std::string path; + std::shared_ptr stream; }; }