2018-11-15 17:10:05 +08:00
|
|
|
#include "MESA_handle_logger.h"
|
2019-08-31 23:35:44 +08:00
|
|
|
#include "zlog.h"
|
2018-11-15 17:10:05 +08:00
|
|
|
|
2019-09-04 20:04:43 +08:00
|
|
|
#include <fcntl.h>
|
2018-11-15 17:10:05 +08:00
|
|
|
#include <stdarg.h>
|
2019-08-31 23:35:44 +08:00
|
|
|
#include <stdlib.h>
|
2018-11-15 17:10:05 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2019-09-04 20:04:43 +08:00
|
|
|
static int g_zlog_conf_fp = -1;
|
|
|
|
|
char tmp_conf_filepath[1024] = "";
|
|
|
|
|
|
2019-08-31 23:35:44 +08:00
|
|
|
#define MAX_HANDLE_LOG_PATH 4096
|
|
|
|
|
#define GLOB_ZLOG_CONF "GLOB_ZLOG_CONF"
|
|
|
|
|
|
2018-11-15 17:10:05 +08:00
|
|
|
typedef struct log_handle_t
|
|
|
|
|
{
|
|
|
|
|
int runtime_log_level;
|
2019-08-31 23:35:44 +08:00
|
|
|
zlog_category_t *zc;
|
|
|
|
|
const char *global_conf_path;
|
|
|
|
|
char runtime_log_file[MAX_HANDLE_LOG_PATH];
|
2018-11-15 17:10:05 +08:00
|
|
|
} log_handle_t;
|
|
|
|
|
|
2019-07-15 11:13:28 +08:00
|
|
|
|
2018-11-15 17:10:05 +08:00
|
|
|
|
2019-02-24 13:44:18 +06:00
|
|
|
//const int HANDLE_LOGGER_VERSION_20190218 = 1;
|
|
|
|
|
|
|
|
|
|
#define GIT_VERSION_CATTER(v) __attribute__((__used__)) const char * GIT_VERSION_##v = NULL
|
|
|
|
|
#define GIT_VERSION_EXPEND(v) GIT_VERSION_CATTER(v)
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C"
|
|
|
|
|
{
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* VERSION TAG */
|
|
|
|
|
#ifdef GIT_VERSION
|
|
|
|
|
GIT_VERSION_EXPEND(GIT_VERSION);
|
|
|
|
|
#else
|
|
|
|
|
static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL;
|
|
|
|
|
#endif
|
|
|
|
|
#undef GIT_VERSION_CATTER
|
|
|
|
|
#undef GIT_VERSION_EXPEND
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-12-05 21:04:42 +06:00
|
|
|
|
2018-11-15 17:10:05 +08:00
|
|
|
static int create_dir(const char *dir_path, int path_len)
|
|
|
|
|
{
|
|
|
|
|
if(dir_path == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
char *buf = (char *)calloc(path_len+1, 1);
|
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
|
|
memcpy(buf, dir_path, path_len);
|
|
|
|
|
if(access(buf, R_OK) != 0)
|
|
|
|
|
{
|
|
|
|
|
if(mkdir(buf, 0755)!= 0)
|
|
|
|
|
ret = -1;
|
|
|
|
|
else
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
ret = 1;
|
|
|
|
|
free(buf);
|
|
|
|
|
buf = NULL;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int create_path(const char *path, int path_len)
|
|
|
|
|
{
|
|
|
|
|
const char *p_cur = path;
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
if(create_dir(path, path_len) >= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for(;i<=path_len;i++,p_cur++)
|
|
|
|
|
{
|
|
|
|
|
if(*p_cur == '/')
|
|
|
|
|
{
|
|
|
|
|
if(create_dir(path, i+1) < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 23:35:44 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 20:04:43 +08:00
|
|
|
static int get_filepath(int fp, char *buf, int buflen)
|
|
|
|
|
{
|
|
|
|
|
if(buf == NULL || buflen <=0)return -1;
|
|
|
|
|
char proclnk[0xFFF] = "";
|
|
|
|
|
//int fno = fileno(fp);
|
|
|
|
|
int fno = fp;
|
|
|
|
|
sprintf(proclnk, "/proc/self/fd/%d", fno);
|
|
|
|
|
int r = readlink(proclnk, buf, buflen);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char tmp_template[] = "/tmp/handle_loggger.XXXXXX";
|
|
|
|
|
|
|
|
|
|
static void escape_for_zlog(char *in_buf, int buflen)
|
|
|
|
|
{
|
|
|
|
|
if(in_buf == NULL || buflen <= 0)return;
|
|
|
|
|
int i = 0;
|
|
|
|
|
for(i = 0; i < buflen; i++)
|
|
|
|
|
{
|
|
|
|
|
if ((!isalnum(in_buf[i])) && (in_buf[i] != '_') && (in_buf[i] != '-') && (in_buf[i] != '*') && (in_buf[i] != '!'))
|
|
|
|
|
{
|
|
|
|
|
in_buf[i] = '_';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 17:10:05 +08:00
|
|
|
void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
|
|
|
|
{
|
|
|
|
|
if(file_path == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2019-09-04 20:04:43 +08:00
|
|
|
int rc = -1;
|
|
|
|
|
zlog_category_t *zc = NULL;
|
2018-11-15 17:10:05 +08:00
|
|
|
FILE *fp = NULL;
|
|
|
|
|
log_handle_t *p_handle = NULL;
|
|
|
|
|
|
2019-09-04 20:04:43 +08:00
|
|
|
char zlog_rule_conf_content[MAX_HANDLE_LOG_PATH + 1];
|
|
|
|
|
char handle_name[MAX_HANDLE_LOG_PATH];
|
2019-08-31 23:35:44 +08:00
|
|
|
char *p_path_end = rindex(file_path, '/');
|
|
|
|
|
char *pathvar = getenv(GLOB_ZLOG_CONF);
|
2018-11-15 17:10:05 +08:00
|
|
|
|
2019-09-04 20:04:43 +08:00
|
|
|
char *p_name = p_path_end+1;
|
|
|
|
|
|
|
|
|
|
strcpy(handle_name, file_path);
|
|
|
|
|
escape_for_zlog(handle_name, strlen(handle_name));
|
|
|
|
|
p_name = handle_name;
|
|
|
|
|
|
2019-08-31 23:35:44 +08:00
|
|
|
if (pathvar == NULL)
|
|
|
|
|
{
|
|
|
|
|
//creating file_path failed, return NULL
|
2019-09-04 20:04:43 +08:00
|
|
|
if (create_path(file_path, p_path_end - file_path) < 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
if (g_zlog_conf_fp == -1)
|
2019-08-31 23:35:44 +08:00
|
|
|
{
|
2019-09-04 20:04:43 +08:00
|
|
|
|
|
|
|
|
//g_zlog_conf_fp = tmpfile();
|
|
|
|
|
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 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),
|
2020-09-03 11:00:17 +08:00
|
|
|
"[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);
|
2019-09-04 20:04:43 +08:00
|
|
|
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
|
|
|
|
|
{
|
2020-09-03 11:00:17 +08:00
|
|
|
|
2019-09-04 20:04:43 +08:00
|
|
|
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),
|
2020-09-03 11:00:17 +08:00
|
|
|
"\n%s.%d \"%s.%%d(%%F)\"",
|
|
|
|
|
p_name, level, file_path);
|
2019-09-04 20:04:43 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-31 23:35:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-09-04 20:04:43 +08:00
|
|
|
zc = init_zlog(pathvar, p_name);
|
|
|
|
|
if (zc == NULL)
|
2020-09-03 11:00:17 +08:00
|
|
|
{
|
|
|
|
|
zlog_fini();
|
|
|
|
|
//return NULL;
|
|
|
|
|
}
|
2019-08-31 23:35:44 +08:00
|
|
|
}
|
2018-11-15 17:10:05 +08:00
|
|
|
p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1);
|
2019-08-31 23:35:44 +08:00
|
|
|
strncpy(p_handle->runtime_log_file, file_path, sizeof(p_handle->runtime_log_file) - 1);
|
2018-11-15 17:10:05 +08:00
|
|
|
p_handle->runtime_log_level = level;
|
2019-08-31 23:35:44 +08:00
|
|
|
p_handle->zc = zc;
|
|
|
|
|
p_handle->global_conf_path = pathvar;
|
2018-11-15 17:10:05 +08:00
|
|
|
return (void *)p_handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MESA_destroy_runtime_log_handle(void *handle)
|
|
|
|
|
{
|
|
|
|
|
if(handle != NULL)
|
|
|
|
|
{
|
2019-02-18 17:50:44 +08:00
|
|
|
log_handle_t *p_handle = (log_handle_t *)handle;
|
2019-09-04 11:47:33 +08:00
|
|
|
zlog_fini();
|
2019-09-04 20:04:43 +08:00
|
|
|
if(g_zlog_conf_fp != -1)
|
|
|
|
|
{
|
|
|
|
|
unlink(tmp_conf_filepath);
|
|
|
|
|
close(g_zlog_conf_fp);
|
|
|
|
|
}
|
2019-08-31 23:35:44 +08:00
|
|
|
free(handle);
|
2018-11-15 17:10:05 +08:00
|
|
|
handle = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
|
|
|
|
|
{
|
2019-08-31 23:35:44 +08:00
|
|
|
|
2018-11-15 17:10:05 +08:00
|
|
|
log_handle_t *p_handle = (log_handle_t *)handle;
|
|
|
|
|
|
|
|
|
|
if(p_handle == NULL || p_handle->runtime_log_file == NULL)return;
|
|
|
|
|
|
2020-09-03 11:00:17 +08:00
|
|
|
if(p_handle->zc == NULL)return;
|
|
|
|
|
|
2018-11-15 17:10:05 +08:00
|
|
|
if(level < p_handle->runtime_log_level) return;
|
2019-08-31 23:35:44 +08:00
|
|
|
|
|
|
|
|
va_list ap;
|
2018-11-15 17:10:05 +08:00
|
|
|
va_start(ap, fmt);
|
2019-08-31 23:35:44 +08:00
|
|
|
vzlog(p_handle->zc, __FILE__, sizeof(__FILE__) - 1, __func__, sizeof(__func__) - 1, __LINE__, level, fmt, ap);
|
2018-11-15 17:10:05 +08:00
|
|
|
va_end(ap);
|
2019-08-31 23:35:44 +08:00
|
|
|
return ;
|
2018-11-15 17:10:05 +08:00
|
|
|
}
|
|
|
|
|
|