使用mkstemp生成临时conf文件
This commit is contained in:
@@ -1,11 +1,15 @@
|
|||||||
#include "MESA_handle_logger.h"
|
#include "MESA_handle_logger.h"
|
||||||
#include "zlog.h"
|
#include "zlog.h"
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.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"
|
#define GLOB_ZLOG_CONF "GLOB_ZLOG_CONF"
|
||||||
|
|
||||||
@@ -113,61 +117,131 @@ static zlog_category_t *init_zlog(const char *conf_path, const char *category)
|
|||||||
return zc;
|
return zc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
int rc;
|
int rc = -1;
|
||||||
zlog_category_t *zc;
|
zlog_category_t *zc = NULL;
|
||||||
FILE *fp = NULL;
|
FILE *fp = NULL;
|
||||||
log_handle_t *p_handle = NULL;
|
log_handle_t *p_handle = NULL;
|
||||||
|
|
||||||
const char *p_conf_path = NULL;
|
char zlog_rule_conf_content[MAX_HANDLE_LOG_PATH + 1];
|
||||||
const char *p_name = NULL;
|
char handle_name[MAX_HANDLE_LOG_PATH];
|
||||||
char *p_path_end = rindex(file_path, '/');
|
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);
|
char *pathvar = getenv(GLOB_ZLOG_CONF);
|
||||||
|
|
||||||
|
char *p_name = p_path_end+1;
|
||||||
|
|
||||||
|
strcpy(handle_name, file_path);
|
||||||
|
escape_for_zlog(handle_name, strlen(handle_name));
|
||||||
|
p_name = handle_name;
|
||||||
|
|
||||||
if (pathvar == NULL)
|
if (pathvar == NULL)
|
||||||
{
|
{
|
||||||
//creating file_path failed, return NULL
|
//creating file_path failed, return NULL
|
||||||
if (path_len > 0 && create_path(file_path, path_len) < 0)return NULL;
|
if (create_path(file_path, p_path_end - file_path) < 0)
|
||||||
char default_zlog_conf_path[MAX_HANDLE_LOG_PATH + 5];
|
return NULL;
|
||||||
snprintf(default_zlog_conf_path, sizeof(default_zlog_conf_path), "%s.conf", file_path);
|
if (g_zlog_conf_fp == -1)
|
||||||
if (access(default_zlog_conf_path, R_OK) != 0)
|
|
||||||
{
|
{
|
||||||
fp = fopen(default_zlog_conf_path, "w");
|
|
||||||
if(fp == NULL)return NULL;
|
//g_zlog_conf_fp = tmpfile();
|
||||||
char zlog_rule_conf_content[MAX_HANDLE_LOG_PATH + 1];
|
char temp_filename[1024]="";
|
||||||
snprintf(zlog_rule_conf_content, sizeof(zlog_rule_conf_content),
|
sprintf(temp_filename, "/tmp/MESA_handle_logger_%d.XXXXXX", getpid());
|
||||||
"[global]\ndefault format = \"%%d(%%c), %%V, %%m%%n\" \n[levels]\nDEBUG=10\nINFO=20\nFATAL=30\n[rules]\n %s.* \"%s.%%d(%%F)\"",
|
g_zlog_conf_fp = mkstemp(temp_filename);
|
||||||
p_name, file_path);
|
if (g_zlog_conf_fp == -1)
|
||||||
fwrite(zlog_rule_conf_content, strlen(zlog_rule_conf_content), 1, fp);
|
{
|
||||||
fclose(fp);
|
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.* \"%s.%%d(%%F)\"",
|
||||||
|
p_name, 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);
|
||||||
|
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.* \"%s.%%d(%%F)\"",
|
||||||
|
p_name, 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
p_conf_path = default_zlog_conf_path;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (access(pathvar, R_OK) != 0)
|
zc = init_zlog(pathvar, p_name);
|
||||||
{
|
if (zc == NULL)
|
||||||
return NULL;
|
goto error;
|
||||||
}
|
|
||||||
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);
|
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;
|
||||||
@@ -182,6 +256,11 @@ void MESA_destroy_runtime_log_handle(void *handle)
|
|||||||
{
|
{
|
||||||
log_handle_t *p_handle = (log_handle_t *)handle;
|
log_handle_t *p_handle = (log_handle_t *)handle;
|
||||||
zlog_fini();
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user