From e012082cb3c91af96619f0f982d5f2e8b4b705af Mon Sep 17 00:00:00 2001 From: wxx <1321839169@qq.com> Date: 星期三, 30 十月 2024 15:07:26 +0800 Subject: [PATCH] Merge branch 'master' of ssh://115.28.86.8:29418/~admin/昆仑_1025 --- Server/马丽萍/code/log/log.h | 115 ++++++++++++++++++++++++++++----------------------------- 1 files changed, 57 insertions(+), 58 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 e0f2c25..be28a84 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" @@ -5,93 +5,92 @@ #include <iostream> #include <string> #include <stdarg.h> -#include <pthread.h> +#include <thread> #include "block_queue.h" +#include "locker.h" // 纭繚 locker.h 琚寘鍚� +#include <fstream> using namespace std; + +// 鏃ュ織绾у埆鏋氫妇 +enum LogLevel { + DEBUG = 0, + INFO, + WARN, + ERROR +}; + +// 瀛樺偍瑙f瀽鏃ュ織鏂囦欢鐨勭粨鏋勪綋 +struct ParsedLog { + std::string timestamp; // 鏃堕棿鎴� + std::string device_id; // 璁惧ID + std::string level; // 鏃ュ織绾у埆 + std::string content; // 鏃ュ織鍐呭 +}; class Log { public: - // C++11浠ュ悗,浣跨敤灞�閮ㄥ彉閲忔噿姹変笉鐢ㄥ姞閿� + // 鑾峰彇鍗曚緥瀹炰緥 static Log *get_instance() { static Log instance; return &instance; } - static void *flush_log_thread(void *args) - { - Log::get_instance()->async_write_log(); - } + // 鍒锋柊鏃ュ織绾跨▼ + void flush_log_thread(); // 淇敼涓洪潪闈欐�佹垚鍛樺嚱鏁� - // 鍙�夋嫨鐨勫弬鏁版湁鏃ュ織鏂囦欢銆佹棩蹇楃紦鍐插尯澶у皬銆佹渶澶ц鏁颁互鍙婃渶闀挎棩蹇楁潯闃熷垪 - bool init(const char *file_name, int close_log, int log_buf_size = 8192, int split_lines = 5000000, int max_queue_size = 0); + // 鍒濆鍖栨棩蹇楃郴缁燂紝鍙傛暟鍖呮嫭鏂囦欢鍚嶃�佹槸鍚﹀叧闂棩蹇椼�佺紦鍐插尯澶у皬銆佹渶澶ц鏁板拰鏈�澶ч槦鍒楀ぇ灏� + bool Log::init(const char *file_name, int close_log, int log_buf_size, int split_lines, int max_queue_size); + + // 鍐欏叆鏃ュ織 void write_log(int level, const char *format, ...); + + // 鍒锋柊鏃ュ織鏂囦欢 void flush(void); + //鎺ユ敹鍘熷鐨勮鍏叡绫� + void receiveLog(const std::string& raw_log); // 鎺ユ敹鍘熷鏃ュ織 + private: - Log(); - virtual ~Log(); + Log(); // 鏋勯�犲嚱鏁� + virtual ~Log(); // 鏋愭瀯鍑芥暟 + + void async_write_log(); // 寮傛鍐欏叆鏃ュ織 + bool check_log_size(); // 妫�鏌ユ棩蹇楁枃浠跺ぇ灏� + void rotate_logs(); // 杞崲鏃ュ織鏂囦欢 - void *async_write_log() - { - string single_log; - // 浠庨樆濉為槦鍒椾腑鍙栧嚭涓�涓棩蹇梥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(); - } - } + ParsedLog parseLog(const std::string& log); // 瑙f瀽鏃ュ織 + bool init(const std::string &file_name, int close_log, int log_buf_size, int split_lines, int max_queue_size); + void queryLogs(const std::string &device_id); // 鏌ヨ鏃ュ織 - // 妫�鏌ユ棩蹇楁枃浠跺ぇ灏� - 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; // 閲嶇疆鏃ュ織璁℃暟 - } + const char* log_level_to_string(int level); // 灏嗘棩蹇楃骇鍒浆鎹负瀛楃涓� + int levelToInt(const std::string &level); private: - char dir_name[128]; // 璺緞鍚� - char log_name[128]; // log鏂囦欢鍚� + char dir_name[128]; // 鏃ュ織鏂囦欢鐩綍 + char log_name[128]; // 鏃ュ織鏂囦欢鍚� int m_split_lines; // 鏃ュ織鏈�澶ц鏁� int m_log_buf_size; // 鏃ュ織缂撳啿鍖哄ぇ灏� long long m_count; // 鏃ュ織琛屾暟璁板綍 - int m_today; // 鍥犱负鎸夊ぉ鍒嗙被,璁板綍褰撳墠鏃堕棿鏄偅涓�澶� - FILE *m_fp; // 鎵撳紑log鐨勬枃浠舵寚閽� - char *m_buf; + int m_today; // 璁板綍褰撳墠鏃堕棿 + std::ofstream m_fp; // 灏� FILE * m_fp 鏇存敼涓� std::ofstream m_fp + char *m_buf; // 鏃ュ織缂撳啿鍖� block_queue<string> *m_log_queue; // 闃诲闃熷垪 - bool m_is_async; // 鏄惁鍚屾鏍囧織浣� - locker m_mutex; - int m_close_log; // 鍏抽棴鏃ュ織 + bool m_is_async; // 鏄惁寮傛鏍囧織浣� + locker m_mutex; // 浜掓枼閿� + int m_close_log; // 鍏抽棴鏃ュ織鐨勬爣蹇� long max_size = 10 * 1024 * 1024; // 10MB + //娴嬭瘯鏂囦欢閲岄潰鐨� + int m_close_log; // 纭繚鍦ㄨ繖閲屽畾涔� }; -#define LOG_DEBUG(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(0, format, ##__VA_ARGS__); Log::get_instance()->flush();} -#define LOG_INFO(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(1, format, ##__VA_ARGS__); Log::get_instance()->flush();} -#define LOG_WARN(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(2, format, ##__VA_ARGS__); Log::get_instance()->flush();} -#define LOG_ERROR(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(3, format, ##__VA_ARGS__); Log::get_instance()->flush();} +// 瀹忓畾涔夌敤浜庝笉鍚岀骇鍒殑鏃ュ織璁板綍 +#define LOG_DEBUG(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(DEBUG, format, ##__VA_ARGS__); Log::get_instance()->flush();} +#define LOG_INFO(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(INFO, format, ##__VA_ARGS__); Log::get_instance()->flush();} +#define LOG_WARN(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(WARN, format, ##__VA_ARGS__); Log::get_instance()->flush();} +#define LOG_ERROR(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(ERROR, format, ##__VA_ARGS__); Log::get_instance()->flush();} #endif -- Gitblit v1.8.0