Skip to content

Commit

Permalink
Add a switch to enable/disable usecs in error log
Browse files Browse the repository at this point in the history
Just to keep compatibility with the existing log parsers
  • Loading branch information
mikhainin committed Nov 11, 2022
1 parent 5e0db9f commit 0ec472a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
15 changes: 12 additions & 3 deletions main/main.c
Expand Up @@ -715,6 +715,7 @@ PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("output_encoding", NULL, PHP_INI_ALL, OnUpdateOutputEncoding, output_encoding, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("error_log", NULL, PHP_INI_ALL, OnUpdateErrorLog, error_log, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("error_log_mode", "0644", PHP_INI_ALL, OnUpdateLong, error_log_mode, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("error_log_usec", "0", PHP_INI_ALL, OnUpdateBool, error_log_usec, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("extension_dir", PHP_EXTENSION_DIR, PHP_INI_SYSTEM, OnUpdateStringUnempty, extension_dir, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("sys_temp_dir", NULL, PHP_INI_SYSTEM, OnUpdateStringUnempty, sys_temp_dir, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("include_path", PHP_INCLUDE_PATH, PHP_INI_ALL, OnUpdateStringUnempty, include_path, php_core_globals, core_globals)
Expand Down Expand Up @@ -832,6 +833,14 @@ PHPAPI ZEND_COLD void php_log_err_with_severity(const char *log_message, int sys
time_t error_time = 0;
suseconds_t error_time_usec = 0;

const char *error_log_format = "d-M-Y H:i:s e";
size_t error_log_format_len = sizeof("d-M-Y H:i:s e") - 1;

if (PG(error_log_usec)) {
error_log_format = "d-M-Y H:i:s.u e";
error_log_format_len = sizeof("d-M-Y H:i:s.u e") - 1;
}

#if HAVE_GETTIMEOFDAY
struct timeval tp = {0}; /* For setting microseconds */

Expand All @@ -844,12 +853,12 @@ PHPAPI ZEND_COLD void php_log_err_with_severity(const char *log_message, int sys

#ifdef ZTS
if (!php_during_module_startup()) {
error_time_str = php_format_timestamp("d-M-Y H:i:s.u e", 15, error_time, error_time_usec, true);
error_time_str = php_format_timestamp(error_log_format, error_log_format_len, error_time, error_time_usec, true);
} else {
error_time_str = php_format_timestamp("d-M-Y H:i:s.u e", 15, error_time, error_time_usec, false);
error_time_str = php_format_timestamp(error_log_format, error_log_format_len, error_time, error_time_usec, false);
}
#else
error_time_str = php_format_timestamp("d-M-Y H:i:s.u e", 15, error_time, error_time_usec, true);
error_time_str = php_format_timestamp(error_log_format, error_log_format_len, error_time, error_time_usec, true);
#endif
len = spprintf(&tmp, 0, "[%s] %s%s", ZSTR_VAL(error_time_str), log_message, PHP_EOL);
#ifdef PHP_WIN32
Expand Down
1 change: 1 addition & 0 deletions main/php_globals.h
Expand Up @@ -166,6 +166,7 @@ struct _php_core_globals {
bool have_called_openlog;
zend_long syslog_filter;
zend_long error_log_mode;
zend_bool error_log_usec;
};


Expand Down
2 changes: 1 addition & 1 deletion tests/basic/errorlog_permission.phpt
Expand Up @@ -35,4 +35,4 @@ try {
--EXPECTF--
got permissions=600
errorlog contents
[%d-%s-%d %d:%d:%d.%d %s] hello world
[%d-%s-%d %d:%d:%d %s] hello world
36 changes: 36 additions & 0 deletions tests/basic/errorlog_usec.phpt
@@ -0,0 +1,36 @@
--TEST--
Check permissions for created errorlog file
--SKIPIF--
<?php
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
die("skip this test on windows");
}
?>
--INI--
error_log=error_usec_test.log
error_log_usec=On
--FILE--
<?php

const LOG_FILENAME='error_usec_test.log';

try {
if (file_exists(LOG_FILENAME)) {
unlink(LOG_FILENAME);
}
$oldMask = umask(0000);

error_log("hello world");

assert(file_exists(LOG_FILENAME));

printf("errorlog contents\n%s", file_get_contents(LOG_FILENAME));

umask($oldMask);
} finally {
unlink(LOG_FILENAME);
}
?>
--EXPECTF--
errorlog contents
[%d-%s-%d %d:%d:%d.%d %s] hello world

0 comments on commit 0ec472a

Please sign in to comment.