[nginx] Core: consolidated log-related code.
Homutov Vladimir
vl at nginx.com
Wed Jul 3 08:15:24 UTC 2013
details: http://hg.nginx.org/nginx/rev/e088695737c3
branches:
changeset: 5260:e088695737c3
user: Vladimir Homutov <vl at nginx.com>
date: Fri Jun 28 17:24:54 2013 +0400
description:
Core: consolidated log-related code.
The stderr redirection code is moved to ngx_log_redirect_stderr().
The opening of the default log code is moved to ngx_log_open_default().
diffstat:
src/core/nginx.c | 9 ++-------
src/core/ngx_cycle.c | 28 +++++-----------------------
src/core/ngx_log.c | 42 ++++++++++++++++++++++++++++++++++++++++++
src/core/ngx_log.h | 2 ++
4 files changed, 51 insertions(+), 30 deletions(-)
diffs (142 lines):
diff -r 0c699e1d1071 -r e088695737c3 src/core/nginx.c
--- a/src/core/nginx.c Tue Jul 02 20:05:49 2013 +0400
+++ b/src/core/nginx.c Fri Jun 28 17:24:54 2013 +0400
@@ -387,13 +387,8 @@ main(int argc, char *const *argv)
return 1;
}
- if (!cycle->log_use_stderr && cycle->log->file->fd != ngx_stderr) {
-
- if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
- ngx_set_stderr_n " failed");
- return 1;
- }
+ if (ngx_log_redirect_stderr(cycle) != NGX_OK) {
+ return 1;
}
if (log->file->fd != ngx_stderr) {
diff -r 0c699e1d1071 -r e088695737c3 src/core/ngx_cycle.c
--- a/src/core/ngx_cycle.c Tue Jul 02 20:05:49 2013 +0400
+++ b/src/core/ngx_cycle.c Fri Jun 28 17:24:54 2013 +0400
@@ -36,8 +36,6 @@ ngx_tls_key_t ngx_core_tls_key;
static ngx_connection_t dumb;
/* STUB */
-static ngx_str_t error_log = ngx_string(NGX_ERROR_LOG_PATH);
-
ngx_cycle_t *
ngx_init_cycle(ngx_cycle_t *old_cycle)
@@ -338,13 +336,8 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
}
- if (cycle->new_log.file == NULL) {
- cycle->new_log.file = ngx_conf_open_file(cycle, &error_log);
- if (cycle->new_log.file == NULL) {
- goto failed;
- }
-
- cycle->new_log.log_level = NGX_LOG_ERR;
+ if (ngx_log_open_default(cycle) != NGX_OK) {
+ goto failed;
}
/* open the new files */
@@ -583,13 +576,8 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
/* commit the new cycle configuration */
- if (!ngx_use_stderr && !cycle->log_use_stderr
- && cycle->log->file->fd != ngx_stderr)
- {
- if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
- ngx_set_stderr_n " failed");
- }
+ if (!ngx_use_stderr) {
+ (void) ngx_log_redirect_stderr(cycle);
}
pool->log = cycle->log;
@@ -1230,13 +1218,7 @@ ngx_reopen_files(ngx_cycle_t *cycle, ngx
file[i].fd = fd;
}
- if (!cycle->log_use_stderr && cycle->log->file->fd != ngx_stderr) {
-
- if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
- ngx_set_stderr_n " failed");
- }
- }
+ (void) ngx_log_redirect_stderr(cycle);
}
diff -r 0c699e1d1071 -r e088695737c3 src/core/ngx_log.c
--- a/src/core/ngx_log.c Tue Jul 02 20:05:49 2013 +0400
+++ b/src/core/ngx_log.c Fri Jun 28 17:24:54 2013 +0400
@@ -363,6 +363,48 @@ ngx_log_init(u_char *prefix)
}
+ngx_int_t
+ngx_log_open_default(ngx_cycle_t *cycle)
+{
+ static ngx_str_t error_log = ngx_string(NGX_ERROR_LOG_PATH);
+
+ if (cycle->new_log.file == NULL) {
+ cycle->new_log.file = ngx_conf_open_file(cycle, &error_log);
+ if (cycle->new_log.file == NULL) {
+ return NGX_ERROR;
+ }
+
+ cycle->new_log.log_level = NGX_LOG_ERR;
+ }
+
+ return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_log_redirect_stderr(ngx_cycle_t *cycle)
+{
+ ngx_fd_t fd;
+
+ if (cycle->log_use_stderr) {
+ return NGX_OK;
+ }
+
+ fd = cycle->log->file->fd;
+
+ if (fd != ngx_stderr) {
+ if (ngx_set_stderr(fd) == NGX_FILE_ERROR) {
+ ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
+ ngx_set_stderr_n " failed");
+
+ return NGX_ERROR;
+ }
+ }
+
+ return NGX_OK;
+}
+
+
static char *
ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)
{
diff -r 0c699e1d1071 -r e088695737c3 src/core/ngx_log.h
--- a/src/core/ngx_log.h Tue Jul 02 20:05:49 2013 +0400
+++ b/src/core/ngx_log.h Fri Jun 28 17:24:54 2013 +0400
@@ -225,6 +225,8 @@ ngx_log_t *ngx_log_init(u_char *prefix);
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);
+ngx_int_t ngx_log_open_default(ngx_cycle_t *cycle);
+ngx_int_t ngx_log_redirect_stderr(ngx_cycle_t *cycle);
char *ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head);
More information about the nginx-devel
mailing list