Add basic change for v1453
Change-Id: I9497a61bbc3717f66413794a4e7dee0347c0bc33
diff --git a/mbtk/liblynq_lib_rilv2/lynq_log.c b/mbtk/liblynq_lib_rilv2/lynq_log.c
new file mode 100755
index 0000000..b844490
--- /dev/null
+++ b/mbtk/liblynq_lib_rilv2/lynq_log.c
@@ -0,0 +1,640 @@
+#include <stdarg.h>
+#include <sys/un.h>
+#include <sys/socket.h>
+#include "json-c/json.h"
+#include "lynq_deflog.h"
+#include "mbtk_type.h"
+#include <signal.h>
+#include "mbtk_utils.h"
+
+#define LOG_CONFIG_PATH "/etc/mbtk/mbtk_log.json"
+#define SYSLOG_PATCH "syslog"
+#define SYSLOG_NAME "syslog"
+#define SYSLOG_INDEX 0
+
+#define RADIOLOG_NAME "radio"
+#define RADIO_INDEX 1
+
+void lynq_log_configuration_init(const char *log_name)
+{
+ //UNUSED(log_name);
+ mbtk_log_init(SYSLOG_PATCH, (char*)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;
+ const 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);
+ mbtk_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");
+ mbtk_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");
+ mbtk_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;
+ const 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;
+ const 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;
+ const 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_UNSET:
+ 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);
+ const 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);
+ const 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 clientFd,nwrite;
+ 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;
+}
+
+int lynq_write_log_to_file_now()
+{
+ FILE *fp;
+ char command[256];
+ char buffer[256];
+ int pid = -1;
+ const char *process_name = "mbtk_logd";
+
+
+ snprintf(command, sizeof(command), "pgrep %s", process_name);
+ fp = popen(command, "r");
+ if (fp == NULL)
+ {
+ perror("error comman");
+ return -1;
+ }
+
+ if (fgets(buffer, sizeof(buffer), fp) != NULL)
+ {
+ pid = atoi(buffer);
+ }
+ pclose(fp);
+
+ if (pid != -1)
+ {
+ printf("mbtk %s PID: %d\n", process_name, pid);
+ if (kill(pid, SIGTERM) == -1)
+ {
+ perror("send SIGTERM signal failed");
+ return -1;
+ }
+ printf("send %d SIGTERM signal\n", pid);
+ }
+ else
+ {
+ printf("no find %s\n", process_name);
+ }
+
+ //lynq_log_configuration_init("log_end");
+ //lynq_log_global_output(LOG_DEBUG,"mbtk_logd_out");
+ return 0;
+
+}
+
+int lynq_stop_record_log(int value)
+{
+
+ json_object* jsonobj = NULL;
+ json_object* tmpjson = NULL;
+ json_object* datajson = NULL;
+ json_object* listjson = NULL;
+
+// int tmp_int;
+ const char* tmp_string = NULL;
+
+ if(value != 0 && value != 1)
+ {
+ printf("invalid value ");
+ return -1;
+ }
+
+ jsonobj = json_object_from_file(LOG_CONFIG_PATH);
+ if (NULL == jsonobj)
+ {
+ printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
+ mbtk_system("echo Can't open config file > /dev/console");
+ return -1;
+ }
+
+
+ json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
+
+ /***获取syslog json data***/
+ 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");
+ mbtk_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);
+ json_object_get_int(listjson);
+
+ if(strcmp(tmp_string, SYSLOG_NAME) != 0)
+ {
+ json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+ json_object_put(jsonobj);
+ printf("SYSLOG_NAME error, \n");
+ mbtk_system("echo SYSLOG_NAME error, > /dev/console");
+ return -1;
+ }
+ else
+ {
+ //set 0 to syslog
+ json_object_object_add(datajson, "enable", json_object_new_int(value));
+ }
+
+ /***获取radiolog json data***/
+ datajson = json_object_array_get_idx(tmpjson, RADIO_INDEX);
+ if (NULL == datajson)
+ {
+ json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+ json_object_put(jsonobj);
+ printf("NULL == datajson\n");
+ mbtk_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);
+ json_object_get_int(listjson);
+
+ if(strcmp(tmp_string, RADIOLOG_NAME) != 0)
+ {
+ json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+ json_object_put(jsonobj);
+ printf("RADIO_NAME error or \n");
+ mbtk_system("echo RADIO_NAME error, > /dev/console");
+ return -1;
+ }
+ else
+ {
+ //set 0 to radio_log
+ json_object_object_add(datajson, "enable", json_object_new_int(value));
+ }
+
+ /***释放json对象***/
+ json_object_to_file(LOG_CONFIG_PATH, jsonobj);
+ json_object_put(jsonobj);
+ return 0;
+}
\ No newline at end of file