From 30ec3fac7793238795d19cbfd7840073a63dcc62 Mon Sep 17 00:00:00 2001 From: liangxia Date: Fri, 30 Aug 2019 10:36:56 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=20aaa-12=20(1)=E5=A2=9E=E5=8A=A0log-pty?= =?UTF-8?q?=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96=E5=B7=A5=E4=BD=9C--?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E5=B9=B6?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE;=20(2)=E5=A2=9E=E5=8A=A0lo?= =?UTF-8?q?g-pty=E7=9A=84exit=E5=89=8D=E6=94=B6=E5=B0=BE=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?;=20(3)log-pty=E9=85=8D=E7=BD=AE=E6=97=B6=E5=86=99=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6;=20(4)log-sched=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E5=9C=A8=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE=E5=90=8E?= =?UTF-8?q?=E9=87=8D=E5=90=AFrsyslog=20RCA=EF=BC=9A=20SOL=EF=BC=9A=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9Aliangxia=20=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E4=BA=BA=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Platform/user/ulog/log-sched/log_common.c | 70 ++++++++++++++----- Platform/user/ulog/log-sched/log_common.h | 2 +- Platform/user/ulog/log-sched/log_console.c | 3 +- Platform/user/ulog/log-sched/log_pty.c | 80 ++++++++++++++++++++++ Platform/user/ulog/log-sched/log_pty.h | 2 + Platform/user/ulog/log-sched/log_sched.c | 30 ++++++-- Platform/user/ulog/log-sched/sev_sched.c | 2 +- Platform/user/ulog/log-sched/sev_sched.h | 1 + 8 files changed, 161 insertions(+), 29 deletions(-) diff --git a/Platform/user/ulog/log-sched/log_common.c b/Platform/user/ulog/log-sched/log_common.c index b201e1718..e87740d53 100755 --- a/Platform/user/ulog/log-sched/log_common.c +++ b/Platform/user/ulog/log-sched/log_common.c @@ -383,9 +383,13 @@ int write_log_file_conf(const char *p_key_str, const char *p_value_str) return 0; } -int get_log_file_conf(const char *key_str, char *value_str, int value_len) +ret_code get_log_file_conf(const char *key_str, char *value_str, int value_len) { - int ret = -1; + ret_code ret = RET_OK; + char *pos = NULL; + char *pos2 = NULL; + size_t str_len = 0; + if (fseek(g_conf_fp, 0, SEEK_SET) == -1) { ULOG_ERR(g_log, "Seeknig config to begin is faiure:%s", strerror(errno)); return ret; @@ -394,23 +398,53 @@ int get_log_file_conf(const char *key_str, char *value_str, int value_len) ssize_t n, n1, n2; char *line = NULL; char tmp_key[MAX_LINE_SZ], tmp_value[MAX_LINE_SZ]; - while ((n1 = getline(&line, &n, g_conf_fp)) != -1) { - ULOG_DEBUG(g_log, "config file line:%s", line); - n2 = sscanf(line, "%s=%s", tmp_key, tmp_value); - if (n2 == 1) { - if (strcmp(tmp_key, key_str) == 0) { - strncpy(value_str, tmp_value, value_len); - ret = 0; - break; - } - } else if (n2 == EOF) { - ULOG_ERR(g_log, "Parsing level from configure is failure:%s", strerror(errno)); - break; - } else { - ULOG_DEBUG(g_log, "Unknown row:%s", line); - } - } + while ((getline(&line, &n, g_conf_fp)) != -1) + { + if (strstr(line, key_str) == NULL ) + { + continue; + } + + pos = strstr(line, "="); + if (pos != NULL) + { + if (strlen(pos) <= 1) + { + continue; + } + + pos ++; + if ((pos2 = strstr(pos, " ")) != NULL) + { + str_len = strlen(pos) - strlen(pos2); + } + else + { + str_len = strlen(pos); + } + + if (str_len >= value_len) + { + ULOG_ERR(g_log, "Get log-sched config %s, but invaild value", key_str); + ret = RET_ERR; + break; + } + + memset(value_str, 0, value_len); + strncpy(value_str, pos, str_len); + + /* 滤除掉段尾的回车换行 */ + if ('\n' == value_str[strlen(value_str)-1]) + { + value_str[strlen(value_str)-1] = '\0'; + } + + ULOG_DEBUG(g_log, "Get log-sched config %s:%s", key_str, value_str); + break; + } + } + return ret; } diff --git a/Platform/user/ulog/log-sched/log_common.h b/Platform/user/ulog/log-sched/log_common.h index 8f92a9cd0..19595e554 100755 --- a/Platform/user/ulog/log-sched/log_common.h +++ b/Platform/user/ulog/log-sched/log_common.h @@ -31,7 +31,7 @@ int log_level_to_str(const u8 level, char *str, u32 len); int log_off_with_file(const char *path, const char *file_name); void rpc_conf_proc(rpc_conn *conn, pointer input, int input_len, int need_len, rpc_cb cb, void *arg); int write_log_file_conf(const char *p_key_str, const char *p_value_str); -int get_log_file_conf(const char *key_str, char *value_str, int value_len); +ret_code get_log_file_conf(const char *key_str, char *value_str, int value_len); #endif diff --git a/Platform/user/ulog/log-sched/log_console.c b/Platform/user/ulog/log-sched/log_console.c index 08fca2ebf..adf1aaa27 100755 --- a/Platform/user/ulog/log-sched/log_console.c +++ b/Platform/user/ulog/log-sched/log_console.c @@ -138,7 +138,7 @@ ret_code console_initial() char *pos = NULL; char *pos2 = NULL; char ttyfile_str[128] = ""; - int str_len = 0; + size_t str_len = 0; fp = fopen(CM_LOG_CONF_CONSOLE_FILE, "r"); if (NULL == fp) { @@ -178,7 +178,6 @@ ret_code console_initial() ttyfile_str[strlen(ttyfile_str)-1] = '\0'; } - if(modify_authorizing(ttyfile_str) == RET_OK) { ULOG_DEBUG(g_log, "Modify authorizing %s successed.", ttyfile_str); diff --git a/Platform/user/ulog/log-sched/log_pty.c b/Platform/user/ulog/log-sched/log_pty.c index f1a981f41..0674d5468 100755 --- a/Platform/user/ulog/log-sched/log_pty.c +++ b/Platform/user/ulog/log-sched/log_pty.c @@ -15,12 +15,17 @@ #define MAX_EVENT_NUMBER 64 #define LOG_DEV_PTY_DIR LOG_DEV_DIR"pts/" #define LOG_CONF_PTY_FILE_NAME "log-pty.conf" +#define LOG_CONF_KEY_PTY_LEVEL_STR "pty.level" +#define LOG_CONF_KEY_PTY_MODULE_STR "pty.module" + + int g_epoll_fd = -1; int g_watch_fd = -1; int g_inotify_fd = -1; pthread_t g_monitor_thread; +int access(const char *pathname, int mode); static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg); static void *pty_monitor_thread(void *arg) @@ -206,13 +211,25 @@ static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, v static int config_log_pty(const log_pty_t *conf) { int ret = -1; + char value_str[128]=""; switch (conf->on) { case LOG_OFF: stop_dev_pty_monitor(); ret = log_off_with_file(LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME); + + /* off时,将log-sched配置文件中pty日志级别调成默认值info,即level=6 */ + if (0 == ret) { + memset(value_str, 0, sizeof(value_str)); + sprintf(value_str, "%u", 6); + if (write_log_file_conf(LOG_CONF_KEY_PTY_LEVEL_STR, value_str) != 0) { + ULOG_ERR(g_log, "Pty-level which is written is failure"); + return -1; + } + } break; + case LOG_ON: if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME, conf->module, write_pty_content, NULL) != 0) { @@ -221,6 +238,24 @@ static int config_log_pty(const log_pty_t *conf) } else { ret = 0; } + + /* 写pty日志级别与模块名称到log-sched配置文件 */ + if (0 == ret) { + memset(value_str, 0, sizeof(value_str)); + sprintf(value_str, "%u", conf->level); + if (write_log_file_conf(LOG_CONF_KEY_PTY_LEVEL_STR, value_str) != 0) { + ULOG_ERR(g_log, "Pty-level which is written is failure"); + return -1; + } + + memset(value_str, 0, sizeof(value_str)); + sprintf(value_str, "%s", conf->module); + if (write_log_file_conf(LOG_CONF_KEY_PTY_MODULE_STR, value_str) != 0) { + ULOG_ERR(g_log, "Pty-module which is written is failure"); + return -1; + } + } + break; default: ULOG_WARNING(g_log, "Unknown on value:%u", conf->on); @@ -244,3 +279,48 @@ void rpc_conf_log_pty(rpc_conn *conn, pointer input, int input_len, pointer data rpc_conf_proc(conn, input, input_len, sizeof(log_pty_t), __rpc_conf_log_pty, NULL); } +ret_code pty_initial() +{ + char value_str[128]=""; + uint level = 6; + char *module = NULL; + ret_code ret = RET_OK; + + memset(value_str, 0, sizeof(value_str)); + sprintf(value_str, "%s%s", LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME); + if (access(value_str, 0) == 0) + { + /* 获取配置 */ + memset(value_str, 0, sizeof(value_str)); + if (get_log_file_conf(LOG_CONF_KEY_PTY_LEVEL_STR, value_str, sizeof(value_str)) == 0) + { + level = atoi(value_str); + } + + memset(value_str, 0, sizeof(value_str)); + if (get_log_file_conf(LOG_CONF_KEY_PTY_MODULE_STR, value_str, sizeof(value_str)) == 0) + { + module = value_str; + } + + if (log_conf((u8)level, LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME, module, + write_pty_content, NULL) != 0) + { + ULOG_ERR(g_log, "Log's pty configure which is written is failure"); + ret = RET_ERR; + } + else + { + ULOG_DEBUG(g_log, "Pty config level(%u) and module(%s) successed.", level, module!=NULL?module:"NULL"); + } + } + + return ret; +} + +ret_code pty_exit() +{ + stop_dev_pty_monitor(); + return RET_OK; +} + diff --git a/Platform/user/ulog/log-sched/log_pty.h b/Platform/user/ulog/log-sched/log_pty.h index 4b5b3b41f..bb25ddbf2 100755 --- a/Platform/user/ulog/log-sched/log_pty.h +++ b/Platform/user/ulog/log-sched/log_pty.h @@ -5,5 +5,7 @@ #include "rpc.h" void rpc_conf_log_pty(rpc_conn *conn, pointer input, int input_len, pointer data); +ret_code pty_initial(); +ret_code pty_exit(); #endif \ No newline at end of file diff --git a/Platform/user/ulog/log-sched/log_sched.c b/Platform/user/ulog/log-sched/log_sched.c index df4b06451..566541a68 100755 --- a/Platform/user/ulog/log-sched/log_sched.c +++ b/Platform/user/ulog/log-sched/log_sched.c @@ -24,13 +24,16 @@ FILE *g_conf_fp; char g_conf_file[MAX_PATH_SZ]; typedef ret_code (*initial_cb)(); -typedef struct _log_initial { +typedef ret_code (*exit_cb)(); +typedef struct _log_opt { char type[20]; - initial_cb cb; -} log_initial_t; + initial_cb init_func; + exit_cb exit_func; +} log_opt_t; -log_initial_t g_log_initial[] = { - {"console", console_initial} +log_opt_t g_log_opt[] = { + {"console", console_initial, NULL}, + {"monitor", pty_initial, pty_exit} }; @@ -77,10 +80,15 @@ int main(int argc, char **argv) } /* 各类配置初始化 */ - for (int i = 0; i < (sizeof(g_log_initial) / sizeof(log_initial_t)); i++) { - g_log_initial[i].cb(); + for (int i = 0; i < (sizeof(g_log_opt) / sizeof(log_opt_t)); i++) { + if ( NULL != g_log_opt[i].init_func) { + g_log_opt[i].init_func(); + } } + /* 重启syslog */ + sev_restart(); + rpc_server *server = rpc_server_create_ex(RPC_MODULE_SYSLOG_NAME); if (server == NULL) { @@ -140,6 +148,14 @@ int main(int argc, char **argv) ULOG_INFO(g_log, "%s is shutdown", LOG_SCHED_MODULE_NAME); END: + + /* 各类配置退出前的收尾 */ + for (int i = 0; i < (sizeof(g_log_opt) / sizeof(log_opt_t)); i++) { + if ( NULL != g_log_opt[i].exit_func) { + g_log_opt[i].exit_func(); + } + } + if (g_conf_fp != NULL) { fclose(g_conf_fp); } diff --git a/Platform/user/ulog/log-sched/sev_sched.c b/Platform/user/ulog/log-sched/sev_sched.c index 63db2589d..1bf9ff99b 100755 --- a/Platform/user/ulog/log-sched/sev_sched.c +++ b/Platform/user/ulog/log-sched/sev_sched.c @@ -19,7 +19,7 @@ static sem_t g_sem; static volatile int g_is_exit = 0; static volatile sev_state_t g_sev_state = SEV_STATE_WAIT; -static void sev_restart() +void sev_restart() { ULOG_DEBUG(g_log, "Log service is restarting"); diff --git a/Platform/user/ulog/log-sched/sev_sched.h b/Platform/user/ulog/log-sched/sev_sched.h index 803d57f68..c23d8eebb 100755 --- a/Platform/user/ulog/log-sched/sev_sched.h +++ b/Platform/user/ulog/log-sched/sev_sched.h @@ -4,6 +4,7 @@ int log_sev_init(); int log_sev_exit(); int log_sev_restart(); +void sev_restart(); void sev_loop(); #endif \ No newline at end of file