diff --git a/Common/log_types.h b/Common/log_types.h index c3ea4a21a..b9542131b 100755 --- a/Common/log_types.h +++ b/Common/log_types.h @@ -3,10 +3,15 @@ #include "ulog_api.h" +typedef enum { + LOG_OFF = 0, + LOG_ON +} log_sw_t; + typedef struct _log_console { - u8 level; - u8 on; - char module_name[MAX_MODULE_NAME_SZ]; + u8 level; + log_sw_t on; + char module_name[MAX_MODULE_NAME_SZ]; } log_console_t; typedef log_console_t log_pty_t; diff --git a/Makefile b/Makefile index e0b11919a..5f0fc8dac 100755 --- a/Makefile +++ b/Makefile @@ -28,9 +28,9 @@ MAKE_FLAGS += -j$(shell cat /proc/cpuinfo | grep processor | wc -l) endif endif -.PHONY : demo database openrpc ulog conntrack netlink trace redismq usermanager configm webauth matchrule +.PHONY : demo database openrpc ulog conntrack netlink trace redismq usermanager configm webauth matchrule logging -all: demo database openrpc ulog conntrack netlink trace redismq usermanager configm webauth matchrule +all: demo database openrpc ulog conntrack netlink trace redismq usermanager configm webauth matchrule logging ifeq ($(OPT), install) #$(shell `find ../release -name "*.zip" -delete`) @@ -203,5 +203,15 @@ else $(MLOG)make all $(MAKE_FLAGS) -C Product/build -f module.matchrule.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=matchrule endif +logging: +ifeq ($(OPT), clean) + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.logging.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=logging +else ifeq ($(OPT), install) + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.logging.Makefile install MLOG=$(MLOG) MAKE_TARGET=logging +else + $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.logging.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=logging +endif + + diff --git a/Platform/build/user.logging.Makefile b/Platform/build/user.logging.Makefile new file mode 100755 index 000000000..387dbad99 --- /dev/null +++ b/Platform/build/user.logging.Makefile @@ -0,0 +1,76 @@ +# target name, the target name must have the same name of c source file +TARGET_NAME=logging + +# target +# for linux module driver: KO +# for application: EXE +# for dynamic library: DLL +TARGET_TYPE = EXE + +# target object +# for application: APP +# for device driver: DRV +TARGET_OBJ = APP + +# custom install dir +TARGET_BOX = + +#debug mode or release mode +DEBUG = TRUE + +PLAT_LINUX ?= TRUE +PLAT_ARM64 ?= TRUE + +VPATH = ../user/logging + +# source code + +# set the source file, don't used .o because of ... + +COMMON_SRCS = logging_main.c cmd_console.c + +# MRS Board Source Files +PLAT_LINUX_SRCS = $(COMMON_SRCS) +PLAT_ARM64_SRCS = $(COMMON_SRCS) + +# gcc CFLAGS +PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common -I../common/rpc -I../common/configm -I../common/ulog -I../user/ulog -I../user/configm/config-server/include +PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) + + +PLAT_ARM64_LDFLAGS := +PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) + +#gcc libs +ARM64_LIBS := ./libopenrpc-arm64.so ./libulogapi-arm64.so ../thirdparty/arm64/libev-arm64.so ./libconfigmapi-arm64.so -lpthread -lcjson +LINUX_LIBS := ./libopenrpc-linux.so ./libulogapi-linux.so ../thirdparty/x86_64/libev-linux.so ./libconfigmapi-linux.so -lpthread -lcjson + +ifeq ($(PLAT_ARM64), TRUE) +DEPEND_LIB += ./debug/libopenrpc-arm64.so ./debug/libulogapi-arm64.so +USER_CLEAN_ITEMS += ./libopenrpc-arm64.so ./libulogapi-arm64.so +endif + +ifeq ($(PLAT_LINUX), TRUE) +DEPEND_LIB += ./debug/libopenrpc-linux.so ./debug/libulogapi-linux.so +USER_CLEAN_ITEMS += ./libopenrpc-linux.so ./libulogapi-linux.so +endif + +# this line must be at below of thus, because of... +include ../../Common/common.Makefile + +ifneq ($(MAKECMDGOALS), clean) +ifneq ($(MAKECMDGOALS), cleanall) +ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB))) +$(shell $(CP) $(DEPEND_LIB) ./) +endif +endif +endif + +ifeq ($(MAKECMDGOALS), ) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +else +ifeq ($(MAKECMDGOALS), all) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +endif +endif + diff --git a/Platform/common/ulog/ulog_in.h b/Platform/common/ulog/ulog_in.h index 23a6d400b..14e8c251a 100755 --- a/Platform/common/ulog/ulog_in.h +++ b/Platform/common/ulog/ulog_in.h @@ -1,6 +1,10 @@ #ifndef _ULOG_IN_H #define _ULOG_IN_H +#include + +#include "common_types.h" + #define SERVICE_LOG_FILE_NAME "log-file" #define SERIVCE_LOG_CONSOLE_NAME "log-console" #define SERVICE_LOG_PTY_NAME "log-pty" @@ -8,4 +12,32 @@ #define CONF_LOG_CONSOLE_FUNC "conf_log_console" +typedef struct _level_str { + u32 level; + char str[10]; +} level_str_t; + +static level_str_t g_level_array[] = { + {LOG_EMERG, "emerg"}, + {LOG_ALERT, "alert"}, + {LOG_CRIT, "crit"}, + {LOG_ERR, "err"}, + {LOG_WARNING, "warn"}, + {LOG_NOTICE, "notice"}, + {LOG_INFO, "info"}, + {LOG_DEBUG, "debug"} +}; + +static inline u8 log_str_to_level(const char *str_level) +{ + int n = sizeof(g_level_array) / sizeof(level_str_t); + for (int i = 0; i < n; i++) { + if (strcmp(g_level_array[i].str, str_level) == 0) { + return g_level_array[i].level; + } + } + + return -1; +} + #endif \ No newline at end of file diff --git a/Platform/user/configm/config-server/log_config/log_config_console.c b/Platform/user/configm/config-server/log_config/log_config_console.c index 6fd98cebf..325f26896 100755 --- a/Platform/user/configm/config-server/log_config/log_config_console.c +++ b/Platform/user/configm/config-server/log_config/log_config_console.c @@ -23,7 +23,7 @@ ret_code log_console_config_chk(uint source, uint *config_type, return RET_INPUTERR; } - ULOG_DEBUG("json input: %s", cJSON_Print(json_obj)); + ULOG_DEBUG(g_log_h, "json input: %s", cJSON_Print(json_obj)); s2j_create_struct_obj(log_console, log_console_t); if(log_console == NULL) diff --git a/Platform/user/configm/config-server/log_config/log_config_init.c b/Platform/user/configm/config-server/log_config/log_config_init.c index 9b4066e5c..ded1a55b8 100755 --- a/Platform/user/configm/config-server/log_config/log_config_init.c +++ b/Platform/user/configm/config-server/log_config/log_config_init.c @@ -1,3 +1,4 @@ +#include "rpc_client.h" #include "rpc_server.h" #include "log_config.h" #include "ulog_in.h" @@ -10,7 +11,7 @@ ulog_t *g_log_h; ret_code log_config_init() { g_log_h = ulog_init("log_config", 1); - if (g_log_h) { + if (g_log_h == NULL) { fprintf(stderr, "Initiating log_config is failure"); return RET_ERR; } diff --git a/Platform/user/logging/cmd_console.c b/Platform/user/logging/cmd_console.c new file mode 100755 index 000000000..2cbb62db2 --- /dev/null +++ b/Platform/user/logging/cmd_console.c @@ -0,0 +1,74 @@ +#include "cmd_console.h" +#include "ulog_api.h" +#include "log_types.h" +#include "configm.h" +#include "configmapi.h" +#include "logging_common.h" +#include "ulog/ulog_in.h" +#include "s2j/s2j.h" + +static int __conf_console(cJSON *json_obj, const char *str_level, const log_sw_t on, const char *module_name) +{ + log_console_t console = {0}; + char *output; + int output_len; + int ret = -1; + u8 level; + + if ((level = log_str_to_level(str_level)) < 0) { + ULOG_WARNING(g_log, "Unknown log level:%s", str_level); + return -1; + } + + console.level = level; + console.on = on; + if (module_name != NULL) { + strncpy(console.module_name, module_name, sizeof(console.module_name)); + } + + s2j_json_set_basic_element(json_obj, &console, int, level); + s2j_json_set_basic_element(json_obj, &console, int, on); + s2j_json_set_basic_element(json_obj, &console, string, module_name); + + char *json = cJSON_PrintUnformatted(json_obj); + if (json == NULL) { + ULOG_ERR(g_log, "Converting struct to json is failure"); + return -1; + } + + ULOG_DEBUG(g_log, "Console json is %s", json); + ret_code ret_c = web_config_exec_sync(CM_CONFIG_SET, LOG_CONFIG_CONSOLE, + json, strlen(json), + &output, &output_len); + if (ret_c != RET_OK) { + ULOG_ERR(g_log, "Console of Web config is failure:%d", ret_c); + } else { + ret = 0; + } + + if (json != NULL) { + free(json); + } + return ret; +} + +int conf_console(cJSON *json_obj, int argc, char **argv) +{ + log_sw_t on = LOG_ON; + char *level = DEFAULT_LOG_LEVEL; + if (argc >= 3) { + if (strcasecmp(argv[2], "off") == 0) { + on = LOG_OFF; + } else { + level = argv[2]; + } + } + + char *module = NULL; + if (argc >= 4) { + module = argv[3]; + } + + return __conf_console(json_obj, level, on, module); +} + diff --git a/Platform/user/logging/cmd_console.h b/Platform/user/logging/cmd_console.h new file mode 100755 index 000000000..0594b5ece --- /dev/null +++ b/Platform/user/logging/cmd_console.h @@ -0,0 +1,9 @@ +#ifndef _CMD_CONSOLE_H +#define _CMD_CONSOLE_H + +#include "cjson/cJSON.h" + +//int conf_console(cJSON *json_obj, const char *str_level, const char *module_name); +int conf_console(cJSON *json_obj, int argc, char **argv); + +#endif diff --git a/Platform/user/logging/logging_common.h b/Platform/user/logging/logging_common.h new file mode 100755 index 000000000..37b09dbb0 --- /dev/null +++ b/Platform/user/logging/logging_common.h @@ -0,0 +1,8 @@ +#ifndef _LOGGING_COMMON_H +#define _LOGGING_COMMON_H + +#define DEFAULT_LOG_LEVEL "info" + +extern ulog_t *g_log; + +#endif diff --git a/Platform/user/logging/logging_main.c b/Platform/user/logging/logging_main.c new file mode 100755 index 000000000..b3d80007f --- /dev/null +++ b/Platform/user/logging/logging_main.c @@ -0,0 +1,66 @@ +#include + +#include "cmd_console.h" +#include "ulog_api.h" +#include "s2j/s2j.h" + +#define LOGGING_MODULE_NAME "logging" + +typedef int (*cmd_cb)(cJSON *, int, char **); + +typedef struct _log_cmd { + char cmd[20]; + cmd_cb cb; +} log_cmd_t; + +ulog_t *g_log; +log_cmd_t log_cmd[] = { + {"console", conf_console} +}; + +cmd_cb get_cb_by_cmd(const char *cmd) +{ + for (int i = 0; i < (sizeof(log_cmd) / sizeof(log_cmd_t)); i++) { + if (strcmp(cmd, log_cmd[i].cmd) == 0) { + return log_cmd[i].cb; + } + } + + return NULL; +} + +int main(int argc, char **argv) +{ + if (argc < 2) { + fprintf(stdout, "Parameter too few"); + return -1; + } + + g_log = ulog_init(LOGGING_MODULE_NAME, 1); + if (g_log == NULL) { + fprintf(stderr, "Initiating ulog is failure"); + return -1; + } + + cmd_cb func = get_cb_by_cmd(argv[1]); + if (func == NULL) { + ULOG_WARNING(g_log, "Not find logging cmd:%s", argv[1]); + goto END; + } + + s2j_create_json_obj(json_obj); + if(json_obj == NULL) + { + ULOG_ERR(g_log, "Creating json object is failure"); + goto END; + } + + func(json_obj, argc, argv); + +END: + if(json_obj != NULL) { + cJSON_Delete(json_obj); + } + ulog_close(g_log); + return 0; +} diff --git a/Platform/user/ulog/log-sched/log_common.c b/Platform/user/ulog/log-sched/log_common.c index f6e978fe1..9dd40354c 100755 --- a/Platform/user/ulog/log-sched/log_common.c +++ b/Platform/user/ulog/log-sched/log_common.c @@ -6,6 +6,7 @@ #include "ulog.h" #include "log_common.h" +#include "ulog_in.h" #define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n" diff --git a/Platform/user/ulog/log-sched/log_common.h b/Platform/user/ulog/log-sched/log_common.h index 885e8f942..5ede0401f 100755 --- a/Platform/user/ulog/log-sched/log_common.h +++ b/Platform/user/ulog/log-sched/log_common.h @@ -14,27 +14,10 @@ #define REDIRECT_SEPERATE " " - -typedef struct _level_str { - u32 level; - char str[10]; -} level_str_t; - extern ulog_t *g_log; extern FILE *g_conf_fp; extern char g_conf_file[MAX_PATH_SZ]; -static level_str_t g_level_array[] = { - {LOG_EMERG, "emerg"}, - {LOG_ALERT, "alert"}, - {LOG_CRIT, "crit"}, - {LOG_ERR, "err"}, - {LOG_WARNING, "warn"}, - {LOG_NOTICE, "notice"}, - {LOG_INFO, "info"}, - {LOG_DEBUG, "debug"} -}; - int log_conf(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg); int log_conf_append(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, diff --git a/Platform/user/ulog/log-sched/log_console.c b/Platform/user/ulog/log-sched/log_console.c index 2635d6542..a564b6a13 100755 --- a/Platform/user/ulog/log-sched/log_console.c +++ b/Platform/user/ulog/log-sched/log_console.c @@ -1,10 +1,12 @@ #include #include +#include #include #include "log_console.h" #include "log_types.h" #include "log_common.h" +#include "rpc_conn.h" #define LOG_CONF_COSOLE_FILE_NAME "log-console.conf" @@ -15,7 +17,7 @@ static int write_console_content(FILE *fp, const u8 level, const char *filter_mo DIR *dir; if ((dir = opendir(LOG_DEV_DIR)) == NULL) { - ULOG_ERR(g_log, "Open dir:[%s] is failure:%d\n", LOG_DEV_DIR, strerror(errno)); + ULOG_ERR(g_log, "Open dir:[%s] is failure:%d", LOG_DEV_DIR, strerror(errno)); return -1; } @@ -25,12 +27,12 @@ static int write_console_content(FILE *fp, const u8 level, const char *filter_mo if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0) || (ptr->d_type == DT_DIR)) { ///current dir OR parrent dir - ULOG_DEBUG(g_log,"The file:[%s] or directory jump over\n", ptr->d_name); + ULOG_DEBUG(g_log,"The file:[%s] or directory jump over", ptr->d_name); continue; } if ((strstr(ptr->d_name, "ttyS") == NULL)) { - ULOG_DEBUG(g_log,"The file:[%s] isn't redirected\n", ptr->d_name); + ULOG_DEBUG(g_log,"The file:[%s] isn't redirected", ptr->d_name); continue; } @@ -51,13 +53,37 @@ static int write_console_content(FILE *fp, const u8 level, const char *filter_mo static int config_log_console(const log_console_t *conf) -{ - if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, conf->module_name, - write_console_content, NULL) != 0) { - ULOG_ERR(g_log, "Log's console configure which is written is failure"); - return -1; - } - +{ + char file[MAX_PATH_SZ]; + int ret = 0; + + switch (conf->on) + { + case LOG_OFF: + if (snprintf(file, sizeof(file), "%s%s", LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME) < 0) { + ULOG_ERR(g_log, "Setting file path(dir:%s, file:%s) is failure", + LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME); + ret = -1; + break; + } + if (unlink(file) < 0) { + ULOG_ERR(g_log, "Deleting configure file(dir:%s, file:%s) is failure:%s", + LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, strerror(errno)); + ret = -1; + break; + } + break; + case LOG_ON: + if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, conf->module_name, + write_console_content, NULL) != 0) { + ULOG_ERR(g_log, "Log's console configure which is written is failure"); + ret = -1; + } + break; + default: + break; + } + return 0; } @@ -71,6 +97,9 @@ void rpc_conf_log_console(rpc_conn *conn, pointer input, int input_len, pointer return; } - config_log_console((const log_console_t *)input); + if (config_log_console((const log_console_t *)input) != 0) { + rpc_return_error(conn, RET_ERR, "Configuring console of log is faiure"); + } + rpc_return_null(conn); } diff --git a/Platform/user/ulog/log-sched/log_file.c b/Platform/user/ulog/log-sched/log_file.c index 82a019d93..8be02d471 100755 --- a/Platform/user/ulog/log-sched/log_file.c +++ b/Platform/user/ulog/log-sched/log_file.c @@ -3,6 +3,8 @@ #include "log_file.h" #include "log_common.h" +#include "ulog_in.h" + #define LOG_CONF_FILE_NAME "log-file.conf"