From aafcd765dcf1b4af42532982eb3b024f144ed1da Mon Sep 17 00:00:00 2001 From: yuqing Date: Mon, 15 Sep 2014 14:22:12 +0800 Subject: [PATCH] check file size before log access header --- HISTORY | 3 ++- libfastcommon.spec | 2 +- src/logger.c | 33 +++++++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/HISTORY b/HISTORY index 1f0c097..256bf90 100644 --- a/HISTORY +++ b/HISTORY @@ -1,7 +1,8 @@ -Version 1.07 2014-09-11 +Version 1.07 2014-09-15 * increment alloc task buffer * add function free_queue_alloc_connections + * check file size before log access header Version 1.06 2014-08-27 * update source code from FastDFS V5.02 diff --git a/libfastcommon.spec b/libfastcommon.spec index f3e6605..9807629 100644 --- a/libfastcommon.spec +++ b/libfastcommon.spec @@ -1,5 +1,5 @@ Name: libfastcommon -Version: 1.0.0 +Version: 1.0.7 Release: 1%{?dist} Summary: c common functions library extracted from my open source projects FastDFS License: GPL diff --git a/src/logger.c b/src/logger.c index 3c32f25..e3b90a9 100644 --- a/src/logger.c +++ b/src/logger.c @@ -103,6 +103,24 @@ int log_init_ex(LogContext *pContext) return 0; } +static int log_print_header(LogContext *pContext) +{ + usleep(100 * 1000); + pContext->current_size = lseek(pContext->log_fd, 0, SEEK_END); + if (pContext->current_size < 0) + { + fprintf(stderr, "lseek file \"%s\" fail, " \ + "errno: %d, error info: %s\n", \ + pContext->log_filename, errno, STRERROR(errno)); + return errno != 0 ? errno : EACCES; + } + if (pContext->current_size == 0) + { + pContext->print_header_callback(pContext); + } + return 0; +} + static int log_open(LogContext *pContext) { if ((pContext->log_fd = open(pContext->log_filename, O_WRONLY | \ @@ -141,7 +159,7 @@ static int log_open(LogContext *pContext) } if (pContext->current_size == 0 && pContext->print_header_callback != NULL) { - pContext->print_header_callback(pContext); + log_print_header(pContext); } return 0; @@ -215,12 +233,15 @@ void log_set_header_callback(LogContext *pContext, LogHeaderCallback header_call pContext->print_header_callback = header_callback; if (pContext->print_header_callback != NULL) { + int64_t current_size; + pthread_mutex_lock(&(pContext->log_thread_lock)); - if (pContext->current_size == 0) - { - pContext->print_header_callback(pContext); - } + current_size = pContext->current_size; pthread_mutex_unlock(&(pContext->log_thread_lock)); + if (current_size == 0) + { + log_print_header(pContext); + } } } @@ -429,9 +450,9 @@ static int log_fsync(LogContext *pContext, const bool bNeedLock) __LINE__, lock_res, STRERROR(lock_res)); } + pContext->current_size += write_bytes; if (pContext->rotate_size > 0) { - pContext->current_size += write_bytes; if (pContext->current_size > pContext->rotate_size) { pContext->rotate_immediately = true;