2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
2003-05-20 23:37:55 +08:00
|
|
|
#include <ngx_core.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
static void ngx_log_write(ngx_log_t *log, char *errstr, size_t len);
|
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_command_t ngx_errlog_commands[] = {
|
|
|
|
|
|
|
|
{ngx_string("error_log"),
|
2003-12-09 04:48:12 +08:00
|
|
|
NGX_MAIN_CONF|NGX_CONF_1MORE,
|
2003-06-03 23:42:58 +08:00
|
|
|
ngx_set_error_log,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL},
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
static ngx_core_module_t ngx_errlog_module_ctx = {
|
|
|
|
ngx_string("errlog"),
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
ngx_module_t ngx_errlog_module = {
|
|
|
|
NGX_MODULE,
|
2004-04-13 00:38:09 +08:00
|
|
|
&ngx_errlog_module_ctx, /* module context */
|
2003-06-03 23:42:58 +08:00
|
|
|
ngx_errlog_commands, /* module directives */
|
|
|
|
NGX_CORE_MODULE, /* module type */
|
2003-07-11 12:50:59 +08:00
|
|
|
NULL, /* init module */
|
|
|
|
NULL /* init child */
|
2003-06-03 23:42:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-06-26 23:35:36 +08:00
|
|
|
static ngx_open_file_t ngx_stderr;
|
|
|
|
static ngx_log_t ngx_log;
|
2003-06-03 23:42:58 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2003-12-09 04:48:12 +08:00
|
|
|
static const char *debug_levels[] = {
|
2004-02-12 01:08:49 +08:00
|
|
|
"debug_core", "debug_alloc", "debug_event", "debug_http"
|
2003-12-09 04:48:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
#if (HAVE_VARIADIC_MACROS)
|
2004-03-04 15:04:55 +08:00
|
|
|
void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
|
2002-08-07 00:39:45 +08:00
|
|
|
const char *fmt, ...)
|
|
|
|
#else
|
2004-03-04 15:04:55 +08:00
|
|
|
void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
|
2002-08-07 00:39:45 +08:00
|
|
|
const char *fmt, va_list args)
|
|
|
|
#endif
|
|
|
|
{
|
2003-05-20 23:37:55 +08:00
|
|
|
char errstr[MAX_ERROR_STR];
|
2004-01-22 14:47:28 +08:00
|
|
|
size_t len, max;
|
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-07-21 05:15:59 +08:00
|
|
|
if (log->file->fd == NGX_INVALID_FILE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-11-13 14:14:05 +08:00
|
|
|
ngx_memcpy(errstr, ngx_cached_err_log_time.data,
|
|
|
|
ngx_cached_err_log_time.len);
|
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
#if (WIN32)
|
|
|
|
max = MAX_ERROR_STR - 2;
|
|
|
|
#else
|
|
|
|
max = MAX_ERROR_STR - 1;
|
|
|
|
#endif
|
|
|
|
|
2003-11-13 14:14:05 +08:00
|
|
|
len = ngx_cached_err_log_time.len;
|
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
len += ngx_snprintf(errstr + len, max - len, " [%s] ", err_levels[level]);
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
/* pid#tid */
|
2004-01-22 14:47:28 +08:00
|
|
|
len += ngx_snprintf(errstr + len, max - len,
|
2004-02-24 04:57:12 +08:00
|
|
|
PID_T_FMT "#" TID_T_FMT ": ", ngx_log_pid, ngx_log_tid);
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2004-02-12 01:08:49 +08:00
|
|
|
if (log->data && *(int *) log->data != -1) {
|
2004-01-22 14:47:28 +08:00
|
|
|
len += ngx_snprintf(errstr + len, max - len,
|
2004-02-12 01:08:49 +08:00
|
|
|
"*%u ", *(u_int *) log->data);
|
2003-11-20 00:26:41 +08:00
|
|
|
}
|
|
|
|
|
2002-08-26 23:18:19 +08:00
|
|
|
#if (HAVE_VARIADIC_MACROS)
|
2003-11-20 00:26:41 +08:00
|
|
|
|
2002-08-26 23:18:19 +08:00
|
|
|
va_start(args, fmt);
|
2004-01-22 14:47:28 +08:00
|
|
|
len += ngx_vsnprintf(errstr + len, max - len, fmt, args);
|
2002-08-26 23:18:19 +08:00
|
|
|
va_end(args);
|
2003-11-20 00:26:41 +08:00
|
|
|
|
2002-08-26 23:18:19 +08:00
|
|
|
#else
|
2003-11-20 00:26:41 +08:00
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
len += ngx_vsnprintf(errstr + len, max - len, fmt, args);
|
2003-11-20 00:26:41 +08:00
|
|
|
|
2002-08-26 23:18:19 +08:00
|
|
|
#endif
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
if (err) {
|
2003-04-28 23:06:39 +08:00
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
if (len > max - 50) {
|
2004-01-22 00:38:54 +08:00
|
|
|
/* leave a space for an error code */
|
2004-01-22 14:47:28 +08:00
|
|
|
len = max - 50;
|
2004-01-22 00:38:54 +08:00
|
|
|
errstr[len++] = '.';
|
|
|
|
errstr[len++] = '.';
|
|
|
|
errstr[len++] = '.';
|
|
|
|
}
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
#if (WIN32)
|
2003-04-28 23:06:39 +08:00
|
|
|
if ((unsigned) err >= 0x80000000) {
|
2004-01-22 14:47:28 +08:00
|
|
|
len += ngx_snprintf(errstr + len, max - len, " (%X: ", err);
|
2003-04-28 23:06:39 +08:00
|
|
|
} else {
|
2004-01-22 14:47:28 +08:00
|
|
|
len += ngx_snprintf(errstr + len, max - len, " (%d: ", err);
|
2003-04-28 23:06:39 +08:00
|
|
|
}
|
|
|
|
#else
|
2004-01-22 14:47:28 +08:00
|
|
|
len += ngx_snprintf(errstr + len, max - len, " (%d: ", err);
|
2003-04-28 23:06:39 +08:00
|
|
|
#endif
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
if (len >= max) {
|
|
|
|
ngx_log_write(log, errstr, max);
|
2004-01-22 00:38:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
len += ngx_strerror_r(err, errstr + len, max - len);
|
2004-01-22 00:38:54 +08:00
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
if (len >= max) {
|
|
|
|
ngx_log_write(log, errstr, max);
|
2004-01-22 00:38:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
errstr[len++] = ')';
|
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
if (len >= max) {
|
|
|
|
ngx_log_write(log, errstr, max);
|
2004-01-22 00:38:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2004-01-22 14:47:28 +08:00
|
|
|
if (len >= max) {
|
|
|
|
ngx_log_write(log, errstr, max);
|
2004-01-22 00:38:54 +08:00
|
|
|
return;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
if (level != NGX_LOG_DEBUG && log->handler) {
|
2004-01-22 14:47:28 +08:00
|
|
|
len += log->handler(log->data, errstr + len, max - len);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
if (len >= max) {
|
|
|
|
len = max;
|
2004-01-22 00:38:54 +08:00
|
|
|
}
|
2002-12-23 14:29:22 +08:00
|
|
|
}
|
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
ngx_log_write(log, errstr, len);
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
2003-05-20 23:37:55 +08:00
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
static void ngx_log_write(ngx_log_t *log, char *errstr, size_t len)
|
2004-01-22 00:38:54 +08:00
|
|
|
{
|
|
|
|
#if (WIN32)
|
|
|
|
u_long written;
|
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
errstr[len++] = CR;
|
|
|
|
errstr[len++] = LF;
|
|
|
|
WriteFile(log->file->fd, errstr, len, &written, NULL);
|
2004-01-22 00:38:54 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
errstr[len++] = LF;
|
|
|
|
write(log->file->fd, errstr, len);
|
2004-01-22 00:38:54 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
#if !(HAVE_VARIADIC_MACROS)
|
|
|
|
|
2004-03-04 15:04:55 +08:00
|
|
|
void ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
|
2002-08-07 00:39:45 +08:00
|
|
|
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
|
|
|
|
2003-12-01 04:03:18 +08:00
|
|
|
void ngx_log_debug_core(ngx_log_t *log, ngx_err_t err, const char *fmt, ...)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2003-12-01 04:03:18 +08:00
|
|
|
ngx_log_error_core(NGX_LOG_DEBUG, log, err, fmt, args);
|
2002-08-07 00:39:45 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ngx_log_t *ngx_log_init_errlog()
|
|
|
|
{
|
|
|
|
#if (WIN32)
|
2003-06-16 02:32:13 +08:00
|
|
|
|
2003-06-26 23:35:36 +08:00
|
|
|
ngx_stderr.fd = GetStdHandle(STD_ERROR_HANDLE);
|
2003-06-03 23:42:58 +08:00
|
|
|
|
2003-06-26 23:35:36 +08:00
|
|
|
if (ngx_stderr.fd == NGX_INVALID_FILE) {
|
2003-12-06 01:07:27 +08:00
|
|
|
/* TODO: where can we log error ? */
|
2003-06-03 23:42:58 +08:00
|
|
|
return NULL;
|
|
|
|
|
2003-06-26 23:35:36 +08:00
|
|
|
} else if (ngx_stderr.fd == NULL) {
|
2003-12-06 01:07:27 +08:00
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
/* there are no associated standard handles */
|
2003-12-06 01:07:27 +08:00
|
|
|
|
|
|
|
/* TODO: where can we can log possible errors ? */
|
2003-07-21 05:15:59 +08:00
|
|
|
|
|
|
|
ngx_stderr.fd = NGX_INVALID_FILE;
|
2003-06-03 23:42:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2003-06-16 02:32:13 +08:00
|
|
|
|
2003-06-26 23:35:36 +08:00
|
|
|
ngx_stderr.fd = STDERR_FILENO;
|
2003-06-16 02:32:13 +08:00
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
#endif
|
|
|
|
|
2003-06-26 23:35:36 +08:00
|
|
|
ngx_log.file = &ngx_stderr;
|
2003-06-03 23:42:58 +08:00
|
|
|
ngx_log.log_level = NGX_LOG_INFO;
|
2003-12-06 01:07:27 +08:00
|
|
|
|
|
|
|
#if 0
|
2003-06-03 23:42:58 +08:00
|
|
|
/* STUB */ ngx_log.log_level = NGX_LOG_DEBUG;
|
2003-12-06 01:07:27 +08:00
|
|
|
#endif
|
2003-06-03 23:42:58 +08:00
|
|
|
|
|
|
|
return &ngx_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-09 04:48:12 +08:00
|
|
|
ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_array_t *args)
|
2003-07-02 22:41:17 +08:00
|
|
|
{
|
|
|
|
ngx_log_t *log;
|
2003-12-09 04:48:12 +08:00
|
|
|
ngx_str_t *value, *name;
|
|
|
|
|
|
|
|
if (args) {
|
|
|
|
value = args->elts;
|
|
|
|
name = &value[1];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
name = NULL;
|
|
|
|
}
|
2003-07-02 22:41:17 +08:00
|
|
|
|
|
|
|
ngx_test_null(log, ngx_pcalloc(cycle->pool, sizeof(ngx_log_t)), NULL);
|
2003-07-21 05:15:59 +08:00
|
|
|
ngx_test_null(log->file, ngx_conf_open_file(cycle, name), NULL);
|
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
#if 0
|
2004-01-15 02:19:42 +08:00
|
|
|
/* STUB */ log->log_level = NGX_LOG_DEBUG | NGX_LOG_DEBUG_CORE | NGX_LOG_DEBUG_ALLOC | NGX_LOG_DEBUG_EVENT | NGX_LOG_DEBUG_HTTP;
|
2003-12-06 01:07:27 +08:00
|
|
|
#endif
|
2003-07-02 22:41:17 +08:00
|
|
|
|
|
|
|
return log;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-21 04:40:08 +08:00
|
|
|
char *ngx_set_error_log_levels(ngx_conf_t *cf, ngx_log_t *log)
|
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
ngx_uint_t i, n, d;
|
|
|
|
ngx_str_t *value;
|
2004-01-21 04:40:08 +08:00
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
2003-12-09 04:48:12 +08:00
|
|
|
for (i = 2; i < cf->args->nelts; i++) {
|
|
|
|
|
2004-02-12 01:08:49 +08:00
|
|
|
for (n = 1; n <= NGX_LOG_DEBUG; n++) {
|
2003-12-09 04:48:12 +08:00
|
|
|
if (ngx_strcmp(value[i].data, err_levels[n]) == 0) {
|
|
|
|
|
2004-01-21 04:40:08 +08:00
|
|
|
if (log->log_level != 0) {
|
2003-12-09 04:48:12 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid log level \"%s\"",
|
|
|
|
value[i].data);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-01-21 04:40:08 +08:00
|
|
|
log->log_level = n;
|
2003-12-09 04:48:12 +08:00
|
|
|
continue;
|
2003-12-06 01:07:27 +08:00
|
|
|
}
|
|
|
|
}
|
2003-12-09 04:48:12 +08:00
|
|
|
|
2004-01-22 14:47:28 +08:00
|
|
|
for (n = 0, d = NGX_LOG_DEBUG_FIRST; d <= NGX_LOG_DEBUG_LAST; d <<= 1) {
|
|
|
|
if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) {
|
2004-01-21 04:40:08 +08:00
|
|
|
if (log->log_level & ~NGX_LOG_DEBUG_ALL) {
|
2003-12-09 04:48:12 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid log level \"%s\"",
|
|
|
|
value[i].data);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-01-21 04:40:08 +08:00
|
|
|
log->log_level |= d;
|
2003-12-09 04:48:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-21 04:40:08 +08:00
|
|
|
if (log->log_level == 0) {
|
2003-12-06 01:07:27 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
2003-12-09 04:48:12 +08:00
|
|
|
"invalid log level \"%s\"", value[i].data);
|
2003-12-06 01:07:27 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2003-06-03 23:42:58 +08:00
|
|
|
}
|
|
|
|
|
2004-02-12 01:08:49 +08:00
|
|
|
if (log->log_level == NGX_LOG_DEBUG) {
|
|
|
|
log->log_level = NGX_LOG_DEBUG_ALL;
|
|
|
|
}
|
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
2004-04-15 23:34:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
|
|
|
ngx_str_t *value;
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
|
|
|
if (value[1].len == 6 && ngx_strcmp(value[1].data, "stderr") == 0) {
|
|
|
|
cf->cycle->new_log->file = &ngx_stderr;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
cf->cycle->new_log->file->name = value[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ngx_set_error_log_levels(cf, cf->cycle->new_log);
|
|
|
|
}
|