1 #ifndef COSMO_UTILS_IODATA_H 2 #define COSMO_UTILS_IODATA_H 10 #define COSMO_IODATA_VERBOSITY_OFF 0 11 #define COSMO_IODATA_VERBOSITY_ON 1 12 #define COSMO_IODATA_VERBOSITY_DEBUG 2 22 std::string output_dir;
23 std::ofstream logfile;
25 idx_t slice_output_interval;
26 idx_t grid_output_interval;
27 idx_t meta_output_interval;
28 idx_t spec_output_interval;
30 void _init(std::string output_dir_in,
int verbosity_in)
32 output_dir = output_dir_in;
33 verbosity = verbosity_in;
34 size_t len_dir_name = output_dir.length();
36 std::string log_filename =
"log.txt";
40 output_dir =
"output";
43 if(output_dir[len_dir_name - 1] ==
'/')
45 output_dir = output_dir.substr(0, len_dir_name - 1);
49 if(std::ifstream(output_dir +
"/" + log_filename))
51 std::cout <<
"Data files in output directory seem to already exist!";
54 output_dir +
"." + std::to_string(s) +
"/" + log_filename
58 output_dir = output_dir +
"." + std::to_string(s);
59 std::cout <<
" Using '" << output_dir <<
"' instead.\n";
62 mkdir(output_dir.c_str(), 0755);
68 logfile.open(output_dir + log_filename);
69 log(
"Log file open.");
73 IOData(std::string output_dir_in)
75 _init(output_dir_in, COSMO_IODATA_VERBOSITY_ON);
78 IOData(std::string output_dir_in,
int verbosity_in)
80 _init(output_dir_in, verbosity_in);
91 void log(std::string message)
93 if(verbosity >= COSMO_IODATA_VERBOSITY_ON)
95 std::cout << message <<
"\n" << std::flush;
97 logfile << message <<
"\n";
106 if(verbosity >= COSMO_IODATA_VERBOSITY_DEBUG)
108 std::cout << message <<
"\n" << std::flush;
109 logfile << message <<
"\n";
119 std::ifstream source(file, std::ios::binary);
120 std::ofstream dest(output_dir + file, std::ios::binary);
121 dest << source.rdbuf();
136 template <
typename T>
137 std::string stringify(
const T value)
139 std::ostringstream out;
140 out << std::setprecision(18) << value;
std::string dir()
Return output directory.
Definition: IOData.h:129
void backupFile(std::string file)
Copy a file to the output dir.
Definition: IOData.h:117
void log(std::string message)
Write message to log file; console out if desired.
Definition: IOData.h:91
void debug(std::string message)
Only write if in debug mode.
Definition: IOData.h:104