Skip to content

Commit

Permalink
reduce total number of open file handles held at any given time
Browse files Browse the repository at this point in the history
  • Loading branch information
hellkite500 committed Nov 2, 2023
1 parent 33d76a1 commit 3d07831
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions include/utilities/FileStreamHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::ofstream>();
stream->open(path, std::ios::trunc);
stream = std::make_shared<std::ofstream>();
stream->open(path, std::ios::trunc); //clear any existing data
stream->close(); //avoid too many open files
output_stream = stream;
}
virtual ~FileStreamHandler(){}

template<class DataType> 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<std::ofstream> stream;
};

}
Expand Down

0 comments on commit 3d07831

Please sign in to comment.