Add aaa-12 Adding key-value function and corresponding modify
RCA: SOL: 修改人:zhangtao 检视人:
This commit is contained in:
parent
b7814728ed
commit
1fa00a7b54
|
@ -9,6 +9,8 @@
|
||||||
#include "ulog_in.h"
|
#include "ulog_in.h"
|
||||||
|
|
||||||
#define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n"
|
#define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n"
|
||||||
|
#define DEFAULT_CONFIG_FILE_TMP "/etc/log-sched.conf.tmp"
|
||||||
|
|
||||||
|
|
||||||
static int copy_file(const char *src, const char *dst)
|
static int copy_file(const char *src, const char *dst)
|
||||||
{
|
{
|
||||||
|
@ -271,3 +273,147 @@ FAIL:
|
||||||
rpc_return_error(conn, RET_ERR, str_err);
|
rpc_return_error(conn, RET_ERR, str_err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 写配置到配置文件。追加在配置文件最后 */
|
||||||
|
int write_log_file_conf(const char *p_key_str, const char *p_value_str)
|
||||||
|
{
|
||||||
|
char conf_value_str[MAX_LINE_SZ] = "";
|
||||||
|
|
||||||
|
char buffer[MAX_LINE_SZ] = "";
|
||||||
|
char *p_pos = NULL;
|
||||||
|
int is_exist = 0;
|
||||||
|
FILE *tmp_conf_fp = NULL;
|
||||||
|
|
||||||
|
if (NULL == p_key_str || '\0' == p_key_str[0] || NULL == p_value_str) {
|
||||||
|
ULOG_ERR(g_log, "Bad input");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 定位到起始位置 */
|
||||||
|
if (fseek(g_conf_fp, 0, SEEK_SET) == -1) {
|
||||||
|
ULOG_ERR(g_log, "Seeknig config to begin is faiure:%s", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 查看配置是否已存在 */
|
||||||
|
while (fgets(buffer,MAX_LINE_SZ,g_conf_fp)!=NULL) {
|
||||||
|
p_pos = strstr(buffer, p_key_str);
|
||||||
|
if (p_pos != NULL) {
|
||||||
|
memset(conf_value_str, 0, sizeof(conf_value_str));
|
||||||
|
strcpy(conf_value_str, p_pos+strlen(p_key_str));
|
||||||
|
|
||||||
|
is_exist = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_exist == 1) {
|
||||||
|
/* 判断已有配置是否与当前传入配置相同 */
|
||||||
|
if (strlen(conf_value_str) == strlen(p_value_str) && 0 == strcmp(conf_value_str, p_value_str)) {
|
||||||
|
/* 无需做任何变更,本函数返回 */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 定位到起始位置 */
|
||||||
|
if (fseek(g_conf_fp, 0, SEEK_SET) == -1) {
|
||||||
|
ULOG_ERR(g_log, "Seeknig config to begin is faiure:%s", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 打开临时文件 */
|
||||||
|
tmp_conf_fp = fopen(DEFAULT_CONFIG_FILE_TMP, "w+");
|
||||||
|
if (NULL == tmp_conf_fp){
|
||||||
|
ULOG_ERR(g_log, "Open temp config file failed: %s", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 将原文件内容写入临时文件,但跳过当前配置相关项 */
|
||||||
|
while (fgets(buffer,MAX_LINE_SZ,g_conf_fp)!=NULL) {
|
||||||
|
if (strstr(buffer, p_key_str) != NULL) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
fputs(buffer,tmp_conf_fp);
|
||||||
|
}
|
||||||
|
fflush(tmp_conf_fp);
|
||||||
|
|
||||||
|
/* 将临时文件写回原配置文件 */
|
||||||
|
truncate(g_conf_file, 0);
|
||||||
|
// fclose(g_conf_fp);
|
||||||
|
// g_conf_fp = fopen(g_conf_file, "w+");
|
||||||
|
|
||||||
|
if (fseek(g_conf_fp, 0, SEEK_SET) == -1) {
|
||||||
|
ULOG_ERR(g_log, "Seeknig config to begin is faiure:%s", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fseek(tmp_conf_fp, 0, SEEK_SET) == -1) {
|
||||||
|
ULOG_ERR(g_log, "Seeknig config to begin is faiure:%s", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (fgets(buffer,MAX_LINE_SZ,tmp_conf_fp)!=NULL) {
|
||||||
|
fputs(buffer,g_conf_fp);
|
||||||
|
}
|
||||||
|
fflush(g_conf_fp);
|
||||||
|
|
||||||
|
/* 关闭临时文件 */
|
||||||
|
fclose(tmp_conf_fp);
|
||||||
|
tmp_conf_fp = NULL;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* 定位到结束位置 */
|
||||||
|
if (fseek(g_conf_fp, 0, SEEK_END) == -1) {
|
||||||
|
ULOG_ERR(g_log, "Seeknig config to end is faiure:%s", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 向配置文件中追加配置项 */
|
||||||
|
char fmt[MAX_LINE_SZ]="";
|
||||||
|
snprintf(fmt, sizeof(fmt), "%s=%s\n", p_key_str, p_value_str);
|
||||||
|
ULOG_DEBUG(g_log, "Configure content is %s", fmt);
|
||||||
|
int len = strlen(fmt);
|
||||||
|
int n = fwrite(fmt, 1, len, g_conf_fp);
|
||||||
|
if (len != n) {
|
||||||
|
ULOG_ERR(g_log, "Writing conf is failure:%s", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
fflush(g_conf_fp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_log_file_conf(const char *key_str, char *value_str, int value_len)
|
||||||
|
{
|
||||||
|
int ret = -1;
|
||||||
|
if (fseek(g_conf_fp, 0, SEEK_SET) == -1) {
|
||||||
|
ULOG_ERR(g_log, "Seeknig config to begin is faiure:%s", strerror(errno));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -30,5 +30,8 @@ int write_conf_content_authorizing(FILE *fp, const u8 level, const char *filter_
|
||||||
int log_level_to_str(const u8 level, char *str, u32 len);
|
int log_level_to_str(const u8 level, char *str, u32 len);
|
||||||
int log_off_with_file(const char *path, const char *file_name);
|
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);
|
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);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -27,6 +27,8 @@
|
||||||
|
|
||||||
#define STR_COMPRESS " compress\n"
|
#define STR_COMPRESS " compress\n"
|
||||||
|
|
||||||
|
#define LOG_CONF_KEY_FILE_MAX_SIZE_STR "file.max_size"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
typedef struct _level_str {
|
typedef struct _level_str {
|
||||||
u32 level;
|
u32 level;
|
||||||
|
@ -94,6 +96,7 @@ static int conf_log_file(const log_file_t *conf)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
u32 max_level = MAX_LOG_LEVEL_VALUE;
|
u32 max_level = MAX_LOG_LEVEL_VALUE;
|
||||||
|
char value_str[128] = "";
|
||||||
if (conf->level > max_level) {
|
if (conf->level > max_level) {
|
||||||
ULOG_WARNING(g_log, "Configure log level:%u more than max value:%u", conf->level, max_level);
|
ULOG_WARNING(g_log, "Configure log level:%u more than max value:%u", conf->level, max_level);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -119,14 +122,27 @@ static int conf_log_file(const log_file_t *conf)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 将日志文件大小上限写入本程序配置文件中 */
|
||||||
if (conf->del_over_size > 0) {
|
memset(value_str, 0, sizeof(value_str));
|
||||||
}
|
sprintf(value_str, "%llu", conf->del_over_size);
|
||||||
|
if (write_log_file_conf(LOG_CONF_KEY_FILE_MAX_SIZE_STR, value_str) != 0) {
|
||||||
|
ULOG_ERR(g_log, "Del-over-size which is written is failure");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int __rpc_conf_log_file(pointer input, const void *arg, char *str_err, int str_len)
|
||||||
|
{
|
||||||
|
if (conf_log_file((const log_file_t *)input) != 0) {
|
||||||
|
strncpy(str_err, "Configuring file of log is faiure", str_len);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void rpc_conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data)
|
void rpc_conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data)
|
||||||
{
|
{
|
||||||
u32 need_len = sizeof(log_file_t);
|
u32 need_len = sizeof(log_file_t);
|
||||||
|
@ -143,5 +159,7 @@ void rpc_conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer dat
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
rpc_return_null(conn);
|
rpc_return_null(conn);
|
||||||
|
|
||||||
|
rpc_conf_proc(conn, input, input_len, sizeof(log_file_t), __rpc_conf_log_file, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
#define RESET_SEEK(fp) fseek(fp, 0, SEEK_SET)
|
#define RESET_SEEK(fp) fseek(fp, 0, SEEK_SET)
|
||||||
|
|
||||||
#define LOG_CONF_KEY_REMOTE_LEVEL "remote.level="
|
#define LOG_CONF_KEY_REMOTE_LEVEL "remote.level"
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
LOG_REMOTE_OP_ADD = 0,
|
LOG_REMOTE_OP_ADD = 0,
|
||||||
|
@ -56,7 +56,7 @@ static int match_remote_config(const char *line, const void *src)
|
||||||
ULOG_DEBUG(g_log, "The line:%s can't be parsed", line);
|
ULOG_DEBUG(g_log, "The line:%s can't be parsed", line);
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod,
|
static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod,
|
||||||
|
@ -89,6 +89,8 @@ static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod,
|
||||||
} else if (match == 0) {
|
} else if (match == 0) {
|
||||||
ULOG_DEBUG(g_log, "Be matched");
|
ULOG_DEBUG(g_log, "Be matched");
|
||||||
continue;
|
continue;
|
||||||
|
} else {
|
||||||
|
ULOG_DEBUG(g_log, "Not be matched");
|
||||||
}
|
}
|
||||||
|
|
||||||
ULOG_DEBUG(g_log, "Recover old line:%s to file:%s", line, path);
|
ULOG_DEBUG(g_log, "Recover old line:%s to file:%s", line, path);
|
||||||
|
@ -197,25 +199,9 @@ static int modify_remote_conf_log_level(FILE *fp, const u8 level, const char *fi
|
||||||
|
|
||||||
static int get_log_level()
|
static int get_log_level()
|
||||||
{
|
{
|
||||||
if (RESET_SEEK(g_conf_fp) == -1) {
|
char value[MAX_LINE_SZ];
|
||||||
ULOG_ERR(g_log, "[get log level]Seeknig config to begin is faiure:%s", strerror(errno));
|
if (get_log_file_conf(LOG_CONF_KEY_REMOTE_LEVEL, value, sizeof(value)) == 0) {
|
||||||
return -1;
|
return atoi(value);
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t n, n1, n2;
|
|
||||||
char *line = NULL;
|
|
||||||
u8 value;
|
|
||||||
while ((n1 = getline(&line, &n, g_conf_fp)) != -1) {
|
|
||||||
ULOG_DEBUG(g_log, "config file line:%s", line);
|
|
||||||
n2 = sscanf(line, LOG_CONF_KEY_REMOTE_LEVEL"%u", &value);
|
|
||||||
if (n2 == 1) {
|
|
||||||
return value;
|
|
||||||
} else if (n2 == EOF) {
|
|
||||||
ULOG_ERR(g_log, "Parsing level from configure is failure:%s", strerror(errno));
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
ULOG_DEBUG(g_log, "Unknown level row:%s", line);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return LOG_INFO;
|
return LOG_INFO;
|
||||||
|
@ -297,34 +283,24 @@ static int config_log_remote_host(const log_op_t op, const log_remote_h
|
||||||
|
|
||||||
static int config_log_remote_level(const log_remote_level_t *level)
|
static int config_log_remote_level(const log_remote_level_t *level)
|
||||||
{
|
{
|
||||||
if (truncate(g_conf_file, 0) == -1) {
|
char str_level[4] = {0};
|
||||||
ULOG_ERR(g_log, "Truncate configure:%s is faiure:%s", g_conf_file, strerror(errno));
|
if (snprintf(str_level, sizeof(str_level), "%u", level->level) < 0) {
|
||||||
|
ULOG_ERR(g_log, "Building level value:%u to string is failure", level->level);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
/* 定位到开始位置 */
|
if (write_log_file_conf(LOG_CONF_KEY_REMOTE_LEVEL, str_level) != 0) {
|
||||||
if (RESET_SEEK(g_conf_fp) == -1) {
|
ULOG_ERR(g_log, "Writeing key-value[%s-%s] to config file is failure", LOG_CONF_KEY_REMOTE_LEVEL, str_level);
|
||||||
ULOG_ERR(g_log, "Seeknig config to begin is faiure:%s", strerror(errno));
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
char fmt[MAX_LINE_SZ];
|
|
||||||
snprintf(fmt, sizeof(fmt), LOG_CONF_KEY_REMOTE_LEVEL"%u", level->level);
|
|
||||||
ULOG_DEBUG(g_log, "Configure content is %s", fmt);
|
|
||||||
int len = strlen(fmt);
|
|
||||||
int n = fwrite(fmt, 1, len, g_conf_fp);
|
|
||||||
if (len != n) {
|
|
||||||
ULOG_ERR(g_log, "Writing remote level is failure:%s", strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
fflush(g_conf_fp);
|
|
||||||
|
|
||||||
//判断是否有rsyslog的remote配置,如果有,才写入
|
//判断是否有rsyslog的remote配置,如果有,才写入
|
||||||
char path[MAX_PATH_SZ];
|
char path[MAX_PATH_SZ];
|
||||||
if (snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME) < 0) {
|
if (snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME) < 0) {
|
||||||
ULOG_DEBUG(g_log, "Build backup path(%s) is failure", BAK_FILE, LOG_CONF_REMOTE_FILE_NAME);
|
ULOG_ERR(g_log, "Build backup path(%s) is failure", BAK_FILE, LOG_CONF_REMOTE_FILE_NAME);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(access(path, F_OK) != 0) {
|
if(access(path, F_OK) != 0) {
|
||||||
ULOG_DEBUG(g_log, "Backup file:%s is not exist:%s", path, strerror(errno));
|
ULOG_ERR(g_log, "Backup file:%s is not exist:%s", path, strerror(errno));
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
if (log_conf(level->level, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL,
|
if (log_conf(level->level, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL,
|
||||||
|
|
Loading…
Reference in New Issue