check file size before log access header

pull/2/head
yuqing 2014-09-15 14:22:12 +08:00
parent 5e5b3e84c7
commit aafcd765dc
3 changed files with 30 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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;