From ff5683933430799b7baf31c08a9ca8eaa9b17087 Mon Sep 17 00:00:00 2001 From: cxh Date: Thu, 13 Nov 2025 14:24:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=BB=93=E6=9E=84=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/logger.hpp | 31 +++++++------- src/logger.cpp | 103 +++++++++++++++++++++------------------------ src/main.cpp | 6 +-- 3 files changed, 66 insertions(+), 74 deletions(-) diff --git a/include/logger.hpp b/include/logger.hpp index 0eef0e9..2d1016d 100644 --- a/include/logger.hpp +++ b/include/logger.hpp @@ -1,13 +1,14 @@ // logger.hpp #pragma once -#include -#include -#include -#include #include +#include +#include +#include #include +#include #include +#include enum class LogLevel { @@ -18,23 +19,23 @@ enum class LogLevel class Logger { -public: - static void set_log_to_file(const std::string &filename); + public: + static void init(const std::string& log_dir); - static void log(LogLevel level, const std::string &msg); + static void log(LogLevel level, const std::string& msg); static std::string get_current_time_utc8(); -private: + private: static std::ofstream log_file; - static std::string current_log_filename; + static std::string log_directory; + static std::string current_date; + static std::string current_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; + static std::string get_today_date(); + static void open_log_file(); + static void check_date_rollover(); }; // 简化宏 diff --git a/src/logger.cpp b/src/logger.cpp index 6e47d64..2ccd986 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -1,23 +1,41 @@ -// logger.cpp #include "logger.hpp" -#include -#include -#include -#include -#include -#include - std::ofstream Logger::log_file; -std::string Logger::current_log_filename; +std::string Logger::log_directory; +std::string Logger::current_date; +std::string Logger::current_filename; -void Logger::set_log_to_file(const std::string& filename) +void Logger::init(const std::string& log_dir) { - current_log_filename = filename; - log_file.open(filename, std::ios::app); + log_directory = log_dir; + + if (!std::filesystem::exists(log_directory)) std::filesystem::create_directories(log_directory); + + current_date = get_today_date(); + open_log_file(); +} + +void Logger::open_log_file() +{ + current_filename = log_directory + "/" + current_date + ".log"; + + if (log_file.is_open()) log_file.close(); + + log_file.open(current_filename, std::ios::app); + if (!log_file.is_open()) { - std::cerr << "[Logger] Failed to open log file: " << filename << std::endl; + 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(); } } @@ -32,29 +50,38 @@ std::string Logger::get_time_string() std::tm tm_now = *std::localtime(&t); std::ostringstream ss; - ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S") << '.' << std::setw(3) << std::setfill('0') << ms.count(); + ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S") << "." << std::setw(3) << std::setfill('0') << ms.count(); + return ss.str(); +} +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"); return ss.str(); } std::string Logger::get_current_time_utc8() { - using namespace std::chrono; + auto now = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; - auto now = system_clock::now(); - auto ms = duration_cast(now.time_since_epoch()) % 1000; - - std::time_t t = system_clock::to_time_t(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%H%M%S") << std::setw(3) << std::setfill('0') << ms.count(); - return ss.str(); } void Logger::log(LogLevel level, const std::string& msg) { + check_date_rollover(); + std::string level_str; switch (level) { @@ -77,41 +104,5 @@ void Logger::log(LogLevel level, const std::string& msg) { log_file << full_msg << std::endl; log_file.flush(); - - // 检查是否需要轮转 - if (get_file_size(current_log_filename) >= MAX_LOG_FILE_SIZE) - { - rotate_logs(); - } } } - -size_t Logger::get_file_size(const std::string& filename) -{ - std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary); - return in.is_open() ? static_cast(in.tellg()) : 0; -} - -void Logger::rotate_logs() -{ - log_file.close(); - - // 删除最旧的日志 - std::string oldest = current_log_filename + "." + std::to_string(MAX_LOG_BACKUP_COUNT); - std::remove(oldest.c_str()); - - // 重命名 *.4 -> *.5, ..., *.1 -> *.2 - for (int i = MAX_LOG_BACKUP_COUNT - 1; i >= 1; --i) - { - std::string old_name = current_log_filename + "." + std::to_string(i); - std::string new_name = current_log_filename + "." + std::to_string(i + 1); - std::rename(old_name.c_str(), new_name.c_str()); - } - - // 当前日志 -> .1 - std::string first_backup = current_log_filename + ".1"; - std::rename(current_log_filename.c_str(), first_backup.c_str()); - - // 重新打开新日志 - log_file.open(current_log_filename, std::ios::trunc); -} diff --git a/src/main.cpp b/src/main.cpp index 5bd5b22..0e4feb3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,7 @@ int main() signal(SIGPIPE, SIG_IGN); // ---------- 初始化日志 ---------- - Logger::set_log_to_file(get_executable_dir_file_path("app.log")); + Logger::init(get_executable_dir_file_path("logs")); LOG_INFO("[MAIN] ===== Vehicle Video Service Starting ====="); try @@ -41,7 +41,7 @@ int main() g_app_config = AppConfig::load_from_file(get_executable_dir_file_path("config.json")); LOG_INFO("[MAIN] Loaded config from config.json"); } - catch (const std::exception &e) + catch (const std::exception& e) { LOG_ERROR(std::string("[MAIN] Failed to load config: ") + e.what()); return -1; @@ -63,7 +63,7 @@ int main() LOG_INFO("[MAIN] MQTT thread started."); mqtt_client_thread_func(); // 在回调里执行推流控制 } - catch (const std::exception &e) + catch (const std::exception& e) { LOG_ERROR(std::string("[MAIN] MQTT thread crashed: ") + e.what()); }