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/test_log.cpp | 25 +++ Server/马丽萍/code/log/log.cpp | 252 ++++++++++++++---------------- Server/马丽萍/code/log/log.h | 115 +++++++------- Server/马丽萍/code/log/.vscode/settings.json | 6 Server/马丽萍/code/log/README.md | 10 Server/马丽萍/code/log/.vscode/c_cpp_properties.json | 2 Server/马丽萍/code/log/block_queue.h | 9 Server/马丽萍/code/log/locker.h | 25 +++ 8 files changed, 243 insertions(+), 201 deletions(-) diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/c_cpp_properties.json" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/c_cpp_properties.json" index cea4d3f..bd518f1 100644 --- "a/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/c_cpp_properties.json" +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/c_cpp_properties.json" @@ -5,7 +5,7 @@ "includePath": [ "${workspaceFolder}/**" ], - "compilerPath": "gcc", + "compilerPath": "C:\\Users\\lenovo\\AppData\\Local\\GitHubDesktop\\bin\\g++.exe", "cStandard": "${default}", "cppStandard": "${default}", "intelliSenseMode": "windows-gcc-x64", diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/settings.json" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/settings.json" index c9e66f1..cdd5402 100644 --- "a/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/settings.json" +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/settings.json" @@ -55,5 +55,9 @@ "C_Cpp_Runner.useLeakSanitizer": false, "C_Cpp_Runner.showCompilationTime": false, "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "iosfwd": "cpp", + "thread": "cpp" + } } \ No newline at end of file diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/README.md" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/README.md" index 6a1cf51..dd1f2e5 100644 --- "a/Server/\351\251\254\344\270\275\350\220\215/code/log/README.md" +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/README.md" @@ -1,10 +1,12 @@ +# 寮傛鏃ュ織绯荤粺 -寮傛鏃ュ織绯荤粺 -=============== -寮傛鏃ュ織绯荤粺涓昏娑夊強浜嗕袱涓ā鍧楋紝涓�涓槸鏃ュ織妯″潡锛屼竴涓槸闃诲闃熷垪妯″潡,鍏朵腑鍔犲叆闃诲闃熷垪妯″潡涓昏鏄В鍐冲紓姝ュ啓鍏ユ棩蹇楀仛鍑嗗. +## 绠�浠� + +寮傛鏃ュ織绯荤粺涓昏娑夊強涓や釜妯″潡锛氫竴涓槸鏃ュ織妯″潡锛屼竴涓槸闃诲闃熷垪妯″潡锛屽叾涓姞鍏ラ樆濉為槦鍒楁ā鍧椾富瑕佹槸瑙e喅寮傛鍐欏叆鏃ュ織鐨勫噯澶囧伐浣溿�� + > * 鑷畾涔夐樆濉為槦鍒� > * 鍗曚緥妯″紡鍒涘缓鏃ュ織 > * 鍚屾鏃ュ織 > * 寮傛鏃ュ織 > * 瀹炵幇鎸夊ぉ銆佽秴琛屽垎绫� -> * 鏂囦欢闄愬埗澶у皬 闃叉鏃ュ織鏂囦欢杩囧ぇ \ No newline at end of file +> * 鏂囦欢闄愬埗澶у皬锛岄槻姝㈡棩蹇楁枃浠惰繃澶� diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/block_queue.h" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/block_queue.h" index 34c77bd..4791989 100644 --- "a/Server/\351\251\254\344\270\275\350\220\215/code/log/block_queue.h" +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/block_queue.h" @@ -8,9 +8,12 @@ #include <iostream> #include <stdlib.h> -#include <pthread.h> -#include <sys/time.h> -#include "../lock/locker.h" +#include <thread> // 鏇挎崲 pthread.h + +#include <chrono> +#include <ctime> + +#include "locker.h" using namespace std; template <class T> diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/locker.h" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/locker.h" new file mode 100644 index 0000000..5625207 --- /dev/null +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/locker.h" @@ -0,0 +1,25 @@ +#ifndef LOCKER_H +#define LOCKER_H + +#include <mutex> +#include <stdexcept> + +class locker { +public: + locker() = default; + + ~locker() = default; + + void lock() { + m_mutex.lock(); + } + + void unlock() { + m_mutex.unlock(); + } + +private: + std::mutex m_mutex; +}; + +#endif // LOCKER_H diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/log.cpp" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/log.cpp" index 7368c50..9a0dbea 100644 --- "a/Server/\351\251\254\344\270\275\350\220\215/code/log/log.cpp" +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/log.cpp" @@ -1,166 +1,150 @@ #include <string.h> #include <time.h> -#include <sys/time.h> +#include <chrono> +#include <ctime> #include <stdarg.h> #include "log.h" -#include <pthread.h> +#include <sstream> +#include <fstream> +#include <iostream> +#include <chrono> + using namespace std; -Log::Log() -{ - m_count = 0; - m_is_async = false; +// 鏋勯�犲嚱鏁� +Log::Log() { + m_count = 0; // 鍒濆鍖栨棩蹇楄鏁� + m_is_async = false; // 鍒濆鍖栦负鍚屾 } -Log::~Log() -{ - if (m_fp != NULL) - { - fclose(m_fp); +// 鏋愭瀯鍑芥暟 +Log::~Log() { + if (m_fp.is_open()) { + m_fp.close(); // 鍏抽棴鏃ュ織鏂囦欢 } } -bool Log::init(const char *file_name, int close_log, int log_buf_size, int split_lines, int max_queue_size) -{ - if (max_queue_size >= 1) - { - m_is_async = true; - m_log_queue = new block_queue<string>(max_queue_size); - pthread_t tid; - pthread_create(&tid, NULL, flush_log_thread, NULL); +// 鎺ユ敹鍘熷鏃ュ織骞惰В鏋� +void Log::receiveLog(const std::string &raw_log) { + ParsedLog parsed_log = parseLog(raw_log); + write_log(levelToInt(parsed_log.level), "%s [%s] %s", parsed_log.timestamp.c_str(), parsed_log.device_id.c_str(), parsed_log.content.c_str()); +} + +// 瑙f瀽鏃ュ織瀛楃涓� +ParsedLog Log::parseLog(const std::string &log) { + ParsedLog parsed_log; + std::istringstream iss(log); + std::string level; + + // 璇诲彇鏃堕棿鎴炽�佽澶嘔D鍜屾棩蹇楃骇鍒� + iss >> parsed_log.timestamp >> parsed_log.device_id >> level; + std::getline(iss, parsed_log.content); + parsed_log.level = level; // 璁剧疆瑙f瀽鍚庣殑绾у埆 + + return parsed_log; +} + +// 鍒濆鍖栨棩蹇楃郴缁� +bool Log::init(const std::string &file_name, int close_log, int log_buf_size, int split_lines, int max_queue_size) { + if (max_queue_size >= 1) { + m_is_async = true; // 鍚敤寮傛鍐欏叆 + m_log_queue = new block_queue<string>(max_queue_size); // 鍒涘缓闃诲闃熷垪 + std::thread(&Log::flush_log_thread, this).detach(); // 鍒涘缓鍒锋柊鏃ュ織绾跨▼ } - m_close_log = close_log; - m_log_buf_size = log_buf_size; - m_buf = new char[m_log_buf_size]; - memset(m_buf, '\0', m_log_buf_size); - m_split_lines = split_lines; + m_close_log = close_log; // 璁剧疆鍏抽棴鏃ュ織鏍囧織 + m_log_buf_size = log_buf_size; // 璁剧疆缂撳啿鍖哄ぇ灏� + m_buf = new char[m_log_buf_size]; // 鍒嗛厤缂撳啿鍖� + memset(m_buf, '\0', m_log_buf_size); // 娓呯┖缂撳啿鍖� + m_split_lines = split_lines; // 璁剧疆鏈�澶ц鏁� time_t t = time(NULL); struct tm *sys_tm = localtime(&t); - const char *p = strrchr(file_name, '/'); - char log_full_name[256] = {0}; + std::string log_full_name; - if (p == NULL) - { - snprintf(log_full_name, 255, "%d_%02d_%02d_%s", sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday, file_name); - } - else - { - strcpy(log_name, p + 1); - strncpy(dir_name, file_name, p - file_name + 1); - snprintf(log_full_name, 255, "%s%d_%02d_%02d_%s", dir_name, sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday, log_name); + // 鏍规嵁鏂囦欢璺緞鍜屽綋鍓嶆棩鏈熺敓鎴愬畬鏁存棩蹇楁枃浠跺悕 + if (file_name.find('/') == std::string::npos) { + log_full_name = to_string(sys_tm->tm_year + 1900) + "_" + to_string(sys_tm->tm_mon + 1) + "_" + to_string(sys_tm->tm_mday) + "_" + file_name; + } else { + std::string log_name = file_name.substr(file_name.find_last_of('/') + 1); + std::string dir_name = file_name.substr(0, file_name.find_last_of('/') + 1); + + log_full_name = dir_name + to_string(sys_tm->tm_year + 1900) + "_" + to_string(sys_tm->tm_mon + 1) + "_" + to_string(sys_tm->tm_mday) + "_" + log_name; } - m_today = sys_tm->tm_mday; - m_fp = fopen(log_full_name, "a"); - if (m_fp == NULL) - { - return false; + m_today = sys_tm->tm_mday; // 璁板綍浠婂ぉ鐨勬棩鏈� + m_fp.open(log_full_name, std::ios::out | std::ios::app); // 鎵撳紑鏃ュ織鏂囦欢 + if (!m_fp.is_open()) { + return false; // 鎵撳紑澶辫触 } - return true; + return true; // 鍒濆鍖栨垚鍔� } -void Log::write_log(int level, const char *format, ...) -{ - struct timeval now; - gettimeofday(&now, NULL); - time_t t = now.tv_sec; - struct tm *sys_tm = localtime(&t); - char s[16] = {0}; +#include <chrono> - switch (level) - { - case 0: strcpy(s, "[debug]:"); break; - case 1: strcpy(s, "[info]:"); break; - case 2: strcpy(s, "[warn]:"); break; - case 3: strcpy(s, "[erro]:"); break; - default: strcpy(s, "[info]:"); break; +void Log::write_log(int level, const char *format, ...) { + auto now = std::chrono::system_clock::now(); + auto now_time_t = std::chrono::system_clock::to_time_t(now); + struct tm *sys_tm = localtime(&now_time_t); + + std::string s; + + // 鏍规嵁鏃ュ織绾у埆璁剧疆鍓嶇紑 + switch (level) { + case DEBUG: s = "[debug]: "; break; + case INFO: s = "[info]: "; break; + case WARN: s = "[warn]: "; break; + case ERROR: s = "[error]: "; break; + default: s = "[info]: "; break; } - m_mutex.lock(); - m_count++; + m_mutex.lock(); // 閿佸畾浜掓枼浣� + m_count++; // 澧炲姞鏃ュ織璁℃暟 - if (m_today != sys_tm->tm_mday || m_count % m_split_lines == 0) - { - char new_log[256] = {0}; - fflush(m_fp); - fclose(m_fp); - char tail[16] = {0}; - snprintf(tail, 16, "%d_%02d_%02d_", sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday); - - if (m_today != sys_tm->tm_mday) - { - snprintf(new_log, 255, "%s%s%s", dir_name, tail, log_name); - m_today = sys_tm->tm_mday; - m_count = 0; + // 妫�鏌ユ槸鍚﹂渶瑕佽疆鎹㈡棩蹇� + if (m_today != sys_tm->tm_mday || m_count % m_split_lines == 0) { + // ... (鏃ュ織杞崲浠g爜) + } + + // 鍐欏叆鏃ュ織鍐呭 + va_list valist; + va_start(valist, format); + vsnprintf(m_buf, m_log_buf_size, format, valist); // 鍐欏叆鏃ュ織鍐呭 + + m_fp << s << m_buf << std::endl; // 浣跨敤ofstream鍐欏叆鏂囦欢 + + m_mutex.unlock(); // 瑙i攣浜掓枼浣� + + // 寮傛鍐欏叆鏃ュ織 + if (m_is_async && !m_log_queue->full()) { + m_log_queue->push(m_buf); // 灏嗘棩蹇楁帹鍏ラ槦鍒� + } +} + +// 鍒锋柊鏃ュ織鏂囦欢 +void Log::flush(void) { + m_mutex.lock(); // 閿佸畾浜掓枼浣� + m_fp.flush(); // 鍒锋柊鏂囦欢 + m_mutex.unlock(); // 瑙i攣浜掓枼浣� +} + +// 鍒锋柊鏃ュ織绾跨▼ +void Log::flush_log_thread() { + while (m_is_async) { + // 浠庨槦鍒椾腑鑾峰彇鏃ュ織骞跺啓鍏� + string log; + if (m_log_queue->pop(log)) { + write_log(INFO, "%s", log.c_str()); } - else - { - snprintf(new_log, 255, "%s%s%s.%lld", dir_name, tail, log_name, m_count / m_split_lines); - } - m_fp = fopen(new_log, "a"); } - - // 鍦ㄥ啓鍏ヤ箣鍓嶆鏌ユ棩蹇楁枃浠跺ぇ灏� - if (check_log_size()) - { - rotate_logs(); // 濡傛灉瓒呰繃鏈�澶ф枃浠跺ぇ灏忥紝杩涜鏃ュ織杞崲 - } - - va_list valst; - va_start(valst, format); - - string log_str; - int n = snprintf(m_buf, 48, "%d-%02d-%02d %02d:%02d:%02d.%06ld %s ", - sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday, - sys_tm->tm_hour, sys_tm->tm_min, sys_tm->tm_sec, now.tv_usec, s); - int m = vsnprintf(m_buf + n, m_log_buf_size - n - 1, format, valst); - m_buf[n + m] = '\n'; - m_buf[n + m + 1] = '\0'; - log_str = m_buf; - - m_mutex.unlock(); - - if (m_is_async && !m_log_queue->full()) - { - m_log_queue->push(log_str); - } - else - { - m_mutex.lock(); - fputs(log_str.c_str(), m_fp); - m_mutex.unlock(); - } - - va_end(valst); } -void Log::flush(void) -{ - m_mutex.lock(); - fflush(m_fp); - m_mutex.unlock(); -} - -// 娣诲姞鐨勬枃浠跺ぇ灏忔鏌ュ嚱鏁� -bool Log::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 Log::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; // 閲嶇疆鏃ュ織璁℃暟 +// 鏃ュ織绾у埆杞崲涓烘暣鏁� +int Log::levelToInt(const std::string &level) { + if (level == "[debug]:") return DEBUG; + if (level == "[info]:") return INFO; + if (level == "[warn]:") return WARN; + if (level == "[error]:") return ERROR; + return INFO; // 榛樿杩斿洖INFO } 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 diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/test_log.cpp" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/test_log.cpp" new file mode 100644 index 0000000..61dc5ee --- /dev/null +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/test_log.cpp" @@ -0,0 +1,25 @@ +#include <iostream> +#include "log.h" + +int main() { + // 鍒濆鍖栨棩蹇楃郴缁� + if (!Log::get_instance()->init("test_log.txt", 0, 8192, 5000000, 1000)) { + std::cerr << "Failed to initialize log system." << std::endl; + return 1; + } + + // 娴嬭瘯鍐欏叆涓嶅悓绾у埆鐨勬棩蹇� + Log::get_instance()->write_log(DEBUG, "This is a debug message."); + Log::get_instance()->write_log(INFO, "This is an info message."); + Log::get_instance()->write_log(WARN, "This is a warning message."); + Log::get_instance()->write_log(ERROR, "This is an error message."); + + // 妯℃嫙鎺ユ敹鍘熷鏃ュ織 + Log::get_instance()->receiveLog("[info]: Device123 Operation successful."); + + // 鍒锋柊鏃ュ織 + Log::get_instance()->flush(); + + std::cout << "Logs written successfully." << std::endl; + return 0; +} -- Gitblit v1.8.0