diff --git a/php-fastcommon/fastcommon.c b/php-fastcommon/fastcommon.c index 8ea9388..92d2853 100644 --- a/php-fastcommon/fastcommon.c +++ b/php-fastcommon/fastcommon.c @@ -119,12 +119,29 @@ ZEND_RSRC_DTOR_FUNC(id_generator_dtor) } +#define FASTCOMMON_REGISTER_CHAR_STR_CONSTANT(key, c, buff) \ + *(buff) = c; \ + REGISTER_STRING_CONSTANT(key, buff, CONST_CS | CONST_PERSISTENT) + PHP_MINIT_FUNCTION(fastcommon) { - log_init(); + static char buff[16]; + + log_init(); le_consumer = zend_register_list_destructors_ex(id_generator_dtor, NULL, PHP_IDG_RESOURCE_NAME, module_number); - return SUCCESS; + + memset(buff, 0, sizeof(buff)); + FASTCOMMON_REGISTER_CHAR_STR_CONSTANT("FASTCOMMON_LOG_TIME_PRECISION_SECOND", + LOG_TIME_PRECISION_SECOND, buff); + FASTCOMMON_REGISTER_CHAR_STR_CONSTANT("FASTCOMMON_LOG_TIME_PRECISION_MSECOND", + LOG_TIME_PRECISION_MSECOND, buff + 2); + FASTCOMMON_REGISTER_CHAR_STR_CONSTANT("FASTCOMMON_LOG_TIME_PRECISION_USECOND", + LOG_TIME_PRECISION_USECOND, buff + 4); + FASTCOMMON_REGISTER_CHAR_STR_CONSTANT("FASTCOMMON_LOG_TIME_PRECISION_NONE", + LOG_TIME_PRECISION_NONE, buff + 6); + + return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(fastcommon) @@ -835,7 +852,7 @@ static LogContext *fetch_logger_context(const char *filename) return NULL; } -static LogContext *alloc_logger_context(const char *filename) +static LogContext *alloc_logger_context(const char *filename, const int time_precision) { LogContext *ctx; if (logger_array.alloc <= logger_array.count) { @@ -868,19 +885,20 @@ static LogContext *alloc_logger_context(const char *filename) if (log_set_filename_ex(ctx, filename) != 0) { return NULL; } + log_set_time_precision(ctx, time_precision); logger_array.count++; return ctx; } -static LogContext *get_logger_context(const char *filename) +static LogContext *get_logger_context(const char *filename, const int time_precision) { LogContext *ctx; if ((ctx=fetch_logger_context(filename)) != NULL) { return ctx; } - return alloc_logger_context(filename); + return alloc_logger_context(filename, time_precision); } #define _INIT_ZSTRING(z, s, len) \ @@ -933,7 +951,15 @@ ZEND_FUNCTION(fastcommon_error_log) if (message_type == 3 && filename != NULL) { LogContext *ctx; - if ((ctx=get_logger_context(filename)) != NULL) { + int time_precision; + + if (extra_headers == NULL) { + time_precision = LOG_TIME_PRECISION_NONE; + } else { + time_precision = extra_headers[0]; + } + + if ((ctx=get_logger_context(filename, time_precision)) != NULL) { if (msg_len > 0 && message[msg_len - 1] == '\n') { --msg_len; } diff --git a/php-fastcommon/php-fastcommon.spec.in b/php-fastcommon/php-fastcommon.spec.in index f12c97f..13e8680 100644 --- a/php-fastcommon/php-fastcommon.spec.in +++ b/php-fastcommon/php-fastcommon.spec.in @@ -11,8 +11,8 @@ Source: https://github.com/happyfish100/libfastcommon/%{name}-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) -BuildRequires: libfastcommon-devel >= 1.0.30 -Requires: libfastcommon >= 1.0.30 +BuildRequires: libfastcommon-devel >= 1.0.34 +Requires: libfastcommon >= 1.0.34 %description This package provides the php extension for libfastcommon diff --git a/php-fastcommon/test.php b/php-fastcommon/test.php index cf25c12..d05d515 100644 --- a/php-fastcommon/test.php +++ b/php-fastcommon/test.php @@ -63,10 +63,10 @@ fastcommon_id_generator_destroy($handle1); fastcommon_id_generator_destroy($handle2); fastcommon_error_log("this is a test\n", 3, "/tmp/test.log"); -fastcommon_error_log("this is a test11\n", 3, "/tmp/test1.log"); -fastcommon_error_log("this is a test12\n", 3, "/tmp/test1.log"); -fastcommon_error_log("this is a test21\n", 3, "/tmp/test2.log"); -fastcommon_error_log("this is a test22\n", 3, "/tmp/test2.log"); -fastcommon_error_log("this is a test31\n", 3, "/tmp/test3.log"); -fastcommon_error_log("this is a test32\n", 3, "/tmp/test3.log"); -fastcommon_error_log("this is a test33\n", 3, "/tmp/test3.log"); +fastcommon_error_log("this is a test11\n", 3, "/tmp/test1.log", FASTCOMMON_LOG_TIME_PRECISION_MSECOND); +fastcommon_error_log("this is a test12\n", 3, "/tmp/test1.log", FASTCOMMON_LOG_TIME_PRECISION_MSECOND); +fastcommon_error_log("this is a test21\n", 3, "/tmp/test2.log", FASTCOMMON_LOG_TIME_PRECISION_USECOND); +fastcommon_error_log("this is a test22\n", 3, "/tmp/test2.log", FASTCOMMON_LOG_TIME_PRECISION_USECOND); +fastcommon_error_log("this is a test31\n", 3, "/tmp/test3.log", FASTCOMMON_LOG_TIME_PRECISION_SECOND); +fastcommon_error_log("this is a test32\n", 3, "/tmp/test3.log", FASTCOMMON_LOG_TIME_PRECISION_SECOND); +fastcommon_error_log("this is a test33\n", 3, "/tmp/test3.log", FASTCOMMON_LOG_TIME_PRECISION_SECOND); diff --git a/src/logger.c b/src/logger.c index 5c9e2b8..0f8a6b8 100644 --- a/src/logger.c +++ b/src/logger.c @@ -940,7 +940,8 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \ int buff_len; int result; - if (pContext->time_precision == LOG_TIME_PRECISION_SECOND) + if ((pContext->time_precision == LOG_TIME_PRECISION_SECOND) + || (pContext->time_precision == LOG_TIME_PRECISION_NONE)) { time_fragment = 0; } @@ -956,7 +957,6 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \ } } - localtime_r(&tv->tv_sec, &tm); if (bNeedLock && (result=pthread_mutex_lock(&pContext->log_thread_lock)) != 0) { fprintf(stderr, "file: "__FILE__", line: %d, " \ @@ -983,21 +983,25 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \ log_fsync(pContext, false); } - if (pContext->time_precision == LOG_TIME_PRECISION_SECOND) - { - buff_len = sprintf(pContext->pcurrent_buff, \ - "[%04d-%02d-%02d %02d:%02d:%02d] ", \ - tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, \ - tm.tm_hour, tm.tm_min, tm.tm_sec); - } - else - { - buff_len = sprintf(pContext->pcurrent_buff, \ - "[%04d-%02d-%02d %02d:%02d:%02d.%03d] ", \ - tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, \ - tm.tm_hour, tm.tm_min, tm.tm_sec, time_fragment); - } - pContext->pcurrent_buff += buff_len; + if (pContext->time_precision != LOG_TIME_PRECISION_NONE) + { + localtime_r(&tv->tv_sec, &tm); + if (pContext->time_precision == LOG_TIME_PRECISION_SECOND) + { + buff_len = sprintf(pContext->pcurrent_buff, \ + "[%04d-%02d-%02d %02d:%02d:%02d] ", \ + tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, \ + tm.tm_hour, tm.tm_min, tm.tm_sec); + } + else + { + buff_len = sprintf(pContext->pcurrent_buff, \ + "[%04d-%02d-%02d %02d:%02d:%02d.%03d] ", \ + tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, \ + tm.tm_hour, tm.tm_min, tm.tm_sec, time_fragment); + } + pContext->pcurrent_buff += buff_len; + } if (caption != NULL) { @@ -1033,7 +1037,7 @@ void log_it_ex2(LogContext *pContext, const char *caption, \ tv.tv_sec = get_current_time(); tv.tv_usec = 0; } - else + else if (pContext->time_precision != LOG_TIME_PRECISION_NONE) { gettimeofday(&tv, NULL); } diff --git a/src/logger.h b/src/logger.h index de6d97f..d227095 100644 --- a/src/logger.h +++ b/src/logger.h @@ -21,7 +21,8 @@ extern "C" { //log time precision #define LOG_TIME_PRECISION_SECOND 's' //second #define LOG_TIME_PRECISION_MSECOND 'm' //millisecond -#define LOG_TIME_PRECISION_USSECOND 'u' //microsecond +#define LOG_TIME_PRECISION_USECOND 'u' //microsecond +#define LOG_TIME_PRECISION_NONE '0' //do NOT output timestamp //log compress flags #define LOG_COMPRESS_FLAGS_NONE 0