适配zlog使用模式,增加creation,reconstruction,destruction三个函数,用于初始化,重载和销毁zlog运行环境
This commit is contained in:
@@ -19,11 +19,15 @@ extern "C"
|
|||||||
#define RLOG_LV_FATAL 30
|
#define RLOG_LV_FATAL 30
|
||||||
|
|
||||||
|
|
||||||
|
int MESA_handle_runtime_log_creation(const char *conf_path);
|
||||||
|
int MESA_handle_runtime_log_reconstruction(const char *conf_path);
|
||||||
|
void MESA_handle_runtime_log_destruction();
|
||||||
|
|
||||||
#define MESA_HANDLE_RUNTIME_LOG(handle, lv, mod, fmt, args...) \
|
#define MESA_HANDLE_RUNTIME_LOG(handle, lv, mod, fmt, args...) \
|
||||||
MESA_handle_runtime_log((handle), (lv), (mod), "file %s, line %d, " fmt, \
|
MESA_handle_runtime_log((handle), (lv), (mod), "file %s, line %d, " fmt, \
|
||||||
__FILE__, __LINE__, ##args)
|
__FILE__, __LINE__, ##args)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* name: MESA_create_runtime_log_handle
|
* name: MESA_create_runtime_log_handle
|
||||||
* functionality: get runtime_log handle;
|
* functionality: get runtime_log handle;
|
||||||
* params:
|
* params:
|
||||||
@@ -33,7 +37,7 @@ extern "C"
|
|||||||
* not NULL, if succeeded;
|
* not NULL, if succeeded;
|
||||||
* NULL, if file is not absolute path, or failed to create log file;
|
* NULL, if file is not absolute path, or failed to create log file;
|
||||||
*/
|
*/
|
||||||
void *MESA_create_runtime_log_handle(const char *file_path, int level);
|
void *MESA_create_runtime_log_handle(const char *file_path, int level);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* name: MESA_handle_runtime_log
|
* name: MESA_handle_runtime_log
|
||||||
|
|||||||
@@ -6,12 +6,12 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
static int g_zlog_conf_fp = -1;
|
|
||||||
char tmp_conf_filepath[1024] = "";
|
|
||||||
|
|
||||||
#define MAX_HANDLE_LOG_PATH 4096
|
#define MAX_HANDLE_LOG_PATH 4096
|
||||||
#define GLOB_ZLOG_CONF "GLOB_ZLOG_CONF"
|
static int g_zlog_conf_fp = -1;
|
||||||
|
static char global_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
|
||||||
|
static char tmp_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
|
||||||
|
|
||||||
typedef struct log_handle_t
|
typedef struct log_handle_t
|
||||||
{
|
{
|
||||||
@@ -89,34 +89,6 @@ static int create_path(const char *path, int path_len)
|
|||||||
return 0;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int get_filepath(int fp, char *buf, int buflen)
|
static int get_filepath(int fp, char *buf, int buflen)
|
||||||
{
|
{
|
||||||
if(buf == NULL || buflen <=0)return -1;
|
if(buf == NULL || buflen <=0)return -1;
|
||||||
@@ -144,6 +116,35 @@ static void escape_for_zlog(char *in_buf, int buflen)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void snapshot_handle_info(const char *handle_name, int level)
|
||||||
|
{
|
||||||
|
char zlog_rule_conf_content[MAX_HANDLE_LOG_PATH + 1];
|
||||||
|
if (g_zlog_conf_fp == -1)
|
||||||
|
{
|
||||||
|
char temp_filename[1024] = "";
|
||||||
|
sprintf(temp_filename, "/tmp/MESA_handle_logger_%d.XXXXXX", getpid());
|
||||||
|
g_zlog_conf_fp = mkstemp(temp_filename);
|
||||||
|
if (g_zlog_conf_fp == -1)
|
||||||
|
{
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
if (get_filepath(g_zlog_conf_fp, tmp_conf_filepath, sizeof(tmp_conf_filepath)) < 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int len = snprintf(zlog_rule_conf_content, sizeof(zlog_rule_conf_content),
|
||||||
|
"[global]\ndefault format = \"%%d(%%c), %%V, %%m%%n\" \n[levels]\nDEBUG=10\nINFO=20\nFATAL=30\n[rules]");
|
||||||
|
write(g_zlog_conf_fp, zlog_rule_conf_content, strlen(zlog_rule_conf_content));
|
||||||
|
fsync(g_zlog_conf_fp);
|
||||||
|
}
|
||||||
|
snprintf(zlog_rule_conf_content, sizeof(zlog_rule_conf_content),
|
||||||
|
"\n%s.%d \"%s.%%d(%%F)\"",
|
||||||
|
handle_name, level, handle_name);
|
||||||
|
write(g_zlog_conf_fp, zlog_rule_conf_content, strlen(zlog_rule_conf_content));
|
||||||
|
fsync(g_zlog_conf_fp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
||||||
{
|
{
|
||||||
if(file_path == NULL)
|
if(file_path == NULL)
|
||||||
@@ -154,10 +155,8 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
|||||||
FILE *fp = NULL;
|
FILE *fp = NULL;
|
||||||
log_handle_t *p_handle = NULL;
|
log_handle_t *p_handle = NULL;
|
||||||
|
|
||||||
char zlog_rule_conf_content[MAX_HANDLE_LOG_PATH + 1];
|
|
||||||
char handle_name[MAX_HANDLE_LOG_PATH];
|
char handle_name[MAX_HANDLE_LOG_PATH];
|
||||||
char *p_path_end = rindex(file_path, '/');
|
char *p_path_end = rindex(file_path, '/');
|
||||||
char *pathvar = getenv(GLOB_ZLOG_CONF);
|
|
||||||
|
|
||||||
char *p_name = p_path_end+1;
|
char *p_name = p_path_end+1;
|
||||||
|
|
||||||
@@ -165,92 +164,21 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
|||||||
escape_for_zlog(handle_name, strlen(handle_name));
|
escape_for_zlog(handle_name, strlen(handle_name));
|
||||||
p_name = handle_name;
|
p_name = handle_name;
|
||||||
|
|
||||||
if (pathvar == NULL)
|
|
||||||
{
|
|
||||||
//creating file_path failed, return NULL
|
|
||||||
if (create_path(file_path, p_path_end - file_path) < 0)
|
|
||||||
return NULL;
|
|
||||||
if (g_zlog_conf_fp == -1)
|
|
||||||
{
|
|
||||||
|
|
||||||
//g_zlog_conf_fp = tmpfile();
|
//creating file_path failed, return NULL
|
||||||
char temp_filename[1024]="";
|
if (create_path(file_path, p_path_end - file_path) < 0)
|
||||||
sprintf(temp_filename, "/tmp/MESA_handle_logger_%d.XXXXXX", getpid());
|
return NULL;
|
||||||
g_zlog_conf_fp = mkstemp(temp_filename);
|
|
||||||
if (g_zlog_conf_fp == -1)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (get_filepath(g_zlog_conf_fp, tmp_conf_filepath, sizeof(tmp_conf_filepath)) < 0)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
snprintf(zlog_rule_conf_content, sizeof(zlog_rule_conf_content),
|
|
||||||
"[global]\ndefault format = \"%%d(%%c), %%V, %%m%%n\" \n[levels]\nDEBUG=10\nINFO=20\nFATAL=30\n[rules]\n%s.%d \"%s.%%d(%%F)\"",
|
|
||||||
p_name, level, file_path);
|
|
||||||
write(g_zlog_conf_fp, zlog_rule_conf_content, strlen(zlog_rule_conf_content));
|
|
||||||
fsync(g_zlog_conf_fp);
|
|
||||||
int rc = zlog_init(tmp_conf_filepath);
|
|
||||||
if(rc)
|
|
||||||
{
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
zc = zlog_get_category(p_name);
|
|
||||||
if (!zc)
|
|
||||||
{
|
|
||||||
printf("get zlog category %s in %s fail\n", file_path, tmp_conf_filepath);
|
|
||||||
error:
|
|
||||||
unlink(tmp_conf_filepath);
|
|
||||||
close(g_zlog_conf_fp);
|
|
||||||
g_zlog_conf_fp = -1;
|
|
||||||
memset(tmp_conf_filepath, 0, sizeof(tmp_conf_filepath));
|
|
||||||
zlog_fini();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
|
|
||||||
zc = zlog_get_category(p_name);
|
snapshot_handle_info(p_name, level);
|
||||||
if (!zc)
|
zc = zlog_get_category(p_name);
|
||||||
{
|
if (!zc)
|
||||||
printf("get zlog category %s in %s fail\n", file_path, tmp_conf_filepath);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//if (get_filepath(g_zlog_conf_fp, tmp_conf_filepath, sizeof(tmp_conf_filepath)) < 0)
|
|
||||||
// return NULL;
|
|
||||||
snprintf(zlog_rule_conf_content, sizeof(zlog_rule_conf_content),
|
|
||||||
"\n%s.%d \"%s.%%d(%%F)\"",
|
|
||||||
p_name, level, file_path);
|
|
||||||
write(g_zlog_conf_fp, zlog_rule_conf_content, strlen(zlog_rule_conf_content));
|
|
||||||
fsync(g_zlog_conf_fp);
|
|
||||||
int rc = zlog_reload(tmp_conf_filepath);
|
|
||||||
if (rc)return NULL;
|
|
||||||
zc = zlog_get_category(p_name);
|
|
||||||
if (!zc)
|
|
||||||
{
|
|
||||||
printf("get zlog category %s in %s fail\n", file_path, tmp_conf_filepath);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
zc = init_zlog(pathvar, p_name);
|
printf("get zlog category %s in %s fail\n", p_name, tmp_conf_filepath);
|
||||||
if (zc == NULL)
|
|
||||||
{
|
|
||||||
zlog_fini();
|
|
||||||
//return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1);
|
p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1);
|
||||||
strncpy(p_handle->runtime_log_file, file_path, sizeof(p_handle->runtime_log_file) - 1);
|
strncpy(p_handle->runtime_log_file, file_path, sizeof(p_handle->runtime_log_file) - 1);
|
||||||
p_handle->runtime_log_level = level;
|
p_handle->runtime_log_level = level;
|
||||||
p_handle->zc = zc;
|
p_handle->zc = zc;
|
||||||
p_handle->global_conf_path = pathvar;
|
|
||||||
return (void *)p_handle;
|
return (void *)p_handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,13 +186,6 @@ void MESA_destroy_runtime_log_handle(void *handle)
|
|||||||
{
|
{
|
||||||
if(handle != NULL)
|
if(handle != NULL)
|
||||||
{
|
{
|
||||||
log_handle_t *p_handle = (log_handle_t *)handle;
|
|
||||||
zlog_fini();
|
|
||||||
if(g_zlog_conf_fp != -1)
|
|
||||||
{
|
|
||||||
unlink(tmp_conf_filepath);
|
|
||||||
close(g_zlog_conf_fp);
|
|
||||||
}
|
|
||||||
free(handle);
|
free(handle);
|
||||||
handle = NULL;
|
handle = NULL;
|
||||||
}
|
}
|
||||||
@@ -272,6 +193,7 @@ void MESA_destroy_runtime_log_handle(void *handle)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
|
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -281,8 +203,6 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
|
|||||||
|
|
||||||
if(p_handle->zc == NULL)return;
|
if(p_handle->zc == NULL)return;
|
||||||
|
|
||||||
if(level < p_handle->runtime_log_level) return;
|
|
||||||
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
vzlog(p_handle->zc, __FILE__, sizeof(__FILE__) - 1, __func__, sizeof(__func__) - 1, __LINE__, level, fmt, ap);
|
vzlog(p_handle->zc, __FILE__, sizeof(__FILE__) - 1, __func__, sizeof(__func__) - 1, __LINE__, level, fmt, ap);
|
||||||
@@ -290,3 +210,79 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
|
|||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int MESA_handle_runtime_log_creation(const char *conf_path)
|
||||||
|
{
|
||||||
|
if(conf_path == NULL)
|
||||||
|
{
|
||||||
|
printf("MESA_handle_runtime_log_creationUsing NULL, will using ZLOG_CONF_PATH or strout as default profile\n");
|
||||||
|
return zlog_init(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (access(conf_path, R_OK) != -1)
|
||||||
|
{
|
||||||
|
|
||||||
|
int rc = zlog_init(conf_path);
|
||||||
|
if (rc)
|
||||||
|
{
|
||||||
|
printf("init zlog by %s failed\n", conf_path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
strcpy(global_conf_filepath, conf_path);
|
||||||
|
zlog_profile();
|
||||||
|
}
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int MESA_handle_runtime_log_reconstruction(const char *conf_path)
|
||||||
|
{
|
||||||
|
char zlog_rule_conf_content[MAX_HANDLE_LOG_PATH + 1];
|
||||||
|
time_t t;
|
||||||
|
struct tm local_time;
|
||||||
|
if (access(conf_path, R_OK) != -1)
|
||||||
|
{
|
||||||
|
int rc = zlog_reload(conf_path);
|
||||||
|
#ifdef DEBUG
|
||||||
|
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"};
|
||||||
|
if (!rc)
|
||||||
|
{
|
||||||
|
strcpy(global_conf_filepath, conf_path);
|
||||||
|
zlog_profile();
|
||||||
|
if (g_zlog_conf_fp != -1)
|
||||||
|
{
|
||||||
|
time(&t);
|
||||||
|
if (NULL != (localtime_r(&t, &local_time)))
|
||||||
|
{
|
||||||
|
snprintf(zlog_rule_conf_content, sizeof(zlog_rule_conf_content),
|
||||||
|
"\n%s %s %d %02d:%02d:%02d %d, RECONSTRUCTION to %s\n", 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, conf_path);
|
||||||
|
write(g_zlog_conf_fp, zlog_rule_conf_content, strlen(zlog_rule_conf_content));
|
||||||
|
fsync(g_zlog_conf_fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MESA_handle_runtime_log_destruction()
|
||||||
|
{
|
||||||
|
zlog_fini();
|
||||||
|
if (g_zlog_conf_fp != -1)
|
||||||
|
{
|
||||||
|
unlink(tmp_conf_filepath);
|
||||||
|
close(g_zlog_conf_fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user