18 Commits

Author SHA1 Message Date
wangmenglan
c2c9a8a704 1. 将trylock替换为lock 2. 修改测试程序 2022-01-24 17:10:49 +08:00
wangmenglan
3ecbc885fb 调整限制日志输出逻辑,通过调用函数位置做单独的限制输出速率,不再通过handle做整体限制 2022-01-21 10:23:51 +08:00
wangmenglan
4c9b58db56 删除无关文件 2022-01-20 11:40:21 +08:00
wangmenglan
74d3c95994 修改宏位置 2022-01-20 11:21:42 +08:00
wangmenglan
d5de7fa545 修改日志逻辑,增加时间周期和周期内打印条数设置 2022-01-20 11:06:36 +08:00
wangmenglan
ff694442c0 日志逻辑实现,时间通过clock_gettime, 线程安全通过互斥锁 2022-01-17 17:27:59 +08:00
wangmenglan
174a57e01f 临时修改 2022-01-17 11:04:09 +08:00
yangwei
f76af2f036 feat(printf): 使用fprintf输出屏显至stderr,替换printf 2021-06-08 15:34:03 +08:00
杨威
31bd3fed9c Update .gitlab-ci.yml testing源仅debug版开启asan 2020-10-30 23:05:32 +08:00
杨威
9ecf746f0f Update .gitlab-ci.yml 2020-10-15 09:47:55 +08:00
杨威
040ce55a1b Update travis.sh, ASAN using devtoolset-7 2020-10-14 15:02:23 +08:00
liuxueli
cb4ad71c50 编译增加ASAN的选项 2020-10-09 10:30:54 +08:00
liuxueli
02100e4232 上传debuginfo至sentry 2020-10-09 10:23:26 +08:00
yangwei
63f5231e9f 🐞fix(package.cmake): 修复PROFILE描述错误的bug 2020-09-28 19:59:36 +08:00
yangwei
15fe685383 🦄refactor(tmpfile):
snapshot目录更新至/tmp/MESA_handle_logger,同时增加按1小时回收文件的脚本,rpm安装后将安装至/usr/lib/tmpfiles.d/目录
2020-09-28 19:38:08 +08:00
yangwei
e1057e6d0c 🔧build(cmake/): 增加changelog,并调整version和changelog目录 2020-09-28 16:42:42 +08:00
yangwei
2e4e4dff17 🐞fix(creat_dir): 修复计算待创建目录不包含/时长度计算错误的bug 2020-09-24 15:18:14 +08:00
yangwei
150255033c 🔧build(CMakeLists.txt): 动态链接库链接pthread 2020-09-22 14:26:51 +08:00
11 changed files with 237 additions and 95 deletions

2
.gitignore vendored
View File

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

View File

@@ -3,6 +3,7 @@ variables:
GIT_STRATEGY: "clone"
BUILD_PADDING_PREFIX: /tmp/padding_for_CPACK_RPM_BUILD_SOURCE_DIRS_PREFIX_PREFIX_PREFIX_PREFIX_PREFIX_PREFIX/
INSTALL_PREFIX: "/opt/MESA/lib/"
INSTALL_DEPENDENCY_LIBRARY: ""
stages:
- build
@@ -49,6 +50,10 @@ develop_build_debug:
variables:
BUILD_TYPE: Debug
PACKAGE: 1
UPLOAD_RPM: 1
ASAN_OPTION: ADDRESS
PULP3_REPO_NAME: framework-testing-x86_64.el7
PULP3_DIST_NAME: framework-testing-x86_64.el7
artifacts:
name: "MESA_handle_logger-$CI_COMMIT_REF_NAME-debug"
paths:
@@ -64,6 +69,10 @@ develop_build_release:
variables:
BUILD_TYPE: RelWithDebInfo
PACKAGE: 1
UPLOAD_RPM: 1
ASAN_OPTION: "OFF"
PULP3_REPO_NAME: framework-testing-x86_64.el7
PULP3_DIST_NAME: framework-testing-x86_64.el7
artifacts:
name: "MESA_handle_logger-$CI_COMMIT_REF_NAME-release"
paths:
@@ -74,12 +83,30 @@ develop_build_release:
- /^master.*$/i
release_build_debug:
stage: build
variables:
BUILD_TYPE: Debug
PACKAGE: 1
UPLOAD_RPM: 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
release_build_release:
stage: build
variables:
BUILD_TYPE: RelWithDebInfo
PACKAGE: 1
UPLOAD: 1
UPLOAD_RPM: 1
UPLOAD_SYMBOL_FILES: 1
SYMBOL_TARGET: libMESA_handle_logger
PULP3_REPO_NAME: framework-stable-x86_64.el7
PULP3_DIST_NAME: framework-stable-x86_64.el7
extends: .build_by_travis
@@ -89,57 +116,3 @@ 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

@@ -40,6 +40,7 @@ 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)
set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name} LIBRARY_OUTPUT_DIRECTORY ${MESA_SHARED_INSTALL_DIR})
@@ -61,10 +62,11 @@ 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/MESA_handle_logger.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT HEADER)
install(FILES conf/MESA_handle_logger.conf DESTINATION /usr/lib/tmpfiles.d/ COMPONENT PROFILE)
add_executable(${lib_name}_demo demo/test_handle_logger.c)
target_link_libraries(${lib_name}_demo rt pthread ${lib_name}_shared)
target_link_libraries(${lib_name}_demo pthread ${lib_name}_shared)
include(Package)

View File

@@ -33,7 +33,14 @@ env | sort
: "${COMPILER_IS_GNUCXX:=OFF}"
# Install dependency from YUM
#yum install -y mrzcpd framework numactl-devel zlib-devel librdkafka-devel systemd-devel
if [ -n "${INSTALL_DEPENDENCY_LIBRARY}" ]; then
yum install -y $INSTALL_DEPENDENCY_LIBRARY
fi
if [ $ASAN_OPTION ];then
source /opt/rh/devtoolset-7/enable
fi
mkdir build || true
cd build
@@ -41,6 +48,8 @@ cmake3 -DCMAKE_CXX_FLAGS=$CXX_FLAGS \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
-DENABLE_DEVEL=$ENABLE_DEVEL_SWITCH \
-DASAN_OPTION=$ASAN_OPTION \
-DVERSION_DAILY_BUILD=$TESTING_VERSION_BUILD \
..
make
@@ -49,13 +58,14 @@ if [ -n "${PACKAGE}" ]; then
make package
fi
if [ -n "${UPLOAD}" ]; then
if [ -n "${UPLOAD_RPM}" ]; then
cp ~/rpm_upload_tools.py ./
python3 rpm_upload_tools.py ${PULP3_REPO_NAME} ${PULP3_DIST_NAME} *.rpm
fi
#if [ -n "${UPLOAD_SYMBOL_FILES}" ]; then
# rpm -i tfe*debuginfo*.rpm
# cp /usr/lib/debug/opt/tsg/tfe/bin/tfe.debug /tmp/tfe.debuginfo.${CI_COMMIT_SHORT_SHA}
# sentry-cli upload-dif -t elf /tmp/tfe.debuginfo.${CI_COMMIT_SHORT_SHA}
#fi
if [ -n "${UPLOAD_SYMBOL_FILES}" ]; then
rpm -i $SYMBOL_TARGET*debuginfo*.rpm
_symbol_file=`find /usr/lib/debug/ -name "$SYMBOL_TARGET*.so*.debug"`
cp $_symbol_file ${_symbol_file}info.${CI_COMMIT_SHORT_SHA}
sentry-cli upload-dif -t elf ${_symbol_file}info.${CI_COMMIT_SHORT_SHA}
fi

View File

@@ -14,6 +14,9 @@ set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
set(CPACK_PACKAGE_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}.${VERSION_BUILD}")
execute_process(COMMAND bash -c "echo -ne \"`uname -r | awk -F'.' '{print $5\".\"$6\".\"$7}'`\"" OUTPUT_VARIABLE SYSTEM_VERSION)
execute_process(COMMAND sh changelog.sh ${CMAKE_BINARY_DIR} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/cmake)
SET(CPACK_RPM_CHANGELOG_FILE ${CMAKE_BINARY_DIR}/changelog.txt)
# RPM Build
set(CPACK_GENERATOR "RPM")
set(CPACK_RPM_PACKAGE_VENDOR "MESA")
@@ -35,6 +38,7 @@ set(CPACK_RPM_FILE_NAME "${CPACK_RPM_LIBRARIES_PACKAGE_NAME}-${CPACK_PACKAGE_VER
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_COMPONENT_PROFILE_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")
@@ -43,7 +47,7 @@ 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 EXECUTABLE)
set(CPACK_COMPONENTS_ALL LIBRARIES HEADER EXECUTABLE PROFILE)
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)

View File

@@ -3,7 +3,7 @@
set(__SOURCE_AUTORESIVISION ${CMAKE_SOURCE_DIR}/autorevision.sh)
set(__AUTORESIVISION ${CMAKE_BINARY_DIR}/autorevision.sh)
set(__VERSION_CACHE ${CMAKE_SOURCE_DIR}/version.txt)
set(__VERSION_CACHE ${CMAKE_BINARY_DIR}/version.txt)
set(__VERSION_CONFIG ${CMAKE_BINARY_DIR}/version.cmake)
file(COPY ${__SOURCE_AUTORESIVISION} DESTINATION ${CMAKE_BINARY_DIR}

4
cmake/changelog.sh Normal file
View File

@@ -0,0 +1,4 @@
#!/bin/sh
work_path=$1
branch=`git status | grep branch | awk '{print $NF}'`
git log --branches=$branch --no-merges --date=local --show-signature --pretty="* %ad %an %ae %nhash: %H%ncommit:%n%B" | awk -F"-" '{print "- "$0}' | sed 's/- \*/\*/g' | sed 's/- $//g' | sed 's/-/ -/g' | sed 's/[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}//g' > $work_path/changelog.txt

View File

@@ -0,0 +1,2 @@
#Type Path Mode User Group Age Argument
d /tmp/MESA_handle_logger/ 0755 - - 1h -

View File

@@ -16,11 +16,12 @@ void *test_handle = NULL;
int g_mode = 0;
int g_log_num = 0;
int g_thread_num = 0;
int g_log_print_way = 0;
const char *g_zlog_conf = NULL;
volatile long g_start_time = 0;
volatile long g_end_time = 0;
void call_logger(int log_num, int thread_num)
void call_logger(int log_num, int thread_num, int way)
{
int i = 0;
struct timespec start, end;
@@ -40,12 +41,25 @@ void call_logger(int log_num, int thread_num)
}
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);
//sleep(1);
switch (way)
{
case 1:
MESA_handle_runtime_log(sample_handle, RLOG_LV_DEBUG, "sample", "sample_handle RUNTIEM_LOG test, 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);
//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);
break;
case 2:
MESA_HANDLE_RUNTIME_LOG_RATELIMIT(sample_handle, RLOG_LV_DEBUG, "sample", "sample_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
MESA_HANDLE_RUNTIME_LOG_RATELIMIT(test_handle, RLOG_LV_INFO, "test", "test_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
break;
case 3:
MESA_HANDLE_RUNTIME_LOG(sample_handle, RLOG_LV_DEBUG, "sample", "sample_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
MESA_HANDLE_RUNTIME_LOG(test_handle, RLOG_LV_INFO, "test", "test_handle RUNTIEM_LOG test, i = %d, thread_num = %d", i, thread_num);
break;
default:
break;
}
// if (i % 10000 == 0)
// sleep(1);
}
clock_gettime(CLOCK_MONOTONIC, &end);
end_time = end.tv_sec*1000000 + end.tv_nsec/1000;
@@ -70,7 +84,7 @@ void *thread_logger(void *arg)
{
int thread_num = (int)(unsigned long long)arg;
printf("thread %d created! \n", thread_num);
call_logger(g_log_num, thread_num);
call_logger(g_log_num, thread_num, g_log_print_way);
printf("thread %d finished! \n", thread_num);
return NULL;
}
@@ -96,20 +110,39 @@ void sig_hup_handler(int sig)
MESA_handle_runtime_log_reconstruction(g_zlog_conf);
}
void sig_user1_handler(int sig)
{
printf("SIGUSR1 recviced! set interval:3 burst:5\n");
MESA_set_runtime_log_handle_ratelimit(sample_handle, 3, 5);
}
void sig_user2_handler(int sig)
{
printf("SIGUSR2 recviced! set interval:5 burst:3\n");
MESA_set_runtime_log_handle_ratelimit(sample_handle, 5, 3);
}
void sig_min_handler(int sig)
{
printf("SIGRTMIN recviced! unlimit\n");
MESA_set_runtime_log_handle_ratelimit(sample_handle, 0, 3);
}
int main(int argc, char ** args)
{
pthread_t t[MAX_THREAD_NUM];
int i = 0;
if (argc != 5)
if (argc != 6)
{
printf("Usage: ./($app) $mode[1 or 2] $zlog_conf_path $thread_num $log_num \n");
printf("Usage: ./($app) $mode[1 or 2] $zlog_conf_path $thread_num $log_num $log_print_way[1 2 3]\n");
return -1;
}
g_mode = atoi(args[1]);
g_zlog_conf = args[2];
g_thread_num = atoi(args[3]);
g_log_num = atoi(args[4]);
g_log_print_way = atoi(args[5]);
if(g_thread_num <= 0 || g_log_num <= 0)
{
@@ -144,6 +177,9 @@ int main(int argc, char ** args)
}
signal(SIGINT, sig_int_handler);
signal(SIGHUP, sig_hup_handler);
signal(SIGUSR1, sig_user1_handler);
signal(SIGUSR2, sig_user2_handler);
signal(SIGRTMIN, sig_min_handler);
while(1)
;
//MESA_destroy_runtime_log_handle(sample_handle);

View File

@@ -14,10 +14,21 @@ extern "C"
{
#endif
#include <pthread.h>
#define RLOG_LV_DEBUG 10
#define RLOG_LV_INFO 20
#define RLOG_LV_FATAL 30
typedef struct log_ratelimit_s
{
int interval;
int burst;
int printed;
int missed;
long begin;
pthread_mutex_t mutex;
}log_ratelimit_t;
int MESA_handle_runtime_log_creation(const char *conf_path);
int MESA_handle_runtime_log_reconstruction(const char *conf_path);
@@ -27,6 +38,15 @@ void MESA_handle_runtime_log_destruction();
MESA_handle_runtime_log((handle), (lv), (mod), "file %s, line %d, " fmt, \
__FILE__, __LINE__, ##args)
#define MESA_HANDLE_RUNTIME_LOG_RATELIMIT(handle, lv, mod, fmt, args...) \
({ \
static log_ratelimit_t _rs = { \
.mutex = PTHREAD_MUTEX_INITIALIZER, \
}; \
if (MESA_runtime_log_ratelimit((handle), (&_rs), (lv), (mod), (__func__))) \
MESA_handle_runtime_log((handle), (lv), (mod), fmt, ##args); \
})
/*
* name: MESA_create_runtime_log_handle
* functionality: get runtime_log handle;
@@ -63,6 +83,18 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
*/
void MESA_destroy_runtime_log_handle(void *handle);
/*
* name: MESA_set_runtime_log_handle_ratelimit
* functionality: set not more than @burst in every @interval when appends log message
* params:
* handle: runtime log handle which is going to be released;
* interval: interval time(second). if set '0', it means unlimit.
* burst: in interval time max count
* returns:
* none;
*/
void MESA_set_runtime_log_handle_ratelimit(void *handle, int interval, int burst);
#ifdef __cplusplus
}
#endif

View File

@@ -9,6 +9,9 @@
#include <time.h>
#define MAX_HANDLE_LOG_PATH 4096
#define DEFAULT_BURST 10
#define DEFAULT_INTERVAL 5
static int g_zlog_inited = 0;
static int g_zlog_conf_fp = -1;
static char global_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
@@ -16,6 +19,9 @@ static char tmp_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
typedef struct log_handle_t
{
int interval;
int burst;
pthread_mutex_t mutex;
int runtime_log_level;
zlog_category_t *zc;
const char *global_conf_path;
@@ -47,7 +53,6 @@ static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL;
}
#endif
static int create_dir(const char *dir_path, int path_len)
{
if(dir_path == NULL)
@@ -94,14 +99,13 @@ 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 char tmp_template[] = "/tmp/MESA_handle_loggger/handle_loggger.XXXXXX";
static void escape_for_zlog(char *in_buf, int buflen)
{
@@ -129,11 +133,14 @@ 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 *tmp_prefix = "/tmp/MESA_handle_logger/";
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)
{
if (create_path("/tmp/MESA_handle_logger/", strlen(tmp_prefix)) < 0)
return;
char temp_filename[1024] = "";
sprintf(temp_filename, "/tmp/MESA_handle_logger_%d.XXXXXX", getpid());
sprintf(temp_filename, "/tmp/MESA_handle_logger/MESA_logger_snapshot_%d.XXXXXX", getpid());
g_zlog_conf_fp = mkstemp(temp_filename);
if (g_zlog_conf_fp == -1)
{
@@ -154,13 +161,67 @@ static void snapshot_handle_info(const char *handle_name, const char *log_path,
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);
fprintf(stderr,"[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;
}
static void log_update_time(log_handle_t *p_handle, log_ratelimit_t *rs, int level, const char *module, const char *func)
{
struct timespec current = {0, 0};
clock_gettime(CLOCK_MONOTONIC, &current);
if (((long)(rs->begin + rs->interval - current.tv_sec)) < 0)
{
if (rs->missed)
{
zlog(p_handle->zc, p_handle->runtime_log_file, strlen(p_handle->runtime_log_file), module, strlen(module), __LINE__, level, "%s: %d callbacks suppressed\n", func, rs->missed);
rs->missed = 0;
}
rs->begin = current.tv_sec;
rs->printed = 0;
}
return ;
}
int MESA_runtime_log_ratelimit(void *handle, log_ratelimit_t *rs, int level, const char *module, const char *func)
{
int ret = 0;
struct timespec timer = {0, 0};
log_handle_t *p_handle = (log_handle_t *)handle;
if (pthread_mutex_lock(&rs->mutex) != 0)return 0;
if (!rs->begin)
{
clock_gettime(CLOCK_MONOTONIC, &timer);
rs->begin = timer.tv_sec;
}
rs->burst = p_handle->burst;
rs->interval = p_handle->interval;
if (!rs->interval)
{
pthread_mutex_unlock(&rs->mutex);
return 1;
}
log_update_time(p_handle, rs, level, module, func);
if (rs->burst && rs->burst > rs->printed)
{
rs->printed++;
ret = 1;
}
else
{
rs->missed++;
ret = 0;
}
pthread_mutex_unlock(&rs->mutex);
return ret;
}
void *MESA_create_runtime_log_handle(const char *file_path, int level)
{
if(file_path == NULL)
@@ -173,7 +234,6 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
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);
@@ -181,28 +241,36 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
p_name = handle_name;
if(p_path_end != NULL && p_path_end > file_path)
{
//creating file_path failed, return NULL
if (create_path(file_path, p_path_end - file_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)
{
printf("[MESA_create_runtime_log_handle], get zlog category (%s) in global_conf_filepath(%s) fail\n", p_name, global_conf_filepath);
fprintf(stderr,"[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);
p_handle->runtime_log_level = level;
p_handle->zc = zc;
p_handle->burst = DEFAULT_BURST;
p_handle->interval = DEFAULT_INTERVAL;
pthread_mutex_init(&p_handle->mutex, NULL);
return (void *)p_handle;
}
void MESA_destroy_runtime_log_handle(void *handle)
{
log_handle_t *p_handle = (log_handle_t *)handle;
if(handle != NULL)
{
pthread_mutex_destroy(&p_handle->mutex);
free(handle);
handle = NULL;
}
@@ -210,23 +278,34 @@ void MESA_destroy_runtime_log_handle(void *handle)
return;
}
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
{
va_list ap;
log_handle_t *p_handle = (log_handle_t *)handle;
if(p_handle == NULL || p_handle->runtime_log_file == NULL)return;
if(p_handle->zc == NULL)return;
va_list ap;
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);
va_end(ap);
return ;
}
void MESA_set_runtime_log_handle_ratelimit(void *handle, int interval, int burst)
{
log_handle_t *p_handle = (log_handle_t *)handle;
if ((p_handle == NULL) || (interval < 0) || (burst < 0))return;
pthread_mutex_lock(&p_handle->mutex);
p_handle->interval = interval;
p_handle->burst = burst;
pthread_mutex_unlock(&p_handle->mutex);
return;
}
int MESA_handle_runtime_log_creation(const char *conf_path)
{
@@ -234,11 +313,11 @@ int MESA_handle_runtime_log_creation(const char *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);
fprintf(stderr,"[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);
fprintf(stderr,"[MESA_handle_runtime_log_creation], INIT zlog by (NULL) or (%s) failed !!! \n", env);
return -1;
}
else
@@ -254,11 +333,11 @@ int MESA_handle_runtime_log_creation(const char *conf_path)
rc = zlog_init(conf_path);
if (rc)
{
printf("[MESA_handle_runtime_log_creation], INIT zlog by (%s) failed , using [NULL] retry !\n", conf_path);
fprintf(stderr,"[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);
fprintf(stderr,"[MESA_handle_runtime_log_creation], INIT zlog by (NULL) or (%s) failed \n", env);
return -1;
}
else
@@ -276,7 +355,7 @@ int MESA_handle_runtime_log_creation(const char *conf_path)
}
}
printf("[MESA_handle_runtime_log_creation], INIT zlog finish, Using (%s). \n", global_conf_filepath);
fprintf(stderr,"[MESA_handle_runtime_log_creation], INIT zlog finish, Using (%s). \n", global_conf_filepath);
g_zlog_inited = 1;
zlog_profile();