yituo_video/include/logger.hpp

44 lines
947 B
C++
Raw Permalink Normal View History

2025-09-08 14:55:07 +08:00
// logger.hpp
2025-09-08 10:59:08 +08:00
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <chrono>
#include <iomanip>
#include <sstream>
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);
2025-09-09 14:16:18 +08:00
static std::string get_current_time_utc8();
2025-09-08 10:59:08 +08:00
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)