Avoid locks when appening to the error log file

This commit is contained in:
Ilia Alshanetsky 2007-04-06 13:58:48 +00:00
parent d9adb6715f
commit aa18505f34

View File

@ -27,6 +27,7 @@
#include "php.h" #include "php.h"
#include <stdio.h> #include <stdio.h>
#include <fcntl.h>
#ifdef PHP_WIN32 #ifdef PHP_WIN32
#include "win32/time.h" #include "win32/time.h"
#include "win32/signal.h" #include "win32/signal.h"
@ -59,10 +60,8 @@
#include "ext/standard/php_standard.h" #include "ext/standard/php_standard.h"
#include "php_variables.h" #include "php_variables.h"
#include "ext/standard/credits.h" #include "ext/standard/credits.h"
#include "ext/standard/flock_compat.h"
#ifdef PHP_WIN32 #ifdef PHP_WIN32
#include <io.h> #include <io.h>
#include <fcntl.h>
#include "win32/php_registry.h" #include "win32/php_registry.h"
#endif #endif
#include "php_syslog.h" #include "php_syslog.h"
@ -343,7 +342,7 @@ static int module_shutdown = 0;
*/ */
PHPAPI void php_log_err(char *log_message TSRMLS_DC) PHPAPI void php_log_err(char *log_message TSRMLS_DC)
{ {
FILE *log_file; int fd = -1;
char error_time_str[128]; char error_time_str[128];
struct tm tmbuf; struct tm tmbuf;
time_t error_time; time_t error_time;
@ -356,13 +355,16 @@ PHPAPI void php_log_err(char *log_message TSRMLS_DC)
return; return;
} }
#endif #endif
log_file = VCWD_FOPEN(PG(error_log), "ab"); fd = VCWD_OPEN_MODE(PG(error_log), O_CREAT | O_APPEND | O_WRONLY, 0644);
if (log_file != NULL) { if (fd != -1) {
char *tmp;
int len;
time(&error_time); time(&error_time);
strftime(error_time_str, sizeof(error_time_str), "%d-%b-%Y %H:%M:%S", php_localtime_r(&error_time, &tmbuf)); strftime(error_time_str, sizeof(error_time_str), "%d-%b-%Y %H:%M:%S", php_localtime_r(&error_time, &tmbuf));
php_flock(fileno(log_file), 2); len = spprintf(&tmp, 0, "[%s] %s%s", error_time_str, log_message, PHP_EOL);
fprintf(log_file, "[%s] %s%s", error_time_str, log_message, PHP_EOL); write(fd, tmp, len);
fclose(log_file); efree(tmp);
close(fd);
return; return;
} }
} }