// logger.hpp #pragma once #include #include #include #include #include #include #include enum class LogLevel { INFO, WARN, ERROR }; class Logger { public: static void set_log_to_file(const std::string &filename); static void log(LogLevel level, const std::string &msg); static std::string get_current_time_utc8(); private: static std::ofstream log_file; static std::string current_log_filename; static std::string get_time_string(); static void rotate_logs(); static size_t get_file_size(const std::string &filename); static constexpr size_t MAX_LOG_FILE_SIZE = 10 * 1024 * 1024; // 10MB static constexpr int MAX_LOG_BACKUP_COUNT = 5; }; // 简化宏 #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)