[nginx] svn commit: r4986 - in trunk/src: core http/modules

vbart at nginx.com vbart at nginx.com
Sun Dec 23 15:36:52 UTC 2012


Author: vbart
Date: 2012-12-23 15:36:52 +0000 (Sun, 23 Dec 2012)
New Revision: 4986
URL: http://trac.nginx.org/nginx/changeset/4986/nginx

Log:
Reopening log files code moved to a separate function.

The code refactored in a way to call custom handler that can do appropriate
cleanup work (if any), like flushing buffers, finishing compress streams,
finalizing connections to log daemon, etc..



Modified:
   trunk/src/core/ngx_conf_file.c
   trunk/src/core/ngx_conf_file.h
   trunk/src/core/ngx_cycle.c
   trunk/src/http/modules/ngx_http_log_module.c

Modified: trunk/src/core/ngx_conf_file.c
===================================================================
--- trunk/src/core/ngx_conf_file.c	2012-12-23 15:27:55 UTC (rev 4985)
+++ trunk/src/core/ngx_conf_file.c	2012-12-23 15:36:52 UTC (rev 4986)
@@ -945,7 +945,7 @@
         file->name = *name;
     }
 
-    file->buffer = NULL;
+    file->flush = NULL;
 
     return file;
 }
@@ -954,7 +954,6 @@
 static void
 ngx_conf_flush_files(ngx_cycle_t *cycle)
 {
-    ssize_t           n, len;
     ngx_uint_t        i;
     ngx_list_part_t  *part;
     ngx_open_file_t  *file;
@@ -975,24 +974,9 @@
             i = 0;
         }
 
-        len = file[i].pos - file[i].buffer;
-
-        if (file[i].buffer == NULL || len == 0) {
-            continue;
+        if (file[i].flush) {
+            file[i].flush(&file[i], cycle->log);
         }
-
-        n = ngx_write_fd(file[i].fd, file[i].buffer, len);
-
-        if (n == -1) {
-            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
-                          ngx_write_fd_n " to \"%s\" failed",
-                          file[i].name.data);
-
-        } else if (n != len) {
-            ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
-                          ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
-                          file[i].name.data, n, len);
-        }
     }
 }
 

Modified: trunk/src/core/ngx_conf_file.h
===================================================================
--- trunk/src/core/ngx_conf_file.h	2012-12-23 15:27:55 UTC (rev 4985)
+++ trunk/src/core/ngx_conf_file.h	2012-12-23 15:36:52 UTC (rev 4986)
@@ -91,17 +91,8 @@
     ngx_fd_t              fd;
     ngx_str_t             name;
 
-    u_char               *buffer;
-    u_char               *pos;
-    u_char               *last;
-
-#if 0
-    /* e.g. append mode, error_log */
-    ngx_uint_t            flags;
-    /* e.g. reopen db file */
-    ngx_uint_t          (*handler)(void *data, ngx_open_file_t *file);
+    void                (*flush)(ngx_open_file_t *file, ngx_log_t *log);
     void                 *data;
-#endif
 };
 
 

Modified: trunk/src/core/ngx_cycle.c
===================================================================
--- trunk/src/core/ngx_cycle.c	2012-12-23 15:27:55 UTC (rev 4985)
+++ trunk/src/core/ngx_cycle.c	2012-12-23 15:36:52 UTC (rev 4986)
@@ -1115,7 +1115,6 @@
 void
 ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
 {
-    ssize_t           n, len;
     ngx_fd_t          fd;
     ngx_uint_t        i;
     ngx_list_part_t  *part;
@@ -1139,24 +1138,8 @@
             continue;
         }
 
-        len = file[i].pos - file[i].buffer;
-
-        if (file[i].buffer && len != 0) {
-
-            n = ngx_write_fd(file[i].fd, file[i].buffer, len);
-
-            if (n == -1) {
-                ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
-                              ngx_write_fd_n " to \"%s\" failed",
-                              file[i].name.data);
-
-            } else if (n != len) {
-                ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
-                          ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
-                          file[i].name.data, n, len);
-            }
-
-            file[i].pos = file[i].buffer;
+        if (file[i].flush) {
+            file[i].flush(&file[i], cycle->log);
         }
 
         fd = ngx_open_file(file[i].name.data, NGX_FILE_APPEND,

Modified: trunk/src/http/modules/ngx_http_log_module.c
===================================================================
--- trunk/src/http/modules/ngx_http_log_module.c	2012-12-23 15:27:55 UTC (rev 4985)
+++ trunk/src/http/modules/ngx_http_log_module.c	2012-12-23 15:36:52 UTC (rev 4986)
@@ -41,6 +41,13 @@
 
 
 typedef struct {
+    u_char                     *start;
+    u_char                     *pos;
+    u_char                     *last;
+} ngx_http_log_buf_t;
+
+
+typedef struct {
     ngx_array_t                *lengths;
     ngx_array_t                *values;
 } ngx_http_log_script_t;
@@ -78,6 +85,8 @@
 static ssize_t ngx_http_log_script_write(ngx_http_request_t *r,
     ngx_http_log_script_t *script, u_char **name, u_char *buf, size_t len);
 
+static void ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log);
+
 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
     ngx_http_log_op_t *op);
 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
@@ -216,8 +225,8 @@
     size_t                    len;
     ngx_uint_t                i, l;
     ngx_http_log_t           *log;
-    ngx_open_file_t          *file;
     ngx_http_log_op_t        *op;
+    ngx_http_log_buf_t       *buffer;
     ngx_http_log_loc_conf_t  *lcf;
 
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -258,21 +267,21 @@
 
         len += NGX_LINEFEED_SIZE;
 
-        file = log[l].file;
+        buffer = log[l].file ? log[l].file->data : NULL;
 
-        if (file && file->buffer) {
+        if (buffer) {
 
-            if (len > (size_t) (file->last - file->pos)) {
+            if (len > (size_t) (buffer->last - buffer->pos)) {
 
-                ngx_http_log_write(r, &log[l], file->buffer,
-                                   file->pos - file->buffer);
+                ngx_http_log_write(r, &log[l], buffer->start,
+                                   buffer->pos - buffer->start);
 
-                file->pos = file->buffer;
+                buffer->pos = buffer->start;
             }
 
-            if (len <= (size_t) (file->last - file->pos)) {
+            if (len <= (size_t) (buffer->last - buffer->pos)) {
 
-                p = file->pos;
+                p = buffer->pos;
 
                 for (i = 0; i < log[l].format->ops->nelts; i++) {
                     p = op[i].run(r, p, &op[i]);
@@ -280,7 +289,7 @@
 
                 ngx_linefeed(p);
 
-                file->pos = p;
+                buffer->pos = p;
 
                 continue;
             }
@@ -465,6 +474,38 @@
 }
 
 
+static void
+ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log)
+{
+    size_t               len;
+    ssize_t              n;
+    ngx_http_log_buf_t  *buffer;
+
+    buffer = file->data;
+
+    len = buffer->pos - buffer->start;
+
+    if (len == 0) {
+        return;
+    }
+
+    n = ngx_write_fd(file->fd, buffer->start, len);
+
+    if (n == -1) {
+        ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
+                      ngx_write_fd_n " to \"%s\" failed",
+                      file->name.data);
+
+    } else if ((size_t) n != len) {
+        ngx_log_error(NGX_LOG_ALERT, log, 0,
+                      ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
+                      file->name.data, n, len);
+    }
+
+    buffer->pos = buffer->start;
+}
+
+
 static u_char *
 ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
     ngx_http_log_op_t *op)
@@ -848,10 +889,11 @@
 {
     ngx_http_log_loc_conf_t *llcf = conf;
 
-    ssize_t                     buf;
+    ssize_t                     size;
     ngx_uint_t                  i, n;
     ngx_str_t                  *value, name;
     ngx_http_log_t             *log;
+    ngx_http_log_buf_t         *buffer;
     ngx_http_log_fmt_t         *fmt;
     ngx_http_log_main_conf_t   *lmcf;
     ngx_http_script_compile_t   sc;
@@ -962,16 +1004,18 @@
         name.len = value[3].len - 7;
         name.data = value[3].data + 7;
 
-        buf = ngx_parse_size(&name);
+        size = ngx_parse_size(&name);
 
-        if (buf == NGX_ERROR) {
+        if (size == NGX_ERROR) {
             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                "invalid buffer value \"%V\"", &name);
             return NGX_CONF_ERROR;
         }
 
-        if (log->file->buffer) {
-            if (log->file->last - log->file->pos != buf) {
+        if (log->file->data) {
+            buffer = log->file->data;
+
+            if (buffer->last - buffer->start != size) {
                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                    "access_log \"%V\" already defined "
                                    "with different buffer size", &value[1]);
@@ -981,13 +1025,21 @@
             return NGX_CONF_OK;
         }
 
-        log->file->buffer = ngx_palloc(cf->pool, buf);
-        if (log->file->buffer == NULL) {
+        buffer = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_buf_t));
+        if (buffer == NULL) {
             return NGX_CONF_ERROR;
         }
 
-        log->file->pos = log->file->buffer;
-        log->file->last = log->file->buffer + buf;
+        buffer->start = ngx_pnalloc(cf->pool, size);
+        if (buffer->start == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        buffer->pos = buffer->start;
+        buffer->last = buffer->start + size;
+
+        log->file->flush = ngx_http_log_flush;
+        log->file->data = buffer;
     }
 
     return NGX_CONF_OK;



More information about the nginx-devel mailing list