add time_precision macro: LOG_TIME_PRECISION_NONE

pull/37/head
yuqing 2017-02-07 15:24:42 +08:00
parent 158aa4aa2c
commit 14b477f034
5 changed files with 65 additions and 34 deletions

View File

@ -119,11 +119,28 @@ 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) PHP_MINIT_FUNCTION(fastcommon)
{ {
static char buff[16];
log_init(); log_init();
le_consumer = zend_register_list_destructors_ex(id_generator_dtor, NULL, le_consumer = zend_register_list_destructors_ex(id_generator_dtor, NULL,
PHP_IDG_RESOURCE_NAME, module_number); PHP_IDG_RESOURCE_NAME, module_number);
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; return SUCCESS;
} }
@ -835,7 +852,7 @@ static LogContext *fetch_logger_context(const char *filename)
return NULL; return NULL;
} }
static LogContext *alloc_logger_context(const char *filename) static LogContext *alloc_logger_context(const char *filename, const int time_precision)
{ {
LogContext *ctx; LogContext *ctx;
if (logger_array.alloc <= logger_array.count) { 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) { if (log_set_filename_ex(ctx, filename) != 0) {
return NULL; return NULL;
} }
log_set_time_precision(ctx, time_precision);
logger_array.count++; logger_array.count++;
return ctx; return ctx;
} }
static LogContext *get_logger_context(const char *filename) static LogContext *get_logger_context(const char *filename, const int time_precision)
{ {
LogContext *ctx; LogContext *ctx;
if ((ctx=fetch_logger_context(filename)) != NULL) { if ((ctx=fetch_logger_context(filename)) != NULL) {
return ctx; return ctx;
} }
return alloc_logger_context(filename); return alloc_logger_context(filename, time_precision);
} }
#define _INIT_ZSTRING(z, s, len) \ #define _INIT_ZSTRING(z, s, len) \
@ -933,7 +951,15 @@ ZEND_FUNCTION(fastcommon_error_log)
if (message_type == 3 && filename != NULL) { if (message_type == 3 && filename != NULL) {
LogContext *ctx; 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') { if (msg_len > 0 && message[msg_len - 1] == '\n') {
--msg_len; --msg_len;
} }

View File

@ -11,8 +11,8 @@ Source: https://github.com/happyfish100/libfastcommon/%{name}-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildRequires: libfastcommon-devel >= 1.0.30 BuildRequires: libfastcommon-devel >= 1.0.34
Requires: libfastcommon >= 1.0.30 Requires: libfastcommon >= 1.0.34
%description %description
This package provides the php extension for libfastcommon This package provides the php extension for libfastcommon

View File

@ -63,10 +63,10 @@ fastcommon_id_generator_destroy($handle1);
fastcommon_id_generator_destroy($handle2); fastcommon_id_generator_destroy($handle2);
fastcommon_error_log("this is a test\n", 3, "/tmp/test.log"); 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 test11\n", 3, "/tmp/test1.log", FASTCOMMON_LOG_TIME_PRECISION_MSECOND);
fastcommon_error_log("this is a test12\n", 3, "/tmp/test1.log"); 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_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_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_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_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_error_log("this is a test33\n", 3, "/tmp/test3.log", FASTCOMMON_LOG_TIME_PRECISION_SECOND);

View File

@ -940,7 +940,8 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \
int buff_len; int buff_len;
int result; 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; 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) if (bNeedLock && (result=pthread_mutex_lock(&pContext->log_thread_lock)) != 0)
{ {
fprintf(stderr, "file: "__FILE__", line: %d, " \ fprintf(stderr, "file: "__FILE__", line: %d, " \
@ -983,6 +983,9 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \
log_fsync(pContext, false); log_fsync(pContext, false);
} }
if (pContext->time_precision != LOG_TIME_PRECISION_NONE)
{
localtime_r(&tv->tv_sec, &tm);
if (pContext->time_precision == LOG_TIME_PRECISION_SECOND) if (pContext->time_precision == LOG_TIME_PRECISION_SECOND)
{ {
buff_len = sprintf(pContext->pcurrent_buff, \ buff_len = sprintf(pContext->pcurrent_buff, \
@ -998,6 +1001,7 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \
tm.tm_hour, tm.tm_min, tm.tm_sec, time_fragment); tm.tm_hour, tm.tm_min, tm.tm_sec, time_fragment);
} }
pContext->pcurrent_buff += buff_len; pContext->pcurrent_buff += buff_len;
}
if (caption != NULL) if (caption != NULL)
{ {
@ -1033,7 +1037,7 @@ void log_it_ex2(LogContext *pContext, const char *caption, \
tv.tv_sec = get_current_time(); tv.tv_sec = get_current_time();
tv.tv_usec = 0; tv.tv_usec = 0;
} }
else else if (pContext->time_precision != LOG_TIME_PRECISION_NONE)
{ {
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
} }

View File

@ -21,7 +21,8 @@ extern "C" {
//log time precision //log time precision
#define LOG_TIME_PRECISION_SECOND 's' //second #define LOG_TIME_PRECISION_SECOND 's' //second
#define LOG_TIME_PRECISION_MSECOND 'm' //millisecond #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 //log compress flags
#define LOG_COMPRESS_FLAGS_NONE 0 #define LOG_COMPRESS_FLAGS_NONE 0