first commit

This commit is contained in:
cxh 2025-09-09 14:21:24 +08:00
parent e27c5514d5
commit 748c7e0d1b

View File

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