diff --git a/Platform/user/ulog/log-sched/log_common.c b/Platform/user/ulog/log-sched/log_common.c index 9f3b31462..c0350fd48 100755 --- a/Platform/user/ulog/log-sched/log_common.c +++ b/Platform/user/ulog/log-sched/log_common.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "ulog.h" @@ -8,7 +9,7 @@ #define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n" -#define BAK_FILE "/tmp/%s" + static int copy_file(const char *src, const char *dst) { @@ -97,6 +98,10 @@ END: if (rename(bak_file, conf_path_file) < 0) { ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno)); } + } else { + if (unlink(bak_file) < 0) { + ULOG_WARNING(g_log, "Delete back file:%s is failure", bak_file); + } } return ret; @@ -142,7 +147,7 @@ int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *a strcat(line, ";"); } } - strcat(line, " "); + strcat(line, REDIRECT_SEPERATE); strcat(line, (const char *)arg); strcat(line, "\n"); diff --git a/Platform/user/ulog/log-sched/log_common.h b/Platform/user/ulog/log-sched/log_common.h index b81ccd022..67eb51606 100755 --- a/Platform/user/ulog/log-sched/log_common.h +++ b/Platform/user/ulog/log-sched/log_common.h @@ -9,6 +9,10 @@ #define LOG_CONF_PATH "/etc/rsyslog.d/" #define LOG_DEV_DIR "/dev/" +#define BAK_FILE "/tmp/%s" + + +#define REDIRECT_SEPERATE " " typedef struct _level_str { diff --git a/Platform/user/ulog/log-sched/log_remote.c b/Platform/user/ulog/log-sched/log_remote.c index 9c6733ca1..dda147bcc 100755 --- a/Platform/user/ulog/log-sched/log_remote.c +++ b/Platform/user/ulog/log-sched/log_remote.c @@ -10,16 +10,100 @@ #define LOG_CONF_REMOTE_FILE_NAME "log-remote.conf" -static int config_log_remote_host(const log_remote_host_t *conf) -{ - char redirect[MAX_LINE_SZ]; +typedef struct _rfc_key_fmt { + log_rfc_t rfc; + char fmt[20]; +} rfc_key_fmt; - if (snprintf(redirect, sizeof(redirect), "@%s:%u", conf->host, conf->port) < 0) { - ULOG_ERR(g_log, "Setting remote redirect[%s:%u] is faulure", conf->host, conf->port); +static rfc_key_fmt rfc_tbl[] = { + {LOG_RFC_3164, "RFC3164fmt"}, + {LOG_RFC_5424, "RFC5424fmt"} +}; + +static int match_line(const log_remote_host_t *conf) +{ + +} + +static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) +{ + int ret = 1; + FILE *bak_fp = NULL; + char path[MAX_PATH_SZ]; + snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME); + bak_fp = fopen(bak_fp, "r"); + if (bak_fp == NULL) { + ULOG_ERR(g_log, "Opening remote back file:%s is failure:%s", path, strerror(errno)); return 1; } - log_conf_append(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL, - write_conf_content, (void *)redirect); + + char *line = NULL; + ssize_t n, n1; + char *pad, *host, *rfc_fmt; + u16 port; + while ((n = getline(&line, &n, bak_fp)) != -1) { + n1 = sscanf(line, "%s"REDIRECT_SEPERATE"%s:%u:%s", &pad, &host, &port, &rfc_fmt); + if (n1 == 4) { + // 匹配到 + } if (errno != 0) { + // 错误发生 + } else { + // 未知行,跳过 + continue; + } + + if (write_conf_content(fp, level, filter_mod, arg) != 0) { + ULOG_ERR(g_log, "Write remote configure is failure"); + goto END; + } + } + + + ret = 0; +END: + if (line != NULL) { + free(line); + } + if (bak_fp != NULL) { + fclose(bak_fp); + } + + return ret; +} + +static int add_remote_host(const log_remote_host_t *conf) +{ + char prefix[1] = ""; + if (conf->rfc == LOG_RFC_5424) { + strcat(prefix, "@"); + }; + + char redirect[MAX_LINE_SZ]; + if (snprintf(redirect, sizeof(redirect), "%s@%s:%u:%s", + prefix, conf->host, conf->port, rfc_tbl[conf->rfc].fmt) < 0) { + ULOG_ERR(g_log, "Setting remote redirect[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt); + return 1; + } + if (log_conf_append(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL, + write_conf_content, (void *)redirect) != 0) { + ULOG_ERR(g_log, "Adding remote server[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt); + return 1; + } + + return 0; +} + +static int del_remote_host(const log_remote_host_t *conf) +{ +} + +static int config_log_remote_host(const log_remote_host_t *conf) +{ + if ((sizeof(rfc_tbl) / sizeof(rfc_key_fmt)) < conf->rfc) { + ULOG_WARNING(g_log, "Unknown rfc format key:%u", conf->rfc); + return 1; + } + return 0; } diff --git a/Platform/user/ulog/log-sched/log_remote.h b/Platform/user/ulog/log-sched/log_remote.h index b37b76346..c813fce19 100755 --- a/Platform/user/ulog/log-sched/log_remote.h +++ b/Platform/user/ulog/log-sched/log_remote.h @@ -10,9 +10,14 @@ typedef enum { LOG_REMOTE_OP_DEL } log_op_t; +typedef enum { + LOG_RFC_3164 = 0, + LOG_RFC_5424 +} log_rfc_t; + typedef struct _log_remote_host { log_op_t op; - u8 rfc; + log_rfc_t rfc; char host[256]; u16 port; } log_remote_host_t; diff --git a/Platform/user/ulog/log-sched/log_sched.c b/Platform/user/ulog/log-sched/log_sched.c index ae6ea10f3..9057d1449 100755 --- a/Platform/user/ulog/log-sched/log_sched.c +++ b/Platform/user/ulog/log-sched/log_sched.c @@ -83,6 +83,7 @@ int main(int argc, char **argv) log_remote_host_t remote = {0}; strcpy(remote.host, "192.168.3.1"); + remote.rfc = LOG_RFC_5424; remote.port = 514; rpc_conf_log_remote(NULL, &remote, sizeof(remote), NULL); END: