37 Commits

Author SHA1 Message Date
yangwei
0f0e55c877 🔧build(CMakeLists.txt):
放弃logger2,重新使用logger作为包名称
2020-09-21 21:21:15 +08:00
yangwei
8857f282b9 🌈style(snapshot_handle_info):
兼容logger1行为时,屏显输出handle_name,log_path和level
2020-09-21 21:16:25 +08:00
yangwei
cc0912eed0 🐎ci(.gitlab-ci.yml): 增加testing编译devel包 2020-09-21 21:03:02 +08:00
yangwei
186571e823 UPDATE:logger2行为模式更新:
1、对于logger1的行为,默认使用snapshot文件初始化
2、对于logger2传入null或者无法访问或者错误格式的初始化profile,尝试使用null初始化,默认行为为stdout
2020-09-21 20:42:25 +08:00
yangwei
e39e641103 UPDATE:demo使用动态链接 2020-09-21 20:40:29 +08:00
yangwei
f461cbbc3d UPDATE:versio_map增加导出GIT_VERSION_*符号 2020-09-21 20:39:46 +08:00
yangwei
05a8eded4b UPDATE:zlog代码更新,修复zlog_init传入NULL时导致segfault的bug 2020-09-21 20:39:16 +08:00
yangwei
92815d2d1f 更新ci.yml,使用epel阿里云镜像repo 2020-09-18 15:54:41 +08:00
yangwei
ea4185727b 更新Version.cmake,正确设置GIT_VERSION是否定义的开关 2020-09-17 17:02:14 +08:00
yangwei
7066b2b086 更新写入snapshot默认部分的定义为const static buff 2020-09-17 13:34:02 +08:00
yangwei
52db3f0001 更新snapshot记录的内容,level与rules中名称一致 2020-09-17 13:19:10 +08:00
yangwei
7417fed852 ci中tyum makecache增加fast参数 2020-09-14 17:08:25 +08:00
yangwei
0cb2236be0 cmake IMPORT动态库安装未解决,改回使用静态连接zlog 2020-09-14 16:45:54 +08:00
yangwei
332201d4b0 install zlog.so.1.2 2020-09-11 18:34:18 +08:00
yangwei
ce403b8ca5 cmake不支持IMPORTED target作为install TARGETS,无法创建zlog soversion连接,更新zlog打包流程,安装和卸载前后使用postinstall和postuninstall执行ldconfig 2020-09-11 11:22:17 +08:00
yangwei
f15a1fd617 zlog使用动态链接 2020-09-10 15:38:49 +08:00
yangwei
d42c5eeb0f 更新CI规则,包含testing的tag上传至testing目录 2020-09-10 14:55:44 +08:00
yangwei
49b61b11a9 更新cmake安装zlog流程,指定include为zlog/src 2020-09-10 09:46:51 +08:00
yangwei
bc575233b7 更新zlog编译和安装过程,install相关工具bin目录 2020-09-09 17:41:05 +08:00
yangwei
b4f239ae9c UPDATE: snapshot中rules路径使用原始输入的file_path;日志中__FILE__字段使用创建handle时输入的file_path 2020-09-08 18:20:14 +08:00
yangwei
90fb91cd42 ci增加对rel和dev分支的artifact 2020-09-08 17:02:47 +08:00
yangwei
9c410a1e7e UPDATE:snapshot中记录的format默认增加%U 2020-09-08 17:01:16 +08:00
yangwei
8dee487439 UPDATE:补充module输入至日志文件,同时默认输出格式模板增加改module输出 2020-09-08 17:00:25 +08:00
yangwei
2e4c2d3098 更新reconstruction流程,对conf_path的判断交由zlog内部完成 2020-09-07 19:49:21 +08:00
yangwei
40512a0421 头文件保持MESA_handle_logger.h 2020-09-07 18:30:57 +08:00
liuxueli
ab7f4edda1 修复rpm打包失败的问题
Conflicts:
	CMakeLists.txt
2020-09-07 18:29:17 +08:00
yangwei
bafd63face 为避免误操作,更新名称为libMESA_handle_logger2,ABI向前兼容,install后创建软连接至libMESA_handle_logger.so 2020-09-07 16:53:11 +08:00
yangwei
e6e1f8494e UPDATE:增加soversion 2020-09-07 14:15:48 +08:00
yangwei
19911a962c 更新zlog目录,改为tar包使用ExternalProject编译解压 2020-09-07 12:54:42 +08:00
yangwei
c21cdcb3f0 更新test_handle_log,适配更新zlog后的三个函数 2020-09-06 18:46:14 +08:00
yangwei
745f908d70 适配zlog使用模式,增加creation,reconstruction,destruction三个函数,用于初始化,重载和销毁zlog运行环境 2020-09-06 18:40:20 +08:00
yangwei
23609f6f02 demo目录增加配置文件示例 2020-09-06 18:39:14 +08:00
yangwei
6ae5d5ffa9 1、Update 默认rules后面带level,而不是*
2、带pathvar初始化失败后,直接结束zlog,而不是跳转至error避免操作不存在的conf_file
3、创建句柄中的zlog_get_category失败后,返回句柄中zc为空,之后写入日志操作中判断zc,如果为null,直接返回,用于适配无rules即等效于关闭日志的场景
2020-09-03 11:00:17 +08:00
yangwei
1f9fc66152 更新cmakelist中zlog头文件目录 2019-09-16 09:59:49 +08:00
“yangwei”
635bb180bf 使用mkstemp生成临时conf文件 2019-09-04 20:04:43 +08:00
“yangwei”
117720197e 1、调整自动生成的zlog.conf格式
2、更新gitignore内容,增加手动build目录
2019-09-04 11:47:33 +08:00
yangwei
49822347cb 后台改为zlog实现 2019-08-31 23:35:44 +08:00
14 changed files with 420 additions and 192 deletions

1
.gitignore vendored
View File

@@ -12,6 +12,7 @@ core.*
version.txt
demo/test_handle_logger
cmake-build-debug
build-*/
GPATH
GRTAGS
GTAGS

View File

@@ -14,7 +14,8 @@ stages:
- cd $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH
- chmod +x ./ci/travis.sh
script:
- yum makecache
- wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
- yum makecache fast
- ./ci/travis.sh
- cd build
tags:
@@ -26,7 +27,8 @@ branch_build_debug:
variables:
BUILD_TYPE: Debug
except:
- /^develop.*$/i
- /^dev*.*$/i
- /^rel*.*$/i
- /^master.*$/i
- tags
@@ -36,7 +38,8 @@ branch_build_release:
BUILD_TYPE: RelWithDebInfo
extends: .build_by_travis
except:
- /^develop.*$/i
- /^dev*.*$/i
- /^rel*.*$/i
- /^master.*$/i
- tags
@@ -51,7 +54,8 @@ develop_build_debug:
paths:
- build/*.rpm
only:
- /^develop.*$/i
- /^dev*.*$/i
- /^rel*.*$/i
- /^master.*$/i
develop_build_release:
@@ -65,7 +69,8 @@ develop_build_release:
paths:
- build/*.rpm
only:
- /^develop.*$/i
- /^dev*.*$/i
- /^rel*.*$/i
- /^master.*$/i
@@ -84,3 +89,57 @@ release_build_release:
- build/*.rpm
only:
- tags
except:
- /^v[0-9].*-testing$/i
release_build_release_devel:
stage: build
variables:
BUILD_TYPE: RelWithDebInfo
ENABLE_DEVEL_SWITCH: "ON"
PACKAGE: 1
UPLOAD: 1
PULP3_REPO_NAME: framework-stable-x86_64.el7
PULP3_DIST_NAME: framework-stable-x86_64.el7
extends: .build_by_travis
artifacts:
name: "MESA_handle_logger-$CI_COMMIT_REF_NAME-release"
paths:
- build/*.rpm
only:
- tags
except:
- /^v[0-9].*-testing$/i
testing_build_release:
stage: build
variables:
BUILD_TYPE: RelWithDebInfo
PACKAGE: 1
UPLOAD: 1
PULP3_REPO_NAME: framework-testing-x86_64.el7
PULP3_DIST_NAME: framework-testing-x86_64.el7
extends: .build_by_travis
artifacts:
name: "MESA_handle_logger-$CI_COMMIT_REF_NAME-release"
paths:
- build/*.rpm
only:
- /^v[0-9].*-testing$/i
testing_build_release_devel:
stage: build
variables:
BUILD_TYPE: RelWithDebInfo
ENABLE_DEVEL_SWITCH: "ON"
PACKAGE: 1
UPLOAD: 1
PULP3_REPO_NAME: framework-testing-x86_64.el7
PULP3_DIST_NAME: framework-testing-x86_64.el7
extends: .build_by_travis
artifacts:
name: "MESA_handle_logger-$CI_COMMIT_REF_NAME-release"
paths:
- build/*.rpm
only:
- /^v[0-9].*-testing$/i

View File

@@ -4,27 +4,28 @@ set(lib_name MESA_handle_logger)
project (${lib_name})
set(LIB_MAJOR_VERSION 2)
set(LIB_MINOR_VERSION 0)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include(Version)
#SET(CMAKE_C_COMPILER "/usr/bin/g++")
set(CMAKE_MACOSX_RPATH 0)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
set(CMAKE_INSTALL_PREFIX /opt/MESA)
#for ASAN
set(ASAN_OPTION "OFF" CACHE STRING " set asan type chosen by the user, using OFF as default")
set_property(CACHE ASAN_OPTION PROPERTY STRINGS OFF ADDRESS THREAD)
message(STATUS "ASAN_OPTION='${ASAN_OPTION}'")
option(ENABLE_SANITIZE_ADDRESS "Enable AddressSanitizer" FALSE)
option(ENABLE_SANITIZE_THREAD "Enable ThreadSanitizer" FALSE)
if(ASAN_OPTION MATCHES "ADDRESS")
set(CMAKE_C_FLAGS "${CMAKADDRESS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=address -fno-omit-frame-pointer")
if(ENABLE_SANITIZE_ADDRESS)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
elseif(ASAN_OPTION MATCHES "THREAD")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=thread -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=thread -fno-omit-frame-pointer")
elseif(ENABLE_SANITIZE_THREAD)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
endif()
@@ -32,36 +33,38 @@ endif()
include_directories(${PROJECT_SOURCE_DIR}/inc/)
file(GLOB SRC
"src/*.c"
"src/*.cpp"
)
add_subdirectory(zlog)
# Shared Library Output
add_library(${lib_name}_shared SHARED ${SRC})
add_library(${lib_name}_shared SHARED src/MESA_handle_logger.c)
target_link_libraries(${lib_name}_shared zlog_static)
set_target_properties(${lib_name}_shared PROPERTIES LINK_FLAGS
"-Wl,--version-script=${PROJECT_SOURCE_DIR}/src/version.map")
if(DEFINED MESA_SHARED_INSTALL_DIR)
set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name} LIBRARY_OUTPUT_DIRECTORY ${MESA_SHARED_INSTALL_DIR})
else()
set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name})
endif()
set_target_properties(${lib_name}_shared PROPERTIES VERSION ${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION})
set_target_properties(${lib_name}_shared PROPERTIES SOVERSION ${LIB_MAJOR_VERSION})
# static Library Output
add_library(${lib_name}_static STATIC ${SRC})
add_library(${lib_name}_static STATIC src/MESA_handle_logger.c)
target_link_libraries(${lib_name}_static zlog_static)
set_target_properties(${lib_name}_static PROPERTIES OUTPUT_NAME ${lib_name})
set(CMAKE_INSTALL_PREFIX /opt/MESA)
install(FILES inc/MESA_handle_logger.h DESTINATION
${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT devel)
install(TARGETS ${lib_name}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARIES)
install(FILES inc/${lib_name}.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT HEADER)
install(FILES inc/MESA_handle_logger.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT HEADER)
file(GLOB DEMO
"demo/*.c"
"demo/*.cpp"
)
add_executable(${lib_name}_demo ${DEMO})
target_link_libraries(${lib_name}_demo rt pthread ${lib_name}_static)
add_executable(${lib_name}_demo demo/test_handle_logger.c)
target_link_libraries(${lib_name}_demo rt pthread ${lib_name}_shared)
include(Package)

12
README
View File

@@ -4,13 +4,19 @@
*Author:yangwei@iie.ac.cn
*History:
2014-03-24 created by yw
1)first version
1)第一个动态库版本
2014-04-16 modified by yw
1)strict inspection parameters, compatible with g++ 4.4.4
1) 支持g++ 4.4.4编译
2014-05-29 update by yw
1)create directory is supported
1)支持创建用户指定的日志目录
2019-2-18 update by yw
1)性能优化句柄中存储文件fp定期flush至文件
2020-09-07 update to libMESA_handle_logger2 by yw
1)内部使用zlog实现行为变更更新库为libMESA_handle_logger2
-------------------------------------------------------------------------------------
NOTICE:
1)compatible with g++, extern with c;

View File

@@ -34,6 +34,7 @@ set(CPACK_RPM_LIBRARIES_PACKAGE_NAME ${MY_RPM_NAME_PREFIX})
set(CPACK_RPM_FILE_NAME "${CPACK_RPM_LIBRARIES_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${SYSTEM_VERSION}.rpm")
set(CPACK_RPM_LIBRARIES_DEBUGINFO_FILE_NAME "${CPACK_RPM_LIBRARIES_PACKAGE_NAME}-debuginfo-${CPACK_PACKAGE_VERSION}-${SYSTEM_VERSION}.rpm")
set(CPACK_COMPONENT_LIBRARIES_GROUP "libraries")
set(CPACK_COMPONENT_EXECUTABLE_GROUP "libraries")
set(CPACK_RPM_HEADER_FILE_NAME "${CPACK_RPM_HEADER_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${SYSTEM_VERSION}.rpm")
set(CPACK_RPM_HEADER_DEBUGINFO_FILE_NAME "${CPACK_RPM_HEADER_PACKAGE_NAME}-debuginfo-${CPACK_PACKAGE_VERSION}-${SYSTEM_VERSION}.rpm")
@@ -42,8 +43,10 @@ set(CPACK_COMPONENT_HEADER_GROUP "header")
set(CPACK_RPM_HEADER_PACKAGE_REQUIRES_PRE ${CPACK_RPM_LIBRARIES_PACKAGE_NAME})
set(CPACK_RPM_HEADER_PACKAGE_CONFLICTS ${CPACK_RPM_HEADER_PACKAGE_NAME})
set(CPACK_COMPONENTS_ALL LIBRARIES HEADER)
set(CPACK_COMPONENTS_ALL LIBRARIES HEADER EXECUTABLE)
set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/cmake/PostInstall.in)
set(CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE ${CMAKE_SOURCE_DIR}/cmake/PostInstall.in)
set(CPACK_BUILD_SOURCE_DIRS "${CMAKE_SOURCE_DIR}")

2
cmake/PostInstall.in Normal file
View File

@@ -0,0 +1,2 @@
#!/bin/sh
ldconfig

View File

@@ -38,16 +38,12 @@ set(VERSION_BUILD "${VCS_SHORT_HASH}")
# print information
message(STATUS "Version: ${VERSION}-${VERSION_BUILD}")
if(NOT DEFINE_GIT_VERSION)
option(DEFINE_GIT_VERSION "Set DEFINE_GIT_VERSION to OFF" OFF)
option(DEFINE_GIT_VERSION "Set DEFINE_GIT_VERSION to TRUE or FALSE" TRUE)
if(DEFINE_GIT_VERSION)
set(GIT_VERSION
"${VERSION}-${CMAKE_BUILD_TYPE}-${VERSION_BUILD}-${VCS_BRANCH}-${VCS_TAG}-${VCS_DATE}")
string(REGEX REPLACE "[-:+/\\.]" "_" GIT_VERSION ${GIT_VERSION})
if(DEFINE_GIT_VERSION)
add_definitions(-DGIT_VERSION=${GIT_VERSION})
option(DEFINE_GIT_VERSION "Set DEFINE_GIT_VERSION to OFF" ON)
endif()
endif()

13
demo/demo_zlog.conf Normal file
View File

@@ -0,0 +1,13 @@
[global]
default format = "%d(%c), %V, %U, %m%n"
[levels]
DEBUG=10
INFO=20
FATAL=30
[formats]
other = "%d(%c), %V, %F, %U, %m%n"
[rules]
*.=debug "./log/runtimelog_debug.%d(%F)"
*.=info "./log/runtimelog_info.%d(%F)"
*.=fatal "./log/runtimelog_fatal.%d(%F)"
!.* "./log/runtimelog_other.%d(%F)"; other

View File

@@ -1,9 +1,10 @@
#include "MESA_handle_logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void *sample_handle = NULL;
@@ -12,8 +13,10 @@ void *test_handle = NULL;
#define LOG_NUM 100000
#define MAX_THREAD_NUM 65535
int g_mode = 0;
int g_log_num = 0;
int g_thread_num = 0;
const char *g_zlog_conf = NULL;
volatile long g_start_time = 0;
volatile long g_end_time = 0;
@@ -37,11 +40,11 @@ void call_logger(int log_num, int thread_num)
}
for(i = 0; i < log_num; i++)
{
MESA_handle_runtime_log(sample_handle, RLOG_LV_INFO, "sample", "sample_handle MESA_handle_runtime_log, i = %d, thread_num = %d", i, thread_num);
MESA_handle_runtime_log(sample_handle, RLOG_LV_DEBUG, "sample", "sample_handle MESA_handle_runtime_log, i = %d, thread_num = %d", i, thread_num);
//sleep(1);
MESA_handle_runtime_log(test_handle, RLOG_LV_INFO, "test", "test_handle MESA_handle_runtime_log, i = %d, thread_num = %d", i, thread_num);
//MESA_HANDLE_RUNTIME_LOG(sample_handle, RLOG_LV_FATAL, "sample", "sample_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
////sleep(1);
//sleep(1);
//MESA_HANDLE_RUNTIME_LOG(test_handle, RLOG_LV_FATAL, "test", "test_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
}
clock_gettime(CLOCK_MONOTONIC, &end);
@@ -72,36 +75,56 @@ void *thread_logger(void *arg)
return NULL;
}
void sig_handler(int sig)
void sig_int_handler(int sig)
{
printf("ctrl+c recviced!\n");
MESA_destroy_runtime_log_handle(sample_handle);
MESA_destroy_runtime_log_handle(test_handle);
sample_handle = NULL;
test_handle = NULL;
if (g_mode == 2)
{
MESA_handle_runtime_log_destruction();
}
printf("%d thread write %d log using %ld us, avg speed %f /s, %ld -> %ld\n", g_thread_num, g_thread_num * g_log_num, g_end_time - g_start_time, ((float)(g_thread_num * g_log_num) / (float)(g_end_time - g_start_time)) * 1000000, g_start_time, g_end_time);
exit(0);
}
void sig_hup_handler(int sig)
{
printf("SIGHUP recviced!\n");
MESA_handle_runtime_log_reconstruction(g_zlog_conf);
}
int main(int argc, char ** args)
{
pthread_t t[MAX_THREAD_NUM];
int i = 0;
if(argc != 3)
if (argc != 5)
{
printf("Usage: ./($app) $thread_num $log_num \n");
printf("Usage: ./($app) $mode[1 or 2] $zlog_conf_path $thread_num $log_num \n");
return -1;
}
g_thread_num = atoi(args[1]);
g_log_num = atoi(args[2]);
g_mode = atoi(args[1]);
g_zlog_conf = args[2];
g_thread_num = atoi(args[3]);
g_log_num = atoi(args[4]);
if(g_thread_num <= 0 || g_log_num <= 0)
{
printf("Error, wrong parameter!\n");
return -1;
}
sample_handle = MESA_create_runtime_log_handle("./log/test_log", RLOG_LV_DEBUG);
if (g_mode == 2)
{
if (0 != MESA_handle_runtime_log_creation(g_zlog_conf))
{
printf("MESA_handle_runtime_log_creation return error\n");
return -1;
}
}
sample_handle = MESA_create_runtime_log_handle("./log/sample_log", RLOG_LV_DEBUG);
if (sample_handle == NULL)
{
printf("get log sample_handle error\n");
@@ -119,7 +142,8 @@ int main(int argc, char ** args)
{
pthread_create(&t[i], NULL, thread_logger, (void *)(unsigned long)(i));
}
signal(SIGINT, sig_handler);
signal(SIGINT, sig_int_handler);
signal(SIGHUP, sig_hup_handler);
while(1)
;
//MESA_destroy_runtime_log_handle(sample_handle);

View File

@@ -19,6 +19,10 @@ extern "C"
#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...) \
MESA_handle_runtime_log((handle), (lv), (mod), "file %s, line %d, " fmt, \
__FILE__, __LINE__, ##args)

View File

@@ -1,23 +1,31 @@
#include "MESA_handle_logger.h"
#include "zlog.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include<sys/stat.h>
#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] = "";
typedef struct log_handle_t
{
int runtime_log_level;
char runtime_log_file[1200];
zlog_category_t *zc;
const char *global_conf_path;
char runtime_log_file[MAX_HANDLE_LOG_PATH];
} log_handle_t;
#define THREAD_CTIME(t, buf, len) thread_safe_ctime(t, buf, len)
#define LOGMSG_MAX_LEN 4096
//const int HANDLE_LOGGER_VERSION_20170816 = 1;
//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)
@@ -39,52 +47,6 @@ static __attribute__((__used__)) const char *GIT_VERSION_UNKNOWN = NULL;
}
#endif
static unsigned int month_days[12] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
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"};
char *thread_safe_ctime(const time_t *tp, char *buf, int len)
{
unsigned int year, month, day, weekday, hour, min, sec;
unsigned int year_days = 365;
sec = * tp;
min = sec / 60; sec = sec % 60;
hour = min / 60; min = min % 60; hour += 8;
day = hour / 24; hour = hour % 24;
weekday = day % 7; weekday = (weekday + 4) % 7;
for(year = 1970; day >= year_days;)
{
day -= year_days;
year ++;
if(0 == year % 4 && (0 != year % 100 || 0 == year % 400))
year_days = 366;
else year_days = 365;
}
if(366 == year_days) month_days[1] = 29;
//bug fix by yw 20120808
for(month = 0; day >= month_days[month];)
{
day -= month_days[month];
month ++;
}
/*
snprintf(buf, len, "%02d:%02d:%02d, %04d/%02d/%02d, %s",
hour, min, sec, year, month, day, weekday_str[week_day]);
*/
snprintf(buf, len, "%s %s %d %02d:%02d:%02d %d", weekday_str[weekday],
month_str[month], day + 1, hour, min, sec, year);
return buf;
}
static int create_dir(const char *dir_path, int path_len)
{
@@ -128,34 +90,112 @@ static int create_path(const char *path, int path_len)
return 0;
}
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;
}
static const char *loglevel_to_name(int level)
{
if (level <= RLOG_LV_DEBUG)
return "DEBUG";
if (level > RLOG_LV_DEBUG && level <= RLOG_LV_INFO)
return "INFO";
return "FATAL";
}
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]";
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;
}
write(g_zlog_conf_fp, zlog_conf_init_buff, strlen(zlog_conf_init_buff));
fsync(g_zlog_conf_fp);
}
snprintf(zlog_rule_conf_content, sizeof(zlog_rule_conf_content),
"\n%s.%s \"%s.%%d(%%F)\"",
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, handle_name(%s), log_path(%s), log_level(%d)!!!\n", tmp_conf_filepath, handle_name, log_path, level);
zlog_init(tmp_conf_filepath);
zlog_reload(NULL);
}
return;
}
void *MESA_create_runtime_log_handle(const char *file_path, int level)
{
if(file_path == NULL)
return NULL;
int rc = -1;
zlog_category_t *zc = NULL;
FILE *fp = NULL;
log_handle_t *p_handle = NULL;
char handle_name[MAX_HANDLE_LOG_PATH];
char *p_path_end = rindex(file_path, '/');
char *p_name = p_path_end+1;
strcpy(handle_name, file_path);
escape_for_zlog(handle_name, strlen(handle_name));
p_name = handle_name;
//creating file_path failed, return NULL
char *p_path = rindex(file_path, '/');
if(p_path != 0)
if (create_path(file_path, p_path_end - file_path) < 0)
return NULL;
snapshot_handle_info(p_name, file_path, level);
zlog_reload(NULL);
zc = zlog_get_category(p_name);
if (!zc)
{
if(create_path(file_path, p_path - file_path) < 0)
return NULL;
printf("[MESA_create_runtime_log_handle], get zlog category (%s) in global_conf_filepath(%s) fail\n", p_name, global_conf_filepath);
}
if(NULL == (fp = fopen(file_path, "w")))
return NULL;
fclose(fp);
//remove(file_path);
p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1);
if(p_handle == NULL)
return NULL;
strncpy(p_handle->runtime_log_file, file_path, 1024);
p_handle->runtime_log_file[1024] = '\0';
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;
return (void *)p_handle;
}
@@ -170,73 +210,118 @@ void MESA_destroy_runtime_log_handle(void *handle)
return;
}
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
{
char buf[LOGMSG_MAX_LEN + 1];
time_t t;
int len;
va_list ap;
FILE *fp;
struct tm local_time;
char tmp_log_file_name[1201];
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;
time(&t);
if(NULL == (localtime_r(&t, &local_time))) return;
//THREAD_CTIME(&t, buf, LOGMSG_MAX_LEN);
len = snprintf(buf, sizeof(buf), "%s %s %d %02d:%02d:%02d %d", 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);
//len = strlen(buf);
switch(level)
{
case RLOG_LV_DEBUG:
len += snprintf(buf + len,
LOGMSG_MAX_LEN - len, ", %s, ", "DEBUG");
break;
case RLOG_LV_INFO:
len += snprintf(buf + len,
LOGMSG_MAX_LEN - len, ", %s, ", "INFO");
break;
case RLOG_LV_FATAL:
len += snprintf(buf + len,
LOGMSG_MAX_LEN - len, ", %s, ", "FATAL");
break;
default:
len += snprintf(buf + len,
LOGMSG_MAX_LEN - len, ", %s, ", "UNKNOWN");
break;
}
if(0 >= LOGMSG_MAX_LEN - len) return;
len += snprintf(buf + len, LOGMSG_MAX_LEN - len, "%s, ", module);
if(0 >= LOGMSG_MAX_LEN - len) return;
if(p_handle->zc == NULL)return;
va_list ap;
va_start(ap, fmt);
len += vsnprintf(buf + len, LOGMSG_MAX_LEN - len, fmt, ap);
vzlog(p_handle->zc, p_handle->runtime_log_file, strlen(p_handle->runtime_log_file), module, strlen(module), __LINE__, level, fmt, ap);
va_end(ap);
if(0 >= LOGMSG_MAX_LEN - len) return;
len += snprintf(buf + len, LOGMSG_MAX_LEN - len, "\n");
sprintf(tmp_log_file_name, "%s.%04d-%02d-%02d", p_handle->runtime_log_file,
local_time.tm_year + 1900, local_time.tm_mon + 1,
local_time.tm_mday);
if(NULL == (fp = fopen(tmp_log_file_name, "a"))) return;
fprintf(fp, "%s", buf);
fclose(fp);
return ;
}
int MESA_handle_runtime_log_creation(const char *conf_path)
{
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], 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
{
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;
}
int MESA_handle_runtime_log_reconstruction(const char *conf_path)
{
int rc = zlog_reload(conf_path);
#ifdef DEBUG
char zlog_rule_conf_content[MAX_HANDLE_LOG_PATH + 1];
time_t t;
struct tm local_time;
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;
}
void MESA_handle_runtime_log_destruction()
{
zlog_fini();
if (g_zlog_conf_fp != -1)
{
unlink(tmp_conf_filepath);
close(g_zlog_conf_fp);
}
}

4
src/version.map Normal file
View File

@@ -0,0 +1,4 @@
{
global: MESA*runtime_log*;GIT_VERSION_*;
local: *;
};

28
zlog/CMakeLists.txt Normal file
View File

@@ -0,0 +1,28 @@
cmake_minimum_required (VERSION 3.5)
include(ExternalProject)
ExternalProject_Add(zlog PREFIX zlog
URL ${CMAKE_CURRENT_SOURCE_DIR}/zlog-1.2.15.tar.gz
URL_MD5 5e79f8d08744c7fef8528dd55a3bcd2d
CONFIGURE_COMMAND ""
BUILD_COMMAND make
INSTALL_COMMAND ""
BUILD_IN_SOURCE 1)
ExternalProject_Get_Property(zlog SOURCE_DIR)
file(MAKE_DIRECTORY ${SOURCE_DIR}/src/)
add_library(zlog_static STATIC IMPORTED GLOBAL)
add_dependencies(zlog_static zlog)
set_property(TARGET zlog_static PROPERTY IMPORTED_LOCATION ${SOURCE_DIR}/src/libzlog.a)
set_property(TARGET zlog_static PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${SOURCE_DIR}/src)
add_library(zlog_shared SHARED IMPORTED GLOBAL)
add_dependencies(zlog_shared zlog)
set_property(TARGET zlog_shared PROPERTY IMPORTED_LOCATION ${SOURCE_DIR}/src/libzlog.so.1.2)
set_property(TARGET zlog_shared PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${SOURCE_DIR}/src)
install(FILES ${SOURCE_DIR}/src/libzlog.so.1.2 DESTINATION lib COMPONENT LIBRARIES)
install(FILES ${SOURCE_DIR}/src/zlog-chk-conf DESTINATION bin COMPONENT EXECUTABLE)

BIN
zlog/zlog-1.2.15.tar.gz Normal file

Binary file not shown.