diff --git a/src/sled/log/log.cc b/src/sled/log/log.cc index ac66ee4..8bf10ca 100644 --- a/src/sled/log/log.cc +++ b/src/sled/log/log.cc @@ -142,12 +142,14 @@ SetLogLevel(LogLevel level) void SetLogFileName(const char *file_name) { - std::ofstream tmp_stream(file_name, std::ios_base::app); if (tmp_stream.is_open()) { Waiter waiter(g_request_id.fetch_add(1), g_current_id); waiter.wait(); + g_log_stream.close(); + g_log_file_name = file_name; - std::swap(g_log_stream, tmp_stream); + std::ofstream tmp_stream(file_name, std::ios_base::app); + g_log_stream = tmp_stream; } else { LOGE("log", "can't open new log file [{}]", file_name); return; @@ -171,7 +173,10 @@ Log(LogLevel level, const char *tag, const char *fmt, const char *file_name, int Waiter waiter(g_request_id.fetch_add(1), g_current_id); waiter.wait(); - if (g_log_stream.is_open()) { g_log_stream << msg << std::endl; } + if (g_log_stream.is_open()) { + g_log_stream << msg << std::endl; + if (waiter.id() % 100 == 99) { g_log_stream.flush(); } + } std::cout << GetConsoleColorPrefix(level) << msg << GetConsoleColorSuffix() << std::endl; }