log: add lynq_notify_recalc_log_level

Change-Id: I50da885e860f78585cb86ff2f1d9fba873196f3c
diff --git a/mbtk/mbtk_logd/syslog_read.c b/mbtk/mbtk_logd/syslog_read.c
index 1d6da4b..f2130ca 100755
--- a/mbtk/mbtk_logd/syslog_read.c
+++ b/mbtk/mbtk_logd/syslog_read.c
@@ -20,10 +20,13 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/un.h>
 #include <sys/socket.h>
 
 #define SYSLOG_NAMES
 #include <syslog.h>
+#include "json/json.h"
+#include "json/printbuf.h"
 
 #include <libubox/ustream.h>
 #include <libubox/blobmsg_json.h>
@@ -74,6 +77,7 @@
 static struct file_list_t file_list;
 static struct filter_list_t *filter_log = NULL;
 static char tmp_log[48] = {0};
+pthread_t attr = -1;
 
 static const char* getcodetext(int value, CODE *codetable) {
 	CODE *i;
@@ -131,6 +135,7 @@
 static int syslog_fileter_log(int pri, char *tag, struct filter_list_t *filter)
 {
     struct filter_list_t *_filter = filter;
+	struct filter_list_t *_filter_common = _filter;
 
     while(_filter)
     {
@@ -150,15 +155,16 @@
                 }
            }
         }else{ // have no tag
-            if(pri > p)
-                return -1;
-            else
-                return 0;
+			_filter_common = _filter;
         }
         _filter = _filter->next;
     }
+	//common tag
+	int p = filter_char_to_pri(_filter_common->priority);
+	if(pri > p)
+		return -1;
 
-    return -1;
+	return 0;
 }
 static int log_notify(struct blob_attr *msg)
 {
@@ -269,6 +275,177 @@
 {
 }
 
+int lynq_update_log_level()
+{
+    json_object* jsonobj = NULL;
+    json_object* tmpjson = NULL;
+    json_object* datajson = NULL;
+    json_object* listjson = NULL;
+    json_object* fileterjson = NULL;
+    json_object* fileter_listjson = NULL;
+	struct filter_list_t* filter_list_head = NULL;
+	struct filter_list_t* tmp_filter_list = NULL;
+	struct filter_list_t* _filter_list = NULL;
+
+    int n;
+	int array_length;
+    char* tmp_string = NULL;
+
+    jsonobj = json_object_from_file(LOG_CONFIG_PATH);
+    if (NULL == jsonobj) {
+        printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
+        return -1;
+    }
+    /***获取data***/
+    json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
+    datajson = json_object_array_get_idx(tmpjson, 0);//syslog index is 0
+    if (NULL == datajson) {
+        json_object_put(jsonobj);
+        return -1;
+    }
+    json_object_object_get_ex(datajson, "filter_list", &listjson);
+    if (NULL == listjson) {
+        printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
+        json_object_put(listjson);
+        return -1;
+    }
+	filter_list_head = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
+	_filter_list = filter_list_head;
+
+	array_length = json_object_array_length(listjson);
+    for (n = 0 ; n < array_length; n++) {
+            fileterjson = json_object_array_get_idx(listjson, n);
+            if (NULL == fileterjson) {
+                printf("the fileterjson exit\n");
+                free(tmp_filter_list->next);
+                tmp_filter_list->next = NULL;
+                break;
+            }
+            memset(_filter_list, 0, sizeof(struct filter_list_t));
+            json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
+            char* str = json_object_get_string(fileter_listjson);
+            if (str) {
+                _filter_list->priority = str[0];
+                printf("fileter_listjson: %c\n", _filter_list->priority);
+            }
+
+            json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
+ 
+            str = json_object_get_string(fileter_listjson);
+            if (str) {
+                _filter_list->tag = strdup(str);
+                printf("fileter_listjson: %s\n", _filter_list->tag);
+            }
+            else
+            {
+                _filter_list->tag = "\0";
+            }
+            //json_object_put(fileter_listjson);
+            _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
+            if (NULL == _filter_list->next) {
+                printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
+                break;
+            }
+            tmp_filter_list = _filter_list;
+            _filter_list = _filter_list->next;
+			_filter_list->next = NULL;
+    }
+    /***释放json对象***/
+    json_object_put(jsonobj);
+
+	tmp_filter_list = filter_log;
+	filter_log = filter_list_head;
+
+    while(tmp_filter_list != NULL) {
+		_filter_list = tmp_filter_list;
+        free(tmp_filter_list);
+        tmp_filter_list = _filter_list->next;
+    }
+	
+    return 0;
+}
+
+
+int wait_update_log_level()
+{
+	int i = 0;
+	char recvBuff[100];
+	int serverFd,clientFd,addrLen;
+    struct sockaddr_un serverAddr,clientAddr;
+
+	pthread_detach(pthread_self());
+	printf("MBTK: in wait_update_log_level\n");
+
+    memset(&serverAddr,0,sizeof(serverAddr));
+    serverAddr.sun_family = AF_UNIX;
+    sprintf(serverAddr.sun_path,"%s","/var/log_server.socket");
+
+    unlink("/var/log_server.socket");   /* in case it already exists */
+
+    if ((serverFd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
+    {
+        printf("err -1\n");
+        return -1;
+    }
+
+    if (bind(serverFd,(struct sockaddr *)&serverAddr,sizeof(serverAddr)) < 0)
+    {
+        printf("err -2\n");
+        close(serverFd);
+        return -2;
+    }
+
+    if(listen(serverFd,10) < 0)
+    {
+        printf("err -3\n");
+        return -3;
+    }
+
+    while(1)
+    {
+        addrLen = sizeof(clientAddr);
+        memset(&clientAddr,0,sizeof(clientAddr));
+        memset(&recvBuff,0,100);
+
+        if((clientFd = accept(serverFd,(struct sockaddr*)&clientAddr,&addrLen)) < 0)
+        {
+            printf("err -4\n");
+            continue;
+        }
+		printf("MBTK: wait recv\n");
+        if(recv(clientFd,recvBuff,100,0) < 0)
+        {
+            printf("err -5");
+            close(clientFd);
+            continue;
+        }
+        if(strncmp(recvBuff, "update", strlen("update")) == 0)
+        {
+            lynq_update_log_level();
+        }
+
+        close(clientFd);
+    }
+    close(serverFd);
+
+	return 0;
+}
+
+int syslog_pthread_create()
+{
+    int ret;
+    
+	ret = pthread_create(&attr, NULL, wait_update_log_level, NULL);
+
+    if (ret < 0)
+    {
+        printf("MBTK:pthread create fail");
+        return -1;
+    }
+
+    return -1;
+}
+
 void* syslog_main(void* argv)
 {
 	static struct ubus_request req;
@@ -288,6 +465,8 @@
 	signal(SIGPIPE, SIG_IGN);
 	uloop_init();
 
+
+	syslog_pthread_create();
     //log_file = config->out_path;
 	memset(log_file, 0, sizeof(log_file));
 	memset(log_ip, 0, sizeof(log_ip));
@@ -379,6 +558,13 @@
 		uloop_done();
 
 	} while (ret && tries--);
+	
+	if (attr) {
+		if (pthread_join(attr, &tret) != 0) {
+			printf("MBTK:Join thread: %d error!\n", attr);
+			exit(1);
+		}
+	}
 
     pthread_exit(NULL);
 	return NULL;