UPDATE:logger2行为模式更新:

1、对于logger1的行为,默认使用snapshot文件初始化
2、对于logger2传入null或者无法访问或者错误格式的初始化profile,尝试使用null初始化,默认行为为stdout
This commit is contained in:
yangwei
2020-09-21 20:42:25 +08:00
parent e39e641103
commit 186571e823
2 changed files with 75 additions and 25 deletions

View File

@@ -9,6 +9,7 @@
#include <time.h>
#define MAX_HANDLE_LOG_PATH 4096
static int g_zlog_inited = 0;
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] = "";
@@ -128,7 +129,7 @@ static const char *loglevel_to_name(int level)
static void snapshot_handle_info(const char *handle_name, const char *log_path, int level)
{
char zlog_rule_conf_content[MAX_HANDLE_LOG_PATH + 1] = "";
static char *zlog_conf_init_buff = "[global]\ndefault format = \"%%d(%%c), %%V, %F, %U, %%m%%n\" \n[levels]\nDEBUG=10\nINFO=20\nFATAL=30\n[rules]";
static char *zlog_conf_init_buff = "[global]\ndefault format = \"%d(%c), %V, %F, %U, %m%n\" \n[levels]\nDEBUG=10\nINFO=20\nFATAL=30\n[rules]";
if (g_zlog_conf_fp == -1)
{
char temp_filename[1024] = "";
@@ -150,6 +151,13 @@ static void snapshot_handle_info(const char *handle_name, const char *log_path,
handle_name, loglevel_to_name(level), log_path);
write(g_zlog_conf_fp, zlog_rule_conf_content, strlen(zlog_rule_conf_content));
fsync(g_zlog_conf_fp);
if(g_zlog_inited == 0)
{
printf("[snapshot_handle_info], Nobody call MESA_handle_runtime_log_creation before, Using (%s) to init zlog!!!\n", tmp_conf_filepath);
zlog_init(tmp_conf_filepath);
zlog_reload(NULL);
}
return;
}
@@ -182,7 +190,7 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
zc = zlog_get_category(p_name);
if (!zc)
{
printf("MESA_create_runtime_log_handle, get zlog category %s in global_conf_filepath[%s] fail\n", p_name, global_conf_filepath);
printf("[MESA_create_runtime_log_handle], get zlog category (%s) in global_conf_filepath(%s) fail\n", p_name, global_conf_filepath);
}
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);
@@ -222,23 +230,56 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
int MESA_handle_runtime_log_creation(const char *conf_path)
{
if(conf_path == NULL)
char *env = getenv("ZLOG_CONF_PATH");
int rc = 0;
if (conf_path == NULL || (access(conf_path, R_OK) != 0))
{
printf("MESA_handle_runtime_log_creation USING [NULL], will using ZLOG_CONF_PATH or STDOUT as default profile\n");
return zlog_init(NULL);
}
int rc = zlog_init(conf_path);
if (rc)
{
printf("MESA_handle_runtime_log_creation init zlog by %s failed \n", conf_path);
return -1;
printf("[MESA_handle_runtime_log_creation], PATH (%s) unable to access, will output log to STDOUT or Using ZLOG_CONF_PATH (%s) !\n", conf_path, env);
rc = zlog_init(NULL);
if (rc)
{
printf("[MESA_handle_runtime_log_creation], INIT zlog by (NULL) or (%s) failed !!! \n", env);
return -1;
}
else
{
if (env != NULL)
{
strcpy(global_conf_filepath, env);
}
}
}
else
{
strcpy(global_conf_filepath, conf_path);
zlog_profile();
rc = zlog_init(conf_path);
if (rc)
{
printf("[MESA_handle_runtime_log_creation], INIT zlog by (%s) failed , using [NULL] retry !\n", conf_path);
rc = zlog_init(NULL);
if (rc)
{
printf("[MESA_handle_runtime_log_creation], INIT zlog by (NULL) or (%s) failed \n", env);
return -1;
}
else
{
if(env != NULL)
{
strcpy(global_conf_filepath, env);
}
}
}
else
{
strcpy(global_conf_filepath, conf_path);
}
}
printf("[MESA_handle_runtime_log_creation], INIT zlog finish, Using (%s). \n", global_conf_filepath);
g_zlog_inited = 1;
zlog_profile();
return rc;
}