Add mbtk/liblynq_lib_v2/ mbtk/libql_lib_v2/

Change-Id: Idbb802cd805b34603ccf65bff9818725a6955e51
diff --git a/mbtk/liblynq_lib_v2/src/lynq_log.c b/mbtk/liblynq_lib_v2/src/lynq_log.c
new file mode 100755
index 0000000..e096f8a
--- /dev/null
+++ b/mbtk/liblynq_lib_v2/src/lynq_log.c
@@ -0,0 +1,494 @@
+#include <stdarg.h>
+#include <sys/un.h>
+#include <sys/socket.h>
+#include "json-c/json.h"
+#include "lynq_deflog.h"
+#include "mbtk_type.h"
+
+#define LOG_CONFIG_PATH     "/etc/mbtk/mbtk_log.json"
+#define SYSLOG_PATCH "syslog"
+#define SYSLOG_NAME "syslog"
+#define SYSLOG_INDEX 0
+
+void lynq_log_configuration_init(const char *log_name)
+{
+    //UNUSED(log_name);
+    mbtk_log_init(SYSLOG_PATCH, log_name);
+}
+
+void lynq_log_global_output(log_level_enum Level,const char *format,...)
+{
+    va_list args;
+    va_start(args,format);
+    mbtk_log(Level, format, args);
+    va_end(args);
+}
+
+const char* lynq_read_log_version()
+{
+    return "LOG-V1.0";
+}
+
+int lynq_syslog_set_file_size(int value)
+{
+//    UNUSED(value);
+    json_object* jsonobj = NULL;
+    json_object* tmpjson = NULL;
+    json_object* datajson = NULL;
+    json_object* listjson = NULL;
+
+    int tmp_int;
+    char* tmp_string = NULL;
+
+    if(value < 1)
+    {
+        value = 1;
+    }
+    else if(value > 100)
+    {
+        value = 100;
+    }
+    value = value*1024;
+
+    jsonobj = json_object_from_file(LOG_CONFIG_PATH);
+    if (NULL == jsonobj) {
+        printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
+        system("echo Can't open config file > /dev/console");
+        return -1;
+    }
+    /***获取data***/
+    json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
+    datajson = json_object_array_get_idx(tmpjson, SYSLOG_INDEX);
+    if (NULL == datajson) {
+        json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+        json_object_put(jsonobj);
+        printf("NULL == datajson\n");
+        system("echo NULL == datajson > /dev/console");
+        return -1;
+    }
+
+    json_object_object_get_ex(datajson, "name", &listjson);
+    tmp_string = json_object_get_string(listjson);
+
+    json_object_object_get_ex(datajson, "enable", &listjson);
+    tmp_int = json_object_get_int(listjson);
+
+    if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
+    {
+        json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+        json_object_put(jsonobj);
+        printf("SYSLOG_NAME error, tmp_int != 1\n");
+        system("echo SYSLOG_NAME error, tmp_int != 1 > /dev/console");
+        return -1;
+    }
+
+    json_object_object_add(datajson, "rotate_file_size", json_object_new_int(value));
+
+    /***释放json对象***/
+    json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+    json_object_put(jsonobj);
+    return 0;
+}
+
+int lynq_syslog_get_file_size(void)
+{
+    json_object* jsonobj = NULL;
+    json_object* tmpjson = NULL;
+    json_object* datajson = NULL;
+    json_object* listjson = NULL;
+
+    int tmp_int;
+    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, SYSLOG_INDEX);
+    if (NULL == datajson) {
+        json_object_put(jsonobj);
+        return -1;
+    }
+
+    json_object_object_get_ex(datajson, "name", &listjson);
+    tmp_string = json_object_get_string(listjson);
+
+    json_object_object_get_ex(datajson, "enable", &listjson);
+    tmp_int = json_object_get_int(listjson);
+
+    if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
+    {
+        json_object_put(jsonobj);
+        return -1;
+    }
+
+    json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
+    tmp_int = json_object_get_int(listjson);
+
+    /***释放json对象***/
+    json_object_put(jsonobj);
+
+    return tmp_int/1024;
+//    return tmp_int;
+}
+
+int lynq_syslog_set_file_rotate(int value)
+{
+    //UNUSED(value);
+
+    json_object* jsonobj = NULL;
+    json_object* tmpjson = NULL;
+    json_object* datajson = NULL;
+    json_object* listjson = NULL;
+
+    int tmp_int;
+    char* tmp_string = NULL;
+
+    if(value < 0)
+    {
+        value = 0;
+    }
+    else if(value > 99)
+    {
+        value = 99;
+    }
+
+    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, SYSLOG_INDEX);
+    if (NULL == datajson) {
+        json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+        json_object_put(jsonobj);
+        return -1;
+    }
+
+    json_object_object_get_ex(datajson, "name", &listjson);
+    tmp_string = json_object_get_string(listjson);
+
+    json_object_object_get_ex(datajson, "enable", &listjson);
+    tmp_int = json_object_get_int(listjson);
+
+    if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
+    {
+        json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+        json_object_put(jsonobj);
+        return -1;
+    }
+
+    json_object_object_add(datajson, "rotate_file_count", json_object_new_int(value));
+
+    /***释放json对象***/
+    json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+    json_object_put(jsonobj);
+    return 0;
+}
+
+int lynq_syslog_get_file_rotate(void)
+{
+    json_object* jsonobj = NULL;
+    json_object* tmpjson = NULL;
+    json_object* datajson = NULL;
+    json_object* listjson = NULL;
+
+    int tmp_int;
+    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, SYSLOG_INDEX);
+    if (NULL == datajson) {
+        json_object_put(jsonobj);
+        return -1;
+    }
+
+    json_object_object_get_ex(datajson, "name", &listjson);
+    tmp_string = json_object_get_string(listjson);
+
+    json_object_object_get_ex(datajson, "enable", &listjson);
+    tmp_int = json_object_get_int(listjson);
+
+    if(strcmp(tmp_string, SYSLOG_NAME) != 0 || tmp_int != 1)
+    {
+        json_object_put(jsonobj);
+        return -1;
+    }
+
+    json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
+    tmp_int = json_object_get_int(listjson);
+
+    /***释放json对象***/
+    json_object_put(jsonobj);
+
+    return tmp_int;
+}
+
+log_level_enum filter_char_to_pri(char c)
+{
+    switch (c) {
+        case 'v':
+            return LOG_VERBOSE;
+        case 'd':
+            return LOG_DEBUG;
+        case 'i':
+            return LOG_INFO;
+        case 'w':
+            return LOG_WARNING;
+        case 'e':
+            return LOG_ERROR;
+        case '*':
+        default:
+            return LOG_LEVEL_MAX;
+    }
+}
+
+char filter_pri_to_char(log_level_enum level)
+{
+    char char_level;
+    switch (level) {
+        case LOG_VERBOSE:
+            char_level = 'v';
+            break;
+        case LOG_DEBUG:
+            char_level = 'd';
+            break;
+        case LOG_INFO:
+            char_level = 'i';
+            break;
+        case LOG_WARNING:
+            char_level = 'w';
+            break;
+        case LOG_ERROR:
+            char_level = 'e';
+            break;
+        case LOG_LEVEL_MAX:
+        default:
+            char_level = '*';
+            break;
+    }
+    return char_level;
+}
+
+int lynq_set_log_level(const char * module_name, log_level_enum 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;
+    json_object* new_fileter = NULL;
+
+    int n = 0, array_length = 0;
+    char* tmp_string = NULL;
+    char level_string[5] = {'\0'};
+
+    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, SYSLOG_INDEX);
+    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;
+    }
+    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) {
+            new_fileter = json_object_new_object();
+            sprintf(level_string, "%c", filter_pri_to_char(level));
+            json_object_object_add(new_fileter, "priority", json_object_new_string(level_string));
+            json_object_object_add(new_fileter, "tag", json_object_new_string(module_name));
+            json_object_array_add(listjson, new_fileter);
+            break;
+        }
+
+        json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
+        char *str = json_object_get_string(fileter_listjson);
+        if (str) {
+            tmp_string = strdup(str);
+            if(strcmp(module_name, tmp_string) == 0)
+            {
+                sprintf(level_string, "%c", filter_pri_to_char(level));
+                json_object_object_add(fileterjson, "priority", json_object_new_string(level_string));
+                json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+                json_object_put(jsonobj);
+                return 0;
+            }
+        }
+        else
+        {
+            continue;
+        }
+    }
+    /***释放json对象***/
+    json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+    json_object_put(jsonobj);
+
+    return 0;
+}
+
+int lynq_get_log_level(const char * module_name, log_level_enum *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;
+
+    int n;
+    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, SYSLOG_INDEX);
+    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;
+    }
+
+    for (n = 0 ; n < 5; n++) {
+        fileterjson = json_object_array_get_idx(listjson, n);
+        if (NULL == fileterjson) {
+            printf("the fileterjson exit\n");
+            break;
+        }
+
+        json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
+        char *str = json_object_get_string(fileter_listjson);
+        if (str) {
+            tmp_string = strdup(str);
+            printf("tag is %s\n", tmp_string);
+            if(strcmp(module_name, tmp_string) == 0)
+            {
+                json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
+                str = json_object_get_string(fileter_listjson);
+                if (str) {
+                    tmp_string = str[0];
+                    printf("fileter_listjson: %c\n", tmp_string);
+                    *level = filter_char_to_pri(tmp_string);
+                    //get the log level
+                    json_object_put(jsonobj);
+                    return 0;
+                }
+                else
+                {
+                    break;
+                }
+            }
+        }
+        else
+        {
+            continue;
+        }
+    }
+    *level = LOG_UNSET;
+    /***释放json对象***/
+    json_object_put(jsonobj);
+
+    return 0;
+}
+
+int lynq_set_special_log_level(const char * exe_name, const char * module_name, log_level_enum level)
+{
+    UNUSED(exe_name);
+    UNUSED(module_name);
+    UNUSED(level);
+
+
+    return 0;
+}
+
+int lynq_get_special_log_level(const char * exe_name, const char * module_name, log_level_enum *level)
+{
+    UNUSED(exe_name);
+    UNUSED(module_name);
+    UNUSED(level);
+
+
+    return 0;
+}
+
+int lynq_notify_recalc_log_level(pid_t pid)
+{
+    UNUSED(pid);
+    char sendBuff[100]={'\0'};
+    int serverFd,clientFd,addrLen,nwrite,nread;
+    struct sockaddr_un serverAddr,clientAddr;
+
+    unlink("/var/log_client.socket");   /* in case it already exists */
+
+    memset(&clientAddr,0,sizeof(clientAddr));
+    memset(&serverAddr,0,sizeof(serverAddr));
+    clientAddr.sun_family = AF_UNIX;
+    sprintf(clientAddr.sun_path,"%s","/var/log_client.socket");
+
+    if ((clientFd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
+    {
+        printf("err -1\n");
+        return -1;
+    }
+
+    if (bind(clientFd, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0)
+    {
+        printf("err -2\n");
+        close(clientFd);
+        return -2;
+    }
+
+    serverAddr.sun_family = AF_UNIX;
+    sprintf(serverAddr.sun_path, "/var/log_server.socket");
+    if (connect(clientFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
+    {
+        printf("err -3\n");
+        close(clientFd);
+        return -3;
+    }
+
+    sprintf(sendBuff,"%s","update");
+    if ((nwrite = write(clientFd, sendBuff, 100)) < 0)
+    {
+        printf("err -4\n");
+        close(clientFd);
+        return -4;
+    }
+
+    close(clientFd);
+    return 0;
+}
\ No newline at end of file