From ef019ab04aef3333cc0ea4cccbb7ba940f40e816 Mon Sep 17 00:00:00 2001 From: yuqing Date: Sun, 13 Nov 2016 14:22:12 +0800 Subject: [PATCH] logger context add field: use_file_write_lock --- HISTORY | 1 + src/logger.c | 40 +++++++++++++++++++++++++++---------- src/logger.h | 14 +++++++++++++ src/tests/test_ini_parser.c | 9 +++++++++ 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/HISTORY b/HISTORY index ce3b983..2dbf9b0 100644 --- a/HISTORY +++ b/HISTORY @@ -3,6 +3,7 @@ Version 1.31 2016-11-13 * move SET_SOCKOPT_NOSIGPIPE from sockopt.c to sockopt.h * add function get_time_item_from_str * add file trylock functions + * logger context add field: use_file_write_lock Version 1.30 2016-10-31 * modify php-fastcommon/test.php diff --git a/src/logger.c b/src/logger.c index 2e38318..5c9e2b8 100644 --- a/src/logger.c +++ b/src/logger.c @@ -114,33 +114,40 @@ static int log_print_header(LogContext *pContext) { int result; - if ((result=file_write_lock(pContext->log_fd)) != 0) + if (!pContext->use_file_write_lock) { - return result; + if ((result=file_write_lock(pContext->log_fd)) != 0) + { + return result; + } } pContext->current_size = lseek(pContext->log_fd, 0, SEEK_END); if (pContext->current_size < 0) { result = errno != 0 ? errno : EACCES; - file_unlock(pContext->log_fd); - fprintf(stderr, "lseek file \"%s\" fail, " \ "errno: %d, error info: %s\n", \ pContext->log_filename, result, STRERROR(result)); - return result; } - if (pContext->current_size == 0) - { - pContext->print_header_callback(pContext); + else { + result = 0; + if (pContext->current_size == 0) { + pContext->print_header_callback(pContext); + } } - file_unlock(pContext->log_fd); - return 0; + if (!pContext->use_file_write_lock) + { + file_unlock(pContext->log_fd); + } + + return result; } static int log_open(LogContext *pContext) { + int result; if ((pContext->log_fd = open(pContext->log_filename, O_WRONLY | \ O_CREAT | O_APPEND | pContext->fd_flags, 0644)) < 0) { @@ -151,6 +158,14 @@ static int log_open(LogContext *pContext) return errno != 0 ? errno : EACCES; } + if (pContext->use_file_write_lock) { + if ((result=file_try_write_lock(pContext->log_fd)) != 0) { + close(pContext->log_fd); + pContext->log_fd = STDERR_FILENO; + return result; + } + } + if (pContext->take_over_stderr) { if (dup2(pContext->log_fd, STDERR_FILENO) < 0) { fprintf(stderr, "file: "__FILE__", line: %d, " @@ -229,6 +244,11 @@ void log_set_cache_ex(LogContext *pContext, const bool bLogCache) pContext->log_to_cache = bLogCache; } +void log_set_use_file_write_lock_ex(LogContext *pContext, const bool use_lock) +{ + pContext->use_file_write_lock = use_lock; +} + void log_set_time_precision(LogContext *pContext, const int time_precision) { pContext->time_precision = time_precision; diff --git a/src/logger.h b/src/logger.h index 6705519..de6d97f 100644 --- a/src/logger.h +++ b/src/logger.h @@ -76,6 +76,9 @@ typedef struct log_context /* time precision */ char time_precision; + /* if use file write lock */ + bool use_file_write_lock; + /* compress the log file use gzip command */ short compress_log_flags; @@ -134,6 +137,9 @@ int log_init2(); #define log_set_compress_log_days_before(days_before) \ log_set_compress_log_days_before_ex(&g_log_context, days_before) +#define log_set_use_file_write_lock(use_lock) \ + log_set_use_file_write_lock_ex(&g_log_context, use_lock) + #define log_header(pContext, header, header_len) \ log_it_ex2(pContext, NULL, header, header_len, false, false) @@ -180,6 +186,14 @@ int log_set_filename_ex(LogContext *pContext, const char *log_filename); */ void log_set_cache_ex(LogContext *pContext, const bool bLogCache); +/** set if use file write lock + * parameters: + * pContext: the log context + * use_lock: true for use write lock, false NOT use write lock + * return: none +*/ +void log_set_use_file_write_lock_ex(LogContext *pContext, const bool use_lock); + /** set time precision * parameters: * pContext: the log context diff --git a/src/tests/test_ini_parser.c b/src/tests/test_ini_parser.c index a603d2e..e5cf587 100644 --- a/src/tests/test_ini_parser.c +++ b/src/tests/test_ini_parser.c @@ -18,6 +18,15 @@ int main(int argc, char *argv[]) log_init(); g_log_context.log_level = LOG_DEBUG; + log_take_over_stderr(); + log_take_over_stdout(); + log_set_compress_log_flags(LOG_COMPRESS_FLAGS_ENABLED | LOG_COMPRESS_FLAGS_NEW_THREAD); + + printf("sizeof(LogContext): %d, time_precision: %d, compress_log_flags: %d, " + "use_file_write_lock: %d\n", (int)sizeof(LogContext), + g_log_context.time_precision, + g_log_context.compress_log_flags, + g_log_context.use_file_write_lock); if ((result=iniLoadFromFile(szFilename, &context)) != 0) {