secgateway/Platform/user/ulog/log-sched/log_file.c

140 lines
3.9 KiB
C
Executable File

#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));
}
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,
write_conf_content, (void *)path) != 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);
}