Add aaa-12 add log cmd

RCA:
SOL:
修改人:zhangtao
检视人:
This commit is contained in:
zhangtaohz 2019-08-12 16:56:52 +08:00
parent f558d72165
commit be2edc4649
14 changed files with 331 additions and 35 deletions

View File

@ -3,10 +3,15 @@
#include "ulog_api.h" #include "ulog_api.h"
typedef enum {
LOG_OFF = 0,
LOG_ON
} log_sw_t;
typedef struct _log_console { typedef struct _log_console {
u8 level; u8 level;
u8 on; log_sw_t on;
char module_name[MAX_MODULE_NAME_SZ]; char module_name[MAX_MODULE_NAME_SZ];
} log_console_t; } log_console_t;
typedef log_console_t log_pty_t; typedef log_console_t log_pty_t;

View File

@ -28,9 +28,9 @@ MAKE_FLAGS += -j$(shell cat /proc/cpuinfo | grep processor | wc -l)
endif endif
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) ifeq ($(OPT), install)
#$(shell `find ../release -name "*.zip" -delete`) #$(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 $(MLOG)make all $(MAKE_FLAGS) -C Product/build -f module.matchrule.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=matchrule
endif 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

View File

@ -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

View File

@ -1,6 +1,10 @@
#ifndef _ULOG_IN_H #ifndef _ULOG_IN_H
#define _ULOG_IN_H #define _ULOG_IN_H
#include <syslog.h>
#include "common_types.h"
#define SERVICE_LOG_FILE_NAME "log-file" #define SERVICE_LOG_FILE_NAME "log-file"
#define SERIVCE_LOG_CONSOLE_NAME "log-console" #define SERIVCE_LOG_CONSOLE_NAME "log-console"
#define SERVICE_LOG_PTY_NAME "log-pty" #define SERVICE_LOG_PTY_NAME "log-pty"
@ -8,4 +12,32 @@
#define CONF_LOG_CONSOLE_FUNC "conf_log_console" #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 #endif

View File

@ -23,7 +23,7 @@ ret_code log_console_config_chk(uint source, uint *config_type,
return RET_INPUTERR; 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); s2j_create_struct_obj(log_console, log_console_t);
if(log_console == NULL) if(log_console == NULL)

View File

@ -1,3 +1,4 @@
#include "rpc_client.h"
#include "rpc_server.h" #include "rpc_server.h"
#include "log_config.h" #include "log_config.h"
#include "ulog_in.h" #include "ulog_in.h"
@ -10,7 +11,7 @@ ulog_t *g_log_h;
ret_code log_config_init() ret_code log_config_init()
{ {
g_log_h = ulog_init("log_config", 1); g_log_h = ulog_init("log_config", 1);
if (g_log_h) { if (g_log_h == NULL) {
fprintf(stderr, "Initiating log_config is failure"); fprintf(stderr, "Initiating log_config is failure");
return RET_ERR; return RET_ERR;
} }

View File

@ -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);
}

View File

@ -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

View File

@ -0,0 +1,8 @@
#ifndef _LOGGING_COMMON_H
#define _LOGGING_COMMON_H
#define DEFAULT_LOG_LEVEL "info"
extern ulog_t *g_log;
#endif

View File

@ -0,0 +1,66 @@
#include <stdio.h>
#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;
}

View File

@ -6,6 +6,7 @@
#include "ulog.h" #include "ulog.h"
#include "log_common.h" #include "log_common.h"
#include "ulog_in.h"
#define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n" #define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n"

View File

@ -14,27 +14,10 @@
#define REDIRECT_SEPERATE " " #define REDIRECT_SEPERATE " "
typedef struct _level_str {
u32 level;
char str[10];
} level_str_t;
extern ulog_t *g_log; extern ulog_t *g_log;
extern FILE *g_conf_fp; extern FILE *g_conf_fp;
extern char g_conf_file[MAX_PATH_SZ]; 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 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 (*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, int log_conf_append(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod,

View File

@ -1,10 +1,12 @@
#include <errno.h> #include <errno.h>
#include <dirent.h> #include <dirent.h>
#include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include "log_console.h" #include "log_console.h"
#include "log_types.h" #include "log_types.h"
#include "log_common.h" #include "log_common.h"
#include "rpc_conn.h"
#define LOG_CONF_COSOLE_FILE_NAME "log-console.conf" #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; DIR *dir;
if ((dir = opendir(LOG_DEV_DIR)) == NULL) { 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; 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) if ((strcmp(ptr->d_name, ".") == 0)
|| (strcmp(ptr->d_name, "..") == 0) || (strcmp(ptr->d_name, "..") == 0)
|| (ptr->d_type == DT_DIR)) { ///current dir OR parrent dir || (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; continue;
} }
if ((strstr(ptr->d_name, "ttyS") == NULL)) { 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; continue;
} }
@ -52,10 +54,34 @@ static int write_console_content(FILE *fp, const u8 level, const char *filter_mo
static int config_log_console(const log_console_t *conf) 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, char file[MAX_PATH_SZ];
write_console_content, NULL) != 0) { int ret = 0;
ULOG_ERR(g_log, "Log's console configure which is written is failure");
return -1; 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; return 0;
@ -71,6 +97,9 @@ void rpc_conf_log_console(rpc_conn *conn, pointer input, int input_len, pointer
return; 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);
} }

View File

@ -3,6 +3,8 @@
#include "log_file.h" #include "log_file.h"
#include "log_common.h" #include "log_common.h"
#include "ulog_in.h"
#define LOG_CONF_FILE_NAME "log-file.conf" #define LOG_CONF_FILE_NAME "log-file.conf"