@@ -47,7 +47,7 @@ class SimulationLogger {
4747 this ->initialize (file_name, cout_write);
4848 }
4949
50- void initialize (const std::string& file_name, bool cout_write=false )
50+ void initialize (const std::string& file_name, bool cout_write=false ) const
5151 {
5252 log_file_.open (file_name);
5353 if (log_file_.fail ()) {
@@ -65,7 +65,7 @@ class SimulationLogger {
6565 log_file_.close ();
6666 }
6767
68- template <class T > SimulationLogger& operator << (const T& value)
68+ template <class T > const SimulationLogger& operator << (const T& value) const
6969 {
7070 if (file_name_ == " " ) {
7171 return *this ;
@@ -81,7 +81,7 @@ class SimulationLogger {
8181 }
8282
8383 // Special handling for vector<string>
84- SimulationLogger& operator << (const std::vector<std::string>& values)
84+ const SimulationLogger& operator << (const std::vector<std::string>& values) const
8585 {
8686 if (file_name_ == " " ) {
8787 return *this ;
@@ -101,7 +101,7 @@ class SimulationLogger {
101101 return *this ;
102102 }
103103
104- SimulationLogger& operator <<(std::ostream&(*value)(std::ostream& o))
104+ const SimulationLogger& operator <<(std::ostream&(*value)(std::ostream& o)) const
105105 {
106106 if (file_name_ == " " ) {
107107 return *this ;
@@ -119,7 +119,7 @@ class SimulationLogger {
119119 // / @brief Log a message with automatic space separation
120120 // / @param args Arguments to log
121121 template <typename ... Args>
122- SimulationLogger& log_message (const Args&... args) {
122+ const SimulationLogger& log_message (const Args&... args) const {
123123 if (file_name_ == " " ) return *this ;
124124
125125 bool first = true ;
@@ -129,10 +129,14 @@ class SimulationLogger {
129129 }
130130
131131 private:
132- bool cout_write_ = false ;
133- bool initialized_ = false ;
134- std::string file_name_;
135- std::ofstream log_file_;
132+ // These members are marked mutable because they can be modified in const member functions.
133+ // While logging writes to a file (modifying these members), it doesn't change the logical
134+ // state of the logger - this is exactly what mutable is designed for: implementation
135+ // details that need to change even when the object's public state remains constant.
136+ mutable bool cout_write_ = false ;
137+ mutable bool initialized_ = false ;
138+ mutable std::string file_name_;
139+ mutable std::ofstream log_file_;
136140};
137141
138142#endif
0 commit comments