Compare commits
2 Commits
v1.1.3-rel
...
rel-1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2eef8296f5 | ||
|
|
524bca2714 |
@@ -4,12 +4,10 @@ set(lib_name MESA_handle_logger)
|
||||
|
||||
project (${lib_name})
|
||||
|
||||
set(LIB_MAJOR_VERSION 1)
|
||||
set(LIB_MINOR_VERSION 1)
|
||||
|
||||
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)
|
||||
@@ -34,31 +32,36 @@ endif()
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR}/inc/)
|
||||
|
||||
file(GLOB SRC
|
||||
"src/*.c"
|
||||
"src/*.cpp"
|
||||
)
|
||||
|
||||
# Shared Library Output
|
||||
add_library(${lib_name}_shared SHARED src/MESA_handle_logger.c)
|
||||
add_library(${lib_name}_shared SHARED ${SRC})
|
||||
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/MESA_handle_logger.c)
|
||||
add_library(${lib_name}_static STATIC ${SRC})
|
||||
set_target_properties(${lib_name}_static PROPERTIES OUTPUT_NAME ${lib_name})
|
||||
|
||||
set(CMAKE_INSTALL_PREFIX /opt/MESA)
|
||||
|
||||
install(TARGETS ${lib_name}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARY)
|
||||
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)
|
||||
|
||||
|
||||
|
||||
add_executable(${lib_name}_demo demo/test_handle_logger.c)
|
||||
file(GLOB DEMO
|
||||
"demo/*.c"
|
||||
"demo/*.cpp"
|
||||
)
|
||||
|
||||
add_executable(${lib_name}_demo ${DEMO})
|
||||
target_link_libraries(${lib_name}_demo rt pthread ${lib_name}_static)
|
||||
|
||||
include(Package)
|
||||
|
||||
9
README
9
README
@@ -4,16 +4,13 @@
|
||||
*Author:yangwei@iie.ac.cn
|
||||
*History:
|
||||
2014-03-24 created by yw
|
||||
1)第一个动态库版本
|
||||
1)first version
|
||||
|
||||
2014-04-16 modified by yw
|
||||
1) 支持g++ 4.4.4编译
|
||||
1)strict inspection parameters, compatible with g++ 4.4.4
|
||||
|
||||
2014-05-29 update by yw
|
||||
1)支持创建用户指定的日志目录
|
||||
|
||||
2019-2-18 update by yw
|
||||
1)性能优化,句柄中存储文件fp,定期flush至文件
|
||||
1)create directory is supported
|
||||
-------------------------------------------------------------------------------------
|
||||
NOTICE:
|
||||
1)compatible with g++, extern with c;
|
||||
|
||||
@@ -26,23 +26,23 @@ set(CPACK_RPM_COMPONENT_INSTALL ON)
|
||||
set(CPACK_COMPONENTS_IGNORE_GROUPS 1)
|
||||
set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP)
|
||||
set(CPACK_COMPONENT_HEADER_DISPLAY_NAME "develop")
|
||||
set(CPACK_COMPONENT_LIBRARY_REQUIRED TRUE)
|
||||
set(CPACK_COMPONENT_LIBRARIES_REQUIRED TRUE)
|
||||
set(CPACK_COMPONENT_HEADER_REQUIRED TRUE)
|
||||
set(CPACK_RPM_HEADER_PACKAGE_NAME "${MY_RPM_NAME_PREFIX}-devel")
|
||||
set(CPACK_RPM_LIBRARY_PACKAGE_NAME ${MY_RPM_NAME_PREFIX})
|
||||
set(CPACK_RPM_LIBRARIES_PACKAGE_NAME ${MY_RPM_NAME_PREFIX})
|
||||
|
||||
set(CPACK_RPM_FILE_NAME "${CPACK_RPM_LIBRARY_PACKAGE_NAME}-${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_LIBRARY_GROUP "library")
|
||||
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_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_COMPONENT_HEADER_GROUP "header")
|
||||
|
||||
set(CPACK_RPM_HEADER_PACKAGE_REQUIRES_PRE ${CPACK_RPM_LIBRARY_PACKAGE_NAME})
|
||||
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 LIBRARY HEADER)
|
||||
set(CPACK_COMPONENTS_ALL LIBRARIES HEADER)
|
||||
|
||||
|
||||
set(CPACK_BUILD_SOURCE_DIRS "${CMAKE_SOURCE_DIR}")
|
||||
|
||||
@@ -8,22 +8,16 @@
|
||||
#include <unistd.h>
|
||||
#include<sys/stat.h>
|
||||
|
||||
int LOGMSG_MAX_LEN = 4096;
|
||||
int FLUSH_LOG_NUM = 0;
|
||||
typedef struct log_handle_t
|
||||
{
|
||||
int runtime_log_level;
|
||||
int flush_log_count;
|
||||
FILE *fp;
|
||||
pthread_mutex_t mutex;
|
||||
char runtime_log_file[1200];
|
||||
char cur_log_file[4096];
|
||||
} 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_20190218 = 1;
|
||||
|
||||
//const int HANDLE_LOGGER_VERSION_20170816 = 1;
|
||||
#define GIT_VERSION_CATTER(v) __attribute__((__used__)) const char *GIT_VERSION_##v = NULL
|
||||
#define GIT_VERSION_EXPEND(v) GIT_VERSION_CATTER(v)
|
||||
|
||||
@@ -45,6 +39,8 @@ 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"};
|
||||
@@ -52,6 +48,44 @@ static unsigned char weekday_str[7][4] =
|
||||
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)
|
||||
{
|
||||
if(dir_path == NULL)
|
||||
@@ -98,11 +132,6 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
||||
{
|
||||
if(file_path == 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);
|
||||
|
||||
FILE *fp = NULL;
|
||||
log_handle_t *p_handle = NULL;
|
||||
@@ -117,7 +146,7 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
||||
if(NULL == (fp = fopen(file_path, "w")))
|
||||
return NULL;
|
||||
|
||||
//fclose(fp);
|
||||
fclose(fp);
|
||||
//remove(file_path);
|
||||
p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1);
|
||||
|
||||
@@ -127,8 +156,6 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
||||
strncpy(p_handle->runtime_log_file, file_path, 1024);
|
||||
p_handle->runtime_log_file[1024] = '\0';
|
||||
p_handle->runtime_log_level = level;
|
||||
pthread_mutex_init(&p_handle->mutex,NULL);
|
||||
//p_handle->fp = fp;
|
||||
return (void *)p_handle;
|
||||
}
|
||||
|
||||
@@ -136,12 +163,6 @@ void MESA_destroy_runtime_log_handle(void *handle)
|
||||
{
|
||||
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);
|
||||
handle = NULL;
|
||||
}
|
||||
@@ -149,32 +170,6 @@ void MESA_destroy_runtime_log_handle(void *handle)
|
||||
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, ...)
|
||||
{
|
||||
char buf[LOGMSG_MAX_LEN + 1];
|
||||
@@ -183,7 +178,7 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
|
||||
va_list ap;
|
||||
FILE *fp;
|
||||
struct tm local_time;
|
||||
char tmp_log_file_name[1400];
|
||||
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;
|
||||
@@ -192,6 +187,7 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
|
||||
|
||||
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);
|
||||
@@ -238,30 +234,9 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
|
||||
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;
|
||||
if(NULL == (fp = fopen(tmp_log_file_name, "a"))) return;
|
||||
|
||||
fprintf(fp, "%s", buf);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user