From d4229eb20fd7a3ea3e30c21e065e86fab9b4f7ef Mon Sep 17 00:00:00 2001 From: zhangtaohz Date: Mon, 29 Jul 2019 11:50:57 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=20aaa-12=20add=20ulog=20tty=20RCA?= =?UTF-8?q?=EF=BC=9A=20SOL=EF=BC=9A=20=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9A?= =?UTF-8?q?zhangtao=20=E6=A3=80=E8=A7=86=E4=BA=BA=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/common.Makefile | 4 +- Platform/build/user.ulog.log_sched.Makefile | 2 +- Platform/user/ulog/log-sched/log_common.c | 69 +++++++++++++++------ Platform/user/ulog/log-sched/log_common.h | 12 ++-- Platform/user/ulog/log-sched/log_console.c | 10 +-- Platform/user/ulog/log-sched/log_file.c | 4 +- Platform/user/ulog/log-sched/log_file.h | 4 +- Platform/user/ulog/log-sched/log_tty.c | 33 +++++++--- 8 files changed, 89 insertions(+), 49 deletions(-) diff --git a/Common/common.Makefile b/Common/common.Makefile index 953a1a592..02f81a941 100755 --- a/Common/common.Makefile +++ b/Common/common.Makefile @@ -175,7 +175,7 @@ DEBUG_CFLAGS := -g3 endif # 设置 gcc 编译优化等级参数 -DEBUG_CFLAGS += -O2 +#DEBUG_CFLAGS += -O2 LINUX_EXTFLAG += $(DEBUG_CFLAGS) @@ -259,7 +259,7 @@ else ifeq ($(TARGET_OBJ), APP) ARM64_EXTFLAG += $(DEBUG_CFLAGS) -I$(ARM64_SDK_INCLUDE) # show all warring -#CFLAGS += -Wall +CFLAGS += -Wall # 设置 ARM64 交叉编译链接目录 ARM64_DRV_LDFLAGS := --sysroot=${SDKTARGETSYSROOT} diff --git a/Platform/build/user.ulog.log_sched.Makefile b/Platform/build/user.ulog.log_sched.Makefile index baf911a8e..fd936cd80 100755 --- a/Platform/build/user.ulog.log_sched.Makefile +++ b/Platform/build/user.ulog.log_sched.Makefile @@ -27,7 +27,7 @@ VPATH = ../user/ulog/log-sched # set the source file, don't used .o because of ... -COMMON_SRCS = log_file.c log_console.c log_common.c log_sched.c +COMMON_SRCS = log_file.c log_console.c log_tty.c log_common.c log_sched.c # MRS Board Source Files PLAT_LINUX_SRCS = $(COMMON_SRCS) diff --git a/Platform/user/ulog/log-sched/log_common.c b/Platform/user/ulog/log-sched/log_common.c index e370425ec..b97c3cc76 100755 --- a/Platform/user/ulog/log-sched/log_common.c +++ b/Platform/user/ulog/log-sched/log_common.c @@ -10,18 +10,18 @@ #define BAK_FILE "/tmp/%s" -int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, - const char *rediect_path, const char *filter_mod) +int write_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) { FILE *fp = NULL; int ret = 1; u8 exist_backup = 1; /********** rsyslog configure **********/ - char conf_path_file[MAX_LINE_SIZE]; + char conf_path_file[MAX_PATH_SZ]; snprintf(conf_path_file, sizeof(conf_path_file), "%s%s", conf_path, conf_file); - char bak_file[MAX_LINE_SIZE]; + char bak_file[MAX_PATH_SZ]; snprintf(bak_file, sizeof(bak_file), BAK_FILE, conf_file); if (rename(conf_path_file, bak_file) < 0) { if (errno == ENOENT) { @@ -39,8 +39,34 @@ int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, goto END; } + ULOG_DEBUG(g_log, "cb_content:%x", cb_content); + if (cb_content(fp, level, filter_mod, arg) != 0) { + ULOG_ERR(g_log, "Callback log content is failure"); + goto END; + } + + ret = 0; +END: + if (fp != NULL) { + fclose(fp); + } + + if ((ret == 1) && (exist_backup == 1)) { + // 恢复备份配置 + if (rename(bak_file, conf_path_file) < 0) { + ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno)); + } + } + + return ret; +} + +int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) +{ int i; - char line[MAX_LINE_SIZE + 100] = {0}; + int ret = 1; + char line[MAX_LINE_SZ + 100] = {0}; + ULOG_INFO(g_log, "filter module:%s\n", filter_mod); if ((filter_mod != NULL) && (strlen(filter_mod) > 0)) { snprintf(line, sizeof(line), FILTER_CONTENT, filter_mod); if (fputs(line, fp) == EOF) { @@ -49,7 +75,6 @@ int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, goto END; } } - char tmp[20]; line[0] = '\0'; // 清零 @@ -66,7 +91,8 @@ int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, } strcat(line, " "); - strcat(line, rediect_path); + strcat(line, (const char *)arg); + strcat(line, "\n"); if (fputs(line, fp) == EOF) { ULOG_ERR(g_log, "Configure file which is written is failure:%s", strerror(errno)); @@ -75,26 +101,31 @@ int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, ret = 0; END: - if (fp != NULL) { - fclose(fp); - } - - if ((ret == 1) && (exist_backup == 1)) { - // 回复备份配置 - if (rename(bak_file, conf_path_file) < 0) { - ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno)); - } - } - return ret; } int modify_authorizing(const char *redirect_path) { if (chmod(redirect_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) { - ULOG_ERR(g_log, "Authorizing of %s which is modified is failure:%s", strerror(errno)); + ULOG_ERR(g_log, "Authorizing of %s which is modified is failure:%s", redirect_path, strerror(errno)); return 1; } return 0; } +int write_conf_content_authorizing(FILE *fp, const u8 level, const char *filter_mod, void *arg) +{ + if (write_conf_content(fp, level, filter_mod, arg) != 0) { + ULOG_ERR(g_log, "configure of log conosle which is written is failure"); + return 1; + } + + if (modify_authorizing((const char *)arg) != 0) { + ULOG_ERR(g_log, "Modifying authorizing of %s is failure", (const char *)arg); + return 1; + } + + return 0; +} + + diff --git a/Platform/user/ulog/log-sched/log_common.h b/Platform/user/ulog/log-sched/log_common.h index bdd5b5e42..891f8866b 100755 --- a/Platform/user/ulog/log-sched/log_common.h +++ b/Platform/user/ulog/log-sched/log_common.h @@ -3,7 +3,8 @@ #include "ulog_api.h" -#define MAX_LINE_SIZE 1024 +#define MAX_LINE_SZ 1024 +#define MAX_PATH_SZ MAX_LINE_SZ #define LOG_CONF_PATH "/etc/rsyslog.d/" @@ -25,10 +26,9 @@ static level_str_t g_level_array[] = { {LOG_DEBUG, "debug"} }; -int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, - const char *rediect_path, const char *filter_mod); -int modify_authorizing(const char *redirect_path); - - +int write_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 write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg); +int write_conf_content_authorizing(FILE *fp, const u8 level, const char *filter_mod, void *arg); #endif diff --git a/Platform/user/ulog/log-sched/log_console.c b/Platform/user/ulog/log-sched/log_console.c index f8796e501..a0bd060b1 100755 --- a/Platform/user/ulog/log-sched/log_console.c +++ b/Platform/user/ulog/log-sched/log_console.c @@ -7,15 +7,11 @@ static int config_log_console(const log_console_t *conf) { - if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, LOG_REDIRECT_CONSOLE, conf->module_name) != 0) { - ULOG_ERR(g_log, "configure of log conosle which is written is failure"); + if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, conf->module_name, + write_conf_content_authorizing, (void *)LOG_REDIRECT_CONSOLE) != 0) { + ULOG_ERR(g_log, "Log configure which is written is failure"); return 1; } - - if (modify_authorizing(LOG_REDIRECT_CONSOLE) != 0) { - ULOG_ERR(g_log, "Modifying authorizing of %s is failure", LOG_REDIRECT_CONSOLE); - return 1; - } return 0; } diff --git a/Platform/user/ulog/log-sched/log_file.c b/Platform/user/ulog/log-sched/log_file.c index 8b04c8497..d2bfd4a74 100755 --- a/Platform/user/ulog/log-sched/log_file.c +++ b/Platform/user/ulog/log-sched/log_file.c @@ -103,7 +103,9 @@ static int conf_log_file(const log_file_t *conf) strncpy(path, DEFAULT_LOG_FILE, sizeof(path)); } - if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_FILE_NAME, path, NULL) != 0) { + ULOG_DEBUG(g_log, "write_conf_content:%x", write_conf_content); + if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_FILE_NAME, NULL, + write_conf_content, (void *)path) != 0) { ULOG_ERR(g_log, "Log configure which is written is failure"); return 1; } diff --git a/Platform/user/ulog/log-sched/log_file.h b/Platform/user/ulog/log-sched/log_file.h index 087b2c16c..e6d8120ef 100755 --- a/Platform/user/ulog/log-sched/log_file.h +++ b/Platform/user/ulog/log-sched/log_file.h @@ -8,8 +8,6 @@ #include "rpc_common.h" #include "log_common.h" -#define MAX_LOG_PATH MAX_LINE_SIZE - typedef enum { LOG_UNCOMPRESS = 0, LOG_COMPRESS @@ -17,7 +15,7 @@ typedef enum { typedef struct _log_file { u8 level; - char path[MAX_LOG_PATH]; + char path[MAX_PATH_SZ]; log_compress_t is_compress; u32 del_over_days; u64 del_over_size; diff --git a/Platform/user/ulog/log-sched/log_tty.c b/Platform/user/ulog/log-sched/log_tty.c index 759c50569..ded33b003 100755 --- a/Platform/user/ulog/log-sched/log_tty.c +++ b/Platform/user/ulog/log-sched/log_tty.c @@ -7,18 +7,19 @@ #include "log_common.h" #define LOG_CONF_TTY_FILE_NAME "log-tty.conf" -#define LOG_DEV_DIR "/dev" +#define LOG_DEV_DIR "/dev/" -static int config_log_tty(const log_tty_t *conf) +static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) { 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)); return 1; } struct dirent *ptr; + char path[MAX_PATH_SZ]; while ((ptr = readdir(dir)) != NULL) { if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0) @@ -32,17 +33,29 @@ static int config_log_tty(const log_tty_t *conf) || (strcmp(ptr->d_name, "ttyprintk") == 0) || (strstr(ptr->d_name, "ttyS") != NULL)) { ULOG_DEBUG(g_log,"The file:[%s] isn't redirected\n", ptr->d_name); + continue; } - - } -/* - if (write_log_conf_authorizing(conf->level, LOG_CONF_PATH, - LOG_CONF_COSOLE_FILE_NAME, LOG_REDIRECT_CONSOLE, conf->module_name) != 0) { - ULOG_ERR(g_log, "Writing tty[module:%s] of log is failure", conf->module_name); + ULOG_DEBUG(g_log, "tty name:%s", ptr->d_name); + snprintf(path, sizeof(path), "%s%s", LOG_DEV_DIR, ptr->d_name); + if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) { + ULOG_ERR(g_log, "Writing tty[module:%s] of log is failure", filter_mod); return 1; } -*/ + + } + + return 0; +} + +static int config_log_tty(const log_tty_t *conf) +{ + if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_TTY_FILE_NAME, conf->module_name, + write_tty_content, NULL) != 0) { + ULOG_ERR(g_log, "Log's tty configure which is written is failure"); + return 1; + } + return 0; }