kunlang_video/src/logger.cpp

145 lines
3.7 KiB
C++
Raw Normal View History

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;
2025-11-13 14:40:58 +08:00
int Logger::keep_days_ = 30;
2025-11-13 14:24:34 +08:00
2025-11-13 14:40:58 +08:00
void Logger::init(const std::string& log_dir, int keep_days)
2025-11-13 14:24:34 +08:00
{
2025-11-13 14:40:58 +08:00
keep_days_ = keep_days;
2025-11-13 14:24:34 +08:00
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-11-13 14:40:58 +08:00
cleanup_old_logs();
}
}
void Logger::cleanup_old_logs()
{
namespace fs = std::filesystem;
for (auto& entry : fs::directory_iterator(log_directory))
{
if (!entry.is_regular_file()) continue;
auto path = entry.path();
auto filename = path.filename().string();
if (filename.size() != 14 || filename.substr(4, 1) != "-" || filename.substr(7, 1) != "-")
continue; // 跳过不符合 YYYY-MM-DD.log 的文件
std::tm file_tm{};
std::istringstream ss(filename.substr(0, 10)); // YYYY-MM-DD
ss >> std::get_time(&file_tm, "%Y-%m-%d");
if (ss.fail()) continue;
auto file_time = std::chrono::system_clock::from_time_t(std::mktime(&file_tm));
auto now = std::chrono::system_clock::now();
auto age = std::chrono::duration_cast<std::chrono::hours>(now - file_time).count() / 24;
if (age > keep_days_)
{
fs::remove(path);
}
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-11-13 14:40:58 +08:00
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();
}
}