CosmoGRaPH v0.0
IOData.h
1 #ifndef COSMO_UTILS_IODATA_H
2 #define COSMO_UTILS_IODATA_H
3 
4 #include <string>
5 #include <iostream>
6 #include <iomanip>
7 #include <sys/stat.h>
8 #include <fstream>
9 
10 #define COSMO_IODATA_VERBOSITY_OFF 0
11 #define COSMO_IODATA_VERBOSITY_ON 1
12 #define COSMO_IODATA_VERBOSITY_DEBUG 2
13 
14 namespace cosmo
15 {
16 
18 class IOData
19 {
20  private:
21  int verbosity;
22  std::string output_dir;
23  std::ofstream logfile;
24 
25  idx_t slice_output_interval;
26  idx_t grid_output_interval;
27  idx_t meta_output_interval;
28  idx_t spec_output_interval;
29 
30  void _init(std::string output_dir_in, int verbosity_in)
31  {
32  output_dir = output_dir_in;
33  verbosity = verbosity_in;
34  size_t len_dir_name = output_dir.length();
35 
36  std::string log_filename = "log.txt";
37 
38  // default output directory name
39  if(len_dir_name == 0)
40  output_dir = "output";
41 
42  // temporarily remove trailing slash
43  if(output_dir[len_dir_name - 1] == '/')
44  {
45  output_dir = output_dir.substr(0, len_dir_name - 1);
46  }
47 
48  // check for conflicting data in directory
49  if(std::ifstream(output_dir + "/" + log_filename))
50  {
51  std::cout << "Data files in output directory seem to already exist!";
52  int s=1;
53  while(std::ifstream(
54  output_dir + "." + std::to_string(s) + "/" + log_filename
55  ))
56  s += 1;
57 
58  output_dir = output_dir + "." + std::to_string(s);
59  std::cout << " Using '" << output_dir << "' instead.\n";
60  }
61 
62  mkdir(output_dir.c_str(), 0755);
63 
64  // add in trailing slash
65  output_dir += '/';
66 
67  // open log file
68  logfile.open(output_dir + log_filename);
69  log("Log file open.");
70  }
71 
72  public:
73  IOData(std::string output_dir_in)
74  {
75  _init(output_dir_in, COSMO_IODATA_VERBOSITY_ON);
76  }
77 
78  IOData(std::string output_dir_in, int verbosity_in)
79  {
80  _init(output_dir_in, verbosity_in);
81  }
82 
83  ~IOData()
84  {
85  logfile.close();
86  }
87 
91  void log(std::string message)
92  {
93  if(verbosity >= COSMO_IODATA_VERBOSITY_ON)
94  {
95  std::cout << message << "\n" << std::flush;
96  }
97  logfile << message << "\n";
98  logfile.flush();
99  }
100 
104  void debug(std::string message)
105  {
106  if(verbosity >= COSMO_IODATA_VERBOSITY_DEBUG)
107  {
108  std::cout << message << "\n" << std::flush;
109  logfile << message << "\n";
110  logfile.flush();
111  }
112  }
113 
117  void backupFile(std::string file)
118  {
119  std::ifstream source(file, std::ios::binary);
120  std::ofstream dest(output_dir + file, std::ios::binary);
121  dest << source.rdbuf();
122  source.close();
123  dest.close();
124  }
125 
129  std::string dir()
130  {
131  return output_dir;
132  }
133 
134 };
135 
136 template <typename T>
137 std::string stringify(const T value)
138 {
139  std::ostringstream out;
140  out << std::setprecision(18) << value;
141  return out.str();
142 }
143 
144 } // namespace cosmo
145 
146 #endif
Definition: IOData.h:18
std::string dir()
Return output directory.
Definition: IOData.h:129
Definition: bardeen.cc:5
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