svFSIplus
SimulationLogger.h
1 /**
2  * Copyright (c) Stanford University, The Regents of the University of California, and others.
3  *
4  * All Rights Reserved.
5  *
6  * See Copyright-SimVascular.txt for additional details.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject
14  * to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef SIMULATION_LOGGER_H
33 #define SIMULATION_LOGGER_H
34 
35 #include <fstream>
36 #include <string>
37 
38 /// @brief The SimulationLogger class is used to write information
39 /// to a text file and optionally to cout.
40 //
42 
43  public:
44  SimulationLogger() { }
45 
46  SimulationLogger(const std::string& file_name, bool cout_write=false)
47  {
48  this->initialize(file_name, cout_write);
49  }
50 
51  void initialize(const std::string& file_name, bool cout_write=false)
52  {
53  log_file_.open(file_name);
54  if (log_file_.fail()) {
55  throw std::runtime_error("[SimulationLogger] Unable to open the file '" + file_name + "' for writing.");
56  }
57 
58  cout_write_ = cout_write;
59  file_name_ = file_name;
60  }
61 
63  {
64  log_file_.close();
65  }
66 
67  template <class T> SimulationLogger& operator<< (const T& value)
68  {
69  if (file_name_ == "") {
70  return *this;
71  }
72 
73  log_file_ << value;
74 
75  if (cout_write_) {
76  std::cout << value;
77  }
78 
79  return *this;
80  }
81 
82  SimulationLogger& operator<<(std::ostream&(*value)(std::ostream& o))
83  {
84  if (file_name_ == "") {
85  return *this;
86  }
87 
88  log_file_ << value;
89 
90  if (cout_write_) {
91  std::cout << value;
92  }
93 
94  return *this;
95  };
96 
97  private:
98  bool cout_write_ = false;
99  std::string file_name_;
100  std::ofstream log_file_;
101 };
102 
103 #endif
104 
105 
The SimulationLogger class is used to write information to a text file and optionally to cout.
Definition: SimulationLogger.h:41