From a0a5a8158c38872e947b9ba3a3cdfa3be9a1e0fb Mon Sep 17 00:00:00 2001
From: wxx <1321839169@qq.com>
Date: 星期四, 31 十月 2024 15:24:44 +0800
Subject: [PATCH] 提交日报
---
Server/马丽萍/code/log/log.cpp | 252 +++++++++++++++++++++++--------------------------
1 files changed, 118 insertions(+), 134 deletions(-)
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
}
--
Gitblit v1.8.0