From ef8d758a3c95f04a47c06b4337ebdd549220c1b2 Mon Sep 17 00:00:00 2001 From: yuqing Date: Tue, 1 Mar 2016 16:00:29 +0800 Subject: [PATCH] support compress log file in new thread --- HISTORY | 2 +- src/logger.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++------ src/logger.h | 20 +++++++++++------- 3 files changed, 66 insertions(+), 14 deletions(-) diff --git a/HISTORY b/HISTORY index 71c4796..512a15f 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,4 @@ -Version 1.25 2016-02-29 +Version 1.25 2016-03-01 * php7_ext_wrapper.h add more macro defines * compile passed in FreeBSD 10.2 * bugfixed: free task point correctly in free_queue_destroy diff --git a/src/logger.c b/src/logger.c index 3eda9f7..5f63eea 100644 --- a/src/logger.c +++ b/src/logger.c @@ -31,6 +31,9 @@ #define LOG_BUFF_SIZE 64 * 1024 +#define NEED_COMPRESS_LOG(flags) ((flags & LOG_COMPRESS_FLAGS_ENABLED) != 0) +#define COMPRESS_IN_NEW_THREAD(flags) ((flags & LOG_COMPRESS_FLAGS_NEW_THREAD) != 0) + LogContext g_log_context = {LOG_INFO, STDERR_FILENO, NULL}; static int log_fsync(LogContext *pContext, const bool bNeedLock); @@ -288,9 +291,9 @@ void log_take_over_stdout_ex(LogContext *pContext) pContext->take_over_stdout = true; } -void log_set_compress_log_flag_ex(LogContext *pContext, const bool compress_log_flag) +void log_set_compress_log_flags_ex(LogContext *pContext, const short compress_log_flags) { - pContext->compress_log_flag = compress_log_flag; + pContext->compress_log_flags = compress_log_flags; } void log_set_fd_flags(LogContext *pContext, const int flags) @@ -343,7 +346,7 @@ static int log_delete_old_file(LogContext *pContext, const char *old_filename) { char full_filename[MAX_PATH_SIZE + 128]; - if (pContext->compress_log_flag) + if (NEED_COMPRESS_LOG(pContext->compress_log_flags)) { snprintf(full_filename, sizeof(full_filename), "%s.gz", old_filename); } @@ -532,8 +535,9 @@ int log_delete_old_files(void *args) } } -static void log_gzip(const char *filename) +static void* log_gzip_func(void *args) { + char *filename; char *gzip; char cmd[MAX_PATH_SIZE + 128]; @@ -550,8 +554,50 @@ static void log_gzip(const char *filename) gzip = "gzip"; } + filename = (char *)args; snprintf(cmd, sizeof(cmd), "%s %s", gzip, filename); + free(args); + system(cmd); + return NULL; +} + +static void log_gzip(LogContext *pContext, const char *filename) +{ + char *new_filename; + + new_filename = strdup(filename); + if (new_filename == NULL) + { + fprintf(stderr, "file: "__FILE__", line: %d, " + "strdup %d bytes fail", __LINE__, (int)strlen(filename)); + return; + } + + if (COMPRESS_IN_NEW_THREAD(pContext->compress_log_flags)) + { + int result; + pthread_t tid; + pthread_attr_t thread_attr; + + if ((result=init_pthread_attr(&thread_attr, 0) != 0)) + { + return; + } + if ((result=pthread_create(&tid, &thread_attr, + log_gzip_func, new_filename)) != 0) + { + fprintf(stderr, "file: "__FILE__", line: %d, " \ + "create thread failed, " \ + "errno: %d, error info: %s", \ + __LINE__, result, STRERROR(result)); + } + pthread_attr_destroy(&thread_attr); + } + else + { + log_gzip_func(new_filename); + } } int log_rotate(LogContext *pContext) @@ -609,9 +655,9 @@ int log_rotate(LogContext *pContext) result = log_open(pContext); - if (exist && pContext->compress_log_flag) + if (exist && NEED_COMPRESS_LOG(pContext->compress_log_flags)) { - log_gzip(old_filename); + log_gzip(pContext, old_filename); } return result; diff --git a/src/logger.h b/src/logger.h index 12b0293..2e0f5bd 100644 --- a/src/logger.h +++ b/src/logger.h @@ -18,10 +18,16 @@ extern "C" { #endif +//log time precision #define LOG_TIME_PRECISION_SECOND 's' //second #define LOG_TIME_PRECISION_MSECOND 'm' //millisecond #define LOG_TIME_PRECISION_USSECOND 'u' //microsecond +//log compress flags +#define LOG_COMPRESS_FLAGS_NONE 0 +#define LOG_COMPRESS_FLAGS_ENABLED 1 +#define LOG_COMPRESS_FLAGS_NEW_THREAD 2 + struct log_context; //log header line callback @@ -67,12 +73,12 @@ typedef struct log_context /* if stdout to the log file */ bool take_over_stdout; - /* if compress the log file use gzip command */ - bool compress_log_flag; - /* time precision */ char time_precision; + /* compress the log file use gzip command */ + short compress_log_flags; + /* save the log filename */ char log_filename[MAX_PATH_SIZE]; @@ -118,8 +124,8 @@ int log_init2(); #define log_take_over_stderr() log_take_over_stderr_ex(&g_log_context) #define log_take_over_stdout() log_take_over_stdout_ex(&g_log_context) -#define log_set_compress_log_flag(compress_log_flag) \ - log_set_compress_log_flag_ex(&g_log_context, compress_log_flag) +#define log_set_compress_log_flags(compress_log_flags) \ + log_set_compress_log_flags_ex(&g_log_context, compress_log_flags) #define log_header(pContext, header, header_len) \ log_it_ex2(pContext, NULL, header, header_len, false, false) @@ -209,10 +215,10 @@ void log_take_over_stderr_ex(LogContext *pContext); */ void log_take_over_stdout_ex(LogContext *pContext); -/** set compress_log_flag to true +/** set compress_log_flags to true * return: none */ -void log_set_compress_log_flag_ex(LogContext *pContext, const bool compress_log_flag); +void log_set_compress_log_flags_ex(LogContext *pContext, const short compress_log_flags); /** set log fd flags * parameters: