log_manager.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * log_manager.h - 内存日志缓冲管理系统
  3. * fix24 v36: 日志写入内存,定时刷盘,Web从内存读取
  4. */
  5. #ifndef LOG_MANAGER_H
  6. #define LOG_MANAGER_H
  7. #include <string>
  8. #include <deque>
  9. #include <mutex>
  10. #include <atomic>
  11. #include <thread>
  12. #include <condition_variable>
  13. // ==================== 日志条目结构 ====================
  14. struct LogEntry {
  15. std::string timestamp; // "2026-07-05 23:15:00"
  16. std::string level; // INFO / WARN / ERROR
  17. std::string message; // 日志内容(单行)
  18. size_t byte_size; // 该条目占用内存字节数
  19. LogEntry() : byte_size(0) {}
  20. LogEntry(const std::string& ts, const std::string& lv, const std::string& msg)
  21. : timestamp(ts), level(lv), message(msg) {
  22. byte_size = ts.size() + lv.size() + msg.size() + 32; // overhead
  23. }
  24. };
  25. // ==================== 日志状态(Web API返回) ====================
  26. struct LogStatus {
  27. std::string log_file_path; // 日志文件路径
  28. long file_size_mb; // 磁盘文件大小 (MB)
  29. long buffer_size_mb; // 缓冲区上限配置 (MB)
  30. double buffer_usage_percent; // 缓冲区使用率 (%)
  31. std::string flush_time; // 定时刷盘时间
  32. int retention_days; // 保留天数
  33. long line_count; // 内存中总条数
  34. std::string last_modified; // 最后修改时间(文件/刷盘)
  35. bool redirect_enabled; // stdout 重定向是否启用
  36. bool memory_enabled; // 内存模式是否启用
  37. long memory_usage_bytes; // 当前内存占用(字节)
  38. std::string last_flush_time; // 上次刷盘时间
  39. long total_flush_count; // 累计刷盘次数
  40. };
  41. // ==================== 内存日志缓冲区 ====================
  42. class MemoryLogBuffer {
  43. public:
  44. MemoryLogBuffer();
  45. ~MemoryLogBuffer();
  46. // 初始化(从config读取参数后调用)
  47. void init(size_t max_bytes, const std::string& flush_time,
  48. const std::string& log_file, int retention_days,
  49. bool redirect_stdout, bool auto_flush_on_exit);
  50. // 启动(创建刷盘线程 + stdout重定向)
  51. bool start();
  52. // 停止(刷盘 + 关闭线程)
  53. void stop();
  54. // 写入日志条目
  55. void push(const std::string& level, const std::string& message);
  56. // 获取最后N条
  57. std::deque<LogEntry> tail(int n);
  58. // 获取状态
  59. LogStatus get_status();
  60. // 手动刷盘到文件
  61. bool flush_to_file(std::string& error);
  62. // 清理过期日志
  63. bool cleanup_old_logs(std::string& error);
  64. // 信号处理调用(SIGTERM/SIGINT时调用)
  65. void signal_flush();
  66. // 全局单例
  67. static MemoryLogBuffer& instance();
  68. private:
  69. // 刷盘线程
  70. void flush_thread_func();
  71. // stdout读取线程(当redirect_stdout=true时)
  72. void stdout_reader_func();
  73. // 解析刷盘时间 "HH:MM" → 时、分
  74. bool parse_flush_time(const std::string& time_str, int& hour, int& minute);
  75. // 生成带日期的归档文件名
  76. std::string generate_archive_filename();
  77. // 获取当前时间字符串
  78. std::string current_timestamp();
  79. // 环形缓冲区
  80. std::deque<LogEntry> buffer_;
  81. size_t max_memory_bytes_;
  82. size_t current_bytes_;
  83. std::mutex mutex_;
  84. // 配置
  85. std::string log_file_; // PlateRecApp.log
  86. std::string flush_time_; // "23:20"
  87. int retention_days_;
  88. bool redirect_stdout_;
  89. bool auto_flush_on_exit_;
  90. bool enabled_; // enabled=1 内存模式, =0 直写模式
  91. // 刷盘线程
  92. std::thread flush_thread_;
  93. std::mutex flush_mutex_;
  94. std::condition_variable flush_cv_;
  95. std::atomic<bool> flush_running_;
  96. // stdout重定向
  97. std::thread stdout_thread_;
  98. int pipe_fd_[2]; // pipefd[0]=读端, pipefd[1]=写端
  99. int saved_stdout_; // 备份原始stdout fd
  100. int saved_stderr_; // 备份原始stderr fd
  101. std::atomic<bool> stdout_redirect_active_;
  102. // 统计
  103. std::atomic<long> total_flush_count_;
  104. std::string last_flush_time_;
  105. std::mutex stats_mutex_;
  106. };
  107. // ==================== 兼容旧接口(namespace log_mgr) ====================
  108. namespace log_mgr {
  109. // 获取日志状态
  110. LogStatus get_log_status();
  111. // 手动触发日志刷新(sync)
  112. bool flush_log(std::string &error);
  113. // 清理过期日志
  114. bool cleanup_old_logs(std::string &error);
  115. // 获取日志文件最后 N 行
  116. std::string tail_log(int lines);
  117. } // namespace log_mgr
  118. #endif // LOG_MANAGER_H