]> git.kaiwu.me - nginx.git/commitdiff
Core: consolidated log-related code.
authorVladimir Homutov <vl@nginx.com>
Fri, 28 Jun 2013 13:24:54 +0000 (17:24 +0400)
committerVladimir Homutov <vl@nginx.com>
Fri, 28 Jun 2013 13:24:54 +0000 (17:24 +0400)
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().

src/core/nginx.c
src/core/ngx_cycle.c
src/core/ngx_log.c
src/core/ngx_log.h

index 796717ab984257b61f7e73369e004926b01cebe7..4cc80826edcb38a2487c914e9fe16a49c266d796 100644 (file)
@@ -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) {
index 44edbafd450ccee557ee60f549d9f60b088a39ad..2c006bcb22037e08f2f015893ec4075043eb1665 100644 (file)
@@ -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_uid_t user)
         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);
 }
 
 
index b35a432181d71e15eadce2acc43c17ad71dff007..edf30043d3f2dfbf897047fc41725031dddd5653 100644 (file)
@@ -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)
 {
index 82e1e245186aa9d3b24efe7fcd2cc0adf946f04a..878fb0a27e0f927997f6fcd4e5fe0d94d4e70f92 100644 (file)
@@ -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);