diff --git a/include/realizations/catchment/Bmi_Adapter.hpp b/include/realizations/catchment/Bmi_Adapter.hpp index 640ef34d5f..c03c1d1233 100644 --- a/include/realizations/catchment/Bmi_Adapter.hpp +++ b/include/realizations/catchment/Bmi_Adapter.hpp @@ -33,9 +33,10 @@ namespace models { { // This replicates a lot of Initialize, but it's necessary to be able to do it separately to support // "initializing" on construction, given using Initialize requires use of virtual functions + errno = 0; if (!utils::FileChecker::file_is_readable(this->bmi_init_config)) { init_exception_msg = "Cannot create and initialize " + this->model_name + " using unreadable file '" - + this->bmi_init_config + "'"; + + this->bmi_init_config + "'. Error: "+std::strerror(errno); throw std::runtime_error(init_exception_msg); } } @@ -164,6 +165,7 @@ namespace models { void Initialize() { // If there was previous init attempt but w/ failure exception, throw runtime error and include previous // message + errno = 0; if (model_initialized && !init_exception_msg.empty()) { throw std::runtime_error( "Previous " + model_name + " init attempt had exception: \n\t" + init_exception_msg); @@ -174,7 +176,7 @@ namespace models { } else if (!utils::FileChecker::file_is_readable(bmi_init_config)) { init_exception_msg = "Cannot initialize " + model_name + " using unreadable file '" - + bmi_init_config + "'"; + + bmi_init_config + "'. Error: "+std::strerror(errno);; throw std::runtime_error(init_exception_msg); } else { 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; }; }