logger context add field: use_file_write_lock
parent
8b563ce830
commit
ef019ab04a
1
HISTORY
1
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
|
||||
|
|
|
|||
40
src/logger.c
40
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;
|
||||
|
|
|
|||
14
src/logger.h
14
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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue