Add aaa-12 add ulog console

RCA:
SOL:
修改人:zhangtao
检视人:
This commit is contained in:
zhangtaohz 2019-07-26 11:53:33 +08:00
parent 2f2c32d6eb
commit b560f90520
14 changed files with 455 additions and 88 deletions

View File

@ -34,7 +34,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
PLAT_ARM64_CFLAGS := -fPIC -I../../Common
PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../user/ulog
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
@ -44,6 +44,7 @@ PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
#gcc libs
# this line must be at below of thus, because of...
include ../../Common/common.Makefile

View File

@ -1,5 +1,5 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=syslog-sched
TARGET_NAME=log-sched
# target
# for linux module driver: KO
@ -21,20 +21,20 @@ DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../user/ulog/syslog-schedule
VPATH = ../user/ulog/log-sched
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = log_file.c log_sched.c
COMMON_SRCS = log_file.c log_console.c log_common.c log_sched.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/rpc
PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/rpc -I../user/ulog
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
@ -42,17 +42,17 @@ PLAT_ARM64_LDFLAGS := -lpthread
PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
#gcc libs
ARM64_LIBS := ./libopenrpc-arm64.so ../thirdparty/arm64/libev-arm64.so
LINUX_LIBS := ./libopenrpc-linux.so ../thirdparty/x86_64/libev-linux.so
ARM64_LIBS := ./libopenrpc-arm64.so ./libulogapi-arm64.so ../thirdparty/arm64/libev-arm64.so
LINUX_LIBS := ./libopenrpc-linux.so ./libulogapi-linux.so ../thirdparty/x86_64/libev-linux.so
ifeq ($(PLAT_ARM64), TRUE)
DEPEND_LIB += ./debug/libopenrpc-arm64.so
USER_CLEAN_ITEMS += ./libopenrpc-arm64.so
DEPEND_LIB += ./debug/libopenrpc-arm64.so ./debug/libulogapi-arm64.so
USER_CLEAN_ITEMS += ./libopenrpc-arm64.so ./libulogapi-arm64.so
endif
ifeq ($(PLAT_LINUX), TRUE)
DEPEND_LIB += ./debug/libopenrpc-linux.so
USER_CLEAN_ITEMS += ./libopenrpc-linux.so
DEPEND_LIB += ./debug/libopenrpc-linux.so ./debug/libulogapi-linux.so
USER_CLEAN_ITEMS += ./libopenrpc-linux.so ./libulogapi-linux.so
endif
# this line must be at below of thus, because of...

View File

@ -0,0 +1,100 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include "ulog.h"
#include "log_common.h"
#define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n"
#define BAK_FILE "/tmp/%s"
int write_log_conf(const u8 level, const char *conf_path, const char *conf_file,
const char *rediect_path, const char *filter_mod)
{
FILE *fp = NULL;
int ret = 1;
u8 exist_backup = 1;
/********** rsyslog configure **********/
char conf_path_file[MAX_LINE_SIZE];
snprintf(conf_path_file, sizeof(conf_path_file), "%s%s", conf_path, conf_file);
char bak_file[MAX_LINE_SIZE];
snprintf(bak_file, sizeof(bak_file), BAK_FILE, conf_file);
if (rename(conf_path_file, bak_file) < 0) {
if (errno == ENOENT) {
exist_backup = 0;
ULOG_INFO(g_log, "Been not exist, Configure file:%s is need to backup");
} else {
ULOG_ERR(g_log, "Baking configure file:%s is failure:%s %d", conf_path_file, strerror(errno), errno);
return ret;
}
}
fp = fopen(conf_path_file, "w");
if (fp == NULL) {
ULOG_ERR(g_log, "Opening log configure file:%s is failure:%s", conf_path_file, strerror(errno));
goto END;
}
int i;
char line[MAX_LINE_SIZE + 100] = {0};
if ((filter_mod != NULL) && (strlen(filter_mod) > 0)) {
snprintf(line, sizeof(line), FILTER_CONTENT, filter_mod);
if (fputs(line, fp) == EOF) {
ULOG_ERR(g_log, "Message filter:%s of configure file which is written is failure:%s",
filter_mod, strerror(errno));
goto END;
}
}
char tmp[20];
line[0] = '\0'; // 清零
for (i = 0; i <= level;) {
if (snprintf(tmp, sizeof(tmp), "*.=%s", g_level_array[i].str) < 0) {
ULOG_ERR(g_log, "Setting content of log file configure is failure");
goto END;
}
strcat(line, tmp);
i++;
if (level >= i) {
strcat(line, ";");
}
}
strcat(line, " ");
strcat(line, rediect_path);
if (fputs(line, fp) == EOF) {
ULOG_ERR(g_log, "Configure file which is written is failure:%s", strerror(errno));
goto END;
}
ret = 0;
END:
if (fp != NULL) {
fclose(fp);
}
if ((ret == 1) && (exist_backup == 1)) {
// 回复备份配置
if (rename(bak_file, conf_path_file) < 0) {
ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno));
}
}
return ret;
}
int modify_authorizing(const char *redirect_path)
{
if (chmod(redirect_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) {
ULOG_ERR(g_log, "Authorizing of %s which is modified is failure:%s", strerror(errno));
return 1;
}
return 0;
}

View File

@ -0,0 +1,32 @@
#ifndef _LOG_COMMON_H
#define _LOG_COMMON_H
#include "ulog_api.h"
#define MAX_LINE_SIZE 1024
#define LOG_CONF_PATH "/etc/rsyslog.d/"
typedef struct _level_str {
u32 level;
char str[10];
} level_str_t;
extern ulog_t *g_log;
static level_str_t g_level_array[] = {
{LOG_EMERG, "emerg"},
{LOG_ALERT, "alert"},
{LOG_CRIT, "crit"},
{LOG_ERR, "err"},
{LOG_WARNING, "warn"},
{LOG_NOTICE, "notice"},
{LOG_INFO, "info"},
{LOG_DEBUG, "debug"}
};
int write_log_conf(const u8 level, const char *conf_path, const char *conf_file,
const char *rediect_path, const char *filter_mod);
int modify_authorizing(const char *redirect_path);
#endif

View File

@ -0,0 +1,35 @@
#include "log_console.h"
#include "log_common.h"
#define LOG_CONF_COSOLE_FILE_NAME "log-console.conf"
#define LOG_REDIRECT_CONSOLE "/dev/console"
static int config_log_console(const log_console_t *conf)
{
if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, LOG_REDIRECT_CONSOLE, conf->module_name) != 0) {
ULOG_ERR(g_log, "configure of log conosle which is written is failure");
return 1;
}
if (modify_authorizing(LOG_REDIRECT_CONSOLE) != 0) {
ULOG_ERR(g_log, "Modifying authorizing of %s is failure", LOG_REDIRECT_CONSOLE);
return 1;
}
return 0;
}
void rpc_conf_log_console(rpc_conn *conn, pointer input, int input_len, pointer data)
{
u32 need_len = sizeof(log_console_t);
if (input_len < need_len) {
ULOG_WARNING(g_log,
"The input paramter of rpc log console is needed length of %u, but the actual length is %u",
need_len, input_len);
return;
}
config_log_console((const log_console_t *)input);
}

View File

@ -0,0 +1,16 @@
#ifndef _LOG_CONSOLE_H
#define _LOG_CONSOLE_H
#include "ulog_api.h"
#include "common_types.h"
#include "rpc_common.h"
typedef struct _log_console {
u8 level;
u8 on;
char module_name[MAX_MODULE_NAME_SZ];
} log_console_t;
void rpc_conf_log_console(rpc_conn *conn, pointer input, int input_len, pointer data);
#endif

View File

@ -0,0 +1,137 @@
#include <string.h>
#include <errno.h>
#include "log_file.h"
#include "log_common.h"
#define LOG_CONF_FILE_NAME "log-file.conf"
#define LOG_LOGRATATE_FILE_NAME "/etc/logrotate.d/log-syslog"
#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"
/*
typedef struct _level_str {
u32 level;
char str[10];
} level_str_t;
static level_str_t g_level_array[] = {
{LOG_EMERG, "emerg"},
{LOG_ALERT, "alert"},
{LOG_CRIT, "crit"},
{LOG_ERR, "err"},
{LOG_WARNING, "warn"},
{LOG_NOTICE, "notice"},
{LOG_INFO, "info"},
{LOG_DEBUG, "debug"}
};
*/
#define MAX_LOG_LEVEL_VALUE (sizeof(g_level_array) / sizeof(level_str_t))
static int write_logratate_conf(const log_file_t *conf, const char *log_path)
{
int ret = 1;
FILE *fp = NULL;
/********** logrotate **********/
char str_compress[sizeof(STR_COMPRESS)] = {0};
if (conf->is_compress == LOG_COMPRESS) {
strncpy(str_compress, STR_COMPRESS, sizeof(str_compress));
}
u32 days = DEFAULT_LOG_DEL_OVER_DAYS;
if (conf->del_over_days > 0) {
days = conf->del_over_days;
}
char line[1024] = {0};
if (snprintf(line, sizeof(line), LOGROTATE_CONF, log_path, days, str_compress) < 0) {
ULOG_ERR(g_log, "Setting content of logratote is failure");
goto END;
}
fp = fopen(LOG_LOGRATATE_FILE_NAME, "w");
if (fp == NULL) {
ULOG_ERR(g_log, "Opening logratate file:%s is failure:%s", LOG_CONF_FILE_NAME, strerror(errno));
goto END;
}
if (fputs(line, fp) == EOF) {
ULOG_ERR(g_log, "Configure file of logratate which is written is failure:%s", strerror(errno));
goto END;
}
ret = 0;
END:
if (fp != NULL) {
fclose(fp);
}
return ret;
}
static int conf_log_file(const log_file_t *conf)
{
int ret = 1;
u32 max_level = MAX_LOG_LEVEL_VALUE;
if (conf->level > max_level) {
ULOG_WARNING(g_log, "Configure log level:%u more than max value:%u", conf->level, max_level);
return 1;
}
char path[sizeof(conf->path)];
if (strlen(conf->path) > 0) {
strncpy(path, conf->path, sizeof(path));
} else {
strncpy(path, DEFAULT_LOG_FILE, sizeof(path));
}
if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_FILE_NAME, path, NULL) != 0) {
ULOG_ERR(g_log, "Log configure which is written is failure");
return 1;
}
/********** logrotate **********/
if (write_logratate_conf(conf, path) != 0) {
ULOG_ERR(g_log, "Logratate configure which is written is failure");
return 1;
}
if (conf->del_over_size > 0) {
}
return ret;
}
void rpc_conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data)
{
u32 need_len = sizeof(log_file_t);
if (input_len < need_len) {
ULOG_WARNING(g_log,
"The input paramter of rpc log file is needed length of %u, but the actual length is %u",
need_len, input_len);
return;
}
conf_log_file((const log_file_t *)input);
//rpc_return_null(conn);
}

View File

@ -0,0 +1,29 @@
#ifndef __LOG_H
#define __LOG_H
#include <syslog.h>
#include <linux/limits.h>
#include "common_types.h"
#include "rpc_common.h"
#include "log_common.h"
#define MAX_LOG_PATH MAX_LINE_SIZE
typedef enum {
LOG_UNCOMPRESS = 0,
LOG_COMPRESS
} log_compress_t;
typedef struct _log_file {
u8 level;
char path[MAX_LOG_PATH];
log_compress_t is_compress;
u32 del_over_days;
u64 del_over_size;
} log_file_t;
void rpc_conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data);
#endif

View File

@ -0,0 +1,72 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "rpc_server.h"
#include "ulog_api.h"
#include "log_file.h"
#include "log_console.h"
#include "rpc_module.h"
#define LOG_SCHED_MODULE_NAME "log-sched"
#define SERVICE_LOG_FILE_NAME "log-file"
ulog_t *g_log = NULL;
int main(int argc, char **argv)
{
int run_daemon = 0;
char *options = "d";
int opt;
while ((opt = getopt(argc, argv, options)) != -1) {
switch (opt) {
case 'd':
run_daemon = 1;
break;
}
}
g_log = ulog_init(LOG_SCHED_MODULE_NAME, !run_daemon);
if (g_log == NULL) {
fprintf(stderr, "Initiating ulog is failure");
return 1;
}
if (run_daemon) {
if (daemon(0, 0) == -1) {
ULOG_ERR(g_log, "Setting daemon running is failure:%s", strerror(errno));
goto END;
}
}
rpc_server *server = rpc_server_create_ex(RPC_MODULE_SYSLOG_NAME);
if (server == NULL)
{
ULOG_ERR(g_log, "start server error");
return 1;
}
ULOG_INFO(g_log, "Server of log schedule is started");
/* 注册配置处理函数 */
rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", rpc_conf_log_file);
log_file_t log = {0};
log.is_compress = LOG_UNCOMPRESS; //LOG_COMPRESS;
log.del_over_days = 10;
log.level = LOG_DEBUG;
rpc_conf_log_file(NULL, &log, sizeof(log), NULL);
log_console_t console = {0};
strcpy(console.module_name, "111");
console.level = LOG_DEBUG;
console.on = 1;
rpc_conf_log_console(NULL, &console, sizeof(console), NULL);
END:
ulog_close(g_log);
return 0;
}

View File

@ -1,7 +0,0 @@
#include "log_file.h"
int conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data)
{
return 0;
}

View File

@ -1,20 +0,0 @@
#ifndef __LOG_H
#define __LOG_H
#include <linux/limits.h>
#include "common_types.h"
#include "rpc_common.h"
typedef struct _log_file {
u8 level;
char path[PATH_MAX];
u32 compress_over_days;
u32 del_over_days;
u64 compress_over_size;
u64 del_over_size;
} log_file_t;
int conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data);
#endif

View File

@ -1,41 +0,0 @@
#include <stdio.h>
#include <unistd.h>
#include "log_file.h"
#define SYSLOG_SCHED_RPC_NAME "syslog_schedule_rpc"
#define SERVICE_LOG_FILE_NAME "log_file"
int main(int argc, char **argv)
{
int run_daemon = 0;
char *options = "d";
int opt;
while ((opt = getopt(argc, argv, options)) != -1) {
switch (opt) {
case 'd':
run_daemon = 1;
break;
}
}
if (run_daemon) {
if (daemon(0, 0) == -1) {
return 1;
}
}
rpc_server *server = rpc_server_create_ex(SYSLOG_SCHED_RPC_NAME);
if (server = NULL)
{
printf("start server error\n");
return 1;
}
printf("Server of log schedule is started\n");
/* 注册配置处理函数 */
rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", conf_log_file);
}

View File

@ -1,14 +1,16 @@
#include <stdio.h>
#include <stdarg.h>
#include "ulog.h"
#include "common_types.h"
#include "ulog_api.h"
#define LOG_MSG_SZ 1024
ulog_t *ulog_init(const char *module_name)
ulog_t *ulog_init(const char *module_name, u8 is_print)
{
ulog_t *log;
u32 len = sizeof(module_name);
u32 len = strlen(module_name);
if (len > MAX_MODULE_NAME_SZ) {
fprintf(stderr, "The length:%d of module_name can't more than %d", len, MAX_MODULE_NAME_SZ);
@ -22,30 +24,35 @@ ulog_t *ulog_init(const char *module_name)
}
strncpy(log->module_name, module_name, len);
int opt = LOG_PERROR |LOG_PID;
if (is_print > 0) {
opt |= LOG_CONS;
}
openlog(log->module_name, opt, LOG_USER);
return log;
}
void ulog_close(ulog_t *log)
{
if (log == NULL) {
return;
if (log != NULL) {
free(log);
}
free(log);
closelog();
}
void ulog_record(const ulog_t *log, int level, const char *fmt, ...)
{
if (log == NULL) {
perror("Log is null");
fprintf(stderr, "Log is null");
return;
}
char log_buf[LOG_MSG_SZ];
va_list args;
va_start(args, fmt);
vsnprintf(log_buf, sizeof(log_buf), log_buf, args);
vsnprintf(log_buf, sizeof(log_buf), fmt, args);
va_end(args);
syslog(level, "[%s] %s", log->module_name, log_buf);
syslog(level, MODULE_FMT" %s", log->module_name, log_buf);
}

6
Platform/user/ulog/ulog.h Executable file
View File

@ -0,0 +1,6 @@
#ifndef _ULOG_H
#define _ULOG_H
#define MODULE_FMT "[%s]"
#endif