#include "MESA_handle_logger.h" #include "zlog.h" #include #include #include #include #define MAX_HANDLE_LOG_PATH 4096 #define GLOB_ZLOG_CONF "GLOB_ZLOG_CONF" typedef struct log_handle_t { int runtime_log_level; zlog_category_t *zc; const char *global_conf_path; char runtime_log_file[MAX_HANDLE_LOG_PATH]; } log_handle_t; //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 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; } 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; 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); 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]\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); 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); strncpy(p_handle->runtime_log_file, file_path, sizeof(p_handle->runtime_log_file) - 1); p_handle->runtime_log_level = level; p_handle->zc = zc; p_handle->global_conf_path = pathvar; return (void *)p_handle; } void MESA_destroy_runtime_log_handle(void *handle) { if(handle != NULL) { log_handle_t *p_handle = (log_handle_t *)handle; zlog_fini(); free(handle); handle = NULL; } return; } void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...) { 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; va_list ap; va_start(ap, fmt); vzlog(p_handle->zc, __FILE__, sizeof(__FILE__) - 1, __func__, sizeof(__func__) - 1, __LINE__, level, fmt, ap); va_end(ap); return ; }