2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
TODO: log pid and tid
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2003-04-28 23:06:39 +08:00
|
|
|
"[time as ctime()] [alert] 412#3 (32)Broken pipe: anything"
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
"[time as ctime()] [alert] (32)Broken pipe: anything"
|
|
|
|
"[time as ctime()] [alert] anything"
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
2003-05-20 23:37:55 +08:00
|
|
|
#include <ngx_core.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_str_t errlog_name = ngx_string("errlog");
|
|
|
|
|
|
|
|
static ngx_command_t ngx_errlog_commands[] = {
|
|
|
|
|
|
|
|
{ngx_string("error_log"),
|
|
|
|
NGX_MAIN_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_set_error_log,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL},
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_errlog_module = {
|
|
|
|
NGX_MODULE,
|
|
|
|
&errlog_name, /* module context */
|
|
|
|
ngx_errlog_commands, /* module directives */
|
|
|
|
NGX_CORE_MODULE, /* module type */
|
|
|
|
NULL /* init module */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_log_t ngx_log;
|
|
|
|
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
static const char *err_levels[] = {
|
2003-05-20 23:37:55 +08:00
|
|
|
"stderr", "emerg", "alert", "crit", "error",
|
|
|
|
"warn", "notice", "info", "debug"
|
2002-08-07 00:39:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#if (HAVE_VARIADIC_MACROS)
|
|
|
|
void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err,
|
|
|
|
const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err,
|
|
|
|
const char *fmt, va_list args)
|
|
|
|
#endif
|
|
|
|
{
|
2003-05-20 23:37:55 +08:00
|
|
|
char errstr[MAX_ERROR_STR];
|
|
|
|
ngx_tm_t tm;
|
|
|
|
size_t len;
|
2002-08-07 00:39:45 +08:00
|
|
|
#if (HAVE_VARIADIC_MACROS)
|
2003-05-20 23:37:55 +08:00
|
|
|
va_list args;
|
2002-08-07 00:39:45 +08:00
|
|
|
#endif
|
2003-06-03 23:42:58 +08:00
|
|
|
#if (WIN32)
|
|
|
|
int written;
|
|
|
|
#endif
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
ngx_localtime(&tm);
|
2002-08-26 23:18:19 +08:00
|
|
|
len = ngx_snprintf(errstr, sizeof(errstr), "%4d/%02d/%02d %02d:%02d:%02d",
|
2002-12-16 05:08:04 +08:00
|
|
|
tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday,
|
2002-08-07 00:39:45 +08:00
|
|
|
tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
|
|
|
|
|
2002-08-26 23:18:19 +08:00
|
|
|
len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
|
|
|
|
" [%s] ", err_levels[level]);
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
/* pid#tid */
|
2002-08-26 23:18:19 +08:00
|
|
|
len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
|
2003-06-11 23:28:34 +08:00
|
|
|
PID_FMT "#%d: ", ngx_getpid(), 0);
|
2002-08-26 23:18:19 +08:00
|
|
|
|
|
|
|
#if (HAVE_VARIADIC_MACROS)
|
|
|
|
va_start(args, fmt);
|
|
|
|
len += ngx_vsnprintf(errstr + len, sizeof(errstr) - len - 1, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
#else
|
|
|
|
len += ngx_vsnprintf(errstr + len, sizeof(errstr) - len - 1, fmt, args);
|
|
|
|
#endif
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
if (err) {
|
2003-04-28 23:06:39 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
#if (WIN32)
|
2003-04-28 23:06:39 +08:00
|
|
|
if ((unsigned) err >= 0x80000000) {
|
2002-08-07 00:39:45 +08:00
|
|
|
len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
|
2002-12-23 14:29:22 +08:00
|
|
|
" (%X: ", err);
|
2003-04-28 23:06:39 +08:00
|
|
|
} else {
|
2002-08-07 00:39:45 +08:00
|
|
|
len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
|
2002-12-23 14:29:22 +08:00
|
|
|
" (%d: ", err);
|
2003-04-28 23:06:39 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
|
|
|
|
" (%d: ", err);
|
|
|
|
#endif
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
len += ngx_strerror_r(err, errstr + len, sizeof(errstr) - len - 1);
|
|
|
|
if (len < sizeof(errstr) - 2) {
|
2002-08-26 23:18:19 +08:00
|
|
|
errstr[len++] = ')';
|
2002-08-07 00:39:45 +08:00
|
|
|
} else {
|
|
|
|
len = sizeof(errstr) - 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
if (level != NGX_LOG_DEBUG && log->handler) {
|
2002-08-26 23:18:19 +08:00
|
|
|
len += log->handler(log->data, errstr + len, sizeof(errstr) - len - 1);
|
2002-12-23 14:29:22 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
if (len > sizeof(errstr) - 2) {
|
2002-08-07 00:39:45 +08:00
|
|
|
len = sizeof(errstr) - 2;
|
2002-12-23 14:29:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if (WIN32)
|
|
|
|
errstr[len++] = '\r';
|
|
|
|
errstr[len++] = '\n';
|
2003-06-03 23:42:58 +08:00
|
|
|
if (log->fd) {
|
|
|
|
WriteFile(log->fd, errstr, len, &written, NULL);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
errstr[len++] = '\n';
|
2003-05-20 23:37:55 +08:00
|
|
|
write(log->fd, errstr, len);
|
2003-06-03 23:42:58 +08:00
|
|
|
#endif
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
#if 0
|
|
|
|
errstr[len] = '\0';
|
2002-08-07 00:39:45 +08:00
|
|
|
fputs(errstr, stderr);
|
2002-09-07 18:14:25 +08:00
|
|
|
fflush(stderr);
|
2002-12-23 14:29:22 +08:00
|
|
|
#endif
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
2003-05-20 23:37:55 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
#if !(HAVE_VARIADIC_MACROS)
|
|
|
|
|
|
|
|
void ngx_log_error(int level, ngx_log_t *log, ngx_err_t err,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
if (log->log_level >= level) {
|
|
|
|
va_start(args, fmt);
|
|
|
|
ngx_log_error_core(level, log, err, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-20 23:37:55 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
void ngx_log_debug_core(ngx_log_t *log, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
ngx_log_error_core(NGX_LOG_DEBUG, log, 0, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2003-05-20 23:37:55 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
void ngx_assert_core(ngx_log_t *log, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
ngx_log_error_core(NGX_LOG_ALERT, log, 0, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2003-05-20 23:37:55 +08:00
|
|
|
|
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
#if 0
|
|
|
|
|
2003-05-20 23:37:55 +08:00
|
|
|
void ngx_log_stderr(ngx_event_t *ev)
|
|
|
|
{
|
|
|
|
char errstr[MAX_ERROR_STR];
|
|
|
|
ssize_t n;
|
|
|
|
ngx_err_t err;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
n = read((ngx_fd_t) ev->data, errstr, sizeof(errstr - 1));
|
|
|
|
|
|
|
|
if (n == -1) {
|
2003-05-21 21:28:21 +08:00
|
|
|
err = ngx_errno;
|
2003-05-20 23:37:55 +08:00
|
|
|
if (err == NGX_EAGAIN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, &ngx_log, err, "read() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, &ngx_log, 0, "stderr clolsed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
errstr[n] = '\0';
|
|
|
|
ngx_log_error(NGX_LOG_STDERR, &ngx_log, 0, "%s", errstr);
|
|
|
|
}
|
|
|
|
}
|
2003-06-03 23:42:58 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
|
|
|
return ngx_log_set_errlog(cf, cmd, &ngx_log);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ngx_log_t *ngx_log_init_errlog()
|
|
|
|
{
|
|
|
|
#if (WIN32)
|
2003-06-16 02:32:13 +08:00
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
ngx_log.fd = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
|
|
|
|
if (ngx_log.fd == NGX_INVALID_FILE) {
|
|
|
|
/* TODO: where we can log error ? */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
} else if (ngx_log.fd == NULL) {
|
|
|
|
/* there are no associated standard handles */
|
|
|
|
/* TODO: where we can log possible errors ? */
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2003-06-16 02:32:13 +08:00
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
ngx_log.fd = STDERR_FILENO;
|
2003-06-16 02:32:13 +08:00
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ngx_log.log_level = NGX_LOG_INFO;
|
|
|
|
/* STUB */ ngx_log.log_level = NGX_LOG_DEBUG;
|
|
|
|
|
|
|
|
return &ngx_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *ngx_log_set_errlog(ngx_conf_t *cf, ngx_command_t *cmd, ngx_log_t *log)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
ngx_err_t err;
|
|
|
|
ngx_str_t *value;
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
|
|
|
log->fd = ngx_open_file(value[1].data,
|
|
|
|
NGX_FILE_RDWR,
|
|
|
|
NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND);
|
|
|
|
|
|
|
|
if (log->fd == NGX_INVALID_FILE) {
|
|
|
|
err = ngx_errno;
|
|
|
|
len = ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1,
|
|
|
|
ngx_open_file_n " \"%s\" failed (%d: ",
|
|
|
|
value[1].data, err);
|
|
|
|
len += ngx_strerror_r(err, ngx_conf_errstr + len,
|
|
|
|
sizeof(ngx_conf_errstr) - len - 1);
|
|
|
|
ngx_conf_errstr[len++] = ')';
|
|
|
|
ngx_conf_errstr[len++] = '\0';
|
|
|
|
return ngx_conf_errstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (WIN32)
|
|
|
|
if (ngx_file_append_mode(log->fd) == NGX_ERROR) {
|
|
|
|
err = ngx_errno;
|
|
|
|
len = ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1,
|
|
|
|
ngx_file_append_mode_n " \"%s\" failed (%d: ",
|
|
|
|
value[1].data, err);
|
|
|
|
len += ngx_strerror_r(err, ngx_conf_errstr + len,
|
|
|
|
sizeof(ngx_conf_errstr) - len - 1);
|
|
|
|
ngx_conf_errstr[len++] = ')';
|
|
|
|
ngx_conf_errstr[len++] = '\0';
|
|
|
|
return ngx_conf_errstr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|