@@ -33,7 +33,6 @@ static std::once_flag absl_log_init_flag;
3333std::shared_ptr<TraceOptions> TraceOptions::options_console_ = nullptr ;
3434std::shared_ptr<TraceOptions> TraceOptions::options_file_ = nullptr ;
3535std::mutex TraceOptions::mu_;
36- static std::mutex sink_mutex; // Global for the file
3736
3837#ifdef _WIN32
3938constexpr char kPathSeparator = ' \\ ' ;
@@ -54,10 +53,10 @@ FileLogSink::FileLogSink(std::shared_ptr<TraceOptions> opts)
5453}
5554
5655FileLogSink::~FileLogSink () {
57- std::lock_guard<std::mutex> lock (sink_mutex);
58- if (file_sink_ && file_sink_. get () == this ) {
59- absl::log_internal::RemoveLogSink ( this );
60- file_sink_ = nullptr ;
56+ // Close the file pointer if it was opened
57+ if (fp_ != nullptr ) {
58+ fclose (fp_ );
59+ fp_ = nullptr ;
6160 }
6261}
6362// Required for custom log formatting and writing to the driver's default log
@@ -152,9 +151,13 @@ std::string GetLogFileWithIndex(std::string const& log_path) {
152151
153152void FileLogSink::InitializeFileLog (
154153 std::shared_ptr<TraceOptions> const & trace_opts) {
155- std::lock_guard<std::mutex> lock (sink_mutex);
156154 if (file_sink_ || !trace_opts) return ;
157155
156+ if (file_sink_) {
157+ absl::log_internal::RemoveLogSink (file_sink_.get ());
158+ file_sink_ = nullptr ;
159+ }
160+
158161 file_sink_ = std::make_unique<FileLogSink>(trace_opts);
159162 absl::log_internal::AddLogSink (file_sink_.get ());
160163}
@@ -170,9 +173,19 @@ bool CanWriteToFile(std::string const& log_file, std::size_t new_log_size,
170173}
171174
172175bool TraceOptions::InitializeLogging (bool is_trace_override) {
176+ // suppress all stderr output
177+ std::call_once (absl_log_init_flag, []() { absl::InitializeLog (); });
178+ absl::SetStderrThreshold (absl::LogSeverityAtLeast::kInfinity );
179+
173180 if (!kTraceOptsFile .Ok ()) return false ;
174181 auto const & trace_opts = kTraceOptsFile .GetValue ();
175182
183+ // If logging is disabled, return false
184+ if (trace_opts->log_level <= 0 ) {
185+ trace_opts->logging_enabled = false ;
186+ return false ;
187+ }
188+
176189 // Logging already initialized and no override requested
177190 if (trace_opts->logging_enabled && !is_trace_override) {
178191 return true ;
@@ -186,23 +199,14 @@ bool TraceOptions::InitializeLogging(bool is_trace_override) {
186199 FileLogSink::InitializeFileLog (trace_opts);
187200 return true ;
188201 }
189- // If logging is disabled, suppress all stderr output
190- if (trace_opts->log_level <= 0 ) {
191- absl::SetStderrThreshold (absl::LogSeverityAtLeast::kInfinity );
192- return false ;
193- }
194202
195203 // Initialize Abseil logging and custom file sink
196- std::call_once (absl_log_init_flag, []() { absl::InitializeLog (); });
197204 auto log_severity =
198205 GetAbslSeverity (static_cast <LogLevel>(trace_opts->log_level ));
199206 absl::SetMinLogLevel (static_cast <absl::LogSeverityAtLeast>(log_severity));
200207
201208 FileLogSink::InitializeFileLog (trace_opts);
202209 trace_opts->logging_enabled = true ;
203-
204- // Disable Abseil's stderr logging
205- absl::SetStderrThreshold (absl::LogSeverityAtLeast::kInfinity );
206210 return true ;
207211}
208212
0 commit comments