修复mbtk_logd进程 read log_radio buffer 节点大小,新增日志落盘功能

Change-Id: I3f1a1d148be26bad19b2e66b0ca7536df1e9c483
diff --git a/mbtk/mbtk_logd/alog_read.c b/mbtk/mbtk_logd/alog_read.c
index cfb7556..6ca249b 100755
--- a/mbtk/mbtk_logd/alog_read.c
+++ b/mbtk/mbtk_logd/alog_read.c
@@ -22,6 +22,9 @@
 #define    RADIOLOG_BUFF_SIZE (4*1024)
 #define    MAX_BUFFER_SIZE (8*1024)
 
+#define LOGGER_ENTRY_MAX_LEN (5*1024)
+
+
 typedef enum android_LogPriority {
     ANDROID_LOG_UNKNOWN = 0,
     ANDROID_LOG_DEFAULT,    /* only for SetMinPriority() */
@@ -56,6 +59,9 @@
 
 int tcp_connect(char* ip, int port);
 
+extern int tmp_rd_fd;
+extern char radiolog_buff[MAX_BUFFER_SIZE];
+
 void hex_print(char* buf, int len)
 {
     int i;
@@ -203,7 +209,7 @@
 {
     char priChar;
     char timeBuf[32];
-    char defaultBuffer[512] = {0};
+    char defaultBuffer[LOGGER_ENTRY_MAX_LEN] = {0};
     size_t totalLen;
     int index = 0;
     int len = 0;
@@ -213,7 +219,8 @@
     static char buffer[MAX_BUFFER_SIZE] = {0};
     static int buffer_index = 0;
 
-    if(access(tmp_log, W_OK) != 0)
+    
+    if(fcntl(fd_radio, F_GETFL) == -1 || access(tmp_log, W_OK) != 0)
     {
         fd_radio = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
         if (fd_radio < 0)
@@ -221,7 +228,7 @@
             fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
             exit(-1);
         }
-
+        tmp_rd_fd = fd_radio;
     }
 
 
@@ -233,7 +240,8 @@
             fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
             exit(-1);
         }
-
+        tmp_rd_fd = fd_radio;
+      
     }
 
     if(fileter_log(entry->priority, entry->tag, config->filter_list))
@@ -257,28 +265,24 @@
             defaultBuffer[index] ^= 1;
         }
     }
-
-
-    if(buffer_index >= RADIOLOG_BUFF_SIZE)
+    
+    if(buffer_index + totalLen >= RADIOLOG_BUFF_SIZE && buffer_index < RADIOLOG_BUFF_SIZE)
     {
-        // Flush the buffer if it's full
-        if (write(fd_radio, buffer, buffer_index) < 0)
+        memcpy(buffer + buffer_index, defaultBuffer, totalLen);
+        buffer_index += totalLen;
+        if (write(fd_radio, buffer, buffer_index) < 0) 
         {
             perror("write error");
+            close(fd_radio);
             return -1;
         }
         buffer_index = 0; // Reset buffer index after flushing
     }
-
-    if(totalLen < RADIOLOG_BUFF_SIZE)
-    {
-        //copy buf to buffer
-        memcpy(buffer + buffer_index, defaultBuffer, totalLen);
-        buffer_index += totalLen;
-    }
     else
     {
-        mbtk_write(fd_radio, defaultBuffer, totalLen);
+        memcpy(buffer + buffer_index, defaultBuffer, totalLen);
+        buffer_index += totalLen;
+        memcpy(radiolog_buff,buffer,buffer_index);
     }
 
 
@@ -290,7 +294,7 @@
     int dev_fd, ret;
 //    int log_fd;
     AndroidLogEntry entry;
-    char buf[512] = {0};
+    char buf[LOGGER_ENTRY_MAX_LEN] = {0};
     static struct file_list_t file_list;
     config = (log_config_entry *)argv;
 
@@ -330,9 +334,16 @@
 
         snprintf(tmp_log,sizeof(tmp_log), "%s", log_file);
         // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
-
-    }
-    else
+       
+        fd_radio = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
+        if (fd_radio < 0) 
+        {
+            fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
+            exit(-1);
+        }
+        tmp_rd_fd = fd_radio;
+    } 
+    else 
     {
         //log_fd = STDOUT_FILENO;
     }
diff --git a/mbtk/mbtk_logd/main.c b/mbtk/mbtk_logd/main.c
index 5dde3b8..a4fb7f3 100755
--- a/mbtk/mbtk_logd/main.c
+++ b/mbtk/mbtk_logd/main.c
@@ -20,12 +20,47 @@
 #include "mbtk_type.h"
 //#define DEBUG 1
 
+#include <signal.h>
+
 #ifdef DEBUG
 #define mbtk_log(...)                    printf(__VA_ARGS__)
 #else
 #define mbtk_log(...)
 #endif
 
+
+#define    MAX_BUFFER_SIZE (8*1024)
+
+int tmp_syslog_fd = -1;
+int tmp_rd_fd = -1;
+
+char syslog_buff[MAX_BUFFER_SIZE] = {0};
+char radiolog_buff[MAX_BUFFER_SIZE] = {0};
+
+
+static void signal_handler(int signum)
+{
+    printf("Received SIGTERM signal, fflush buffer");
+    if(fcntl(tmp_syslog_fd, F_GETFL) != -1 && strlen(syslog_buff) > 0)
+    {
+        if(write(tmp_syslog_fd, syslog_buff, strlen(syslog_buff)) < 0) 
+        {
+            perror("write error");
+        }
+        memset(syslog_buff,0,MAX_BUFFER_SIZE);
+    }
+    
+    if(fcntl(tmp_rd_fd, F_GETFL) != -1 && strlen(radiolog_buff) > 0)
+    {
+        if(write(tmp_rd_fd, radiolog_buff, strlen(radiolog_buff)) < 0) 
+        {
+            perror("write error");
+        }
+        memset(radiolog_buff,0,MAX_BUFFER_SIZE);
+    }
+    
+}
+
 static void handler_free(log_config_entry* listdata)
 {
     int i, n;
@@ -232,8 +267,9 @@
     pthread_t pid[5] = {0};
     int i, ret;
     void* tret;
-//    struct filter_list_t* _filter_list = NULL;
+    //struct filter_list_t* _filter_list = NULL;
 
+    signal(SIGTERM, signal_handler);
 #ifdef MBTK_DUMP_SUPPORT
     mbtk_debug_open(NULL, TRUE);
 #endif
diff --git a/mbtk/mbtk_logd/syslog_read.c b/mbtk/mbtk_logd/syslog_read.c
index bfc70ba..36df306 100755
--- a/mbtk/mbtk_logd/syslog_read.c
+++ b/mbtk/mbtk_logd/syslog_read.c
@@ -67,6 +67,8 @@
 #define    SYSLOG_BUFF_SIZE (4*1024)
 #define    MAX_BUFFER_SIZE (8*1024)
 
+extern int tmp_syslog_fd;
+extern char syslog_buff[MAX_BUFFER_SIZE];
 
 static const struct blobmsg_policy log_policy[] = {
 	[LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
@@ -195,7 +197,8 @@
 
 
     snprintf(tmp_buf,sizeof(tmp_buf), "%s", log_file);
-    if(access(tmp_buf, W_OK) != 0)
+    
+    if(fcntl(sender.fd, F_GETFL) == -1 || access(tmp_buf, W_OK) != 0)
     {
         sender.fd = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600);
         if(sender.fd < 0)
@@ -203,6 +206,7 @@
             perror("Failed to open file ");
             return -1;
         }
+        tmp_syslog_fd = sender.fd;
     }
 
 
@@ -211,14 +215,15 @@
 	if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG])
 		return 1;
 
-    if ((log_type == LOG_FILE) && log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size))
+    if ((log_type == LOG_FILE) && log_size && (!stat(log_file, &s)) && (s.st_size > log_size))
     {
             sender.fd = get_rotate_file(sender.fd, log_file, &file_list);
             if (sender.fd < 0)
             {
-                fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
+                fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
                 exit(-1);
             }
+            tmp_syslog_fd = sender.fd;
     }
 
 	m = blobmsg_get_string(tb[LOG_MSG]);
@@ -279,29 +284,24 @@
                         buf[index] ^= 1;
                     }
                 }
-
-
-
-        if (buffer_index >= SYSLOG_BUFF_SIZE)
+        
+        if(buffer_index + len >= SYSLOG_BUFF_SIZE && buffer_index < SYSLOG_BUFF_SIZE)
         {
-            // Flush the buffer if it's full
-            if (write(sender.fd, buffer, buffer_index) < 0)
-            {
-                perror("write error");
-                return -1;
-            }
-            buffer_index = 0; // Reset buffer index after flushing
-        }
-
-        if(len < SYSLOG_BUFF_SIZE)
-        {
-            //copy buf to buffer
             memcpy(buffer + buffer_index, buf, len);
             buffer_index += len;
+            if (write(sender.fd, buffer, buffer_index) < 0) 
+            {
+                perror("write error");
+                close(sender.fd);
+                return -1;
+            }
+            buffer_index = 0;
         }
         else
         {
-            mbtk_write(sender.fd, buf, len);
+            memcpy(buffer + buffer_index, buf, len);
+            buffer_index += len;
+            memcpy(syslog_buff,buffer,buffer_index);
         }
 
 
@@ -619,6 +619,7 @@
 				fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
 				exit(-1);
 			}
+            tmp_syslog_fd = sender.fd;
 		} else {
 			sender.fd = STDOUT_FILENO;
 		}