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