@@ -137,9 +137,26 @@ void reset_default_logger()
137137 default_logger ().flush_on (rapids_logger::level_enum::debug);
138138}
139139
140+ // Guard object whose destructor resets the logger
141+ struct logger_config_guard {
142+ ~logger_config_guard () { cuopt::reset_default_logger (); }
143+ };
144+
145+ // Weak reference to detect if any init_logger_t instance is still alive
146+ static std::weak_ptr<logger_config_guard> g_active_guard;
147+ static std::mutex g_guard_mutex;
148+
140149init_logger_t::init_logger_t (std::string log_file, bool log_to_console)
141150{
142- // until this function is called, the default sink is the buffer sink
151+ std::lock_guard<std::mutex> lock (g_guard_mutex);
152+
153+ auto existing_guard = g_active_guard.lock ();
154+ if (existing_guard) {
155+ // Reuse existing configuration, just hold a reference to keep it alive
156+ guard_ = existing_guard;
157+ return ;
158+ }
159+
143160 cuopt::default_logger ().sinks ().clear ();
144161
145162 // re-initialize sinks
@@ -164,8 +181,11 @@ init_logger_t::init_logger_t(std::string log_file, bool log_to_console)
164181 for (const auto & entry : buffered_messages) {
165182 cuopt::default_logger ().log (entry.level , entry.msg .c_str ());
166183 }
167- }
168184
169- init_logger_t ::~init_logger_t () { cuopt::reset_default_logger (); }
185+ // Create guard and store weak reference for future instances to find
186+ auto guard = std::make_shared<logger_config_guard>();
187+ g_active_guard = guard;
188+ guard_ = guard;
189+ }
170190
171191} // namespace cuopt
0 commit comments