From d8318ea48f3b7594726283b766db63b70493368a Mon Sep 17 00:00:00 2001
From: fufu <2398318817@qq.com>
Date: 星期三, 30 十月 2024 10:14:21 +0800
Subject: [PATCH] 提交日志文件

---
 Server/马丽萍/code/log/log.cpp |   92 +++++++++++++++++++++++----------------------
 1 files changed, 47 insertions(+), 45 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 411f8eb..7368c50 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"
@@ -19,19 +19,17 @@
         fclose(m_fp);
     }
 }
-//寮傛闇�瑕佽缃樆濉為槦鍒楃殑闀垮害锛屽悓姝ヤ笉闇�瑕佽缃�
+
 bool Log::init(const char *file_name, int close_log, int log_buf_size, int split_lines, int max_queue_size)
 {
-    //濡傛灉璁剧疆浜唌ax_queue_size,鍒欒缃负寮傛
     if (max_queue_size >= 1)
     {
         m_is_async = true;
         m_log_queue = new block_queue<string>(max_queue_size);
         pthread_t tid;
-        //flush_log_thread涓哄洖璋冨嚱鏁�,杩欓噷琛ㄧず鍒涘缓绾跨▼寮傛鍐欐棩蹇�
         pthread_create(&tid, NULL, flush_log_thread, NULL);
     }
-    
+
     m_close_log = close_log;
     m_log_buf_size = log_buf_size;
     m_buf = new char[m_log_buf_size];
@@ -40,25 +38,21 @@
 
     time_t t = time(NULL);
     struct tm *sys_tm = localtime(&t);
-    struct tm my_tm = *sys_tm;
-
- 
     const char *p = strrchr(file_name, '/');
     char log_full_name[256] = {0};
 
     if (p == NULL)
     {
-        snprintf(log_full_name, 255, "%d_%02d_%02d_%s", my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday, file_name);
+        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, my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday, log_name);
+        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);
     }
 
-    m_today = my_tm.tm_mday;
-    
+    m_today = sys_tm->tm_mday;
     m_fp = fopen(log_full_name, "a");
     if (m_fp == NULL)
     {
@@ -70,48 +64,36 @@
 
 void Log::write_log(int level, const char *format, ...)
 {
-    struct timeval now = {0, 0};
+    struct timeval now;
     gettimeofday(&now, NULL);
     time_t t = now.tv_sec;
     struct tm *sys_tm = localtime(&t);
-    struct tm my_tm = *sys_tm;
     char s[16] = {0};
+
     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;
+        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;
     }
-    //鍐欏叆涓�涓猯og锛屽m_count++, m_split_lines鏈�澶ц鏁�
+
     m_mutex.lock();
     m_count++;
 
-    if (m_today != my_tm.tm_mday || m_count % m_split_lines == 0) //everyday log
+    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_", my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday);
-       
-        if (m_today != my_tm.tm_mday)
+        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 = my_tm.tm_mday;
+            m_today = sys_tm->tm_mday;
             m_count = 0;
         }
         else
@@ -120,20 +102,20 @@
         }
         m_fp = fopen(new_log, "a");
     }
- 
-    m_mutex.unlock();
+
+    // 鍦ㄥ啓鍏ヤ箣鍓嶆鏌ユ棩蹇楁枃浠跺ぇ灏�
+    if (check_log_size())
+    {
+        rotate_logs(); // 濡傛灉瓒呰繃鏈�澶ф枃浠跺ぇ灏忥紝杩涜鏃ュ織杞崲
+    }
 
     va_list valst;
     va_start(valst, format);
 
     string log_str;
-    m_mutex.lock();
-
-    //鍐欏叆鐨勫叿浣撴椂闂村唴瀹规牸寮�
     int n = snprintf(m_buf, 48, "%d-%02d-%02d %02d:%02d:%02d.%06ld %s ",
-                     my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday,
-                     my_tm.tm_hour, my_tm.tm_min, my_tm.tm_sec, now.tv_usec, 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';
@@ -158,7 +140,27 @@
 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; // 閲嶇疆鏃ュ織璁℃暟
+}

--
Gitblit v1.8.0