9 Commits

Author SHA1 Message Date
yangwei
8825da8881 cmake更新,指定代码编译,保证向前兼容取消soversion设置 2020-09-07 16:26:27 +08:00
yangwei
7827bb0408 UPDATE:动态库增加soversion 2020-09-07 14:09:28 +08:00
yangwei
7e10ca27d5 增加mutex,解决跨天切换文件时,重复fclose导致double free的bug 2020-08-17 13:52:43 +08:00
liuxueli
bd5f0acc18 按照规范生成devel.rpm 2020-04-10 11:41:11 +08:00
liuxueli
2aed6fd98f 修正rpm包命名规范 2020-04-07 18:46:11 +08:00
liuxueli
64f208f615 隐藏component的后缀 2020-04-07 17:52:06 +08:00
liuxueli
910602d3f8 支持上传yum源 2020-04-07 13:29:54 +08:00
杨威
d7fd259d43 Merge branch 'Feature-using-env-define-flushnum-msgsize' into 'master'
Feature using env define flushnum msgsize

See merge request MESA_framework/MESA_handle_logger!8
2019-09-24 14:03:54 +08:00
杨威
dfd0b4efb7 增加两个环境变量,HLOG_FLUSH_NUM和HLOG_MSG_SIZE,分别用于控制fflush文件的条数和单条log的最大长度,默认值分别为0和4096
如果有调整的需要,可以在进程运行时定义环境变量,作用范围为进程
2019-09-24 14:03:54 +08:00
14 changed files with 203 additions and 408 deletions

1
.gitignore vendored
View File

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

View File

@@ -14,8 +14,7 @@ stages:
- cd $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH - cd $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH
- chmod +x ./ci/travis.sh - chmod +x ./ci/travis.sh
script: script:
- wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo - yum makecache
- yum makecache fast
- ./ci/travis.sh - ./ci/travis.sh
- cd build - cd build
tags: tags:
@@ -27,8 +26,7 @@ branch_build_debug:
variables: variables:
BUILD_TYPE: Debug BUILD_TYPE: Debug
except: except:
- /^dev*.*$/i - /^develop.*$/i
- /^rel*.*$/i
- /^master.*$/i - /^master.*$/i
- tags - tags
@@ -38,8 +36,7 @@ branch_build_release:
BUILD_TYPE: RelWithDebInfo BUILD_TYPE: RelWithDebInfo
extends: .build_by_travis extends: .build_by_travis
except: except:
- /^dev*.*$/i - /^develop.*$/i
- /^rel*.*$/i
- /^master.*$/i - /^master.*$/i
- tags - tags
@@ -54,8 +51,7 @@ develop_build_debug:
paths: paths:
- build/*.rpm - build/*.rpm
only: only:
- /^dev*.*$/i - /^develop.*$/i
- /^rel*.*$/i
- /^master.*$/i - /^master.*$/i
develop_build_release: develop_build_release:
@@ -69,8 +65,7 @@ develop_build_release:
paths: paths:
- build/*.rpm - build/*.rpm
only: only:
- /^dev*.*$/i - /^develop.*$/i
- /^rel*.*$/i
- /^master.*$/i - /^master.*$/i
@@ -89,57 +84,3 @@ release_build_release:
- build/*.rpm - build/*.rpm
only: only:
- tags - 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,8 +4,8 @@ set(lib_name MESA_handle_logger)
project (${lib_name}) project (${lib_name})
set(LIB_MAJOR_VERSION 2) set(LIB_MAJOR_VERSION 1)
set(LIB_MINOR_VERSION 0) set(LIB_MINOR_VERSION 1)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include(Version) include(Version)
@@ -15,17 +15,18 @@ set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
set(CMAKE_INSTALL_PREFIX /opt/MESA) set(CMAKE_INSTALL_PREFIX /opt/MESA)
#for ASAN #for ASAN
option(ENABLE_SANITIZE_ADDRESS "Enable AddressSanitizer" FALSE) set(ASAN_OPTION "OFF" CACHE STRING " set asan type chosen by the user, using OFF as default")
option(ENABLE_SANITIZE_THREAD "Enable ThreadSanitizer" FALSE) set_property(CACHE ASAN_OPTION PROPERTY STRINGS OFF ADDRESS THREAD)
message(STATUS "ASAN_OPTION='${ASAN_OPTION}'")
if(ENABLE_SANITIZE_ADDRESS) if(ASAN_OPTION MATCHES "ADDRESS")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer") set(CMAKE_C_FLAGS "${CMAKADDRESS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
elseif(ENABLE_SANITIZE_THREAD) elseif(ASAN_OPTION MATCHES "THREAD")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer") 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} -fsanitize=thread -fno-omit-frame-pointer") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=thread -fno-omit-frame-pointer")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan")
endif() endif()
@@ -33,39 +34,31 @@ endif()
include_directories(${PROJECT_SOURCE_DIR}/inc/) include_directories(${PROJECT_SOURCE_DIR}/inc/)
add_subdirectory(zlog)
# Shared Library Output # Shared Library Output
add_library(${lib_name}_shared SHARED src/MESA_handle_logger.c) 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")
target_link_libraries(${lib_name}_shared pthread)
if(DEFINED MESA_SHARED_INSTALL_DIR) if(DEFINED MESA_SHARED_INSTALL_DIR)
set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name} LIBRARY_OUTPUT_DIRECTORY ${MESA_SHARED_INSTALL_DIR}) set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name} LIBRARY_OUTPUT_DIRECTORY ${MESA_SHARED_INSTALL_DIR})
else() else()
set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name}) set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name})
endif() endif()
set_target_properties(${lib_name}_shared PROPERTIES VERSION ${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}) #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}) #set_target_properties(${lib_name}_shared PROPERTIES SOVERSION ${LIB_MAJOR_VERSION})
# static Library Output # static Library Output
add_library(${lib_name}_static STATIC src/MESA_handle_logger.c) 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_target_properties(${lib_name}_static PROPERTIES OUTPUT_NAME ${lib_name})
set(CMAKE_INSTALL_PREFIX /opt/MESA) set(CMAKE_INSTALL_PREFIX /opt/MESA)
install(FILES inc/MESA_handle_logger.h DESTINATION install(TARGETS ${lib_name}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARY)
${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT devel) install(FILES inc/${lib_name}.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT HEADER)
install(TARGETS ${lib_name}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARIES)
install(FILES inc/MESA_handle_logger.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT HEADER)
add_executable(${lib_name}_demo demo/test_handle_logger.c) add_executable(${lib_name}_demo demo/test_handle_logger.c)
target_link_libraries(${lib_name}_demo pthread ${lib_name}_shared) target_link_libraries(${lib_name}_demo rt pthread ${lib_name}_static)
include(Package) include(Package)

3
README
View File

@@ -14,9 +14,6 @@
2019-2-18 update by yw 2019-2-18 update by yw
1)性能优化句柄中存储文件fp定期flush至文件 1)性能优化句柄中存储文件fp定期flush至文件
2020-09-07 update to libMESA_handle_logger2 by yw
1)内部使用zlog实现行为变更更新库为libMESA_handle_logger2
------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------
NOTICE: NOTICE:
1)compatible with g++, extern with c; 1)compatible with g++, extern with c;

View File

@@ -26,27 +26,24 @@ set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_COMPONENTS_IGNORE_GROUPS 1) set(CPACK_COMPONENTS_IGNORE_GROUPS 1)
set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP) set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP)
set(CPACK_COMPONENT_HEADER_DISPLAY_NAME "develop") set(CPACK_COMPONENT_HEADER_DISPLAY_NAME "develop")
set(CPACK_COMPONENT_LIBRARIES_REQUIRED TRUE) set(CPACK_COMPONENT_LIBRARY_REQUIRED TRUE)
set(CPACK_COMPONENT_HEADER_REQUIRED TRUE) set(CPACK_COMPONENT_HEADER_REQUIRED TRUE)
set(CPACK_RPM_HEADER_PACKAGE_NAME "${MY_RPM_NAME_PREFIX}-devel") set(CPACK_RPM_HEADER_PACKAGE_NAME "${MY_RPM_NAME_PREFIX}-devel")
set(CPACK_RPM_LIBRARIES_PACKAGE_NAME ${MY_RPM_NAME_PREFIX}) set(CPACK_RPM_LIBRARY_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_FILE_NAME "${CPACK_RPM_LIBRARY_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_RPM_LIBRARY_DEBUGINFO_FILE_NAME "${CPACK_RPM_LIBRARY_PACKAGE_NAME}-debuginfo-${CPACK_PACKAGE_VERSION}-${SYSTEM_VERSION}.rpm")
set(CPACK_COMPONENT_LIBRARIES_GROUP "libraries") set(CPACK_COMPONENT_LIBRARY_GROUP "library")
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_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") set(CPACK_RPM_HEADER_DEBUGINFO_FILE_NAME "${CPACK_RPM_HEADER_PACKAGE_NAME}-debuginfo-${CPACK_PACKAGE_VERSION}-${SYSTEM_VERSION}.rpm")
set(CPACK_COMPONENT_HEADER_GROUP "header") set(CPACK_COMPONENT_HEADER_GROUP "header")
set(CPACK_RPM_HEADER_PACKAGE_REQUIRES_PRE ${CPACK_RPM_LIBRARIES_PACKAGE_NAME}) set(CPACK_RPM_HEADER_PACKAGE_REQUIRES_PRE ${CPACK_RPM_LIBRARY_PACKAGE_NAME})
set(CPACK_RPM_HEADER_PACKAGE_CONFLICTS ${CPACK_RPM_HEADER_PACKAGE_NAME}) set(CPACK_RPM_HEADER_PACKAGE_CONFLICTS ${CPACK_RPM_HEADER_PACKAGE_NAME})
set(CPACK_COMPONENTS_ALL LIBRARIES HEADER EXECUTABLE) set(CPACK_COMPONENTS_ALL LIBRARY HEADER)
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}") set(CPACK_BUILD_SOURCE_DIRS "${CMAKE_SOURCE_DIR}")

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,25 +1,23 @@
#include "MESA_handle_logger.h" #include "MESA_handle_logger.h"
#include "zlog.h"
#include <fcntl.h> #include <stdio.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include<sys/stat.h>
#define MAX_HANDLE_LOG_PATH 4096 int LOGMSG_MAX_LEN = 4096;
static int g_zlog_inited = 0; int FLUSH_LOG_NUM = 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 typedef struct log_handle_t
{ {
int runtime_log_level; int runtime_log_level;
zlog_category_t *zc; int flush_log_count;
const char *global_conf_path; FILE *fp;
char runtime_log_file[MAX_HANDLE_LOG_PATH]; pthread_mutex_t mutex;
char runtime_log_file[1200];
char cur_log_file[4096];
} log_handle_t; } log_handle_t;
@@ -48,6 +46,12 @@ static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL;
#endif #endif
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"};
static int create_dir(const char *dir_path, int path_len) static int create_dir(const char *dir_path, int path_len)
{ {
if(dir_path == NULL) if(dir_path == NULL)
@@ -90,113 +94,41 @@ static int create_path(const char *path, int path_len)
return 0; 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) void *MESA_create_runtime_log_handle(const char *file_path, int level)
{ {
if(file_path == NULL) if(file_path == NULL)
return NULL; return NULL;
char *max_len = getenv("HLOG_MSG_SIZE");
char *flush_num = getenv("HLOG_FLUSH_NUM");
if(max_len != NULL)LOGMSG_MAX_LEN = atoi(max_len);
if(flush_num != NULL)FLUSH_LOG_NUM = atoi(flush_num);
int rc = -1;
zlog_category_t *zc = NULL;
FILE *fp = NULL; FILE *fp = NULL;
log_handle_t *p_handle = 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;
if(p_path_end != NULL && p_path_end > file_path)
{
//creating file_path failed, return NULL //creating file_path failed, return NULL
if (create_path(file_path, p_path_end-file_path) < 0) char *p_path = rindex(file_path, '/');
if(p_path != 0)
{
if(create_path(file_path, p_path - file_path) < 0)
return NULL; return NULL;
} }
snapshot_handle_info(p_name, file_path, level); if(NULL == (fp = fopen(file_path, "w")))
zlog_reload(NULL); return NULL;
zc = zlog_get_category(p_name);
if (!zc) //fclose(fp);
{ //remove(file_path);
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); 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);
if(p_handle == NULL)
return NULL;
strncpy(p_handle->runtime_log_file, file_path, 1024);
p_handle->runtime_log_file[1024] = '\0';
p_handle->runtime_log_level = level; p_handle->runtime_log_level = level;
p_handle->zc = zc; pthread_mutex_init(&p_handle->mutex,NULL);
//p_handle->fp = fp;
return (void *)p_handle; return (void *)p_handle;
} }
@@ -204,6 +136,12 @@ void MESA_destroy_runtime_log_handle(void *handle)
{ {
if(handle != NULL) if(handle != NULL)
{ {
log_handle_t *p_handle = (log_handle_t *)handle;
if (p_handle->fp != NULL)
{
fclose(p_handle->fp);
p_handle->fp = NULL;
}
free(handle); free(handle);
handle = NULL; handle = NULL;
} }
@@ -211,118 +149,119 @@ void MESA_destroy_runtime_log_handle(void *handle)
return; return;
} }
static void MESA_handle_close_file(log_handle_t *p_handle)
{
pthread_mutex_lock(&p_handle->mutex);
if(p_handle->fp != NULL)
{
fclose(p_handle->fp);
p_handle->fp = NULL;
}
pthread_mutex_unlock(&p_handle->mutex);
return;
}
static int MESA_handle_open_file(char *file_name, log_handle_t *p_handle)
{
FILE *fp = NULL;
MESA_handle_close_file(p_handle);
if(NULL == (fp = fopen(file_name, "a")))
{
return -1;
}
p_handle->fp = fp;
p_handle->flush_log_count = 0;
memcpy(p_handle->cur_log_file, file_name, strlen(file_name));
return 0;
}
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...) 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[1400];
log_handle_t *p_handle = (log_handle_t *)handle; log_handle_t *p_handle = (log_handle_t *)handle;
if(p_handle == NULL || p_handle->runtime_log_file == NULL)return; if(p_handle == NULL || p_handle->runtime_log_file == NULL)return;
if(p_handle->zc == NULL)return; if(level < p_handle->runtime_log_level) return;
time(&t);
if(NULL == (localtime_r(&t, &local_time))) return;
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;
va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vzlog(p_handle->zc, p_handle->runtime_log_file, strlen(p_handle->runtime_log_file), module, strlen(module), __LINE__, level, fmt, ap); len += vsnprintf(buf + len, LOGMSG_MAX_LEN - len, fmt, ap);
va_end(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(p_handle->fp == NULL)
{
if(0 != MESA_handle_open_file(tmp_log_file_name,p_handle))return;
}
else
{
if (0 != memcmp(tmp_log_file_name, p_handle->cur_log_file, strlen(tmp_log_file_name)))
{
if(0 != MESA_handle_open_file(tmp_log_file_name,p_handle))return;
}
}
if (0 > fprintf(p_handle->fp, "%s", buf))
{
MESA_handle_close_file(p_handle);
}
else
{
p_handle->flush_log_count+=1;
if (p_handle->flush_log_count >= FLUSH_LOG_NUM)
{
fflush(p_handle->fp);
p_handle->flush_log_count = 0;
}
}
return; 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);
}
}

View File

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

View File

@@ -1,28 +0,0 @@
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)

Binary file not shown.