diff --git a/src/sled/log/log.cc b/src/sled/log/log.cc index 3bd5cd9..f2af81d 100644 --- a/src/sled/log/log.cc +++ b/src/sled/log/log.cc @@ -97,27 +97,6 @@ GetCurrentUTCTime() return result; } -static LogLevel g_log_level = LogLevel::kTrace; -// static std::string g_log_file_name; -static std::ofstream g_log_stream; - -void -SetLogLevel(LogLevel level) -{ - g_log_level = level; -} - -void -SetLogFileName(const char *file_name) -{ - std::ofstream tmp_stream(file_name, std::ios_base::app); - if (tmp_stream.is_open()) { - g_log_stream.open(file_name, std::ios_base::app); - } else { - LOGE("log", "can't open new log file [{}]", file_name); - } -} - static std::atomic g_current_id(0); static std::atomic g_request_id(0); @@ -138,6 +117,31 @@ private: std::atomic ¤t_id_; }; +static LogLevel g_log_level = LogLevel::kTrace; +// static std::string g_log_file_name; +static std::ofstream g_log_stream; + +void +SetLogLevel(LogLevel level) +{ + g_log_level = level; +} + +void +SetLogFileName(const char *file_name) +{ + std::ofstream tmp_stream(file_name, std::ios_base::app); + if (tmp_stream.is_open()) { + // g_log_stream.open(file_name, std::ios_base::app); + // g_log_stream = std::move(tmp_stream); + Waiter waiter(g_request_id.fetch_add(1), g_current_id); + std::swap(g_log_stream, tmp_stream); + } else { + LOGE("log", "can't open new log file [{}]", file_name); + } + LOGD("log", "log to new file [{}]", file_name); +} + void Log(LogLevel level, const char *tag, const char *fmt, const char *file_name, int line, const char *func_name, ...) {