2025-09-08 10:59:08 +08:00
|
|
|
#include "logger.hpp"
|
2025-11-13 13:55:55 +08:00
|
|
|
|
2025-09-08 10:59:08 +08:00
|
|
|
std::ofstream Logger::log_file;
|
2025-11-13 14:24:34 +08:00
|
|
|
std::string Logger::log_directory;
|
|
|
|
|
std::string Logger::current_date;
|
|
|
|
|
std::string Logger::current_filename;
|
|
|
|
|
|
|
|
|
|
void Logger::init(const std::string& log_dir)
|
|
|
|
|
{
|
|
|
|
|
log_directory = log_dir;
|
|
|
|
|
|
|
|
|
|
if (!std::filesystem::exists(log_directory)) std::filesystem::create_directories(log_directory);
|
|
|
|
|
|
|
|
|
|
current_date = get_today_date();
|
|
|
|
|
open_log_file();
|
|
|
|
|
}
|
2025-09-08 10:59:08 +08:00
|
|
|
|
2025-11-13 14:24:34 +08:00
|
|
|
void Logger::open_log_file()
|
2025-09-08 10:59:08 +08:00
|
|
|
{
|
2025-11-13 14:24:34 +08:00
|
|
|
current_filename = log_directory + "/" + current_date + ".log";
|
|
|
|
|
|
|
|
|
|
if (log_file.is_open()) log_file.close();
|
|
|
|
|
|
|
|
|
|
log_file.open(current_filename, std::ios::app);
|
|
|
|
|
|
2025-09-08 10:59:08 +08:00
|
|
|
if (!log_file.is_open())
|
|
|
|
|
{
|
2025-11-13 14:24:34 +08:00
|
|
|
std::cerr << "[Logger] Failed to open log file: " << current_filename << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Logger::check_date_rollover()
|
|
|
|
|
{
|
|
|
|
|
std::string today = get_today_date();
|
|
|
|
|
if (today != current_date)
|
|
|
|
|
{
|
|
|
|
|
current_date = today;
|
|
|
|
|
open_log_file();
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Logger::get_time_string()
|
|
|
|
|
{
|
2025-11-13 14:06:52 +08:00
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
auto now = system_clock::now();
|
2025-11-13 14:10:44 +08:00
|
|
|
auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
|
2025-11-13 13:55:55 +08:00
|
|
|
|
2025-11-13 14:09:31 +08:00
|
|
|
std::time_t t = system_clock::to_time_t(now);
|
|
|
|
|
std::tm tm_now = *std::localtime(&t);
|
2025-09-08 10:59:08 +08:00
|
|
|
|
2025-09-09 14:21:24 +08:00
|
|
|
std::ostringstream ss;
|
2025-11-13 14:24:34 +08:00
|
|
|
ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S") << "." << std::setw(3) << std::setfill('0') << ms.count();
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
2025-11-13 13:55:55 +08:00
|
|
|
|
2025-11-13 14:24:34 +08:00
|
|
|
std::string Logger::get_today_date()
|
|
|
|
|
{
|
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
|
std::time_t t = std::chrono::system_clock::to_time_t(now);
|
|
|
|
|
std::tm tm_now = *std::localtime(&t);
|
|
|
|
|
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
|
ss << std::put_time(&tm_now, "%Y-%m-%d");
|
2025-09-09 14:21:24 +08:00
|
|
|
return ss.str();
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 14:16:18 +08:00
|
|
|
std::string Logger::get_current_time_utc8()
|
|
|
|
|
{
|
2025-11-13 14:24:34 +08:00
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
|
2025-09-09 14:16:18 +08:00
|
|
|
|
2025-11-13 14:24:34 +08:00
|
|
|
std::time_t t = std::chrono::system_clock::to_time_t(now);
|
2025-11-13 14:09:31 +08:00
|
|
|
std::tm tm_now = *std::localtime(&t);
|
2025-09-09 14:16:18 +08:00
|
|
|
|
|
|
|
|
std::ostringstream ss;
|
2025-11-13 14:09:31 +08:00
|
|
|
ss << std::put_time(&tm_now, "%Y%m%d%H%M%S") << std::setw(3) << std::setfill('0') << ms.count();
|
2025-09-09 14:16:18 +08:00
|
|
|
return ss.str();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-13 13:55:55 +08:00
|
|
|
void Logger::log(LogLevel level, const std::string& msg)
|
2025-09-08 10:59:08 +08:00
|
|
|
{
|
2025-11-13 14:24:34 +08:00
|
|
|
check_date_rollover();
|
|
|
|
|
|
2025-09-08 10:59:08 +08:00
|
|
|
std::string level_str;
|
|
|
|
|
switch (level)
|
|
|
|
|
{
|
2025-11-13 13:55:55 +08:00
|
|
|
case LogLevel::INFO:
|
|
|
|
|
level_str = "[INFO] ";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel::WARN:
|
|
|
|
|
level_str = "[WARN] ";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel::ERROR:
|
|
|
|
|
level_str = "[ERROR]";
|
|
|
|
|
break;
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 14:21:24 +08:00
|
|
|
std::string full_msg = get_time_string() + " " + level_str + " " + msg;
|
2025-09-08 10:59:08 +08:00
|
|
|
|
|
|
|
|
std::cout << full_msg << std::endl;
|
|
|
|
|
|
|
|
|
|
if (log_file.is_open())
|
|
|
|
|
{
|
|
|
|
|
log_file << full_msg << std::endl;
|
|
|
|
|
log_file.flush();
|
|
|
|
|
}
|
|
|
|
|
}
|