changeset 257:70e1c7d2b83d

nginx-0.0.2-2004-02-11-20:08:49 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 11 Feb 2004 17:08:49 +0000
parents 8e39cab6abd5
children 733dffa1fe97
files auto/os/linux auto/sources src/core/ngx_alloc.c src/core/ngx_conf_file.c src/core/ngx_conf_file.h src/core/ngx_connection.c src/core/ngx_cycle.c src/core/ngx_file.c src/core/ngx_garbage_collector.c src/core/ngx_log.c src/core/ngx_log.h src/core/ngx_output_chain.c src/event/modules/ngx_aio_module.c src/event/modules/ngx_epoll_module.c src/event/modules/ngx_kqueue_module.c src/event/ngx_event.c src/event/ngx_event.h src/event/ngx_event_accept.c src/event/ngx_event_connect.c src/event/ngx_event_pipe.c src/http/modules/ngx_http_gzip_filter.c src/http/modules/ngx_http_index_handler.c src/http/modules/ngx_http_not_modified_filter.c src/http/modules/ngx_http_static_handler.c src/http/modules/proxy/ngx_http_proxy_cache.c src/http/modules/proxy/ngx_http_proxy_handler.c src/http/modules/proxy/ngx_http_proxy_upstream.c src/http/ngx_http.c src/http/ngx_http_busy_lock.c src/http/ngx_http_core_module.c src/http/ngx_http_core_module.h src/http/ngx_http_file_cache.c src/http/ngx_http_header_filter.c src/http/ngx_http_headers.c src/http/ngx_http_parse.c src/http/ngx_http_request.c src/http/ngx_http_write_filter.c src/os/unix/ngx_aio_read.c src/os/unix/ngx_aio_read_chain.c src/os/unix/ngx_aio_write.c src/os/unix/ngx_aio_write_chain.c src/os/unix/ngx_files.c src/os/unix/ngx_linux_config.h src/os/unix/ngx_readv_chain.c src/os/unix/ngx_recv.c src/os/unix/ngx_solaris_sendfilev_chain.c src/os/unix/ngx_writev_chain.c src/os/win32/ngx_wsasend_chain.c
diffstat 48 files changed, 470 insertions(+), 366 deletions(-) [+]
line wrap: on
line diff
--- a/auto/os/linux
+++ b/auto/os/linux
@@ -21,6 +21,7 @@ ngx_func_test="int efd = 0, fd = 1, n;
 
 if [ $ngx_found = yes ]; then
     have=HAVE_EPOLL . auto/have
+    have=HAVE_CLEAR_EVENT . auto/have
     CORE_SRCS="$CORE_SRCS $EPOLL_SRCS"
     EVENT_MODULES="$EVENT_MODULES $EPOLL_MODULE"
     EVENT_FOUND=YES
--- a/auto/sources
+++ b/auto/sources
@@ -56,7 +56,6 @@ EVENT_DEPS="src/event/ngx_event.h \
 
 EVENT_SRCS="src/event/ngx_event.c \
             src/event/ngx_event_timer.c \
-            src/event/ngx_event_close.c \
             src/event/ngx_event_accept.c \
             src/event/ngx_event_connect.c \
             src/event/ngx_event_pipe.c"
--- a/src/core/ngx_alloc.c
+++ b/src/core/ngx_alloc.c
@@ -12,9 +12,7 @@ void *ngx_alloc(size_t size, ngx_log_t *
                       "malloc() %d bytes failed", size);
     }
 
-#if (NGX_DEBUG_ALLOC)
-    ngx_log_debug(log, "malloc: %08x:%d" _ p _ size);
-#endif
+    ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, "malloc: %08x:%d", p, size);
 
     return p;
 }
@@ -57,27 +55,30 @@ void ngx_destroy_pool(ngx_pool_t *pool)
     ngx_pool_large_t  *l;
 
     for (l = pool->large; l; l = l->next) {
-#if (NGX_DEBUG_ALLOC)
-        ngx_log_debug(pool->log, "free: %08x" _ l->alloc);
-#endif
+
+        ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
+                       "free: %08x", l->alloc);
+
         if (l->alloc) {
             free(l->alloc);
         }
     }
 
+#if (NGX_DEBUG)
+
     /*
-     * we could allocate pool->log from this pool
-     * so we can not use this log while free()ing the pool
+     * we could allocate the pool->log from this pool
+     * so we can not use this log while the free()ing the pool
      */
 
-#if (NGX_DEBUG_ALLOC)
     for (p = pool, n = pool->next; /* void */; p = n, n = n->next) {
-        ngx_log_debug(pool->log, "free: %08x" _ p);
+        ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %08x", p);
 
         if (n == NULL) {
             break;
         }
     }
+
 #endif
 
     for (p = pool, n = pool->next; /* void */; p = n, n = n->next) {
@@ -177,9 +178,8 @@ void ngx_pfree(ngx_pool_t *pool, void *p
 
     for (l = pool->large; l; l = l->next) {
         if (p == l->alloc) {
-#if (NGX_DEBUG_ALLOC)
-            ngx_log_debug(pool->log, "free: %08x" _ l->alloc);
-#endif
+            ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
+                           "free: %08x", l->alloc);
             free(l->alloc);
             l->alloc = NULL;
         }
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -540,7 +540,7 @@ ngx_open_file_t *ngx_conf_open_file(ngx_
 }
 
 
-void ngx_conf_log_error(int level, ngx_conf_t *cf, ngx_err_t err,
+void ngx_conf_log_error(ngx_uint_t level, ngx_conf_t *cf, ngx_err_t err,
                         char *fmt, ...)
 {
     int      len;
--- a/src/core/ngx_conf_file.h
+++ b/src/core/ngx_conf_file.h
@@ -238,7 +238,7 @@ char *ngx_conf_parse(ngx_conf_t *cf, ngx
 
 
 ngx_open_file_t *ngx_conf_open_file(ngx_cycle_t *cycle, ngx_str_t *name);
-void ngx_conf_log_error(int level, ngx_conf_t *cf, ngx_err_t err,
+void ngx_conf_log_error(ngx_uint_t level, ngx_conf_t *cf, ngx_err_t err,
                         char *fmt, ...);
 
 
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -240,7 +240,7 @@ void ngx_close_listening_sockets(ngx_cyc
 
 ngx_int_t ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text)
 {
-    ngx_int_t  level;
+    ngx_uint_t  level;
 
     if (err == NGX_ECONNRESET
         && c->log_error == NGX_ERROR_IGNORE_ECONNRESET)
@@ -252,6 +252,7 @@ ngx_int_t ngx_connection_error(ngx_conne
 
         switch (c->log_error) {
 
+        case NGX_ERROR_IGNORE_ECONNRESET:
         case NGX_ERROR_INFO:
             level = NGX_LOG_INFO;
             break;
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -464,7 +464,7 @@ static void ngx_clean_old_cycles(ngx_eve
     log = ngx_cycle->log;
     ngx_temp_pool->log = log;
 
-    ngx_log_debug(log, "clean old cycles");
+    ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles");
 
     live = 0;
 
@@ -480,7 +480,9 @@ static void ngx_clean_old_cycles(ngx_eve
         for (n = 0; n < cycle[i]->connection_n; n++) {
             if (cycle[i]->connections[n].fd != -1) {
                 found = 1;
-                ngx_log_debug(log, "live fd: %d" _ n);
+
+                ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%d", n);
+
                 break;
             }
         }
@@ -490,15 +492,15 @@ static void ngx_clean_old_cycles(ngx_eve
             continue;
         }
 
-        ngx_log_debug(log, "clean old cycle: %d" _ i);
+        ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %d", i);
+
         ngx_destroy_pool(cycle[i]->pool);
         cycle[i] = NULL;
     }
 
-    ngx_log_debug(log, "old cycles status: %d" _ live);
+    ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "old cycles status: %d", live);
 
     if (live) {
-        ngx_log_debug(log, "TIMER");
         ngx_add_timer(ev, 30000);
 
     } else {
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -75,7 +75,8 @@ int ngx_create_temp_file(ngx_file_t *fil
 #endif
         file->fd = ngx_open_tempfile(file->name.data, 1);
 
-ngx_log_debug(file->log, "temp fd: %d" _ file->fd);
+        ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
+                       "temp fd:%d", file->fd);
 
         if (file->fd != NGX_INVALID_FILE) {
             return NGX_OK;
@@ -123,15 +124,14 @@ void ngx_create_hashed_filename(ngx_file
             break;
         }
 
-        ngx_log_debug(file->log, "hashed path: %s" _ file->name.data);
-
         name -= level;
         file->name.data[pos - 1] = '/';
         ngx_memcpy(&file->name.data[pos], &file->name.data[name], level);
         pos += level + 1;
     }
 
-    ngx_log_debug(file->log, "hashed path: %s" _ file->name.data);
+    ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
+                   "hashed path: %s", file->name.data);
 }
 
 
@@ -151,7 +151,8 @@ int ngx_create_path(ngx_file_t *file, ng
 
         file->name.data[pos] = '\0';
 
-        ngx_log_debug(file->log, "temp: %s" _ file->name.data);
+        ngx_log_debug1(NGX_LOG_DEBUG_CORE, file->log, 0,
+                       "temp file: \"%s\"", file->name.data);
 
         if (ngx_create_dir(file->name.data) == NGX_FILE_ERROR) {
             err = ngx_errno;
--- a/src/core/ngx_garbage_collector.c
+++ b/src/core/ngx_garbage_collector.c
@@ -79,7 +79,8 @@ static int ngx_collect_garbage(ngx_gc_t 
 
     buf.len = 0;
 
-ngx_log_debug(ctx->log, "dir '%s':%d" _ dname->data _ dname->len);
+    ngx_log_debug2(NGX_LOG_DEBUG_CORE, ctx->log, 0,
+                   "gc dir \"%s\":%d", dname->data, dname->len);
 
     if (ngx_open_dir(dname, &dir) == NGX_ERROR) {
         ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
@@ -106,7 +107,8 @@ ngx_log_debug(ctx->log, "dir '%s':%d" _ 
 
         len = ngx_de_namelen(&dir);
 
-ngx_log_debug(ctx->log, "name '%s':%d" _ ngx_de_name(&dir) _ len);
+        ngx_log_debug2(NGX_LOG_DEBUG_CORE, ctx->log, 0,
+                      "gc name \"%s\":%d", ngx_de_name(&dir), len);
 
         if (len == 1 && ngx_de_name(&dir)[0] == '.') {
             continue;
@@ -139,7 +141,8 @@ ngx_log_debug(ctx->log, "name '%s':%d" _
         ngx_memcpy(last, ngx_de_name(&dir), len + 1);
         fname.data = buf.data;
 
-ngx_log_debug(ctx->log, "path %s" _ fname.data);
+        ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
+                       "gc path: \"%s\"", fname.data);
 
         if (!dir.info_valid) {
             if (ngx_de_info(fname.data, &dir) == NGX_FILE_ERROR) {
@@ -151,7 +154,8 @@ ngx_log_debug(ctx->log, "path %s" _ fnam
 
         if (ngx_de_is_dir(&dir)) {
 
-ngx_log_debug(ctx->log, "enter %s" _ fname.data);
+            ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
+                           "gc enter dir \"%s\"", fname.data);
 
             if (level == -1
                    /* there can not be directory on the last level */
@@ -187,7 +191,8 @@ ngx_log_debug(ctx->log, "enter %s" _ fna
 
         } else if (ngx_de_is_file(&dir)) {
 
-ngx_log_debug(ctx->log, "file %s" _ fname.data);
+            ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->log, 0,
+                           "gc file \"%s\"", fname.data);
 
             if (level == -1
                 || (level < NGX_MAX_PATH_LEVEL && ctx->path->level[level] != 0))
@@ -239,10 +244,10 @@ int ngx_garbage_collector_temp_handler(n
                                        ngx_dir_t *dir)
 {
     /*
-     * we use mtime only and do not use atime because:
+     * We use mtime only and do not use atime because:
      *    on NTFS access time has a resolution of 1 hour,
      *    on NT FAT access time has a resolution of 1 day,
-     *    Unices have mount option "noatime"
+     *    Unices have the mount option "noatime".
      */
 
     if (ngx_cached_time - ngx_de_mtime(dir) < 3600) {
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -42,7 +42,7 @@ static const char *err_levels[] = {
 };
 
 static const char *debug_levels[] = {
-    "debug", "debug_core", "debug_alloc", "debug_event", "debug_http"
+    "debug_core", "debug_alloc", "debug_event", "debug_http"
 };
 
 
@@ -81,9 +81,9 @@ void ngx_log_error_core(int level, ngx_l
     len += ngx_snprintf(errstr + len, max - len,
                         PID_T_FMT "#%d: ", ngx_pid, /* STUB */ 0);
 
-    if (log->data) {
+    if (log->data && *(int *) log->data != -1) {
         len += ngx_snprintf(errstr + len, max - len,
-                            "*%u ", * (u_int *) log->data);
+                            "*%u ", *(u_int *) log->data);
     }
 
 #if (HAVE_VARIADIC_MACROS)
@@ -332,7 +332,7 @@ char *ngx_set_error_log_levels(ngx_conf_
 
     for (i = 2; i < cf->args->nelts; i++) {
 
-        for (n = 1; n < NGX_LOG_DEBUG; n++) {
+        for (n = 1; n <= NGX_LOG_DEBUG; n++) {
             if (ngx_strcmp(value[i].data, err_levels[n]) == 0) {
 
                 if (log->log_level != 0) {
@@ -368,5 +368,9 @@ char *ngx_set_error_log_levels(ngx_conf_
         }
     }
 
+    if (log->log_level == NGX_LOG_DEBUG) {
+        log->log_level = NGX_LOG_DEBUG_ALL;
+    }
+
     return NGX_CONF_OK;
 }
--- a/src/core/ngx_log.h
+++ b/src/core/ngx_log.h
@@ -21,9 +21,9 @@
 #define NGX_LOG_DEBUG_EVENT     0x40
 #define NGX_LOG_DEBUG_HTTP      0x80
 
-#define NGX_LOG_DEBUG_FIRST     NGX_LOG_DEBUG
+#define NGX_LOG_DEBUG_FIRST     NGX_LOG_DEBUG_CORE
 #define NGX_LOG_DEBUG_LAST      NGX_LOG_DEBUG_HTTP
-#define NGX_LOG_DEBUG_ALL       0xfffffff8
+#define NGX_LOG_DEBUG_ALL       0xfffffff0
 
 
 /*
@@ -74,7 +74,7 @@ typedef size_t  (*ngx_log_handler_pt) (v
 
 
 struct ngx_log_s {
-    int                  log_level;
+    ngx_uint_t           log_level;
     ngx_open_file_t     *file;
     void                *data;
     ngx_log_handler_pt   handler;
@@ -82,8 +82,6 @@ struct ngx_log_s {
 
 #define MAX_ERROR_STR	2048
 
-#define _               ,
-
 
 /*********************************/
 
@@ -94,21 +92,6 @@ struct ngx_log_s {
 #define ngx_log_error(level, log, args...) \
         if (log->log_level >= level) ngx_log_error_core(level, log, args)
 
-#if (NGX_DEBUG)
-#define ngx_log_debug(log, args...) \
-    if (log->log_level & NGX_LOG_DEBUG) \
-        ngx_log_error_core(NGX_LOG_DEBUG, log, 0, args)
-#else
-#define ngx_log_debug(log, args...)
-#endif
-
-#define ngx_assert(assert, fallback, log, args...) \
-        if (!(assert)) { \
-            if (log->log_level >= NGX_LOG_ALERT) \
-                ngx_log_error_core(NGX_LOG_ALERT, log, 0, args); \
-            fallback; \
-        }
-
 void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err,
                         const char *fmt, ...);
 
@@ -121,21 +104,6 @@ void ngx_log_error_core(int level, ngx_l
 #define ngx_log_error(level, log, ...) \
         if (log->log_level >= level) ngx_log_error_core(level, log, __VA_ARGS__)
 
-#if (NGX_DEBUG)
-#define ngx_log_debug(log, ...) \
-    if (log->log_level == NGX_LOG_DEBUG) \
-        ngx_log_error_core(NGX_LOG_DEBUG, log, 0, __VA_ARGS__)
-#else
-#define ngx_log_debug(log, ...)
-#endif
-
-#define ngx_assert(assert, fallback, log, ...) \
-        if (!(assert)) { \
-            if (log->log_level >= NGX_LOG_ALERT) \
-                ngx_log_error_core(NGX_LOG_ALERT, log, 0, __VA_ARGS__); \
-            fallback; \
-        }
-
 void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err,
                         const char *fmt, ...);
 
@@ -145,21 +113,6 @@ void ngx_log_error_core(int level, ngx_l
 
 #define HAVE_VARIADIC_MACROS  0
 
-#if (NGX_DEBUG)
-#define ngx_log_debug(log, text) \
-    if (log->log_level == NGX_LOG_DEBUG) \
-        ngx_log_debug_core(log, 0, text)
-#else
-#define ngx_log_debug(log, text)
-#endif
-
-#define ngx_assert(assert, fallback, log, text) \
-        if (!(assert)) { \
-            if (log->log_level >= NGX_LOG_ALERT) \
-                ngx_assert_core(log, text); \
-            fallback; \
-        }
-
 void ngx_log_error(int level, ngx_log_t *log, ngx_err_t err,
                    const char *fmt, ...);
 void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err,
--- a/src/core/ngx_output_chain.c
+++ b/src/core/ngx_output_chain.c
@@ -217,12 +217,6 @@ static int ngx_output_chain_copy_hunk(ng
     } else {
         n = ngx_read_file(src->file, dst->pos, size, src->file_pos);
 
-if (n == 0) {
-ngx_log_debug(src->file->log, "READ: %qd:%qd %X:%X %X:%X" _
-              src->file_pos _ src->file_last _
-              dst->pos _ dst->last _ dst->start _ dst->end);
-}
-
         if (n == NGX_ERROR) {
             return n;
         }
--- a/src/event/modules/ngx_aio_module.c
+++ b/src/event/modules/ngx_aio_module.c
@@ -106,7 +106,7 @@ static int ngx_aio_del_connection(ngx_co
 
     rc = aio_cancel(c->fd, NULL);
 
-    ngx_log_debug(c->log, "aio_cancel: %d" _ rc);
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "aio_cancel: %d", rc);
 
     if (rc == AIO_CANCELED) {
         c->read->active = c->write->active = 0;
--- a/src/event/modules/ngx_epoll_module.c
+++ b/src/event/modules/ngx_epoll_module.c
@@ -111,8 +111,8 @@ ngx_event_module_t  ngx_epoll_module_ctx
         ngx_epoll_del_event,             /* delete an event */
         ngx_epoll_add_event,             /* enable an event */
         ngx_epoll_del_event,             /* disable an event */
-        ngx_epoll_add_connection,        /* add an connection */
-        ngx_epoll_del_connection,        /* delete an connection */
+        NULL,                            /* add an connection */
+        NULL,                            /* delete an connection */
         ngx_epoll_process_events,        /* process the events */
         ngx_epoll_init,                  /* init the events */
         ngx_epoll_done,                  /* done the events */
@@ -167,7 +167,11 @@ static int ngx_epoll_init(ngx_cycle_t *c
 
     ngx_event_actions = ngx_epoll_module_ctx.actions;
 
-    ngx_event_flags = NGX_USE_EDGE_EVENT;
+#if (HAVE_CLEAR_EVENT)
+    ngx_event_flags = NGX_USE_CLEAR_EVENT;
+#else
+    ngx_event_flags = NGX_USE_LEVEL_EVENT;
+#endif
 
     return NGX_OK;
 }
@@ -191,33 +195,53 @@ static void ngx_epoll_done(ngx_cycle_t *
 
 static int ngx_epoll_add_event(ngx_event_t *ev, int event, u_int flags)
 {
+    int                  op, prev;
+    ngx_event_t         *e;
+    ngx_connection_t    *c;
     struct epoll_event   ee;
-    ngx_connection_t    *c;
 
     c = ev->data;
 
-#if (NGX_READ_EVENT != EPOLLIN) || (NGX_WRITE_EVENT != EPOLLOUT)
     if (event == NGX_READ_EVENT) {
+        e = c->write;
+        prev = EPOLLOUT;
+#if (NGX_READ_EVENT != EPOLLIN)
         event = EPOLLIN;
-
-    } else {
-        event = EPOLLOUT;
-    }
 #endif
 
-    ee.events = event|EPOLLET;
-    ee.data.ptr = (void *) ((uintptr_t) c | c->read->instance);
+    } else {
+        e = c->read;
+        prev = EPOLLIN;
+#if (NGX_WRITE_EVENT != EPOLLOUT)
+        event = EPOLLOUT;
+#endif
+    }
+
+    if (e->active) {
+        op = EPOLL_CTL_MOD;
+        event |= prev;
 
-    ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                   "epoll add event: fd:%d ev:%08X", c->fd, ee.events);
+    } else {
+        op = EPOLL_CTL_ADD;
+    }
+
+    ee.events = event | flags;
+    ee.data.ptr = (void *) ((uintptr_t) c | ev->instance);
 
-    if (epoll_ctl(ep, EPOLL_CTL_ADD, c->fd, &ee) == -1) {
-        ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
-                      "epoll_ctl(EPOLL_CTL_MOD, %d) failed", c->fd);
+    ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
+                   "epoll add event: fd:%d op:%d ev:%08X",
+                   c->fd, op, ee.events);
+
+    if (epoll_ctl(ep, op, c->fd, &ee) == -1) {
+        ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
+                      "epoll_ctl(%d, %d) failed", op, c->fd);
         return NGX_ERROR;
     }
 
     ev->active = 1;
+#if 0
+    ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1 : 0;
+#endif
 
     return NGX_OK;
 }
@@ -225,20 +249,51 @@ static int ngx_epoll_add_event(ngx_event
 
 static int ngx_epoll_del_event(ngx_event_t *ev, int event, u_int flags)
 {
+    int                  op, prev;
+    ngx_event_t         *e;
+    ngx_connection_t    *c;
     struct epoll_event   ee;
-    ngx_connection_t    *c;
+
+    /*
+     * when the file descriptor is closed the epoll automatically deletes
+     * it from its queue so we do not need to delete explicity the event
+     * before the closing the file descriptor.
+     */
+
+    if (flags & NGX_CLOSE_EVENT) {
+        ev->active = 0;
+        return NGX_OK;
+    }
 
     c = ev->data;
 
-    ee.events = 0;
-    ee.data.ptr = NULL;
+    if (event == NGX_READ_EVENT) {
+        e = c->write;
+        prev = EPOLLOUT;
+
+    } else {
+        e = c->read;
+        prev = EPOLLIN;
+    }
+
+    if (e->active) {
+        op = EPOLL_CTL_MOD;
+        ee.events = prev | flags;
+        ee.data.ptr = (void *) ((uintptr_t) c | ev->instance);
 
-    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                   "epoll del event: fd:%d", c->fd);
+    } else {
+        op = EPOLL_CTL_DEL;
+        ee.events = 0;
+        ee.data.ptr = NULL;
+    }
 
-    if (epoll_ctl(ep, EPOLL_CTL_DEL, c->fd, &ee) == -1) {
-        ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
-                      "epoll_ctl(EPOLL_CTL_MOD, %d) failed", c->fd);
+    ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
+                   "epoll del event: fd:%d op:%d ev:%08X",
+                   c->fd, op, ee.events);
+
+    if (epoll_ctl(ep, op, c->fd, &ee) == -1) {
+        ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
+                      "epoll_ctl(%d, %d) failed", op, c->fd);
         return NGX_ERROR;
     }
 
@@ -248,6 +303,7 @@ static int ngx_epoll_del_event(ngx_event
 }
 
 
+#if 0
 static int ngx_epoll_add_connection(ngx_connection_t *c)
 {
     struct epoll_event  ee;
@@ -278,6 +334,7 @@ static int ngx_epoll_del_connection(ngx_
 
     return NGX_OK;
 }
+#endif
 
 
 int ngx_epoll_process_events(ngx_log_t *log)
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -254,7 +254,7 @@ static int ngx_kqueue_del_event(ngx_even
     }
 
     /*
-     * when the file descriptor is closed a kqueue automatically deletes
+     * when the file descriptor is closed the kqueue automatically deletes
      * its filters so we do not need to delete explicity the event
      * before the closing the file descriptor.
      */
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -19,6 +19,11 @@ extern ngx_module_t ngx_devpoll_module;
 extern ngx_event_module_t ngx_devpoll_module_ctx;
 #endif
 
+#if (HAVE_EPOLL)
+extern ngx_module_t ngx_epoll_module;
+extern ngx_event_module_t ngx_epoll_module_ctx;
+#endif
+
 #if (HAVE_AIO)
 #include <ngx_aio_module.h>
 #endif
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -176,18 +176,19 @@ extern ngx_event_actions_t   ngx_event_a
 
 /*
  * The event filter requires to read/write the whole data -
- * select, poll, /dev/poll, kqueue.
+ * select, poll, /dev/poll, kqueue, epoll.
  */
 #define NGX_USE_LEVEL_EVENT    0x00000001
 
 /*
  * The event filter is deleted after a notification without an additional
- * syscall - select, poll, kqueue.
+ * syscall - select, poll, kqueue, epoll.
  */
 #define NGX_USE_ONESHOT_EVENT  0x00000002
 
 /*
- *  The event filter notifies only the changes and an initial level - kqueue.
+ * The event filter notifies only the changes and an initial level -
+ * kqueue, epoll.
  */
 #define NGX_USE_CLEAR_EVENT    0x00000004
 
@@ -205,7 +206,7 @@ extern ngx_event_actions_t   ngx_event_a
 
 /*
  * The event filter notifies only the changes (the edges)
- * but not an initial level - epoll.
+ * but not an initial level - early epoll patches.
  */
 #define NGX_USE_EDGE_EVENT     0x00000020
 
@@ -275,6 +276,27 @@ extern ngx_event_actions_t   ngx_event_a
 #define NGX_DISABLE_EVENT  EV_DISABLE
 
 
+#elif (HAVE_DEVPOLL)
+
+#define NGX_READ_EVENT     POLLIN
+#define NGX_WRITE_EVENT    POLLOUT
+
+#define NGX_LEVEL_EVENT    0
+
+
+#elif (HAVE_EPOLL)
+
+#define NGX_READ_EVENT     EPOLLIN
+#define NGX_WRITE_EVENT    EPOLLOUT
+
+#define NGX_LEVEL_EVENT    0
+#define NGX_CLEAR_EVENT    EPOLLET
+#define NGX_ONESHOT_EVENT  0x70000000
+#if 0
+#define NGX_ONESHOT_EVENT  EPOLLONESHOT
+#endif
+
+
 #elif (HAVE_POLL)
 
 #define NGX_READ_EVENT     POLLIN
@@ -284,14 +306,6 @@ extern ngx_event_actions_t   ngx_event_a
 #define NGX_ONESHOT_EVENT  1
 
 
-#elif (HAVE_DEVPOLL)
-
-#define NGX_READ_EVENT     POLLIN
-#define NGX_WRITE_EVENT    POLLOUT
-
-#define NGX_LEVEL_EVENT    0
-
-
 #else /* select */
 
 #define NGX_READ_EVENT     0
--- a/src/event/ngx_event_accept.c
+++ b/src/event/ngx_event_accept.c
@@ -5,6 +5,12 @@
 #include <nginx.h>
 
 
+typedef struct {
+    int    flag;
+    char  *name;
+} ngx_accept_log_ctx_t;
+
+
 static size_t ngx_accept_log_error(void *data, char *buf, size_t len);
 
 
@@ -20,6 +26,7 @@ void ngx_event_accept(ngx_event_t *ev)
     ngx_event_t           *rev, *wev;
     ngx_connection_t      *c, *ls;
     ngx_event_conf_t      *ecf;
+    ngx_accept_log_ctx_t  *ctx;
 
     ecf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_core_module);
 
@@ -32,9 +39,9 @@ void ngx_event_accept(ngx_event_t *ev)
 
     ls = ev->data;
 
-    ngx_log_debug(ev->log, "accept on %s ready: %d" _
-                  ls->listening->addr_text.data _
-                  ev->available);
+    ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
+                   "accept on %s, ready: %d",
+                   ls->listening->addr_text.data, ev->available);
 
     ev->ready = 0;
     accepted = 0;
@@ -68,7 +75,16 @@ void ngx_event_accept(ngx_event_t *ev)
         ngx_memcpy(log, ls->log, sizeof(ngx_log_t));
         pool->log = log;
 
-        log->data = ls->listening->addr_text.data;
+        if (!(ctx = ngx_palloc(pool, sizeof(ngx_accept_log_ctx_t)))) {
+            ngx_destroy_pool(pool);
+            return;
+        }
+
+        /* -1 disable logging the connection number */
+        ctx->flag = -1;
+        ctx->name = ls->listening->addr_text.data;
+
+        log->data = ctx;
         log->handler = ngx_accept_log_error;
 
         len = ls->listening->socklen;
@@ -233,8 +249,8 @@ void ngx_event_accept(ngx_event_t *ev)
         wev->log = log;
 
         /*
-         * In the multithreaded model the connection counter is updated by
-         * the main thread only that accept()s connections.
+         * TODO: MT: - atomic increment (x86: lock xadd)
+         *             or protection by critical section or mutex
          *
          * TODO: MP: - allocated in a shared memory
          *           - atomic increment (x86: lock xadd)
@@ -279,7 +295,7 @@ void ngx_event_accept(ngx_event_t *ev)
 
 static size_t ngx_accept_log_error(void *data, char *buf, size_t len)
 {
-    char *sock = data;
+    ngx_accept_log_ctx_t  *ctx = data;
 
-    return ngx_snprintf(buf, len, " while accept() on %s", sock);
+    return ngx_snprintf(buf, len, " while accept() on %s", ctx->name);
 }
--- a/src/event/ngx_event_connect.c
+++ b/src/event/ngx_event_connect.c
@@ -3,6 +3,7 @@
 #include <ngx_core.h>
 #include <ngx_event.h>
 #include <ngx_event_connect.h>
+#include <nginx.h>
 
 
 /* AF_INET only */
@@ -180,6 +181,17 @@ int ngx_event_connect_peer(ngx_peer_conn
 
     pc->connection = c;
 
+    /*
+     * TODO: MT: - atomic increment (x86: lock xadd)
+     *             or protection by critical section or mutex
+     *
+     * TODO: MP: - allocated in a shared memory
+     *           - atomic increment (x86: lock xadd)
+     *             or protection by critical section or mutex
+     */
+
+    c->number = ngx_connection_counter++;
+
     if (ngx_add_conn) {
         if (ngx_add_conn(c) == NGX_ERROR) {
             return NGX_ERROR;
@@ -192,7 +204,8 @@ int ngx_event_connect_peer(ngx_peer_conn
     addr.sin_port = peer->port;
     addr.sin_addr.s_addr = peer->addr;
 
-ngx_log_debug(pc->log, "CONNECT: %s" _ peer->addr_port_text.data);
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pc->log, 0,
+                   "connect to %s", peer->addr_port_text.data);
 
     rc = connect(s, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
 
@@ -269,7 +282,7 @@ ngx_log_debug(pc->log, "CONNECT: %s" _ p
         return NGX_AGAIN;
     }
 
-ngx_log_debug(pc->log, "CONNECTED");
+    ngx_log_debug0(NGX_LOG_DEBUG_EVENT, pc->log, 0, "connected");
 
     wev->ready = 1;
 
--- a/src/event/ngx_event_pipe.c
+++ b/src/event/ngx_event_pipe.c
@@ -83,7 +83,8 @@ int ngx_event_pipe_read_upstream(ngx_eve
         return NGX_OK;
     }
 
-    ngx_log_debug(p->log, "read upstream: %d" _ p->upstream->read->ready);
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
+                   "pipe read upstream: %d", p->upstream->read->ready);
 
     for ( ;; ) {
 
@@ -104,7 +105,8 @@ int ngx_event_pipe_read_upstream(ngx_eve
             p->preread_hunks = NULL;
             n = p->preread_size;
 
-            ngx_log_debug(p->log, "preread: %d" _ n);
+            ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
+                           "pipe preread: %d", n);
 
         } else {
 
@@ -169,7 +171,8 @@ int ngx_event_pipe_read_upstream(ngx_eve
 
                 p->upstream_blocked = 1;
 
-                ngx_log_debug(p->log, "downstream ready");
+                ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
+                               "pipe downstream ready");
 
                 break;
 
@@ -184,7 +187,8 @@ int ngx_event_pipe_read_upstream(ngx_eve
 
                 rc = ngx_event_pipe_write_chain_to_temp_file(p);
 
-                ngx_log_debug(p->log, "temp offset: %d" _ p->temp_file->offset);
+                ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
+                               "pipe temp offset: %d", p->temp_file->offset);
 
                 if (rc == NGX_AGAIN) {
                     if (ngx_event_flags & NGX_USE_LEVEL_EVENT
@@ -215,14 +219,16 @@ int ngx_event_pipe_read_upstream(ngx_eve
 
                 /* if there're no hunks to read in then disable a level event */
 
-                ngx_log_debug(p->log, "no hunks to read in");
+                ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
+                               "no pipe hunks to read in");
     
                 break;
             }
 
             n = ngx_recv_chain(p->upstream, chain);
 
-            ngx_log_debug(p->log, "recv_chain: %d" _ n);
+            ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
+                           "pipe recv chain: %d", n);
 
             if (p->free_raw_hunks) {
                 chain->next = p->free_raw_hunks;
@@ -312,7 +318,8 @@ int ngx_event_pipe_write_to_downstream(n
     ngx_hunk_t   *h;
     ngx_chain_t  *out, **ll, *cl, *tl;
 
-    ngx_log_debug(p->log, "write downstream: %d" _ p->downstream->write->ready);
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
+                   "pipe write downstream: %d", p->downstream->write->ready);
 
     for ( ;; ) {
         if (p->downstream_error) {
@@ -383,7 +390,8 @@ int ngx_event_pipe_write_to_downstream(n
         }
 
         if (out == NULL) {
-            ngx_log_debug(p->log, "no hunks to write BUSY: %d" _ to_write);
+            ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
+                           "pipe busy hunk data: %d", to_write);
 
             if (!(p->upstream_blocked && to_write)) {
                 break;
@@ -472,12 +480,14 @@ static int ngx_event_pipe_write_chain_to
         cl = out;
         ll = NULL;
 
-ngx_log_debug(p->log, "offset: %d" _ p->temp_file->offset);
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
+                       "pipe offset: %d", p->temp_file->offset);
 
         do {
             hsize = cl->hunk->last - cl->hunk->pos;
 
-ngx_log_debug(p->log, "hunk size: %d" _ hsize);
+            ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
+                           "pipe hunk size: %d", hsize);
 
             if ((size + hsize > p->temp_file_write_size)
                || (p->temp_file->offset + size + hsize > p->max_temp_file_size))
@@ -491,7 +501,7 @@ ngx_log_debug(p->log, "hunk size: %d" _ 
 
         } while (cl);
 
-ngx_log_debug(p->log, "size: %d" _ size);
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "size: %d", size);
 
         if (cl) {
            p->in = cl;
@@ -584,7 +594,9 @@ int ngx_event_pipe_copy_input_filter(ngx
     hunk->shadow = h;
 
     ngx_alloc_link_and_set_hunk(cl, h, p->pool, NGX_ERROR);
-ngx_log_debug(p->log, "HUNK %d" _ h->num);
+
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "hunk #%d", h->num);
+
     ngx_chain_add_link(p->in, p->last_in, cl);
 
     return NGX_OK;
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -377,9 +377,11 @@ static int ngx_http_gzip_body_filter(ngx
                 ctx->zstream.avail_out = conf->bufs.size;
             }
 
-ngx_log_debug(r->connection->log, "deflate(): %08x %08x %d %d %d" _
-              ctx->zstream.next_in _ ctx->zstream.next_out _
-              ctx->zstream.avail_in _ ctx->zstream.avail_out _ ctx->flush);
+            ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "deflate in: ni:%X no:%X ai:%d ao:%d fl:%d",
+                           ctx->zstream.next_in, ctx->zstream.next_out,
+                           ctx->zstream.avail_in, ctx->zstream.avail_out,
+                           ctx->flush);
 
             rc = deflate(&ctx->zstream, ctx->flush);
             if (rc != Z_OK && rc != Z_STREAM_END) {
@@ -388,9 +390,11 @@ ngx_log_debug(r->connection->log, "defla
                 return ngx_http_gzip_error(ctx);
             }
 
-ngx_log_debug(r->connection->log, "DEFLATE(): %08x %08x %d %d %d" _
-              ctx->zstream.next_in _ ctx->zstream.next_out _
-              ctx->zstream.avail_in _ ctx->zstream.avail_out _ rc);
+            ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "deflate out: ni:%X no:%X ai:%d ao:%d rc:%d",
+                           ctx->zstream.next_in, ctx->zstream.next_out,
+                           ctx->zstream.avail_in, ctx->zstream.avail_out,
+                           rc);
 
             ctx->in_hunk->pos = (char *) ctx->zstream.next_in;
             ctx->out_hunk->last = (char *) ctx->zstream.next_out;
@@ -512,7 +516,6 @@ static void *ngx_http_gzip_filter_alloc(
     int    alloc;
     void  *p;
 
-
     alloc = items * size;
     if (alloc % 512 != 0) {
 
@@ -527,10 +530,9 @@ static void *ngx_http_gzip_filter_alloc(
         ctx->free_mem += alloc;
         ctx->allocated -= alloc;
 
-#if 1
-        ngx_log_debug(ctx->request->connection->log, "ALLOC: %d:%d:%d:%08X" _
-                      items _ size _ alloc _ p);
-#endif
+        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
+                       "gzip alloc: n:%d s:%d a:%d p:%08X",
+                       items, size, alloc, p);
 
         return p;
     }
@@ -550,7 +552,8 @@ static void ngx_http_gzip_filter_free(vo
     ngx_http_gzip_ctx_t *ctx = opaque;
 
 #if 0
-    ngx_log_debug(ctx->request->connection->log, "FREE: %08X" _ address);
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
+                   "gzip free: %X", address);
 #endif
 }
 
--- a/src/http/modules/ngx_http_index_handler.c
+++ b/src/http/modules/ngx_http_index_handler.c
@@ -286,7 +286,8 @@ static ngx_int_t ngx_http_index_test_dir
     ctx->path.data[ctx->path.len - 1] = '\0';
     ctx->path.data[ctx->path.len] = '\0';
 
-ngx_log_debug(r->connection->log, "IS_DIR: %s" _ ctx->path.data);
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http check dir: \"%s\"", ctx->path.data);
 
     if (ngx_file_info(ctx->path.data, &r->file.info) == -1) {
 
--- a/src/http/modules/ngx_http_not_modified_filter.c
+++ b/src/http/modules/ngx_http_not_modified_filter.c
@@ -49,8 +49,8 @@ static int ngx_http_not_modified_header_
     ims = ngx_http_parse_time(r->headers_in.if_modified_since->value.data,
                               r->headers_in.if_modified_since->value.len);
     
-    ngx_log_debug(r->connection->log, "%d %d" _
-                  ims _ r->headers_out.last_modified_time);
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http ims:%d lm:%d", ims, r->headers_out.last_modified_time);
 
     /*
      * I think that the equality of the dates is correcter
--- a/src/http/modules/ngx_http_static_handler.c
+++ b/src/http/modules/ngx_http_static_handler.c
@@ -59,7 +59,8 @@ static ngx_int_t ngx_http_static_handler
     char                        *last;
     uint32_t                     file_crc, redirect_crc;
     ngx_fd_t                     fd;
-    ngx_int_t                    rc, level;
+    ngx_int_t                    rc;
+    ngx_uint_t                   level;
     ngx_str_t                    name, location;
     ngx_err_t                    err;
     ngx_log_t                   *log;
--- a/src/http/modules/proxy/ngx_http_proxy_cache.c
+++ b/src/http/modules/proxy/ngx_http_proxy_cache.c
@@ -167,8 +167,9 @@ static int ngx_http_proxy_process_cached
 
     ngx_cpystrn(c->status_line.data, p->status_start, c->status_line.len + 1);
 
-    ngx_log_debug(r->connection->log, "http cache status %d '%s'" _ 
-                  c->status _ c->status_line.data);
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http cache status %d \"%s\"", 
+                   c->status, c->status_line.data);
 
     /* TODO: ngx_init_table */
     c->headers_in.headers = ngx_create_table(r->pool, 20);
@@ -212,8 +213,9 @@ static int ngx_http_proxy_process_cached
                 }
             }
 
-            ngx_log_debug(r->connection->log, "HTTP cache header: '%s: %s'" _
-                          h->key.data _ h->value.data);
+            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "http cache header: \"%s: %s\"",
+                           h->key.data, h->value.data);
 
             continue;
 
@@ -221,7 +223,8 @@ static int ngx_http_proxy_process_cached
 
             /* a whole header has been parsed successfully */
 
-            ngx_log_debug(r->connection->log, "HTTP header done");
+            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "http cache header done");
 
             c->ctx.file_start = p->header_in->pos - p->header_in->start;
 
@@ -253,7 +256,8 @@ void ngx_http_proxy_cache_busy_lock(ngx_
     rc = ngx_http_busy_lock_cachable(p->lcf->busy_lock, &p->busy_lock,
                                      p->try_busy_lock);
 
-ngx_log_debug(p->request->connection->log, "LOCK CACHABLE: %d" _ rc);
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, p->request->connection->log, 0,
+                   "http cache busy lock cachable: %d", rc);
 
     if (rc == NGX_OK) {
         if (p->try_busy_lock) {
@@ -354,8 +358,9 @@ static void ngx_http_proxy_cache_look_co
         return;
     }
 
-ngx_log_debug(p->request->connection->log, "OLD: %d, NEW: %d" _
-              p->cache->ctx.file.fd _ ctx->file.fd);
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, p->request->connection->log, 0,
+                   "http cache old fd:%d, new fd:%d",
+                   p->cache->ctx.file.fd, ctx->file.fd);
 
     if (p->cache->ctx.file.fd != NGX_INVALID_FILE) {
         if (ngx_close_file(p->cache->ctx.file.fd) == NGX_FILE_ERROR) {
@@ -603,8 +608,9 @@ int ngx_http_proxy_update_cache(ngx_http
 
     ep = p->upstream->event_pipe;
 
-ngx_log_debug(p->request->connection->log, "LEN: " OFF_T_FMT ", " OFF_T_FMT _
-              p->cache->ctx.length _ ep->read_length);
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, p->request->connection->log, 0,
+                   "http cache update len: " OFF_T_FMT ":" OFF_T_FMT,
+                   p->cache->ctx.length, ep->read_length);
 
     if (p->cache->ctx.length == -1) {
         /* TODO: test rc */
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -346,7 +346,7 @@ void ngx_http_proxy_check_broken_connect
     ngx_http_request_t    *r;
     ngx_http_proxy_ctx_t  *p;
 
-    ngx_log_debug(wev->log, "http proxy check client");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http proxy check client");
 
     c = wev->data;
     r = c->data;
@@ -362,14 +362,16 @@ void ngx_http_proxy_check_broken_connect
 
         if (!p->cachable && p->upstream->peer.connection) {
             ngx_log_error(NGX_LOG_INFO, wev->log, wev->kq_errno,
-                          "client closed prematurely connection, "
+                          "kevent() reported that client have closed "
+                          "prematurely connection, "
                           "so upstream connection is closed too");
             ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
             return;
         }
 
         ngx_log_error(NGX_LOG_INFO, wev->log, wev->kq_errno,
-                      "client closed prematurely connection");
+                      "kevent() reported that client have closed "
+                      "prematurely connection");
 
         if (p->upstream == NULL || p->upstream->peer.connection == NULL) {
             ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
@@ -385,7 +387,7 @@ void ngx_http_proxy_busy_lock_handler(ng
     ngx_http_request_t    *r;
     ngx_http_proxy_ctx_t  *p;
 
-    ngx_log_debug(rev->log, "busy lock");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http proxy busy lock");
 
     c = rev->data;
     r = c->data;
@@ -412,11 +414,12 @@ void ngx_http_proxy_busy_lock_handler(ng
         return;
     }
 
-    ngx_log_debug(rev->log, "client sent while busy lock");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
+                   "http proxy: client sent while busy lock");
 
     /*
      * TODO: kevent() notify about error, otherwise we need to
-     * call ngx_peek(): recv(MSG_PEEK) to get errno. THINK about aio
+     * call ngx_peek(): recv(MSG_PEEK) to get errno. THINK about aio.
      * if there's no error we need to disable event.
      */
 
@@ -452,7 +455,8 @@ void ngx_http_proxy_finalize_request(ngx
 
     r = p->request;
 
-    ngx_log_debug(r->connection->log, "finalize http proxy request");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "finalize http proxy request");
 
     if (p->upstream && p->upstream->peer.connection) {
         ngx_http_proxy_close_connection(p);
@@ -470,12 +474,15 @@ void ngx_http_proxy_finalize_request(ngx
     }
 
     if (p->upstream && p->upstream->event_pipe) {
-ngx_log_debug(r->connection->log, "TEMP FD: %d" _
-              p->upstream->event_pipe->temp_file->file.fd);
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                       "http proxy temp fd: %d",
+                       p->upstream->event_pipe->temp_file->file.fd);
     }
 
     if (p->cache) {
-ngx_log_debug(r->connection->log, "CACHE FD: %d" _ p->cache->ctx.file.fd);
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                       "http proxy cache fd: %d",
+                       p->cache->ctx.file.fd);
     }
 
     if (p->upstream && p->upstream->event_pipe) {
@@ -504,7 +511,8 @@ void ngx_http_proxy_close_connection(ngx
         p->lcf->busy_lock->busy--;
     }
 
-    ngx_log_debug(c->log, "proxy close connection: %d" _ c->fd);
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                   "http proxy close connection: %d", c->fd);
 
     if (c->fd == -1) {
 #if 0
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -192,15 +192,19 @@ static ngx_chain_t *ngx_http_proxy_creat
 
         *(h->last++) = CR; *(h->last++) = LF;
 
-        ngx_log_debug(r->connection->log, "proxy: '%s: %s'" _
-                      header[i].key.data _ header[i].value.data);
+        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                       "http proxy header: \"%s: %s\"",
+                       header[i].key.data, header[i].value.data);
     }
 
     /* add "\r\n" at the header end */
     *(h->last++) = CR; *(h->last++) = LF;
 
-    /* STUB */ *(h->last) = '\0';
-    ngx_log_debug(r->connection->log, "PROXY:\n'%s'" _ h->pos);
+#if (NGX_DEBUG)
+    *(h->last) = '\0';
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http proxy header:\n\"%s\"", h->pos);
+#endif
 
     return chain;
 }
@@ -218,8 +222,9 @@ static void ngx_http_proxy_init_upstream
 
     r = p->request;
 
-ngx_log_debug(r->connection->log, "timer_set: %d" _
-              r->connection->read->timer_set);
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                  "http proxy set timer: %d",
+                  r->connection->read->timer_set);
 
     if (r->connection->read->timer_set) {
         ngx_del_timer(r->connection->read);
@@ -588,7 +593,7 @@ static void ngx_http_proxy_send_request_
 
 static void ngx_http_proxy_dummy_handler(ngx_event_t *wev)
 {
-    ngx_log_debug(wev->log, "dummy handler");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http proxy dummy handler");
 }
 
 
@@ -603,7 +608,8 @@ static void ngx_http_proxy_process_upstr
     p = c->data;
     p->action = "reading upstream status line";
 
-    ngx_log_debug(rev->log, "http proxy process status line");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
+                   "http proxy process status line");
 
     if (rev->timedout) {
         ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
@@ -717,8 +723,9 @@ static void ngx_http_proxy_process_upstr
     ngx_cpystrn(p->upstream->status_line.data, p->status_start,
                 p->upstream->status_line.len + 1);
 
-    ngx_log_debug(rev->log, "http proxy status %d '%s'" _
-                  p->upstream->status _ p->upstream->status_line.data);
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, rev->log, 0,
+                   "http proxy status %d \"%s\"",
+                   p->upstream->status, p->upstream->status_line.data);
 
     if (p->upstream->headers_in.headers) {
         p->upstream->headers_in.headers->nelts = 0;
@@ -747,7 +754,8 @@ static void ngx_http_proxy_process_upstr
     r = p->request;
     p->action = "reading upstream headers";
 
-    ngx_log_debug(rev->log, "http proxy process header line");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
+                   "http proxy process header line");
 
     if (rev->timedout) {
         ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
@@ -818,8 +826,9 @@ static void ngx_http_proxy_process_upstr
                 }
             }
 
-            ngx_log_debug(c->log, "HTTP proxy header: '%s: %s'" _
-                          h->key.data _ h->value.data);
+            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                           "http proxy header: \"%s: %s\"",
+                           h->key.data, h->value.data);
 
             continue;
 
@@ -827,7 +836,8 @@ static void ngx_http_proxy_process_upstr
 
             /* a whole header has been parsed successfully */
 
-            ngx_log_debug(c->log, "HTTP header done");
+            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                           "http proxy header done");
 
             /* TODO: hook to process the upstream header */
 
@@ -1072,13 +1082,15 @@ static void ngx_http_proxy_process_body(
     c = ev->data;
 
     if (ev->write) {
-        ngx_log_debug(ev->log, "http proxy process downstream");
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
+                       "http proxy process downstream");
         r = c->data;
         p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
         p->action = "sending to client";
 
     } else {
-        ngx_log_debug(ev->log, "http proxy process upstream");
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
+                       "http proxy process upstream");
         p = c->data;
         r = p->request;
         p->action = "reading upstream body";
@@ -1125,7 +1137,8 @@ static void ngx_http_proxy_process_body(
         }
 
         if (ep->upstream_done || ep->upstream_eof || ep->upstream_error) {
-            ngx_log_debug(ev->log, "http proxy upstream exit");
+            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
+                           "http proxy upstream exit");
             ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
             ngx_http_proxy_finalize_request(p, 0);
             return;
@@ -1133,30 +1146,12 @@ static void ngx_http_proxy_process_body(
     }
 
     if (ep->downstream_error) {
-        ngx_log_debug(ev->log, "http proxy downstream error");
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
+                       "http proxy downstream error");
         if (!p->cachable && p->upstream->peer.connection) {
             ngx_http_proxy_finalize_request(p, 0);
         }
     }
-
-#if 0
-    if (ep->downstream_done) {
-        ngx_log_debug(ev->log, "http proxy downstream done");
-        ngx_http_proxy_finalize_request(p, 0);
-        return;
-    }
-
-    if (ep->downstream_error) {
-        ngx_log_debug(ev->log, "http proxy downstream error");
-        if (!p->cachable && p->upstream->peer.connection) {
-            ngx_http_proxy_close_connection(p);
-        }
- 
-        if (p->upstream->peer.connection == NULL) {
-            ngx_http_close_request(r);
-        }
-    }
-#endif
 }
 
 
@@ -1164,7 +1159,8 @@ static void ngx_http_proxy_next_upstream
 {
     int  status;
 
-ngx_log_debug(p->request->connection->log, "next upstream: %d" _ ft_type);
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, p->request->connection->log, 0,
+                   "http proxy next upstream: %d", ft_type);
 
     ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
 
--- a/src/http/ngx_http.c
+++ b/src/http/ngx_http.c
@@ -589,23 +589,26 @@ static char *ngx_http_block(ngx_conf_t *
         }
     }
 
-    /* DEBUG STUFF */
+#if (NGX_DEBUG)
     in_port = in_ports.elts;
     for (p = 0; p < in_ports.nelts; p++) {
-ngx_log_debug(cf->log, "port: %d %08x" _ in_port[p].port _ &in_port[p]);
+        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
+                      "port: %d %08x", in_port[p].port, &in_port[p]);
         in_addr = in_port[p].addrs.elts;
         for (a = 0; a < in_port[p].addrs.nelts; a++) {
             char ip[20];
             ngx_inet_ntop(AF_INET, (char *) &in_addr[a].addr, ip, 20);
-ngx_log_debug(cf->log, "%s %08x" _ ip _ in_addr[a].core_srv_conf);
+            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
+                           "%s %08x", ip, in_addr[a].core_srv_conf);
             s_name = in_addr[a].names.elts;
             for (n = 0; n < in_addr[a].names.nelts; n++) {
-ngx_log_debug(cf->log, "%s %08x" _ s_name[n].name.data _
-              s_name[n].core_srv_conf);
+                 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
+                                "%s %08x", s_name[n].name.data,
+                                s_name[n].core_srv_conf);
             }
         }
     }
-    /**/
+#endif
 
     return NGX_CONF_OK;
 }
--- a/src/http/ngx_http_busy_lock.c
+++ b/src/http/ngx_http_busy_lock.c
@@ -59,8 +59,9 @@ int ngx_http_busy_lock_cachable(ngx_http
 
     rc = ngx_http_busy_lock_look_cachable(bl, bc, lock);
 
-ngx_log_debug(bc->event->log, "BUSYLOCK: %d %d:%d" _
-              rc _ bl->waiting _ bl->max_waiting);
+    ngx_log_debug3(NGX_LOG_DEBUG_HTTP, bc->event->log, 0,
+                   "http busylock: %d w:%d mw::%d",
+                   rc, bl->waiting, bl->max_waiting);
 
     if (rc == NGX_OK) {  /* no the same request, there's free slot */
         return NGX_OK;
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -305,7 +305,7 @@ static void ngx_http_phase_event_handler
     c = ev->data;
     r = c->data;
 
-    ngx_log_debug(ev->log, "phase event handler");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0, "phase event handler");
 
     ngx_http_run_phases(r);
 
@@ -612,19 +612,6 @@ int ngx_http_redirect(ngx_http_request_t
 }
 
 
-int ngx_http_error(ngx_http_request_t *r, int error)
-{
-    /* STUB */
-    ngx_log_debug(r->connection->log, "http error: %d" _ error);
-
-    /* log request */
-
-    ngx_http_special_response_handler(r, error);
-    ngx_http_close_request(r, 0);
-    return NGX_OK;
-}
-
-
 ngx_int_t ngx_http_set_exten(ngx_http_request_t *r)
 {
     ngx_int_t  i;
@@ -706,13 +693,15 @@ int ngx_http_delay_handler(ngx_http_requ
     static int  on;
 
     if (on++ == 0) {
-        ngx_log_debug(r->connection->log, "SET http delay");
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                       "http set delay");
         ngx_add_timer(r->connection->write, 10000);
         return NGX_AGAIN;
     }
 
     r->connection->write->timedout = 0;
-    ngx_log_debug(r->connection->log, "RESET http delay");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http reset delay");
     return NGX_DECLINED;
 }
 
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -161,7 +161,6 @@ ngx_int_t ngx_http_set_exten(ngx_http_re
 
 int ngx_http_internal_redirect(ngx_http_request_t *r,
                                ngx_str_t *uri, ngx_str_t *args);
-int ngx_http_error(ngx_http_request_t *r, int error);
 
 
 #endif /* _NGX_HTTP_CORE_H_INCLUDED_ */
--- a/src/http/ngx_http_file_cache.c
+++ b/src/http/ngx_http_file_cache.c
@@ -36,12 +36,14 @@ int ngx_http_cache_get_file(ngx_http_req
     ngx_md5_text(ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len,
                  ctx->md5);
 
-ngx_log_debug(r->connection->log, "URL: %s, md5: %s" _ ctx->key.data _
-              ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len);
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+               "file cache uri: %s, md5: %s", ctx->key.data,
+               ctx->file.name.data + ctx->path->name.len + 1 + ctx->path->len);
 
     ngx_create_hashed_filename(&ctx->file, ctx->path);
 
-ngx_log_debug(r->connection->log, "FILE: %s" _ ctx->file.name.data);
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "file cache name: %s", ctx->file.name.data);
 
     /* TODO: look open files cache */
 
@@ -129,7 +131,10 @@ int ngx_http_cache_open_file(ngx_http_ca
     ctx->buf->last += n;
 
     if (ctx->expires < ngx_time()) {
-ngx_log_debug(ctx->log, "EXPIRED");
+
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
+                       "http file cache expired");
+
         return NGX_HTTP_CACHE_STALE;
     }
 
--- a/src/http/ngx_http_header_filter.c
+++ b/src/http/ngx_http_header_filter.c
@@ -333,10 +333,10 @@ static int ngx_http_header_filter(ngx_ht
         *(h->last++) = CR; *(h->last++) = LF;
     }
 
-    /* STUB */
+#if (NGX_DEBUG)
     *(h->last) = '\0';
-    ngx_log_debug(r->connection->log, "%s\n" _ h->pos);
-    /**/
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "%s\n", h->pos);
+#endif
 
     /* the end of HTTP header */
     *(h->last++) = CR; *(h->last++) = LF;
--- a/src/http/ngx_http_headers.c
+++ b/src/http/ngx_http_headers.c
@@ -11,16 +11,17 @@ ngx_http_header_t  ngx_http_headers_in[]
                          offsetof(ngx_http_headers_in_t, if_modified_since) },
     { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent) },
     { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer) },
-
     { ngx_string("Content-Length"),
                             offsetof(ngx_http_headers_in_t, content_length) },
-    { ngx_string("Accept-Encoding"),
-                           offsetof(ngx_http_headers_in_t, accept_encoding) },
+
     { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range) },
 #if 0
     { ngx_string("If-Range"), offsetof(ngx_http_headers_in_t, if_range) },
 #endif
 
+    { ngx_string("Accept-Encoding"),
+                           offsetof(ngx_http_headers_in_t, accept_encoding) },
+
     { ngx_string("Authorization"),
                              offsetof(ngx_http_headers_in_t, authorization) },
 
--- a/src/http/ngx_http_parse.c
+++ b/src/http/ngx_http_parse.c
@@ -648,8 +648,8 @@ ngx_int_t ngx_http_parse_complex_uri(ngx
 
     while (p < r->uri_start + r->uri.len + 1) {
 
-ngx_log_debug(r->connection->log, "S: %d UN: '%x:%c', URI: '%c'" _
-              state _ ch _ ch _ *u);
+        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                       "s:%d in:'%x:%c', out:'%c'", state, ch, ch, *u);
 
         switch (state) {
         case sw_usual:
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -84,7 +84,7 @@ void ngx_http_init_connection(ngx_connec
     rev = c->read;
     rev->event_handler = ngx_http_init_request;
 
-    /* STUB: epoll */ c->write->event_handler = ngx_http_empty_handler;
+    /* STUB: epoll edge */ c->write->event_handler = ngx_http_empty_handler;
 
     if (rev->ready) {
         /* deferred accept, aio, iocp, epoll */
@@ -1180,6 +1180,8 @@ static void ngx_http_set_keepalive(ngx_h
     }
 
     h = c->buffer;
+    wev = c->write;
+    wev->event_handler = ngx_http_empty_handler;
 
     if (h->pos < h->last) {
 
@@ -1214,8 +1216,6 @@ static void ngx_http_set_keepalive(ngx_h
 
     h->pos = h->last = h->start;
     rev->event_handler = ngx_http_keepalive_handler;
-    wev = c->write;
-    wev->event_handler = ngx_http_empty_handler;
 
     if (wev->active) {
         if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
@@ -1522,7 +1522,7 @@ void ngx_http_close_request(ngx_http_req
 void ngx_http_close_connection(ngx_connection_t *c)
 {
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
-                   "close connection: %d", c->fd);
+                   "close http connection: %d", c->fd);
 
     if (c->pool == NULL) {
         ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed");
--- a/src/http/ngx_http_write_filter.c
+++ b/src/http/ngx_http_write_filter.c
@@ -6,7 +6,7 @@
 
 
 typedef struct {
-    ssize_t  buffer_output;
+    ssize_t  postpone_output;
 } ngx_http_write_filter_conf_t;
 
 
@@ -23,11 +23,19 @@ static int ngx_http_write_filter_init(ng
 
 static ngx_command_t  ngx_http_write_filter_commands[] = {
 
+    /* STUB */
     { ngx_string("buffer_output"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_size_slot,
       NGX_HTTP_LOC_CONF_OFFSET,
-      offsetof(ngx_http_write_filter_conf_t, buffer_output),
+      offsetof(ngx_http_write_filter_conf_t, postpone_output),
+      NULL },
+
+    { ngx_string("postpone_output"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_size_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_write_filter_conf_t, postpone_output),
       NULL },
 
       ngx_null_command
@@ -112,21 +120,19 @@ int ngx_http_write_filter(ngx_http_reque
         }
     }
 
-#if (NGX_DEBUG_WRITE_FILTER)
-    ngx_log_debug(r->connection->log,
-                  "write filter: last:%d flush:%qd size:%qd" _
-                  last _ flush _ size);
-#endif
+    ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http write filter: l:%d f:" OFF_T_FMT " s:" OFF_T_FMT,
+                   last, flush, size);
 
     conf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
                                         ngx_http_write_filter_module);
 
     /*
      * avoid the output if there is no last hunk, no flush point and
-     * the size of the hunks is smaller than "buffer_output" directive
+     * the size of the hunks is smaller than "postpone_output" directive
      */
 
-    if (!last && flush == 0 && size < conf->buffer_output) {
+    if (!last && flush == 0 && size < conf->postpone_output) {
         return NGX_OK;
     }
 
@@ -140,9 +146,8 @@ int ngx_http_write_filter(ngx_http_reque
 
     chain = ngx_write_chain(r->connection, ctx->out);
 
-#if (NGX_DEBUG_WRITE_FILTER)
-    ngx_log_debug(r->connection->log, "write filter %x" _ chain);
-#endif
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http write filter %X", chain);
 
     if (chain == NGX_CHAIN_ERROR) {
         return NGX_ERROR;
@@ -166,7 +171,7 @@ static void *ngx_http_write_filter_creat
                   ngx_palloc(cf->pool, sizeof(ngx_http_write_filter_conf_t)),
                   NULL);
 
-    conf->buffer_output = NGX_CONF_UNSET;
+    conf->postpone_output = NGX_CONF_UNSET;
 
     return conf;
 }
@@ -178,7 +183,8 @@ static char *ngx_http_write_filter_merge
     ngx_http_write_filter_conf_t *prev = parent;
     ngx_http_write_filter_conf_t *conf = child;
 
-    ngx_conf_merge_size_value(conf->buffer_output, prev->buffer_output, 1460);
+    ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
+                              1460);
 
     return NULL;
 }
--- a/src/os/unix/ngx_aio_read.c
+++ b/src/os/unix/ngx_aio_read.c
@@ -29,8 +29,10 @@ ssize_t ngx_aio_read(ngx_connection_t *c
         return NGX_AGAIN;
     }
 
-    ngx_log_debug(c->log, "rev->complete: %d" _ rev->complete);
-    ngx_log_debug(c->log, "aio size: %d" _ size);
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                   "rev->complete: %d", rev->complete);
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                   "aio size: %d", size);
 
     if (!rev->complete) {
         ngx_memzero(&rev->aiocb, sizeof(struct aiocb));
@@ -52,7 +54,8 @@ ssize_t ngx_aio_read(ngx_connection_t *c
             return NGX_ERROR;
         }
 
-        ngx_log_debug(c->log, "aio_read: #%d OK" _ c->fd);
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                       "aio_read: #%d OK", c->fd);
 
         rev->active = 1;
         rev->ready = 0;
@@ -93,7 +96,8 @@ ssize_t ngx_aio_read(ngx_connection_t *c
         return NGX_ERROR;
     }
 
-    ngx_log_debug(rev->log, "aio_read: #%d %d" _ c->fd _ n);
+    ngx_log_debug2(NGX_LOG_DEBUG_EVENT, rev->log, 0,
+                   "aio_read: #%d %d", c->fd, n);
 
     if (n == 0) {
         rev->eof = 1;
--- a/src/os/unix/ngx_aio_read_chain.c
+++ b/src/os/unix/ngx_aio_read_chain.c
@@ -41,7 +41,7 @@ ssize_t ngx_aio_read_chain(ngx_connectio
 
         n = ngx_aio_read(c, buf, size);
 
-        ngx_log_debug(c->log, "aio_read: %d" _ n);
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "aio_read: %d", n);
 
         if (n == NGX_AGAIN) {
             return total ? total : NGX_AGAIN;
@@ -64,7 +64,8 @@ ssize_t ngx_aio_read_chain(ngx_connectio
             total += n;
         }
 
-        ngx_log_debug(c->log, "aio_read total: %d" _ total);
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                       "aio_read total: %d", total);
     }
 
     return total ? total : NGX_AGAIN;
--- a/src/os/unix/ngx_aio_write.c
+++ b/src/os/unix/ngx_aio_write.c
@@ -28,7 +28,8 @@ ssize_t ngx_aio_write(ngx_connection_t *
         return NGX_AGAIN;
     }
 
-ngx_log_debug(wev->log, "aio: wev->complete: %d" _ wev->complete);
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, wev->log, 0,
+                   "aio: wev->complete: %d", wev->complete);
 
     if (!wev->complete) {
         ngx_memzero(&wev->aiocb, sizeof(struct aiocb));
@@ -49,7 +50,7 @@ ngx_log_debug(wev->log, "aio: wev->compl
             return NGX_ERROR;
         }
 
-        ngx_log_debug(wev->log, "aio_write: OK");
+        ngx_log_debug0(NGX_LOG_DEBUG_EVENT, wev->log, 0, "aio_write: OK");
 
         wev->active = 1;
         wev->ready = 0;
@@ -102,7 +103,7 @@ ngx_log_debug(wev->log, "aio: wev->compl
     }
 
 
-    ngx_log_debug(wev->log, "aio_write: %d" _ n);
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, wev->log, 0, "aio_write: %d", n);
 
     wev->active = 0;
     wev->ready = 1;
--- a/src/os/unix/ngx_aio_write_chain.c
+++ b/src/os/unix/ngx_aio_write_chain.c
@@ -44,9 +44,7 @@ ngx_chain_t *ngx_aio_write_chain(ngx_con
 
         n = ngx_aio_write(c, buf, size);
 
-#if (NGX_DEBUG_WRITE_CHAIN)
-        ngx_log_debug(c->log, "aio_write: %d" _ n);
-#endif
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "aio_write: %d", n);
 
         if (n == NGX_ERROR) {
             return NGX_CHAIN_ERROR;
@@ -57,9 +55,8 @@ ngx_chain_t *ngx_aio_write_chain(ngx_con
             c->sent += n;
         }
 
-#if (NGX_DEBUG_WRITE_CHAIN)
-        ngx_log_debug(c->log, "aio_write sent: " OFF_T_FMT _ c->sent);
-#endif
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                       "aio_write sent: " OFF_T_FMT, c->sent);
 
         for (cl = in; cl; cl = cl->next) {
 
--- a/src/os/unix/ngx_files.c
+++ b/src/os/unix/ngx_files.c
@@ -11,15 +11,16 @@ ssize_t ngx_read_file(ngx_file_t *file, 
 {
     ssize_t n;
 
-    ngx_log_debug(file->log, "read: %d, %x, %d, " OFF_T_FMT _
-                  file->fd _ buf _ size _ offset);
+    ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
+                   "read: %d, %X, %d, " OFF_T_FMT, file->fd, buf, size, offset);
 
 #if (HAVE_PREAD)
 
     n = pread(file->fd, buf, size, offset);
 
     if (n == -1) {
-        ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno, "pread() failed");
+        ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno,
+                      "pread() failed, file \"%s\"", file->name.data);
         return NGX_ERROR;
     }
 
--- a/src/os/unix/ngx_linux_config.h
+++ b/src/os/unix/ngx_linux_config.h
@@ -36,9 +36,6 @@
 #include <dirent.h>
 
 
-/* Linux has a broken strerror_r() */
-#define HAVE_STRERROR_R  0
-
 #include <ngx_auto_config.h>
 
 
--- a/src/os/unix/ngx_readv_chain.c
+++ b/src/os/unix/ngx_readv_chain.c
@@ -4,8 +4,6 @@
 #include <ngx_event.h>
 
 
-static int ngx_readv_error(ngx_event_t *rev, ngx_err_t err);
-
 #if (HAVE_KQUEUE)
 
 ssize_t ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)
@@ -20,18 +18,22 @@ ssize_t ngx_readv_chain(ngx_connection_t
     rev = c->read; 
 
     if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
-        ngx_log_debug(c->log, "recv: eof:%d, avail:%d, err:%d" _
-                      rev->kq_eof _ rev->available _ rev->kq_errno);
+        ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                       "readv: eof:%d, avail:%d, err:%d",
+                       rev->kq_eof, rev->available, rev->kq_errno);
 
         if (rev->available == 0) {
             if (rev->kq_eof) {
                 rev->ready = 0;
                 rev->eof = 1;
 
+                ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
+                              "kevent() reported about an closed connection");
+
                 if (rev->kq_errno) {
                     rev->error = 1;
                     ngx_set_socket_errno(rev->kq_errno);
-                    return ngx_readv_error(rev, rev->kq_errno);
+                    return NGX_ERROR;
                 }
 
                 return 0;
@@ -65,7 +67,8 @@ ssize_t ngx_readv_chain(ngx_connection_t
         chain = chain->next;
     }
 
-ngx_log_debug(c->log, "recv: %d:%d" _ io.nelts _ iov->iov_len);
+    ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                   "readv: %d:%d", io.nelts, iov->iov_len);
 
     rev = c->read;
 
@@ -105,11 +108,19 @@ ngx_log_debug(c->log, "recv: %d:%d" _ io
             return n;
         }
 
-        n = ngx_readv_error(rev, ngx_socket_errno);
+        err = ngx_socket_errno;
+
+        if (err == NGX_EAGAIN || err == NGX_EINTR) {
+            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
+                           "readv() not ready");
+            n = NGX_AGAIN;
 
-    } while (n == NGX_EINTR);
+        } else {
+            n = ngx_connection_error(c, err, "readv() failed");
+            break;
+        }
 
-    /* NGX_ERROR || NGX_AGAIN */
+    } while (err == NGX_EINTR);
 
     rev->ready = 0;
 
@@ -154,7 +165,8 @@ ssize_t ngx_readv_chain(ngx_connection_t
         chain = chain->next;
     }
 
-ngx_log_debug(c->log, "recv: %d:%d" _ io.nelts _ iov->iov_len);
+    ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                   "readv: %d:%d", io.nelts, iov->iov_len);
 
     rev = c->read;
 
@@ -173,11 +185,19 @@ ngx_log_debug(c->log, "recv: %d:%d" _ io
             return n;
         }
 
-        n = ngx_readv_error(rev, ngx_socket_errno);
+        err = ngx_socket_errno;
+
+        if (err == NGX_EAGAIN || err == NGX_EINTR) {
+            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
+                           "readv() not ready");
+            n = NGX_AGAIN;
 
-    } while (n == NGX_EINTR);
+        } else {
+            n = ngx_connection_error(c, err, "readv() failed");
+            break;
+        }
 
-    /* NGX_ERROR || NGX_AGAIN */
+    } while (err == NGX_EINTR);
 
     rev->ready = 0;
 
@@ -189,21 +209,3 @@ ngx_log_debug(c->log, "recv: %d:%d" _ io
 }
 
 #endif /* NAVE_KQUEUE */
-
-
-static int ngx_readv_error(ngx_event_t *rev, ngx_err_t err)
-{
-    if (err == NGX_EAGAIN) {
-        ngx_log_error(NGX_LOG_INFO, rev->log, err, "readv() returned EAGAIN");
-        return NGX_AGAIN;
-    }
-
-    if (err == NGX_EINTR) {
-        ngx_log_error(NGX_LOG_INFO, rev->log, err, "readv() returned EINTR");
-        return NGX_EINTR;
-    }
-
-    ngx_log_error(NGX_LOG_ERR, rev->log, err, "readv() failed");
-
-    return NGX_ERROR;
-}
--- a/src/os/unix/ngx_recv.c
+++ b/src/os/unix/ngx_recv.c
@@ -90,7 +90,7 @@ ssize_t ngx_unix_recv(ngx_connection_t *
         err = ngx_socket_errno;
 
         if (err == NGX_EAGAIN || err == NGX_EINTR) {
-            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, rev->log, err,
+            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
                            "recv() not ready");
             n = NGX_AGAIN;
 
@@ -141,7 +141,7 @@ ssize_t ngx_unix_recv(ngx_connection_t *
         err = ngx_socket_errno;
 
         if (err == NGX_EAGAIN || err == NGX_EINTR) {
-            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, rev->log, err,
+            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
                            "recv() not ready");
             n = NGX_AGAIN;
 
--- a/src/os/unix/ngx_solaris_sendfilev_chain.c
+++ b/src/os/unix/ngx_solaris_sendfilev_chain.c
@@ -82,24 +82,24 @@ ngx_chain_t *ngx_solaris_sendfilev_chain
         if (n == -1) {
             err = ngx_errno;
 
-            if (err == NGX_EINTR) {
-                eintr = 1;
-            }
+            if (err == NGX_EAGAIN || err == NGX_EINTR) {
+                if (err == NGX_EINTR) {
+                    eintr = 1;
+                }
 
-            if (err == NGX_EAGAIN || err == NGX_EINTR) {
-                ngx_log_error(NGX_LOG_INFO, c->log, err,
+                ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, err,
                               "sendfilev() sent only " SIZE_T_FMT " bytes",
                               sent);
+
             } else {
                 wev->error = 1;
-                ngx_log_error(NGX_LOG_CRIT, c->log, err, "sendfilev() failed");
+                ngx_connection_error(c, err, "sendfilev() failed");
                 return NGX_CHAIN_ERROR;
             }
         }
 
-#if (NGX_DEBUG_WRITE_CHAIN)
-        ngx_log_debug(c->log, "sendfilev: %d " SIZE_T_FMT _ n _ sent);
-#endif
+        ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                       "sendfilev: %d " SIZE_T_FMT, n, sent);
 
         c->sent += sent;
 
--- a/src/os/unix/ngx_writev_chain.c
+++ b/src/os/unix/ngx_writev_chain.c
@@ -22,6 +22,18 @@ ngx_chain_t *ngx_writev_chain(ngx_connec
         return in;
     }
 
+#if (HAVE_KQUEUE)
+
+    if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) && wev->kq_eof) {
+        ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
+                      "kevent() reported about an closed connection");
+
+        wev->error = 1;
+        return NGX_CHAIN_ERROR;
+    }
+
+#endif
+
     ngx_init_array(io, c->pool, 10, sizeof(struct iovec), NGX_CHAIN_ERROR);
 
     do {
@@ -49,25 +61,26 @@ ngx_chain_t *ngx_writev_chain(ngx_connec
 
         if (n == -1) {
             err = ngx_errno;
-            if (err == NGX_EAGAIN) {
-                ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EAGAIN");
 
-            } else if (err == NGX_EINTR) {
-                eintr = 1;
-                ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EINTR");
+            if (err == NGX_EAGAIN || err == NGX_EINTR) {
+                if (err == NGX_EINTR) {
+                    eintr = 1;
+                }
+
+                ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
+                               "writev() not ready");
 
             } else {
                 wev->error = 1;
-                ngx_log_error(NGX_LOG_CRIT, c->log, err, "writev() failed");
+                ngx_connection_error(c, err, "writev() failed");
                 return NGX_CHAIN_ERROR;
             }
         }
 
         sent = n > 0 ? n : 0;
 
-#if (NGX_DEBUG_WRITE_CHAIN)
-        ngx_log_debug(c->log, "writev: " OFF_T_FMT  _ sent);
-#endif
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
+                       "writev: " OFF_T_FMT, sent);
 
         c->sent += sent;
 
@@ -75,8 +88,6 @@ ngx_chain_t *ngx_writev_chain(ngx_connec
 
             size = cl->hunk->last - cl->hunk->pos;
 
-ngx_log_debug(c->log, "SIZE: %d" _ size);
-
             if (sent >= size) {
                 sent -= size;
 
--- a/src/os/win32/ngx_wsasend_chain.c
+++ b/src/os/win32/ngx_wsasend_chain.c
@@ -64,9 +64,7 @@ ngx_chain_t *ngx_wsasend_chain(ngx_conne
         }
     }
 
-#if (NGX_DEBUG_WRITE_CHAIN)
-    ngx_log_debug(c->log, "WSASend(): %d" _ sent);
-#endif
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "WSASend(): %d", sent);
 
     c->sent += sent;
 
@@ -207,9 +205,7 @@ ngx_chain_t *ngx_overlapped_wsasend_chai
         }
     }
 
-#if (NGX_DEBUG_WRITE_CHAIN)
-    ngx_log_debug(c->log, "WSASend(): %d" _ sent);
-#endif
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "WSASend(): %d", sent);
 
     c->sent += sent;
 
@@ -391,9 +387,7 @@ non-block
         }
     }
 
-#if (NGX_DEBUG_WRITE_CHAIN)
     ngx_log_debug(c->log, "WSASend(): %d" _ sent);
-#endif
 
     c->sent += sent;