Compare commits
1 Commits
rel-1.0
...
v1.1.2-rel
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7827bb0408 |
@@ -4,6 +4,9 @@ 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)
|
||||||
|
|
||||||
@@ -37,6 +40,7 @@ file(GLOB SRC
|
|||||||
"src/*.cpp"
|
"src/*.cpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Shared Library Output
|
# Shared Library Output
|
||||||
add_library(${lib_name}_shared SHARED ${SRC})
|
add_library(${lib_name}_shared SHARED ${SRC})
|
||||||
if(DEFINED MESA_SHARED_INSTALL_DIR)
|
if(DEFINED MESA_SHARED_INSTALL_DIR)
|
||||||
@@ -45,13 +49,17 @@ 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})
|
||||||
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(TARGETS ${lib_name}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARIES)
|
install(TARGETS ${lib_name}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARY)
|
||||||
install(FILES inc/${lib_name}.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT HEADER)
|
install(FILES inc/${lib_name}.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT HEADER)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,23 +26,23 @@ 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_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)
|
set(CPACK_COMPONENTS_ALL LIBRARY HEADER)
|
||||||
|
|
||||||
|
|
||||||
set(CPACK_BUILD_SOURCE_DIRS "${CMAKE_SOURCE_DIR}")
|
set(CPACK_BUILD_SOURCE_DIRS "${CMAKE_SOURCE_DIR}")
|
||||||
|
|||||||
@@ -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,6 +98,11 @@ 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,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;
|
||||||
}
|
}
|
||||||
@@ -170,6 +149,32 @@ 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];
|
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