Add aaa-12 add ulog remote and alert console redirect

RCA:
SOL:
修改人:zhangtao
检视人:
This commit is contained in:
zhangtaohz 2019-07-29 18:20:17 +08:00
parent c26c7db68f
commit bbf0ed8e72
9 changed files with 199 additions and 20 deletions

View File

@ -27,7 +27,7 @@ VPATH = ../user/ulog/log-sched
# set the source file, don't used .o because of ... # set the source file, don't used .o because of ...
COMMON_SRCS = log_file.c log_console.c log_tty.c log_common.c log_sched.c COMMON_SRCS = log_file.c log_console.c log_tty.c log_remote.c log_common.c log_sched.c
# MRS Board Source Files # MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS) PLAT_LINUX_SRCS = $(COMMON_SRCS)

View File

@ -10,7 +10,47 @@
#define BAK_FILE "/tmp/%s" #define BAK_FILE "/tmp/%s"
int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, static int copy_file(const char *src, const char *dst)
{
int ret = 1;
FILE *src_fp = NULL,*dst_fp = NULL;
src_fp = fopen(src, "r");
if (src_fp == NULL) {
ULOG_ERR(g_log, "Opening src file:%s is failure:%s", src, strerror(errno));
goto END;
}
dst_fp = fopen(dst, "w");
if (dst_fp == NULL) {
ULOG_ERR(g_log, "Opening dst file:%s is failure:%s", src, strerror(errno));
goto END;
}
char buff[1024];
int len;
int n;
while(feof(src_fp) > 0)
{
len = fread(buff, 1, sizeof(buff), src_fp);
n = fwrite(buff, 1, len ,dst_fp);
if (n != len) {
ULOG_ERR(g_log, "Configure file which is written[length:%d, actual len:%n] is failure:%s", n, len, strerror(errno));
goto END;
}
}
ret = 0;
END:
if (src_fp != NULL) {
fclose(src_fp);
}
if (dst_fp != NULL) {
fclose(dst_fp);
}
return ret;
}
static int __log_conf(const char *mode, const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod,
int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg) int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg)
{ {
FILE *fp = NULL; FILE *fp = NULL;
@ -23,17 +63,18 @@ int write_log_conf(const u8 level, const char *conf_path, const char *conf_file,
char bak_file[MAX_PATH_SZ]; char bak_file[MAX_PATH_SZ];
snprintf(bak_file, sizeof(bak_file), BAK_FILE, conf_file); snprintf(bak_file, sizeof(bak_file), BAK_FILE, conf_file);
if (rename(conf_path_file, bak_file) < 0) { //if (rename(conf_path_file, bak_file) < 0)
if (copy_file(conf_path_file, bak_file) != 0) {
if (errno == ENOENT) { if (errno == ENOENT) {
exist_backup = 0; exist_backup = 0;
ULOG_INFO(g_log, "Been not exist, Configure file:%s is need to backup"); ULOG_INFO(g_log, "Been not exist, Configure file:%s don't need to backup", conf_path_file);
} else { } else {
ULOG_ERR(g_log, "Baking configure file:%s is failure:%s %d", conf_path_file, strerror(errno), errno); ULOG_ERR(g_log, "Baking configure file:%s is failure:%s", conf_path_file, strerror(errno));
return ret; return ret;
} }
} }
fp = fopen(conf_path_file, "w"); fp = fopen(conf_path_file, mode);
if (fp == NULL) { if (fp == NULL) {
ULOG_ERR(g_log, "Opening log configure file:%s is failure:%s", conf_path_file, strerror(errno)); ULOG_ERR(g_log, "Opening log configure file:%s is failure:%s", conf_path_file, strerror(errno));
goto END; goto END;
@ -61,6 +102,18 @@ END:
return ret; return ret;
} }
int log_conf(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod,
int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg)
{
return __log_conf("w", level, conf_path, conf_file, filter_mod, cb_content, arg);
}
int log_conf_append(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod,
int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg)
{
return __log_conf("a", level, conf_path, conf_file, filter_mod, cb_content, arg);
}
int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg)
{ {
int i; int i;
@ -94,8 +147,10 @@ int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *a
strcat(line, (const char *)arg); strcat(line, (const char *)arg);
strcat(line, "\n"); strcat(line, "\n");
if (fputs(line, fp) == EOF) { //if (fputs(line, fp) == EOF) {
ULOG_ERR(g_log, "Configure file which is written is failure:%s", strerror(errno)); int n = fwrite(line, 1, strlen(line), fp);
if (n != strlen(line)) {
ULOG_ERR(g_log, "Configure file which is written[length:%d] is failure:%s", n, strerror(errno));
goto END; goto END;
} }

View File

@ -8,6 +8,9 @@
#define LOG_CONF_PATH "/etc/rsyslog.d/" #define LOG_CONF_PATH "/etc/rsyslog.d/"
#define LOG_DEV_DIR "/dev/"
typedef struct _level_str { typedef struct _level_str {
u32 level; u32 level;
char str[10]; char str[10];
@ -26,7 +29,9 @@ static level_str_t g_level_array[] = {
{LOG_DEBUG, "debug"} {LOG_DEBUG, "debug"}
}; };
int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod, int log_conf(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod,
int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg);
int log_conf_append(const u8 level, const char *conf_path, const char *conf_file, const char *filter_mod,
int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg); int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg);
int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg); int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg);
int write_conf_content_authorizing(FILE *fp, const u8 level, const char *filter_mod, void *arg); int write_conf_content_authorizing(FILE *fp, const u8 level, const char *filter_mod, void *arg);

View File

@ -1,3 +1,7 @@
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include "log_console.h" #include "log_console.h"
#include "log_common.h" #include "log_common.h"
@ -5,11 +9,51 @@
#define LOG_REDIRECT_CONSOLE "/dev/console" #define LOG_REDIRECT_CONSOLE "/dev/console"
static int write_console_content(FILE *fp, const u8 level, const char *filter_mod, void *arg)
{
DIR *dir;
if ((dir = opendir(LOG_DEV_DIR)) == NULL) {
ULOG_ERR(g_log, "Open dir:[%s] is failure:%d\n", LOG_DEV_DIR, strerror(errno));
return 1;
}
struct dirent *ptr;
char path[MAX_PATH_SZ];
while ((ptr = readdir(dir)) != NULL) {
if ((strcmp(ptr->d_name, ".") == 0)
|| (strcmp(ptr->d_name, "..") == 0)
|| (ptr->d_type == DT_DIR)) { ///current dir OR parrent dir
ULOG_DEBUG(g_log,"The file:[%s] or directory jump over\n", ptr->d_name);
continue;
}
if ((strstr(ptr->d_name, "ttyS") == NULL)) {
ULOG_DEBUG(g_log,"The file:[%s] isn't redirected\n", ptr->d_name);
continue;
}
ULOG_DEBUG(g_log, "ttyS name:%s", ptr->d_name);
if (snprintf(path, sizeof(path), "%s%s", LOG_DEV_DIR, ptr->d_name) < 0) {
ULOG_ERR(g_log, "Setting %s of log console is failure", ptr->d_name);
return 1;
}
if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) {
ULOG_ERR(g_log, "Writing tty[module:%s] of log is failure", filter_mod);
return 1;
}
}
return 0;
}
static int config_log_console(const log_console_t *conf) static int config_log_console(const log_console_t *conf)
{ {
if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, conf->module_name, if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, conf->module_name,
write_conf_content_authorizing, (void *)LOG_REDIRECT_CONSOLE) != 0) { write_console_content, NULL) != 0) {
ULOG_ERR(g_log, "Log configure which is written is failure"); ULOG_ERR(g_log, "Log's console configure which is written is failure");
return 1; return 1;
} }

View File

@ -104,7 +104,7 @@ static int conf_log_file(const log_file_t *conf)
} }
ULOG_DEBUG(g_log, "write_conf_content:%x", write_conf_content); ULOG_DEBUG(g_log, "write_conf_content:%x", write_conf_content);
if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_FILE_NAME, NULL, if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_FILE_NAME, NULL,
write_conf_content, (void *)path) != 0) { write_conf_content, (void *)path) != 0) {
ULOG_ERR(g_log, "Log configure which is written is failure"); ULOG_ERR(g_log, "Log configure which is written is failure");
return 1; return 1;

View File

@ -0,0 +1,39 @@
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <syslog.h>
#include "log_remote.h"
#include "log_common.h"
#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];
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);
return 1;
}
log_conf_append(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL,
write_conf_content, (void *)redirect);
return 0;
}
void rpc_conf_log_remote(rpc_conn *conn, pointer input, int input_len, pointer data)
{
u32 need_len = sizeof(log_remote_host_t);
if (input_len < need_len) {
ULOG_WARNING(g_log,
"The input paramter of rpc log remote host is needed length of %u, but the actual length is %u",
need_len, input_len);
return;
}
config_log_remote_host((const log_remote_host_t *)input);
}

View File

@ -0,0 +1,22 @@
#ifndef _LOG_REMOTE_H
#define _LOG_REMOTE_H
#include "ulog_api.h"
#include "common_types.h"
#include "rpc_common.h"
typedef enum {
LOG_REMOTE_OP_ADD = 0,
LOG_REMOTE_OP_DEL
} log_op_t;
typedef struct _log_remote_host {
log_op_t op;
u8 rfc;
char host[256];
u16 port;
} log_remote_host_t;
void rpc_conf_log_remote(rpc_conn *conn, pointer input, int input_len, pointer data);
#endif

View File

@ -8,11 +8,15 @@
#include "log_file.h" #include "log_file.h"
#include "log_console.h" #include "log_console.h"
#include "log_tty.h" #include "log_tty.h"
#include "log_remote.h"
#include "rpc_module.h" #include "rpc_module.h"
#define LOG_SCHED_MODULE_NAME "log-sched" #define LOG_SCHED_MODULE_NAME "log-sched"
#define SERVICE_LOG_FILE_NAME "log-file" #define SERVICE_LOG_FILE_NAME "log-file"
#define SERIVCE_LOG_CONSOLE_NAME "log-console"
#define SERVICE_LOG_TTY_NAME "log-tty"
ulog_t *g_log = NULL; ulog_t *g_log = NULL;
@ -55,6 +59,8 @@ int main(int argc, char **argv)
/* 注册配置处理函数 */ /* 注册配置处理函数 */
rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", rpc_conf_log_file); rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", rpc_conf_log_file);
rpc_server_regservice(server, SERIVCE_LOG_CONSOLE_NAME, "conf_log_console", rpc_conf_log_console);
rpc_server_regservice(server, SERVICE_LOG_TTY_NAME, "conf_log_tty", rpc_conf_log_tty);
log_file_t log = {0}; log_file_t log = {0};
log.is_compress = LOG_UNCOMPRESS; //LOG_COMPRESS; log.is_compress = LOG_UNCOMPRESS; //LOG_COMPRESS;
@ -70,10 +76,15 @@ int main(int argc, char **argv)
log_tty_t tty = {0}; log_tty_t tty = {0};
strcpy(tty.module_name, "111"); //strcpy(tty.module_name, "111");
tty.level = LOG_DEBUG; tty.level = LOG_DEBUG;
tty.on = 1; tty.on = 1;
rpc_conf_log_tty(NULL, &tty, sizeof(tty), NULL); rpc_conf_log_tty(NULL, &tty, sizeof(tty), NULL);
log_remote_host_t remote = {0};
strcpy(remote.host, "192.168.3.1");
remote.port = 514;
rpc_conf_log_remote(NULL, &remote, sizeof(remote), NULL);
END: END:
ulog_close(g_log); ulog_close(g_log);
return 0; return 0;

View File

@ -7,7 +7,7 @@
#include "log_common.h" #include "log_common.h"
#define LOG_CONF_TTY_FILE_NAME "log-tty.conf" #define LOG_CONF_TTY_FILE_NAME "log-tty.conf"
#define LOG_DEV_DIR "/dev/"
static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg) static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, void *arg)
{ {
@ -37,7 +37,10 @@ static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, v
} }
ULOG_DEBUG(g_log, "tty name:%s", ptr->d_name); ULOG_DEBUG(g_log, "tty name:%s", ptr->d_name);
snprintf(path, sizeof(path), "%s%s", LOG_DEV_DIR, ptr->d_name); if (snprintf(path, sizeof(path), "%s%s", LOG_DEV_DIR, ptr->d_name) < 0) {
ULOG_ERR(g_log, "Setting tty of log[%s] is failure", ptr->d_name);
return 1;
}
if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) { if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) {
ULOG_ERR(g_log, "Writing tty[module:%s] of log is failure", filter_mod); ULOG_ERR(g_log, "Writing tty[module:%s] of log is failure", filter_mod);
return 1; return 1;
@ -50,7 +53,7 @@ static int write_tty_content(FILE *fp, const u8 level, const char *filter_mod, v
static int config_log_tty(const log_tty_t *conf) static int config_log_tty(const log_tty_t *conf)
{ {
if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_TTY_FILE_NAME, conf->module_name, if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_TTY_FILE_NAME, conf->module_name,
write_tty_content, NULL) != 0) { write_tty_content, NULL) != 0) {
ULOG_ERR(g_log, "Log's tty configure which is written is failure"); ULOG_ERR(g_log, "Log's tty configure which is written is failure");
return 1; return 1;
@ -64,7 +67,7 @@ void rpc_conf_log_tty(rpc_conn *conn, pointer input, int input_len, pointer data
u32 need_len = sizeof(log_tty_t); u32 need_len = sizeof(log_tty_t);
if (input_len < need_len) { if (input_len < need_len) {
ULOG_WARNING(g_log, ULOG_WARNING(g_log,
"The input paramter of rpc log console is needed length of %u, but the actual length is %u", "The input paramter of rpc log tty is needed length of %u, but the actual length is %u",
need_len, input_len); need_len, input_len);
return; return;
} }