Compare commits
2 Commits
v1.0.10-re
...
rel-1.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8825da8881 | ||
|
|
7827bb0408 |
@@ -4,10 +4,12 @@ set(lib_name MESA_handle_logger)
|
|||||||
|
|
||||||
project (${lib_name})
|
project (${lib_name})
|
||||||
|
|
||||||
|
set(LIB_MAJOR_VERSION 1)
|
||||||
|
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)
|
||||||
|
|
||||||
#SET(CMAKE_C_COMPILER "/usr/bin/g++")
|
|
||||||
set(CMAKE_MACOSX_RPATH 0)
|
set(CMAKE_MACOSX_RPATH 0)
|
||||||
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
|
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
|
||||||
set(CMAKE_INSTALL_PREFIX /opt/MESA)
|
set(CMAKE_INSTALL_PREFIX /opt/MESA)
|
||||||
@@ -32,21 +34,21 @@ endif()
|
|||||||
|
|
||||||
include_directories(${PROJECT_SOURCE_DIR}/inc/)
|
include_directories(${PROJECT_SOURCE_DIR}/inc/)
|
||||||
|
|
||||||
file(GLOB SRC
|
|
||||||
"src/*.c"
|
|
||||||
"src/*.cpp"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Shared Library Output
|
# Shared Library Output
|
||||||
add_library(${lib_name}_shared SHARED ${SRC})
|
add_library(${lib_name}_shared SHARED src/MESA_handle_logger.c)
|
||||||
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 SOVERSION ${LIB_MAJOR_VERSION})
|
||||||
|
|
||||||
|
|
||||||
# static Library Output
|
# static Library Output
|
||||||
add_library(${lib_name}_static STATIC ${SRC})
|
add_library(${lib_name}_static STATIC src/MESA_handle_logger.c)
|
||||||
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)
|
||||||
@@ -56,12 +58,7 @@ install(FILES inc/${lib_name}.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/ME
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
file(GLOB DEMO
|
add_executable(${lib_name}_demo demo/test_handle_logger.c)
|
||||||
"demo/*.c"
|
|
||||||
"demo/*.cpp"
|
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(${lib_name}_demo ${DEMO})
|
|
||||||
target_link_libraries(${lib_name}_demo rt pthread ${lib_name}_static)
|
target_link_libraries(${lib_name}_demo rt pthread ${lib_name}_static)
|
||||||
|
|
||||||
include(Package)
|
include(Package)
|
||||||
|
|||||||
9
README
9
README
@@ -4,13 +4,16 @@
|
|||||||
*Author:yangwei@iie.ac.cn
|
*Author:yangwei@iie.ac.cn
|
||||||
*History:
|
*History:
|
||||||
2014-03-24 created by yw
|
2014-03-24 created by yw
|
||||||
1)first version
|
1)第一个动态库版本
|
||||||
|
|
||||||
2014-04-16 modified by yw
|
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
|
2014-05-29 update by yw
|
||||||
1)create directory is supported
|
1)支持创建用户指定的日志目录
|
||||||
|
|
||||||
|
2019-2-18 update by yw
|
||||||
|
1)性能优化,句柄中存储文件fp,定期flush至文件
|
||||||
-------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------
|
||||||
NOTICE:
|
NOTICE:
|
||||||
1)compatible with g++, extern with c;
|
1)compatible with g++, extern with c;
|
||||||
|
|||||||
@@ -8,17 +8,23 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include<sys/stat.h>
|
#include<sys/stat.h>
|
||||||
|
|
||||||
|
int LOGMSG_MAX_LEN = 4096;
|
||||||
|
int FLUSH_LOG_NUM = 0;
|
||||||
typedef struct log_handle_t
|
typedef struct log_handle_t
|
||||||
{
|
{
|
||||||
int runtime_log_level;
|
int runtime_log_level;
|
||||||
|
int flush_log_count;
|
||||||
|
FILE *fp;
|
||||||
|
pthread_mutex_t mutex;
|
||||||
char runtime_log_file[1200];
|
char runtime_log_file[1200];
|
||||||
|
char cur_log_file[4096];
|
||||||
} log_handle_t;
|
} 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;
|
|
||||||
#define GIT_VERSION_CATTER(v) __attribute__((__used__)) const char *GIT_VERSION_##v = NULL
|
//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)
|
#define GIT_VERSION_EXPEND(v) GIT_VERSION_CATTER(v)
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@@ -28,9 +34,9 @@ extern "C"
|
|||||||
|
|
||||||
/* VERSION TAG */
|
/* VERSION TAG */
|
||||||
#ifdef GIT_VERSION
|
#ifdef GIT_VERSION
|
||||||
GIT_VERSION_EXPEND(GIT_VERSION);
|
GIT_VERSION_EXPEND(GIT_VERSION);
|
||||||
#else
|
#else
|
||||||
static __attribute__((__used__)) const char *GIT_VERSION_UNKNOWN = NULL;
|
static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL;
|
||||||
#endif
|
#endif
|
||||||
#undef GIT_VERSION_CATTER
|
#undef GIT_VERSION_CATTER
|
||||||
#undef GIT_VERSION_EXPEND
|
#undef GIT_VERSION_EXPEND
|
||||||
@@ -39,8 +45,6 @@ static __attribute__((__used__)) const char *GIT_VERSION_UNKNOWN = NULL;
|
|||||||
}
|
}
|
||||||
#endif
|
#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] =
|
static unsigned char weekday_str[7][4] =
|
||||||
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
||||||
@@ -48,44 +52,6 @@ static unsigned char weekday_str[7][4] =
|
|||||||
static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr",
|
static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr",
|
||||||
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
"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)
|
static int create_dir(const char *dir_path, int path_len)
|
||||||
{
|
{
|
||||||
if(dir_path == NULL)
|
if(dir_path == NULL)
|
||||||
@@ -132,7 +98,12 @@ 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);
|
||||||
|
|
||||||
FILE *fp = NULL;
|
FILE *fp = NULL;
|
||||||
log_handle_t *p_handle = NULL;
|
log_handle_t *p_handle = NULL;
|
||||||
|
|
||||||
@@ -146,7 +117,7 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
|||||||
if(NULL == (fp = fopen(file_path, "w")))
|
if(NULL == (fp = fopen(file_path, "w")))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
fclose(fp);
|
//fclose(fp);
|
||||||
//remove(file_path);
|
//remove(file_path);
|
||||||
p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1);
|
p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1);
|
||||||
|
|
||||||
@@ -156,6 +127,8 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
|||||||
strncpy(p_handle->runtime_log_file, file_path, 1024);
|
strncpy(p_handle->runtime_log_file, file_path, 1024);
|
||||||
p_handle->runtime_log_file[1024] = '\0';
|
p_handle->runtime_log_file[1024] = '\0';
|
||||||
p_handle->runtime_log_level = level;
|
p_handle->runtime_log_level = level;
|
||||||
|
pthread_mutex_init(&p_handle->mutex,NULL);
|
||||||
|
//p_handle->fp = fp;
|
||||||
return (void *)p_handle;
|
return (void *)p_handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,13 +136,45 @@ void MESA_destroy_runtime_log_handle(void *handle)
|
|||||||
{
|
{
|
||||||
if(handle != NULL)
|
if(handle != NULL)
|
||||||
{
|
{
|
||||||
free(handle);
|
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;
|
handle = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
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];
|
char buf[LOGMSG_MAX_LEN + 1];
|
||||||
@@ -178,7 +183,7 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
|
|||||||
va_list ap;
|
va_list ap;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
struct tm local_time;
|
struct tm local_time;
|
||||||
char tmp_log_file_name[1201];
|
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;
|
||||||
@@ -187,7 +192,6 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
|
|||||||
|
|
||||||
time(&t);
|
time(&t);
|
||||||
if(NULL == (localtime_r(&t, &local_time))) return;
|
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],
|
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);
|
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);
|
//len = strlen(buf);
|
||||||
@@ -234,9 +238,30 @@ 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_year + 1900, local_time.tm_mon + 1,
|
||||||
local_time.tm_mday);
|
local_time.tm_mday);
|
||||||
|
|
||||||
if(NULL == (fp = fopen(tmp_log_file_name, "a"))) return;
|
if(p_handle->fp == NULL)
|
||||||
|
{
|
||||||
fprintf(fp, "%s", buf);
|
if(0 != MESA_handle_open_file(tmp_log_file_name,p_handle))return;
|
||||||
fclose(fp);
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user