diff --git a/Platform/common/ulog/ulog_in.h b/Platform/common/ulog/ulog_in.h index 43320c20f..6e59539bc 100755 --- a/Platform/common/ulog/ulog_in.h +++ b/Platform/common/ulog/ulog_in.h @@ -19,6 +19,16 @@ #define CONF_LOG_REMOTE_DEL_HOST_FUNC "conf_log_del_remote" #define CONF_LOG_REMOTE_LEVEL_FUNC "conf_log_remote_level" +#define LOGROTATE_CONF "%s\n" \ + "{\n" \ + " rotate %u\n" \ + " daily\n" \ + " missingok\n" \ + "%s" \ + " postrotate\n" \ + " /usr/lib/rsyslog/rsyslog-rotate\n" \ + " endscript\n" \ + "}" typedef struct _level_str { u32 level; char str[10]; diff --git a/Platform/user/configm/config-server/log_config/log_config_cm.c b/Platform/user/configm/config-server/log_config/log_config_cm.c index d702526c2..9d4d04b79 100755 --- a/Platform/user/configm/config-server/log_config/log_config_cm.c +++ b/Platform/user/configm/config-server/log_config/log_config_cm.c @@ -99,8 +99,13 @@ int cm_log_check_file_is_exist(const char *file_str) } -/* 适用于log-console.conf与log-pty.conf的解析 */ -ret_code cm_log_get_module_from_file(const char *file_str, char *value_str, int value_len) +/* 适用于log-console.conf、log-pty.conf的解析 + * 格式如: + * :msg,contains,"[module_test]" + * *.=emerg;*.=alert;*.=crit;*.=err;*.=warn;*.=notice;*.=info;*.=debug /dev/pts/0 + * 解析结果为: module_test + */ +ret_code cm_log_get_module_from_file(const char *file_str, char *value_str, uint value_len) { ret_code ret = RET_ERR; FILE *fp = NULL; @@ -108,7 +113,7 @@ ret_code cm_log_get_module_from_file(const char *file_str, char *value_str, int char *pos2 = NULL; size_t tmp_len = 0; - if (NULL == file_str || '\0' == file_str[0] || (NULL == value_str && value_len>0)) + if (NULL == file_str || '\0' == file_str[0] || NULL == value_str || 0 == value_len) { ULOG_ERR(g_log_h, "get log module from file: bad input"); return ret; @@ -154,7 +159,11 @@ ret_code cm_log_get_module_from_file(const char *file_str, char *value_str, int return ret; } -/* 适用于log-console.conf与log-pty.conf的解析 */ +/* 适用于log-console.conf、log-pty.conf、log-file.conf的解析 + * 格式如: + * *.=emerg;*.=alert;*.=crit;*.=err;*.=warn;*.=notice;*.=info;*.=debug /dev/ttyS0 + * 解析结果为: debug + */ ret_code cm_log_get_level_from_file(const char *file_str, u8 *value) { ret_code ret = RET_ERR; @@ -212,11 +221,19 @@ ret_code cm_log_get_level_from_file(const char *file_str, u8 *value) pos = sub_line+strlen(".="); } - pos2 = strstr(pos, " "); - if (pos != NULL && pos2 != NULL) + if (pos != NULL) { - tmp_len = pos2-pos; + pos2 = strstr(pos, " "); + if (pos2 != NULL) + { + tmp_len = pos2-pos; + } + else + { + tmp_len = strlen(pos); + } + strncpy(tmp_str, pos, tmp_len); tmp_str[tmp_len] = '\0'; @@ -241,3 +258,93 @@ ret_code cm_log_get_level_from_file(const char *file_str, u8 *value) } +/* 适用于log-file.conf的解析 + * 格式如: + * *.=emerg;*.=alert;*.=crit;*.=err;*.=warn;*.=notice;*.=info;*.=debug /etc/rsyslog.d/filetest.conf + * 解析结果为: /etc/rsyslog.d/filetest.conf + */ +ret_code cm_log_get_path_from_file(const char *file_str, char *value_str, uint value_len) +{ + ret_code ret = RET_ERR; + FILE *fp = NULL; + ssize_t n; + char *line = NULL; + char *sub_line = NULL; + char *pos = NULL; + size_t tmp_len = 0; + + if (NULL == file_str || '\0' == file_str[0] || NULL == value_str || 0 == value_len) + { + ULOG_ERR(g_log_h, "get log path from file: bad input"); + return ret; + } + + fp = fopen(file_str, "r"); + if (NULL == fp) + { + return ret; + } + + if (fseek(fp, 0, SEEK_SET) == -1) + { + ULOG_ERR(g_log_h, "Seeknig config to begin is faiure:%s", strerror(errno)); + + fclose(fp); + return ret; + } + + while ((getline(&line, &n, fp)) != -1) + { + pos = line; + + if (NULL == strstr(pos, ".=")) + { + continue; + } + + /* 找到最后一个空格 */ + while (1) + { + sub_line = strstr(pos, " "); + + if (NULL == sub_line) + { + break; + } + + if (strlen(sub_line) <= strlen(" ")) + { + break; + } + + pos = sub_line+strlen(" "); + } + + if (pos != NULL) + { + tmp_len = strlen(pos); + if (tmp_len < value_len) + { + strncpy(value_str, pos, tmp_len); + value_str[tmp_len] = '\0'; + + /* 需要滤除掉行尾的换行符 */ + if ('\n' == value_str[tmp_len-1]) + { + value_str[tmp_len-1] = '\0'; + } + } + break; + } + } + + ret = RET_OK; + if (NULL != fp) + { + fclose(fp); + } + return ret; +} + + + diff --git a/Platform/user/configm/config-server/log_config/log_config_cm.h b/Platform/user/configm/config-server/log_config/log_config_cm.h index f7a8ae2c2..98d32ffb0 100755 --- a/Platform/user/configm/config-server/log_config/log_config_cm.h +++ b/Platform/user/configm/config-server/log_config/log_config_cm.h @@ -55,10 +55,23 @@ ret_code log_rpc_exec(char* service_name, char* method_name, pointer input, int ret_code cm_log_get_keyvalue_from_file(const char *file_str, const char *key_str, char *value_str, int value_len); int cm_log_check_file_is_exist(const char *file_str); -ret_code cm_log_get_module_from_file(const char *file_str, char *value_str, int value_len); +ret_code cm_log_get_module_from_file(const char *file_str, char *value_str, uint value_len); ret_code cm_log_get_level_from_file(const char *file_str, u8 *value); +ret_code cm_log_get_path_from_file(const char *file_str, char *value_str, uint value_len); +#ifndef CM_LOG_CONF_LOG_SCHED_FILE +#define CM_LOG_CONF_LOG_SCHED_FILE "/etc/log-sched.conf" +#endif + +#ifndef CM_LOG_CONF_LOGROTATE_FILE +#define CM_LOG_CONF_LOGROTATE_FILE "/etc/logrotate.d/log-syslog" +#endif + +#ifndef CM_LOG_CONF_LOGFILE_FILE +#define CM_LOG_CONF_LOGFILE_FILE "/etc/rsyslog.d/log-file.conf" +#endif + #ifndef CM_LOG_CONF_CONSOLE_FILE #define CM_LOG_CONF_CONSOLE_FILE "/etc/rsyslog.d/log-console.conf" #endif @@ -72,11 +85,7 @@ ret_code cm_log_get_level_from_file(const char *file_str, u8 *value); #endif #ifndef CM_LOG_CONF_REMOTE_LEVEL_FILE -#define CM_LOG_CONF_REMOTE_LEVEL_FILE "/etc/log-sched.conf" -#endif - -#ifndef CM_LOG_CONF_LOGFILE_FILE -#define CM_LOG_CONF_LOGFILE_FILE "/etc/log-sched.conf" +#define CM_LOG_CONF_REMOTE_LEVEL_FILE CM_LOG_CONF_LOG_SCHED_FILE #endif #endif diff --git a/Platform/user/configm/config-server/log_config/log_config_file.c b/Platform/user/configm/config-server/log_config/log_config_file.c index 0d297c68e..a83f87b9f 100755 --- a/Platform/user/configm/config-server/log_config/log_config_file.c +++ b/Platform/user/configm/config-server/log_config/log_config_file.c @@ -1,3 +1,4 @@ +#include #include "log_config.h" #include "log_config_cm.h" #include "rpc.h" @@ -27,9 +28,129 @@ ret_code log_file_config_proc(uint source, uint config_type, } + +static ret_code cm_log_get_logrotate_conf(const char *file_str, log_file_t *ret_conf) +{ + ret_code ret = RET_ERR; + FILE *fp = NULL; + ssize_t n; + char *line = NULL; + char *pos = NULL; + + if (NULL == file_str || '\0' == file_str[0] + || NULL == ret_conf) + { + ULOG_ERR(g_log_h, "get logrotate conf from file: bad input"); + } + + memset(ret_conf, 0, sizeof(*ret_conf)); + + fp = fopen(file_str, "r"); + if (NULL == fp) + { + return ret; + } + + if (fseek(fp, 0, SEEK_SET) == -1) + { + ULOG_ERR(g_log_h, "Seeknig config to begin is faiure:%s", strerror(errno)); + goto END; + } + + /* 解析日志存放路径,位于文件首行 */ + if ((getline(&line, &n, fp)) == -1 || NULL == line || strlen(line) == 0) + { + ULOG_DEBUG(g_log_h, "File %s is empty", file_str); + goto END; + } + else + { + strncpy(ret_conf->path, line, strlen(line)); + ret_conf->path[strlen(line)] = '\0'; + + /* 需要滤除掉行尾的换行符 */ + if ('\n' == ret_conf->path[strlen(line)-1]) + { + ret_conf->path[strlen(line)-1] = '\0'; + } + } + + /* 跳过 “{” 行 */ + if (getline(&line, &n, fp) == -1) + { + goto END; + } + + /* 解析日志存放最大天数 */ + if ((getline(&line, &n, fp)) != -1) + { + pos = strstr(line, "rotate "); + if (NULL == pos) + { + goto END; + } + + if (strlen(pos) == strlen("rotate ")) + { + goto END; + } + + pos += strlen("rotate "); + + /* 需要滤除掉行尾的换行符 */ + if ('\n' == pos[strlen(pos)-1]) + { + pos[strlen(pos)-1] = '\0'; + } + ret_conf->del_over_days = (uint)atoi(pos); + } + else + { + goto END; + } + + /* 跳过 “daily” 行 */ + if (getline(&line, &n, fp) == -1) + { + goto END; + } + + /* 跳过 missingok 行 */ + if (getline(&line, &n, fp) == -1) + { + goto END; + } + + /* 解析日志是否压缩 */ + if ((getline(&line, &n, fp)) != -1) + { + if (NULL != strstr(line, "compress")) + { + ret_conf->is_compress = LOG_COMPRESS; + } + else + { + ret_conf->is_compress = LOG_UNCOMPRESS; + } + } + + ret = RET_OK; + +END: + if (NULL != fp) + { + fclose(fp); + } + + return ret; +} + + static ret_code cm_get_log_file_elems(log_file_t *log_file_conf, int *err_no) { - char value[MAX_LINE_SZ] = ""; + char value_str[MAX_LINE_SZ] = ""; + u8 tmp_level = 6; /* INFO */ + log_file_t tmp_file_conf = {0}; if (NULL == log_file_conf || NULL == err_no) { @@ -37,17 +158,40 @@ static ret_code cm_get_log_file_elems(log_file_t *log_file_conf, int *err_no) } memset(log_file_conf, 0, sizeof(*log_file_conf)); + + /* 日志记录级别:level */ + if (cm_log_get_level_from_file(CM_LOG_CONF_LOGFILE_FILE, &tmp_level) == 0) + { + log_file_conf->level = tmp_level; + } - /* level、path、is_compress、del_over_days暂未实现 */ + /* 日志记录路径:path */ + memset(value_str, 0, MAX_LINE_SZ); + if (cm_log_get_path_from_file(CM_LOG_CONF_LOGFILE_FILE, value_str, MAX_PATH_SZ) == 0) + { + memset(log_file_conf->path, 0, MAX_PATH_SZ); + strncpy(log_file_conf->path, value_str, strlen(value_str)); + } + + /* 日志文件是否压缩:is_compress + * 日志存放最大天数:del_over_days */ + if (cm_log_check_file_is_exist(CM_LOG_CONF_CONSOLE_FILE) == 1) + { + if (RET_OK == cm_log_get_logrotate_conf(CM_LOG_CONF_LOGROTATE_FILE, &tmp_file_conf)) + { + log_file_conf->is_compress = tmp_file_conf.is_compress; + log_file_conf->del_over_days = tmp_file_conf.del_over_days; + } + } /* 日志文件大小上限:del_over_size */ - memset(value, 0, MAX_LINE_SZ); - if (cm_log_get_keyvalue_from_file(CM_LOG_CONF_LOGFILE_FILE, LOG_CONF_KEY_FILE_MAX_SIZE_STR, value, sizeof(value)) == 0) + memset(value_str, 0, MAX_LINE_SZ); + if (cm_log_get_keyvalue_from_file(CM_LOG_CONF_LOG_SCHED_FILE, LOG_CONF_KEY_FILE_MAX_SIZE_STR, value_str, sizeof(value_str)) == 0) { - if (strlen(value) < MAX_U64_SZ) + if (strlen(value_str) < MAX_U64_SZ) { memset(log_file_conf->del_over_size, 0, MAX_U64_SZ); - strncpy(log_file_conf->del_over_size, value, strlen(value)); + strncpy(log_file_conf->del_over_size, value_str, strlen(value_str)); } } diff --git a/Platform/user/ulog/log-sched/log_file.c b/Platform/user/ulog/log-sched/log_file.c index 27e1ebd9b..73283504c 100755 --- a/Platform/user/ulog/log-sched/log_file.c +++ b/Platform/user/ulog/log-sched/log_file.c @@ -14,17 +14,6 @@ #define DEFAULT_LOG_FILE "/var/log/syslog-test" #define DEFAULT_LOG_DEL_OVER_DAYS (30 * 6) -#define LOGROTATE_CONF "%s\n" \ - "{\n" \ - " rotate %u\n" \ - " daily\n" \ - " missingok\n" \ - "%s" \ - " postrotate\n" \ - " /usr/lib/rsyslog/rsyslog-rotate\n" \ - " endscript\n" \ - "}" - #define STR_COMPRESS " compress\n" #define LOG_CONF_KEY_FILE_MAX_SIZE_STR "file.max_size"