2025-09-08 14:55:07 +08:00
|
|
|
// logger.hpp
|
2025-09-08 10:59:08 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <chrono>
|
2025-11-13 14:24:34 +08:00
|
|
|
#include <ctime>
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
#include <fstream>
|
2025-09-08 10:59:08 +08:00
|
|
|
#include <iomanip>
|
2025-11-13 14:24:34 +08:00
|
|
|
#include <iostream>
|
2025-09-08 10:59:08 +08:00
|
|
|
#include <sstream>
|
2025-11-13 14:24:34 +08:00
|
|
|
#include <string>
|
2025-09-08 10:59:08 +08:00
|
|
|
|
|
|
|
|
enum class LogLevel
|
|
|
|
|
{
|
|
|
|
|
INFO,
|
|
|
|
|
WARN,
|
|
|
|
|
ERROR
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Logger
|
|
|
|
|
{
|
2025-11-13 14:24:34 +08:00
|
|
|
public:
|
2025-11-13 14:40:58 +08:00
|
|
|
static void init(const std::string& log_dir, int keep_days = 30);
|
2025-09-08 10:59:08 +08:00
|
|
|
|
2025-11-13 14:24:34 +08:00
|
|
|
static void log(LogLevel level, const std::string& msg);
|
2025-09-08 10:59:08 +08:00
|
|
|
|
2025-09-09 14:16:18 +08:00
|
|
|
static std::string get_current_time_utc8();
|
|
|
|
|
|
2025-11-13 14:24:34 +08:00
|
|
|
private:
|
2025-09-08 10:59:08 +08:00
|
|
|
static std::ofstream log_file;
|
2025-11-13 14:24:34 +08:00
|
|
|
static std::string log_directory;
|
|
|
|
|
static std::string current_date;
|
|
|
|
|
static std::string current_filename;
|
2025-11-13 14:40:58 +08:00
|
|
|
static int keep_days_;
|
2025-09-08 10:59:08 +08:00
|
|
|
|
2025-11-13 14:24:34 +08:00
|
|
|
static std::string get_time_string();
|
|
|
|
|
static std::string get_today_date();
|
|
|
|
|
static void open_log_file();
|
|
|
|
|
static void check_date_rollover();
|
2025-11-13 14:40:58 +08:00
|
|
|
static void cleanup_old_logs();
|
2025-09-08 10:59:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 简化宏
|
|
|
|
|
#define LOG_INFO(msg) Logger::log(LogLevel::INFO, msg)
|
|
|
|
|
#define LOG_WARN(msg) Logger::log(LogLevel::WARN, msg)
|
|
|
|
|
#define LOG_ERROR(msg) Logger::log(LogLevel::ERROR, msg)
|