Add aaa-12 (1)增加log-pty的初始化工作--解析配置文件并加载配置; (2)增加log-pty的exit前收尾工作; (3)log-pty配置时写配置文件; (4)log-sched启动时在加载配置后重启rsyslog

RCA:
SOL:
修改人:liangxia
检视人:
This commit is contained in:
liangxia 2019-08-30 10:36:56 +08:00
parent c5d5a411ca
commit 30ec3fac77
8 changed files with 161 additions and 29 deletions

View File

@ -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,20 +398,50 @@ 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;
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;
}
} else if (n2 == EOF) {
ULOG_ERR(g_log, "Parsing level from configure is failure:%s", strerror(errno));
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;
} else {
ULOG_DEBUG(g_log, "Unknown row:%s", line);
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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,9 +80,14 @@ 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);
}

View File

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

View File

@ -4,6 +4,7 @@
int log_sev_init();
int log_sev_exit();
int log_sev_restart();
void sev_restart();
void sev_loop();
#endif