后台改为zlog实现

This commit is contained in:
yangwei
2019-08-31 23:35:44 +08:00
parent cecbe2628d
commit 49822347cb
52 changed files with 8198 additions and 127 deletions

View File

@@ -1,22 +1,20 @@
#include "MESA_handle_logger.h"
#include "zlog.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include<sys/stat.h>
#define LOGMSG_MAX_LEN 4096
#define FLUSH_LOG_NUM 4096
#define MAX_HANDLE_LOG_PATH 4096
#define GLOB_ZLOG_CONF "GLOB_ZLOG_CONF"
typedef struct log_handle_t
{
int runtime_log_level;
int flush_log_count;
FILE *fp;
char runtime_log_file[1200];
char cur_log_file[LOGMSG_MAX_LEN];
zlog_category_t *zc;
const char *global_conf_path;
char runtime_log_file[MAX_HANDLE_LOG_PATH];
} log_handle_t;
@@ -45,12 +43,6 @@ static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL;
#endif
static unsigned char weekday_str[7][4] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static int create_dir(const char *dir_path, int path_len)
{
if(dir_path == NULL)
@@ -93,35 +85,94 @@ static int create_path(const char *path, int path_len)
return 0;
}
static zlog_category_t *init_zlog(const char *conf_path, const char *category)
{
zlog_category_t *zc = NULL;
if (access(conf_path, R_OK) != -1)
{
int rc = zlog_init(conf_path);
if (rc)
{
rc = zlog_reload(conf_path);
if(rc)
{
printf("init zlog by %s failed\n", conf_path);
return NULL;
}
}
zc = zlog_get_category(category);
if (!zc)
{
printf("get zlog category %s in %s fail\n", category, conf_path);
zlog_fini();
return NULL;
}
}
return zc;
}
void *MESA_create_runtime_log_handle(const char *file_path, int level)
{
if(file_path == NULL)
return NULL;
int rc;
zlog_category_t *zc;
FILE *fp = NULL;
log_handle_t *p_handle = NULL;
//creating file_path failed, return NULL
char *p_path = rindex(file_path, '/');
if(p_path != 0)
{
if(create_path(file_path, p_path - file_path) < 0)
return NULL;
}
if(NULL == (fp = fopen(file_path, "w")))
return NULL;
const char *p_conf_path = NULL;
const char *p_name = NULL;
char *p_path_end = rindex(file_path, '/');
int path_len = 0;
if (p_path_end != NULL)
{
path_len = p_path_end - file_path;
if(path_len == 0)return NULL;
p_name = p_path_end+1;
}
else
{
p_name = file_path;
}
char *pathvar = getenv(GLOB_ZLOG_CONF);
//fclose(fp);
//remove(file_path);
if (pathvar == NULL)
{
//creating file_path failed, return NULL
if (path_len > 0 && create_path(file_path, path_len) < 0)return NULL;
char default_zlog_conf_path[MAX_HANDLE_LOG_PATH + 5];
snprintf(default_zlog_conf_path, sizeof(default_zlog_conf_path), "%s.conf", file_path);
if (access(default_zlog_conf_path, R_OK) != 0)
{
fp = fopen(default_zlog_conf_path, "w");
if(fp == NULL)return NULL;
char zlog_rule_conf_content[MAX_HANDLE_LOG_PATH + 1];
snprintf(zlog_rule_conf_content, sizeof(zlog_rule_conf_content),
"[global] \n default format = \"%%d(%%c), %%V, %%m%%n\" \n[levels]\n DEBUG=10\n INFO=20\n FATAL=30\n[rules]\n %s.* \"%s.%%d(%%F)\"",
p_name, file_path);
fwrite(zlog_rule_conf_content, strlen(zlog_rule_conf_content), 1, fp);
fclose(fp);
}
p_conf_path = default_zlog_conf_path;
}
else
{
if (access(pathvar, R_OK) != 0)
{
return NULL;
}
p_conf_path = pathvar;
}
zc = init_zlog(p_conf_path, p_name);
if (zc == NULL)return NULL;
p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1);
if(p_handle == NULL)
return NULL;
strncpy(p_handle->runtime_log_file, file_path, 1024);
p_handle->runtime_log_file[1024] = '\0';
strncpy(p_handle->runtime_log_file, file_path, sizeof(p_handle->runtime_log_file) - 1);
p_handle->runtime_log_level = level;
//p_handle->fp = fp;
p_handle->zc = zc;
p_handle->global_conf_path = pathvar;
return (void *)p_handle;
}
@@ -130,12 +181,7 @@ void MESA_destroy_runtime_log_handle(void *handle)
if(handle != NULL)
{
log_handle_t *p_handle = (log_handle_t *)handle;
if (p_handle->fp != NULL)
{
fclose(p_handle->fp);
p_handle->fp = NULL;
}
free(handle);
free(handle);
handle = NULL;
}
@@ -144,98 +190,17 @@ void MESA_destroy_runtime_log_handle(void *handle)
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
{
char buf[LOGMSG_MAX_LEN + 1];
time_t t;
int len;
va_list ap;
FILE *fp;
struct tm local_time;
char tmp_log_file_name[1400];
log_handle_t *p_handle = (log_handle_t *)handle;
if(p_handle == NULL || p_handle->runtime_log_file == NULL)return;
if(level < p_handle->runtime_log_level) return;
time(&t);
if(NULL == (localtime_r(&t, &local_time))) return;
len = snprintf(buf, sizeof(buf), "%s %s %d %02d:%02d:%02d %d", weekday_str[local_time.tm_wday],
month_str[local_time.tm_mon], local_time.tm_mday, local_time.tm_hour, local_time.tm_min, local_time.tm_sec, local_time.tm_year+1900);
//len = strlen(buf);
switch(level)
{
case RLOG_LV_DEBUG:
len += snprintf(buf + len,
LOGMSG_MAX_LEN - len, ", %s, ", "DEBUG");
break;
case RLOG_LV_INFO:
len += snprintf(buf + len,
LOGMSG_MAX_LEN - len, ", %s, ", "INFO");
break;
case RLOG_LV_FATAL:
len += snprintf(buf + len,
LOGMSG_MAX_LEN - len, ", %s, ", "FATAL");
break;
default:
len += snprintf(buf + len,
LOGMSG_MAX_LEN - len, ", %s, ", "UNKNOWN");
break;
}
if(0 >= LOGMSG_MAX_LEN - len) return;
len += snprintf(buf + len, LOGMSG_MAX_LEN - len, "%s, ", module);
if(0 >= LOGMSG_MAX_LEN - len) return;
va_list ap;
va_start(ap, fmt);
len += vsnprintf(buf + len, LOGMSG_MAX_LEN - len, fmt, ap);
vzlog(p_handle->zc, __FILE__, sizeof(__FILE__) - 1, __func__, sizeof(__func__) - 1, __LINE__, level, fmt, ap);
va_end(ap);
if(0 >= LOGMSG_MAX_LEN - len) return;
len += snprintf(buf + len, LOGMSG_MAX_LEN - len, "\n");
sprintf(tmp_log_file_name, "%s.%04d-%02d-%02d", p_handle->runtime_log_file,
local_time.tm_year + 1900, local_time.tm_mon + 1,
local_time.tm_mday);
OPEN_LOG_FILE:
if(p_handle->fp == NULL)
{
if(NULL == (fp = fopen(tmp_log_file_name, "a"))) return;
p_handle->fp = fp;
p_handle->flush_log_count = 0;
memcpy(p_handle->cur_log_file, tmp_log_file_name, strlen(tmp_log_file_name));
}
if (0 != memcmp(tmp_log_file_name, p_handle->cur_log_file, strlen(tmp_log_file_name)))
{
fclose(p_handle->fp);
p_handle->fp = NULL;
goto OPEN_LOG_FILE;
}
if (0 > fprintf(p_handle->fp, "%s", buf))
{
fclose(p_handle->fp);
p_handle->fp = NULL;
}
else
{
p_handle->flush_log_count+=1;
if (p_handle->flush_log_count >= FLUSH_LOG_NUM)
{
fflush(p_handle->fp);
p_handle->flush_log_count = 0;
}
}
return;
return ;
}