From 3d07831bb9cf8c0d4723dc915fe3330b5a22c52c Mon Sep 17 00:00:00 2001 From: hellkite500 Date: Thu, 2 Nov 2023 07:08:44 -0600 Subject: [PATCH] reduce total number of open file handles held at any given time --- include/utilities/FileStreamHandler.hpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) 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; }; }