修正日志时间

This commit is contained in:
cxh 2025-11-13 13:55:55 +08:00
parent df5da2ec5b
commit 5cf5be60d4

View File

@ -1,14 +1,15 @@
// logger.cpp // logger.cpp
#include "logger.hpp" #include "logger.hpp"
#include <iomanip>
#include <cstdio>
#include <ctime> #include <ctime>
#include <fstream> #include <fstream>
#include <cstdio> #include <iomanip>
std::ofstream Logger::log_file; std::ofstream Logger::log_file;
std::string Logger::current_log_filename; std::string Logger::current_log_filename;
void Logger::set_log_to_file(const std::string &filename) void Logger::set_log_to_file(const std::string& filename)
{ {
current_log_filename = filename; current_log_filename = filename;
log_file.open(filename, std::ios::app); log_file.open(filename, std::ios::app);
@ -22,12 +23,13 @@ std::string Logger::get_time_string()
{ {
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000; 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::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm tm_now = *std::localtime(&t); std::tm tm_now = *std::localtime(&t);
std::ostringstream ss; std::ostringstream ss;
ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S") ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S") << "." << std::setw(3) << std::setfill('0') << millis.count();
<< "." << std::setw(3) << std::setfill('0') << millis.count();
return ss.str(); return ss.str();
} }
@ -36,21 +38,18 @@ std::string Logger::get_current_time_utc8()
using namespace std::chrono; using namespace std::chrono;
auto now = system_clock::now(); auto now = system_clock::now();
now += hours(8); // UTC+8
std::time_t t = system_clock::to_time_t(now);
auto millis = duration_cast<milliseconds>(now.time_since_epoch()) % 1000; auto millis = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
std::time_t t = system_clock::to_time_t(now);
std::tm tm_now = *std::localtime(&t); std::tm tm_now = *std::localtime(&t);
std::ostringstream ss; std::ostringstream ss;
ss << std::put_time(&tm_now, "%Y%m%d%H%M%S") ss << std::put_time(&tm_now, "%Y%m%d%H%M%S") << std::setw(3) << std::setfill('0') << millis.count();
<< std::setw(3) << std::setfill('0') << millis.count();
return ss.str(); return ss.str();
} }
void Logger::log(LogLevel level, const std::string &msg) void Logger::log(LogLevel level, const std::string& msg)
{ {
std::string level_str; std::string level_str;
switch (level) switch (level)
@ -83,7 +82,7 @@ void Logger::log(LogLevel level, const std::string &msg)
} }
} }
size_t Logger::get_file_size(const std::string &filename) size_t Logger::get_file_size(const std::string& filename)
{ {
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary); std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.is_open() ? static_cast<size_t>(in.tellg()) : 0; return in.is_open() ? static_cast<size_t>(in.tellg()) : 0;