[nginx] Core: support several "error_log" directives.
Homutov Vladimir
vl at nginx.com
Fri Jun 21 12:38:11 UTC 2013
details: http://hg.nginx.org/nginx/rev/7ecaa9e4bf1b
branches:
changeset: 5254:7ecaa9e4bf1b
user: Vladimir Homutov <vl at nginx.com>
date: Thu Jun 20 20:47:39 2013 +0400
description:
Core: support several "error_log" directives.
When several "error_log" directives are specified in the same configuration
block, logs are written to all files with a matching log level.
All logs are stored in the singly-linked list that is sorted by log level in
the descending order.
Specific debug levels (NGX_LOG_DEBUG_HTTP,EVENT, etc.) are not supported
if several "error_log" directives are specified. In this case all logs
will use debug level that has largest absolute value.
diffstat:
src/core/ngx_log.c | 137 ++++++++++++++++++++++++++++-----------
src/core/ngx_log.h | 5 +-
src/http/ngx_http_core_module.c | 28 +-------
src/http/ngx_http_request.h | 1 +
src/os/unix/ngx_process_cycle.c | 2 +
5 files changed, 106 insertions(+), 67 deletions(-)
diffs (288 lines):
diff -r a82f305487c2 -r 7ecaa9e4bf1b src/core/ngx_log.c
--- a/src/core/ngx_log.c Wed Jun 19 08:55:08 2013 +0400
+++ b/src/core/ngx_log.c Thu Jun 20 20:47:39 2013 +0400
@@ -10,6 +10,8 @@
static char *ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
+static char *ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log);
+static void ngx_log_insert(ngx_log_t *log, ngx_log_t *new_log);
static ngx_command_t ngx_errlog_commands[] = {
@@ -86,14 +88,11 @@ ngx_log_error_core(ngx_uint_t level, ngx
#endif
{
#if (NGX_HAVE_VARIADIC_MACROS)
- va_list args;
+ va_list args;
#endif
- u_char *p, *last, *msg;
- u_char errstr[NGX_MAX_ERROR_STR];
-
- if (log->file->fd == NGX_INVALID_FILE) {
- return;
- }
+ u_char *p, *last, *msg;
+ u_char errstr[NGX_MAX_ERROR_STR];
+ ngx_uint_t wrote_stderr, debug_connection;
last = errstr + NGX_MAX_ERROR_STR;
@@ -140,11 +139,27 @@ ngx_log_error_core(ngx_uint_t level, ngx
ngx_linefeed(p);
- (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
+ wrote_stderr = 0;
+ debug_connection = (log->log_level & NGX_LOG_DEBUG_CONNECTION) != 0;
+
+ while (log) {
+
+ if (log->log_level < level && !debug_connection) {
+ break;
+ }
+
+ (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
+
+ if (log->file->fd == ngx_stderr) {
+ wrote_stderr = 1;
+ }
+
+ log = log->next;
+ }
if (!ngx_use_stderr
|| level > NGX_LOG_WARN
- || log->file->fd == ngx_stderr)
+ || wrote_stderr)
{
return;
}
@@ -348,31 +363,17 @@ ngx_log_init(u_char *prefix)
}
-ngx_log_t *
-ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name)
-{
- ngx_log_t *log;
-
- log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
- if (log == NULL) {
- return NULL;
- }
-
- log->file = ngx_conf_open_file(cycle, name);
- if (log->file == NULL) {
- return NULL;
- }
-
- return log;
-}
-
-
-char *
+static char *
ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)
{
ngx_uint_t i, n, d, found;
ngx_str_t *value;
+ if (cf->args->nelts == 2) {
+ log->log_level = NGX_LOG_ERR;
+ return NGX_CONF_OK;
+ }
+
value = cf->args->elts;
for (i = 2; i < cf->args->nelts; i++) {
@@ -428,10 +429,33 @@ ngx_log_set_levels(ngx_conf_t *cf, ngx_l
static char *
ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
+ ngx_log_t *dummy;
+
+ dummy = &cf->cycle->new_log;
+
+ return ngx_log_set_log(cf, &dummy);
+}
+
+
+char *
+ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head)
+{
+ ngx_log_t *new_log;
ngx_str_t *value, name;
- if (cf->cycle->new_log.file) {
- return "is duplicate";
+ if (*head != NULL && (*head)->log_level == 0) {
+ new_log = *head;
+
+ } else {
+
+ new_log = ngx_pcalloc(cf->pool, sizeof(ngx_log_t));
+ if (new_log == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ if (*head == NULL) {
+ *head = new_log;
+ }
}
value = cf->args->elts;
@@ -444,15 +468,52 @@ ngx_error_log(ngx_conf_t *cf, ngx_comman
name = value[1];
}
- cf->cycle->new_log.file = ngx_conf_open_file(cf->cycle, &name);
- if (cf->cycle->new_log.file == NULL) {
- return NULL;
+ new_log->file = ngx_conf_open_file(cf->cycle, &name);
+ if (new_log->file == NULL) {
+ return NGX_CONF_ERROR;
}
- if (cf->args->nelts == 2) {
- cf->cycle->new_log.log_level = NGX_LOG_ERR;
- return NGX_CONF_OK;
+ if (ngx_log_set_levels(cf, new_log) != NGX_CONF_OK) {
+ return NGX_CONF_ERROR;
}
- return ngx_log_set_levels(cf, &cf->cycle->new_log);
+ if (*head != new_log) {
+ ngx_log_insert(*head, new_log);
+ }
+
+ return NGX_CONF_OK;
}
+
+
+static void
+ngx_log_insert(ngx_log_t *log, ngx_log_t *new_log)
+{
+ ngx_log_t tmp;
+
+ if (new_log->log_level > log->log_level) {
+
+ /*
+ * list head address is permanent, insert new log after
+ * head and swap its contents with head
+ */
+
+ tmp = *log;
+ *log = *new_log;
+ *new_log = tmp;
+
+ log->next = new_log;
+ return;
+ }
+
+ while (log->next) {
+ if (new_log->log_level > log->next->log_level) {
+ new_log->next = log->next;
+ log->next = new_log;
+ return;
+ }
+
+ log = log->next;
+ }
+
+ log->next = new_log;
+}
diff -r a82f305487c2 -r 7ecaa9e4bf1b src/core/ngx_log.h
--- a/src/core/ngx_log.h Wed Jun 19 08:55:08 2013 +0400
+++ b/src/core/ngx_log.h Thu Jun 20 20:47:39 2013 +0400
@@ -61,6 +61,8 @@ struct ngx_log_s {
*/
char *action;
+
+ ngx_log_t *next;
};
@@ -220,11 +222,10 @@ void ngx_cdecl ngx_log_debug_core(ngx_lo
/*********************************/
ngx_log_t *ngx_log_init(u_char *prefix);
-ngx_log_t *ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name);
-char *ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log);
void ngx_cdecl ngx_log_abort(ngx_err_t err, const char *fmt, ...);
void ngx_cdecl ngx_log_stderr(ngx_err_t err, const char *fmt, ...);
u_char *ngx_log_errno(u_char *buf, u_char *last, ngx_err_t err);
+char *ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head);
/*
diff -r a82f305487c2 -r 7ecaa9e4bf1b src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c Wed Jun 19 08:55:08 2013 +0400
+++ b/src/http/ngx_http_core_module.c Thu Jun 20 20:47:39 2013 +0400
@@ -4888,33 +4888,7 @@ ngx_http_core_error_log(ngx_conf_t *cf,
{
ngx_http_core_loc_conf_t *clcf = conf;
- ngx_str_t *value, name;
-
- if (clcf->error_log) {
- return "is duplicate";
- }
-
- value = cf->args->elts;
-
- if (ngx_strcmp(value[1].data, "stderr") == 0) {
- ngx_str_null(&name);
- cf->cycle->log_use_stderr = 1;
-
- } else {
- name = value[1];
- }
-
- clcf->error_log = ngx_log_create(cf->cycle, &name);
- if (clcf->error_log == NULL) {
- return NGX_CONF_ERROR;
- }
-
- if (cf->args->nelts == 2) {
- clcf->error_log->log_level = NGX_LOG_ERR;
- return NGX_CONF_OK;
- }
-
- return ngx_log_set_levels(cf, clcf->error_log);
+ return ngx_log_set_log(cf, &clcf->error_log);
}
diff -r a82f305487c2 -r 7ecaa9e4bf1b src/http/ngx_http_request.h
--- a/src/http/ngx_http_request.h Wed Jun 19 08:55:08 2013 +0400
+++ b/src/http/ngx_http_request.h Thu Jun 20 20:47:39 2013 +0400
@@ -585,6 +585,7 @@ extern ngx_http_header_out_t ngx_http_
#define ngx_http_set_connection_log(c, l) \
\
c->log->file = l->file; \
+ c->log->next = l->next; \
if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) { \
c->log->log_level = l->log_level; \
}
diff -r a82f305487c2 -r 7ecaa9e4bf1b src/os/unix/ngx_process_cycle.c
--- a/src/os/unix/ngx_process_cycle.c Wed Jun 19 08:55:08 2013 +0400
+++ b/src/os/unix/ngx_process_cycle.c Thu Jun 20 20:47:39 2013 +0400
@@ -714,6 +714,7 @@ ngx_master_process_exit(ngx_cycle_t *cyc
ngx_exit_log = *ngx_cycle->log;
ngx_exit_log.file = &ngx_exit_log_file;
+ ngx_exit_log.next = NULL;
ngx_exit_cycle.log = &ngx_exit_log;
ngx_exit_cycle.files = ngx_cycle->files;
@@ -1066,6 +1067,7 @@ ngx_worker_process_exit(ngx_cycle_t *cyc
ngx_exit_log = *ngx_cycle->log;
ngx_exit_log.file = &ngx_exit_log_file;
+ ngx_exit_log.next = NULL;
ngx_exit_cycle.log = &ngx_exit_log;
ngx_exit_cycle.files = ngx_cycle->files;
More information about the nginx-devel
mailing list