diff --git a/src/logger.cpp b/src/logger.cpp index ca54e1e..452206f 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -20,22 +20,15 @@ void Logger::set_log_to_file(const std::string &filename) std::string Logger::get_time_string() { - using namespace std::chrono; + auto now = std::chrono::system_clock::now(); + auto millis = std::chrono::duration_cast(now.time_since_epoch()) % 1000; + std::time_t t = std::chrono::system_clock::to_time_t(now + std::chrono::hours(8)); // UTC+8 + std::tm tm_now = *std::localtime(&t); - // 获取当前时间点 - auto now = system_clock::now(); - auto ms = duration_cast(now.time_since_epoch()) % 1000; - - // 转换为 time_t(秒) - auto t = system_clock::to_time_t(now); - std::tm ltm = *std::localtime(&t); - - // 拼接格式化字符串 - std::ostringstream oss; - oss << std::put_time(<m, "%Y-%m-%d %H:%M:%S") - << '.' << std::setw(3) << std::setfill('0') << ms.count(); - - return oss.str(); + std::ostringstream ss; + ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S") + << "." << std::setw(3) << std::setfill('0') << millis.count(); + return ss.str(); } std::string Logger::get_current_time_utc8() @@ -73,7 +66,7 @@ void Logger::log(LogLevel level, const std::string &msg) break; } - std::string full_msg = get_current_time_utc8() + " " + level_str + " " + msg; + std::string full_msg = get_time_string() + " " + level_str + " " + msg; std::cout << full_msg << std::endl;