From 79d067bcf500355e65e26a11c345a9427ed0128c Mon Sep 17 00:00:00 2001 From: congmu <congmu2024@163.com> Date: 星期三, 30 十月 2024 15:02:06 +0800 Subject: [PATCH] Merge branch 'master' of ssh://115.28.86.8:29418/~admin/昆仑_1025 --- Server/马丽萍/code/log/log.h | 56 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 42 insertions(+), 14 deletions(-) diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/log.h" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/log.h" index 64972af..e0f2c25 100644 --- "a/Server/\351\251\254\344\270\275\350\220\215/code/log/log.h" +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/log.h" @@ -13,7 +13,7 @@ class Log { public: - //C++11浠ュ悗,浣跨敤灞�閮ㄥ彉閲忔噿姹変笉鐢ㄥ姞閿� + // C++11浠ュ悗,浣跨敤灞�閮ㄥ彉閲忔噿姹変笉鐢ㄥ姞閿� static Log *get_instance() { static Log instance; @@ -24,41 +24,69 @@ { Log::get_instance()->async_write_log(); } - //鍙�夋嫨鐨勫弬鏁版湁鏃ュ織鏂囦欢銆佹棩蹇楃紦鍐插尯澶у皬銆佹渶澶ц鏁颁互鍙婃渶闀挎棩蹇楁潯闃熷垪 + + // 鍙�夋嫨鐨勫弬鏁版湁鏃ュ織鏂囦欢銆佹棩蹇楃紦鍐插尯澶у皬銆佹渶澶ц鏁颁互鍙婃渶闀挎棩蹇楁潯闃熷垪 bool init(const char *file_name, int close_log, int log_buf_size = 8192, int split_lines = 5000000, int max_queue_size = 0); void write_log(int level, const char *format, ...); - void flush(void); private: Log(); virtual ~Log(); + void *async_write_log() { string single_log; - //浠庨樆濉為槦鍒椾腑鍙栧嚭涓�涓棩蹇梥tring锛屽啓鍏ユ枃浠� + // 浠庨樆濉為槦鍒椾腑鍙栧嚭涓�涓棩蹇梥tring锛屽啓鍏ユ枃浠� while (m_log_queue->pop(single_log)) { m_mutex.lock(); + if (check_log_size()) // 妫�鏌ユ枃浠跺ぇ灏� + { + rotate_logs(); // 杞崲鏃ュ織 + } fputs(single_log.c_str(), m_fp); m_mutex.unlock(); } } + // 妫�鏌ユ棩蹇楁枃浠跺ぇ灏� + bool check_log_size() + { + if (m_fp) + { + fseek(m_fp, 0, SEEK_END); + long file_size = ftell(m_fp); + return file_size >= max_size; // 杩斿洖鏂囦欢鏄惁瓒呭嚭闄愬埗 + } + return false; + } + + // 杞崲鏃ュ織鏂囦欢 + void rotate_logs() + { + fclose(m_fp); // 鍏抽棴褰撳墠鏃ュ織鏂囦欢 + char new_log[256] = {0}; + snprintf(new_log, sizeof(new_log), "%s/%s.%lld", dir_name, log_name, m_count / m_split_lines); + m_fp = fopen(new_log, "a"); // 鎵撳紑鏂扮殑鏃ュ織鏂囦欢 + m_count = 0; // 閲嶇疆鏃ュ織璁℃暟 + } + private: - char dir_name[128]; //璺緞鍚� - char log_name[128]; //log鏂囦欢鍚� - int m_split_lines; //鏃ュ織鏈�澶ц鏁� - int m_log_buf_size; //鏃ュ織缂撳啿鍖哄ぇ灏� - long long m_count; //鏃ュ織琛屾暟璁板綍 - int m_today; //鍥犱负鎸夊ぉ鍒嗙被,璁板綍褰撳墠鏃堕棿鏄偅涓�澶� - FILE *m_fp; //鎵撳紑log鐨勬枃浠舵寚閽� + char dir_name[128]; // 璺緞鍚� + char log_name[128]; // log鏂囦欢鍚� + int m_split_lines; // 鏃ュ織鏈�澶ц鏁� + int m_log_buf_size; // 鏃ュ織缂撳啿鍖哄ぇ灏� + long long m_count; // 鏃ュ織琛屾暟璁板綍 + int m_today; // 鍥犱负鎸夊ぉ鍒嗙被,璁板綍褰撳墠鏃堕棿鏄偅涓�澶� + FILE *m_fp; // 鎵撳紑log鐨勬枃浠舵寚閽� char *m_buf; - block_queue<string> *m_log_queue; //闃诲闃熷垪 - bool m_is_async; //鏄惁鍚屾鏍囧織浣� + block_queue<string> *m_log_queue; // 闃诲闃熷垪 + bool m_is_async; // 鏄惁鍚屾鏍囧織浣� locker m_mutex; - int m_close_log; //鍏抽棴鏃ュ織 + int m_close_log; // 鍏抽棴鏃ュ織 + long max_size = 10 * 1024 * 1024; // 10MB }; #define LOG_DEBUG(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(0, format, ##__VA_ARGS__); Log::get_instance()->flush();} -- Gitblit v1.8.0