svMultiPhysics
Loading...
Searching...
No Matches
SimulationLogger.h
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the University of California, and others.
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef SIMULATION_LOGGER_H
5#define SIMULATION_LOGGER_H
6
7#include <fstream>
8#include <string>
9
10/// @brief The SimulationLogger class is used to write information
11/// to a text file and optionally to cout.
12//
14
15 public:
17
18 SimulationLogger(const std::string& file_name, bool cout_write=false)
19 {
20 this->initialize(file_name, cout_write);
21 }
22
23 void initialize(const std::string& file_name, bool cout_write=false) const
24 {
25 log_file_.open(file_name);
26 if (log_file_.fail()) {
27 throw std::runtime_error("[SimulationLogger] Unable to open the file '" + file_name + "' for writing.");
28 }
29
30 cout_write_ = cout_write;
31 file_name_ = file_name;
32 }
33
34 bool is_initialized() const { return log_file_.is_open(); }
35
37 {
38 log_file_.close();
39 }
40
41 template <class T> const SimulationLogger& operator<< (const T& value) const
42 {
43 if (file_name_ == "") {
44 return *this;
45 }
46
47 log_file_ << value;
48
49 if (cout_write_) {
50 std::cout << value;
51 }
52
53 return *this;
54 }
55
56 // Special handling for vector<string>
57 const SimulationLogger& operator<< (const std::vector<std::string>& values) const
58 {
59 if (file_name_ == "") {
60 return *this;
61 }
62
63 bool first = true;
64 for (const auto& value : values) {
65 if (!first) {
66 log_file_ << ", ";
67 if (cout_write_) std::cout << ", ";
68 }
69 log_file_ << value;
70 if (cout_write_) std::cout << value;
71 first = false;
72 }
73
74 return *this;
75 }
76
77 const SimulationLogger& operator<<(std::ostream&(*value)(std::ostream& o)) const
78 {
79 if (file_name_ == "") {
80 return *this;
81 }
82
83 log_file_ << value;
84
85 if (cout_write_) {
86 std::cout << value;
87 }
88
89 return *this;
90 };
91
92 /// @brief Log a message with automatic space separation
93 /// @param args Arguments to log
94 template<typename... Args>
95 const SimulationLogger& log_message(const Args&... args) const {
96 if (file_name_ == "") return *this;
97
98 bool first = true;
99 ((first ? (first = false, (*this << args)) : (*this << ' ' << args)), ...);
100 *this << std::endl;
101 return *this;
102 }
103
104 private:
105 // These members are marked mutable because they can be modified in const member functions.
106 // While logging writes to a file (modifying these members), it doesn't change the logical
107 // state of the logger - this is exactly what mutable is designed for: implementation
108 // details that need to change even when the object's public state remains constant.
109 mutable bool cout_write_ = false;
110 mutable bool initialized_ = false;
111 mutable std::string file_name_;
112 mutable std::ofstream log_file_;
113};
114
115#endif
116
117
The SimulationLogger class is used to write information to a text file and optionally to cout.
Definition SimulationLogger.h:13
const SimulationLogger & log_message(const Args &... args) const
Log a message with automatic space separation.
Definition SimulationLogger.h:95