]> granicus.if.org Git - apache/blobdiff - server/log.c
Stop explicitly including the current pid in WinNT MPM messages,
[apache] / server / log.c
index 7e224a6d02b32d8240ef58abb49cbc4930bfba94..50d9cbcb3ede20e6fef25e00f321ee94ac19fc86 100644 (file)
@@ -28,6 +28,8 @@
 #include "apr_thread_proc.h"
 #include "apr_lib.h"
 #include "apr_signal.h"
+#include "apr_portable.h"
+#include "apr_base64.h"
 
 #define APR_WANT_STDIO
 #define APR_WANT_STRFUNC
@@ -39,6 +41,9 @@
 #if APR_HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#if APR_HAVE_PROCESS_H
+#include <process.h>            /* for getpid() on Win32 */
+#endif
 
 #include "ap_config.h"
 #include "httpd.h"
@@ -49,6 +54,8 @@
 #include "util_time.h"
 #include "ap_mpm.h"
 
+APLOG_USE_MODULE(core);
+
 typedef struct {
     const char *t_name;
     int t_val;
@@ -56,6 +63,7 @@ typedef struct {
 
 APR_HOOK_STRUCT(
     APR_HOOK_LINK(error_log)
+    APR_HOOK_LINK(generate_log_id)
 )
 
 int AP_DECLARE_DATA ap_default_loglevel = DEFAULT_LOGLEVEL;
@@ -134,6 +142,14 @@ static const TRANS priorities[] = {
     {"notice",  APLOG_NOTICE},
     {"info",    APLOG_INFO},
     {"debug",   APLOG_DEBUG},
+    {"trace1",  APLOG_TRACE1},
+    {"trace2",  APLOG_TRACE2},
+    {"trace3",  APLOG_TRACE3},
+    {"trace4",  APLOG_TRACE4},
+    {"trace5",  APLOG_TRACE5},
+    {"trace6",  APLOG_TRACE6},
+    {"trace7",  APLOG_TRACE7},
+    {"trace8",  APLOG_TRACE8},
     {NULL,      -1},
 };
 
@@ -180,16 +196,6 @@ AP_DECLARE(apr_file_t *) ap_piped_log_write_fd(piped_log *pl)
     return pl->write_fd;
 }
 
-/* clear_handle_list() is called when plog is cleared; at that
- * point we need to forget about our old list of pipe read
- * handles.  We let the plog cleanups close the actual pipes.
- */
-static apr_status_t clear_handle_list(void *v)
-{
-    read_handles = NULL;
-    return APR_SUCCESS;
-}
-
 /* remember to close this handle in the child process
  *
  * On Win32 this makes zero sense, because we don't
@@ -367,7 +373,7 @@ static int open_error_log(server_rec *s, int is_main, apr_pool_t *p)
             cmdtype = APR_SHELLCMD_ENV;
             ++fname;
         }
-       
+
         /* Spawn a new child logger.  If this is the main server_rec,
          * the new child must use a dummy stderr since the current
          * stderr might be a pipe to the old logger.  Otherwise, the
@@ -443,7 +449,7 @@ int ap_open_logs(apr_pool_t *pconf, apr_pool_t *p /* plog */,
      * between log phases, so we don't mind losing stderr's 
      * read_handle a little bit early.
      */
-    apr_pool_cleanup_register(p, NULL, clear_handle_list,
+    apr_pool_cleanup_register(p, &read_handles, ap_pool_cleanup_set_null,
                               apr_pool_cleanup_null);
 
     /* HERE we need a stdout log that outlives plog.
@@ -535,78 +541,132 @@ AP_DECLARE(void) ap_error_log2stderr(server_rec *s) {
     }
 }
 
-static void log_error_core(const char *file, int line, int level,
-                           apr_status_t status, const server_rec *s,
-                           const conn_rec *c,
-                           const request_rec *r, apr_pool_t *pool,
-                           const char *fmt, va_list args)
+static int cpystrn(char *buf, const char *arg, int buflen)
 {
-    char errstr[MAX_STRING_LEN];
-#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
-    char scratch[MAX_STRING_LEN];
-#endif
-    apr_size_t len, errstrlen;
-    apr_file_t *logf = NULL;
-    const char *referer;
-    int level_and_mask = level & APLOG_LEVELMASK;
+    char *end;
+    if (!arg)
+        return 0;
+    end = apr_cpystrn(buf, arg, buflen);
+    return end - buf;
+}
 
-    if (r && r->connection) {
-        c = r->connection;
-    }
 
-    if (s == NULL) {
-        /*
-         * If we are doing stderr logging (startup), don't log messages that are
-         * above the default server log level unless it is a startup/shutdown
-         * notice
-         */
-#ifndef DEBUG
-        if ((level_and_mask != APLOG_NOTICE)
-            && (level_and_mask > ap_default_loglevel)) {
-            return;
-        }
-#endif
+static int log_remote_address(const ap_errorlog_info *info, const char *arg,
+                              char *buf, int buflen)
+{
+    if (info->c)
+        return apr_snprintf(buf, buflen, "%s:%d", info->c->remote_ip,
+                            info->c->remote_addr->port);
+    else
+        return 0;
+}
 
-        logf = stderr_log;
-    }
-    else if (s->error_log) {
-        /*
-         * If we are doing normal logging, don't log messages that are
-         * above the server log level unless it is a startup/shutdown notice
-         */
-        if ((level_and_mask != APLOG_NOTICE)
-            && (level_and_mask > s->loglevel)) {
-            return;
-        }
+static int log_local_address(const ap_errorlog_info *info, const char *arg,
+                             char *buf, int buflen)
+{
+    if (info->c)
+        return apr_snprintf(buf, buflen, "%s:%d", info->c->local_ip,
+                            info->c->local_addr->port);
+    else
+        return 0;
+}
 
-        logf = s->error_log;
+static int log_pid(const ap_errorlog_info *info, const char *arg,
+                   char *buf, int buflen)
+{
+    pid_t pid = getpid();
+    return apr_snprintf(buf, buflen, "%" APR_PID_T_FMT, pid);
+}
+
+static int log_tid(const ap_errorlog_info *info, const char *arg,
+                   char *buf, int buflen)
+{
+#if APR_HAS_THREADS
+    int result;
+
+    if (ap_mpm_query(AP_MPMQ_IS_THREADED, &result) == APR_SUCCESS
+        && result != AP_MPMQ_NOT_SUPPORTED)
+    {
+        apr_os_thread_t tid = apr_os_thread_current();
+        return apr_snprintf(buf, buflen, "%pT", &tid);
     }
-    else {
-        /*
-         * If we are doing syslog logging, don't log messages that are
-         * above the server log level (including a startup/shutdown notice)
-         */
-        if (level_and_mask > s->loglevel) {
-            return;
+#endif
+    return 0;
+}
+
+static int log_ctime(const ap_errorlog_info *info, const char *arg,
+                     char *buf, int buflen)
+{
+    int time_len = buflen;
+    int option = AP_CTIME_OPTION_NONE;
+
+    while(arg && *arg) {
+        switch (*arg) {
+            case 'u':   option |= AP_CTIME_OPTION_USEC;
+                        break;
+            case 'c':   option |= AP_CTIME_OPTION_COMPACT;
+                        break;
         }
+        arg++;
     }
 
-    if (logf && ((level & APLOG_STARTUP) != APLOG_STARTUP)) {
-        errstr[0] = '[';
-        ap_recent_ctime(errstr + 1, apr_time_now());
-        errstr[1 + APR_CTIME_LEN - 1] = ']';
-        errstr[1 + APR_CTIME_LEN    ] = ' ';
-        len = 1 + APR_CTIME_LEN + 1;
-    } else {
-        len = 0;
-    }
+    ap_recent_ctime_ex(buf, apr_time_now(), option, &time_len);
 
-    if ((level & APLOG_STARTUP) != APLOG_STARTUP) {
-        len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
-                            "[%s] ", priorities[level_and_mask].t_name);
+    /* ap_recent_ctime_ex includes the trailing \0 in time_len */
+    return time_len - 1;
+}
+
+static int log_loglevel(const ap_errorlog_info *info, const char *arg,
+                        char *buf, int buflen)
+{
+    if (info->level < 0)
+        return 0;
+    else
+        return cpystrn(buf, priorities[info->level].t_name, buflen);
+}
+
+static int log_log_id(const ap_errorlog_info *info, const char *arg,
+                      char *buf, int buflen)
+{
+    /*
+     * C: log conn log_id if available,
+     * c: log conn log id if available and not a once-per-request log line
+     * else: log request log id if available
+     */
+    if (arg && !strcasecmp(arg, "c")) {
+        if (info->c && (*arg != 'C' || !info->r)) {
+            return cpystrn(buf, info->c->log_id, buflen);
+        }
+    }
+    else if (info->r) {
+        return cpystrn(buf, info->r->log_id, buflen);
     }
+    return 0;
+}
+
+static int log_keepalives(const ap_errorlog_info *info, const char *arg,
+                          char *buf, int buflen)
+{
+    if (!info->c)
+        return 0;
+
+    return apr_snprintf(buf, buflen, "%d", info->c->keepalives);
+}
 
-    if (file && level_and_mask == APLOG_DEBUG) {
+static int log_module_name(const ap_errorlog_info *info, const char *arg,
+                           char *buf, int buflen)
+{
+    return cpystrn(buf, ap_find_module_short_name(info->module_index), buflen);
+}
+
+static int log_file_line(const ap_errorlog_info *info, const char *arg,
+                         char *buf, int buflen)
+{
+    if (info->file == NULL) {
+        return 0;
+    }
+    else {
+        const char *file = info->file;
 #if defined(_OSD_POSIX) || defined(WIN32) || defined(__MVS__)
         char tmp[256];
         char *e = strrchr(file, '/');
@@ -638,70 +698,357 @@ static void log_error_core(const char *file, int line, int level,
             file = p + 1;
         }
 #endif /*_OSD_POSIX || WIN32 */
-        len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
-                            "%s(%d): ", file, line);
+        return apr_snprintf(buf, buflen, "%s(%d)", file, info->line);
     }
+}
 
-    if (c) {
-        /* XXX: TODO: add a method of selecting whether logged client
-         * addresses are in dotted quad or resolved form... dotted
-         * quad is the most secure, which is why I'm implementing it
-         * first. -djg
-         */
-        len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
-                            "[client %s] ", c->remote_ip);
+static int log_apr_status(const ap_errorlog_info *info, const char *arg,
+                          char *buf, int buflen)
+{
+    apr_status_t status = info->status;
+    int len;
+    if (!status)
+        return 0;
+
+    if (status < APR_OS_START_EAIERR) {
+        len = apr_snprintf(buf, buflen, "(%d)", status);
     }
-    if (status != 0) {
-        if (status < APR_OS_START_EAIERR) {
-            len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
-                                "(%d)", status);
-        }
-        else if (status < APR_OS_START_SYSERR) {
-            len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
-                                "(EAI %d)", status - APR_OS_START_EAIERR);
-        }
-        else if (status < 100000 + APR_OS_START_SYSERR) {
-            len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
-                                "(OS %d)", status - APR_OS_START_SYSERR);
-        }
-        else {
-            len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
-                                "(os 0x%08x)", status - APR_OS_START_SYSERR);
+    else if (status < APR_OS_START_SYSERR) {
+        len = apr_snprintf(buf, buflen, "(EAI %d)",
+                           status - APR_OS_START_EAIERR);
+    }
+    else if (status < 100000 + APR_OS_START_SYSERR) {
+        len = apr_snprintf(buf, buflen, "(OS %d)",
+                           status - APR_OS_START_SYSERR);
+    }
+    else {
+        len = apr_snprintf(buf, buflen, "(os 0x%08x)",
+                           status - APR_OS_START_SYSERR);
+    }
+    apr_strerror(status, buf + len, buflen - len);
+    len += strlen(buf + len);
+    return len;
+}
+
+static int log_server_name(const ap_errorlog_info *info, const char *arg,
+                           char *buf, int buflen)
+{
+    if (info->r)
+        return cpystrn(buf, ap_get_server_name((request_rec *)info->r), buflen);
+
+    return 0;
+}
+
+static int log_virtual_host(const ap_errorlog_info *info, const char *arg,
+                            char *buf, int buflen)
+{
+    if (info->s)
+        return cpystrn(buf, info->s->server_hostname, buflen);
+
+    return 0;
+}
+
+
+static int log_table_entry(const apr_table_t *table, const char *name,
+                           char *buf, int buflen)
+{
+#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
+    const char *value;
+    char scratch[MAX_STRING_LEN];
+
+    if ((value = apr_table_get(table, name)) != NULL) {
+        ap_escape_errorlog_item(scratch, value, MAX_STRING_LEN);
+        return cpystrn(buf, scratch, buflen);
+    }
+
+    return 0;
+#else
+    return cpystrn(buf, apr_table_get(table, name), buflen);
+#endif
+}
+
+static int log_header(const ap_errorlog_info *info, const char *arg,
+                      char *buf, int buflen)
+{
+    if (info->r)
+        return log_table_entry(info->r->headers_in, arg, buf, buflen);
+
+    return 0;
+}
+
+static int log_note(const ap_errorlog_info *info, const char *arg,
+                      char *buf, int buflen)
+{
+    /* XXX: maybe escaping the entry is not necessary for notes? */
+    if (info->r)
+        return log_table_entry(info->r->notes, arg, buf, buflen);
+
+    return 0;
+}
+
+static int log_env_var(const ap_errorlog_info *info, const char *arg,
+                      char *buf, int buflen)
+{
+    if (info->r)
+        return log_table_entry(info->r->subprocess_env, arg, buf, buflen);
+
+    return 0;
+}
+
+static int core_generate_log_id(const conn_rec *c, const request_rec *r,
+                                 const char **idstring)
+{
+    apr_uint64_t id, tmp;
+    pid_t pid;
+    int len;
+    char *encoded;
+
+    if (r && r->request_time) {
+        id = (apr_uint64_t)r->request_time;
+    }
+    else {
+        id = (apr_uint64_t)apr_time_now();
+    }
+
+    pid = getpid();
+    if (sizeof(pid_t) > 2) {
+        tmp = pid;
+        tmp = tmp << 40;
+        id ^= tmp;
+        pid = pid >> 24;
+        tmp = pid;
+        tmp = tmp << 56;
+        id ^= tmp;
+    }
+    else {
+        tmp = pid;
+        tmp = tmp << 40;
+        id ^= tmp;
+    }
+#if APR_HAS_THREADS
+    {
+        apr_uintptr_t tmp2 = (apr_uintptr_t)c->current_thread;
+        tmp = tmp2;
+        tmp = tmp << 32;
+        id ^= tmp;
+    }
+#endif
+
+    len = apr_base64_encode_len(sizeof(id)); /* includes trailing \0 */
+    encoded = apr_palloc(r ? r->pool : c->pool, len);
+    apr_base64_encode(encoded, (char *)&id, sizeof(id));
+
+    /* Skip the last char, it is always '=' */
+    encoded[len - 2] = '\0';
+
+    *idstring = encoded;
+
+    return OK;
+}
+
+static void add_log_id(const conn_rec *c, const request_rec *r)
+{
+    const char **id;
+    /* need to cast const away */
+    if (r) {
+        id = &((request_rec *)r)->log_id;
+    }
+    else {
+        id = &((conn_rec *)c)->log_id;
+    }
+
+    ap_run_generate_log_id(c, r, id);
+}
+
+AP_DECLARE(void) ap_register_log_hooks(apr_pool_t *p)
+{
+    ap_hook_generate_log_id(core_generate_log_id, NULL, NULL,
+                            APR_HOOK_REALLY_LAST);
+
+    ap_register_errorlog_handler(p, "a", log_remote_address, 0);
+    ap_register_errorlog_handler(p, "A", log_local_address, 0);
+    ap_register_errorlog_handler(p, "e", log_env_var, 0);
+    ap_register_errorlog_handler(p, "E", log_apr_status, 0);
+    ap_register_errorlog_handler(p, "F", log_file_line, 0);
+    ap_register_errorlog_handler(p, "i", log_header, 0);
+    ap_register_errorlog_handler(p, "k", log_keepalives, 0);
+    ap_register_errorlog_handler(p, "l", log_loglevel, 0);
+    ap_register_errorlog_handler(p, "L", log_log_id, 0);
+    ap_register_errorlog_handler(p, "m", log_module_name, 0);
+    ap_register_errorlog_handler(p, "n", log_note, 0);
+    ap_register_errorlog_handler(p, "P", log_pid, 0);
+    ap_register_errorlog_handler(p, "t", log_ctime, 0);
+    ap_register_errorlog_handler(p, "T", log_tid, 0);
+    ap_register_errorlog_handler(p, "v", log_virtual_host, 0);
+    ap_register_errorlog_handler(p, "V", log_server_name, 0);
+}
+
+/*
+ * This is used if no error log format is defined and during startup.
+ * It automatically omits the timestamp if logging to syslog.
+ */
+static int do_errorlog_default(const ap_errorlog_info *info, char *buf,
+                               int buflen, int *errstr_start, int *errstr_end,
+                               const char *errstr_fmt, va_list args)
+{
+    int len = 0;
+    int field_start = 0;
+    int item_len;
+#ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
+    char scratch[MAX_STRING_LEN];
+#endif
+
+    if (!info->using_syslog && !info->startup) {
+        buf[len++] = '[';
+        len += log_ctime(info, "u", buf + len, buflen - len);
+        buf[len++] = ']';
+        buf[len++] = ' ';
+    }
+
+    if (!info->startup) {
+        buf[len++] = '[';
+        len += log_module_name(info, NULL, buf + len, buflen - len);
+        buf[len++] = ':';
+        len += log_loglevel(info, NULL, buf + len, buflen - len);
+        len += cpystrn(buf + len, "] [pid ", buflen - len);
+
+        len += log_pid(info, NULL, buf + len, buflen - len);
+#if APR_HAS_THREADS
+        field_start = len;
+        len += cpystrn(buf + len, ":tid ", buflen - len);
+        item_len = log_tid(info, NULL, buf + len, buflen - len);
+        if (!item_len)
+            len = field_start;
+        else
+            len += item_len;
+#endif
+        buf[len++] = ']';
+        buf[len++] = ' ';
+    }
+
+    if (info->level >= APLOG_DEBUG) {
+        item_len = log_file_line(info, NULL, buf + len, buflen - len);
+        if (item_len) {
+            len += item_len;
+            len += cpystrn(buf + len, ": ", buflen - len);
         }
-        apr_strerror(status, errstr + len, MAX_STRING_LEN - len);
-        len += strlen(errstr + len);
-        if (MAX_STRING_LEN - len > 2) {
-            errstr[len++] = ':';
-            errstr[len++] = ' ';
-            errstr[len] = '\0';
+    }
+
+    if (info->status) {
+        item_len = log_apr_status(info, NULL, buf + len, buflen - len);
+        if (item_len) {
+            len += item_len;
+            len += cpystrn(buf + len, ": ", buflen - len);
         }
     }
 
-    errstrlen = len;
+    if (info->c) {
+        /*
+         * remote_ip can be client or backend server. If we have a scoreboard
+         * handle, it is likely a client.
+         */
+        len += apr_snprintf(buf + len, buflen - len,
+                            info->c->sbh ? "[client %s:%d] " : "[remote %s:%d] ",
+                            info->c->remote_ip, info->c->remote_addr->port);
+    }
+
+    /* the actual error message */
+    *errstr_start = len;
 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
-    if (apr_vsnprintf(scratch, MAX_STRING_LEN - len, fmt, args)) {
-        len += ap_escape_errorlog_item(errstr + len, scratch,
-                                       MAX_STRING_LEN - len);
+    if (apr_vsnprintf(scratch, MAX_STRING_LEN, errstr_fmt, args)) {
+        len += ap_escape_errorlog_item(buf + len, scratch,
+                                       buflen - len);
+
     }
 #else
-    len += apr_vsnprintf(errstr + len, MAX_STRING_LEN - len, fmt, args);
+    len += apr_vsnprintf(buf + len, buflen - len, errstr_fmt, args);
 #endif
+    *errstr_end = len;
+
+    field_start = len;
+    len += cpystrn(buf + len, ", referer: ", buflen - len);
+    item_len = log_header(info, "Referer", buf + len, buflen - len);
+    if (item_len)
+        len += item_len;
+    else
+        len = field_start;
 
-    if (   r && (referer = apr_table_get(r->headers_in, "Referer"))
+    return len;
+}
+
+static int do_errorlog_format(apr_array_header_t *fmt, ap_errorlog_info *info,
+                              char *buf, int buflen, int *errstr_start,
+                              int *errstr_end, const char *err_fmt, va_list args)
+{
 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
-        && ap_escape_errorlog_item(scratch, referer, MAX_STRING_LEN - len)
+    char scratch[MAX_STRING_LEN];
 #endif
-        ) {
-        len += apr_snprintf(errstr + len, MAX_STRING_LEN - len,
-                            ", referer: %s",
+    int i;
+    int len = 0;
+    int field_start = 0;
+    int skipping = 0;
+    ap_errorlog_format_item *items = (ap_errorlog_format_item *)fmt->elts;
+
+    for (i = 0; i < fmt->nelts; ++i) {
+        ap_errorlog_format_item *item = &items[i];
+        if (item->flags & AP_ERRORLOG_FLAG_FIELD_SEP) {
+            if (skipping) {
+                skipping = 0;
+            }
+            else {
+                field_start = len;
+            }
+        }
+
+        if (item->flags & AP_ERRORLOG_FLAG_MESSAGE) {
+            /* the actual error message */
+            *errstr_start = len;
 #ifndef AP_UNSAFE_ERROR_LOG_UNESCAPED
-                            scratch
+            if (apr_vsnprintf(scratch, MAX_STRING_LEN, err_fmt, args)) {
+                len += ap_escape_errorlog_item(buf + len, scratch,
+                                               buflen - len);
+
+            }
 #else
-                            referer
+            len += apr_vsnprintf(buf + len, buflen - len, err_fmt, args);
 #endif
-                            );
+            *errstr_end = len;
+        }
+        else if (skipping) {
+            continue;
+        }
+        else if ((int)item->min_loglevel > info->level) {
+            len = field_start;
+            skipping = 1;
+        }
+        else {
+            int item_len = (*item->func)(info, item->arg, buf + len,
+                                         buflen - len);
+            if (!item_len) {
+                if (item->flags & AP_ERRORLOG_FLAG_REQUIRED) {
+                    /* required item is empty. skip whole line */
+                    buf[0] = '\0';
+                    return 0;
+                }
+                else if (item->flags & AP_ERRORLOG_FLAG_NULL_AS_HYPHEN) {
+                    buf[len++] = '-';
+                }
+                else {
+                    len = field_start;
+                    skipping = 1;
+                }
+            }
+            else {
+                len += item_len;
+            }
+        }
     }
+    return len;
+}
 
+static void write_logline(char *errstr, apr_size_t len, apr_file_t *logf,
+                          int level)
+{
     /* NULL if we are logging to syslog */
     if (logf) {
         /* Truncate for the terminator (as apr_snprintf does) */
@@ -714,44 +1061,234 @@ static void log_error_core(const char *file, int line, int level,
     }
 #ifdef HAVE_SYSLOG
     else {
-        syslog(level_and_mask, "%s", errstr);
+        syslog(level < LOG_PRIMASK ? level : APLOG_DEBUG, "%s", errstr);
+    }
+#endif
+}
+
+static void log_error_core(const char *file, int line, int module_index,
+                           int level,
+                           apr_status_t status, const server_rec *s,
+                           const conn_rec *c,
+                           const request_rec *r, apr_pool_t *pool,
+                           const char *fmt, va_list args)
+{
+    char errstr[MAX_STRING_LEN];
+    apr_file_t *logf = NULL;
+    int level_and_mask = level & APLOG_LEVELMASK;
+    const request_rec *rmain = NULL;
+    core_server_config *sconf = NULL;
+    ap_errorlog_info info;
+
+    /* do we need to log once-per-req or once-per-conn info? */
+    int log_conn_info = 0, log_req_info = 0;
+    apr_array_header_t **lines = NULL;
+    int done = 0;
+    int line_number = 0;
+
+    if (r && r->connection) {
+        c = r->connection;
     }
+
+    if (s == NULL) {
+        /*
+         * If we are doing stderr logging (startup), don't log messages that are
+         * above the default server log level unless it is a startup/shutdown
+         * notice
+         */
+#ifndef DEBUG
+        if ((level_and_mask != APLOG_NOTICE)
+            && (level_and_mask > ap_default_loglevel)) {
+            return;
+        }
 #endif
 
-    ap_run_error_log(file, line, level, status, s, r, pool, errstr + errstrlen);
+        logf = stderr_log;
+    }
+    else {
+        int configured_level = r ? ap_get_request_module_loglevel(r, module_index)        :
+                               c ? ap_get_conn_server_module_loglevel(c, s, module_index) :
+                                   ap_get_server_module_loglevel(s, module_index);
+        if (s->error_log) {
+            /*
+             * If we are doing normal logging, don't log messages that are
+             * above the module's log level unless it is a startup/shutdown notice
+             */
+            if ((level_and_mask != APLOG_NOTICE)
+                && (level_and_mask > configured_level)) {
+                return;
+            }
+
+            logf = s->error_log;
+        }
+        else {
+            /*
+             * If we are doing syslog logging, don't log messages that are
+             * above the module's log level (including a startup/shutdown notice)
+             */
+            if (level_and_mask > configured_level) {
+                return;
+            }
+        }
+
+        /* the faked server_rec from mod_cgid does not have s->module_config */
+        if (s->module_config) {
+            sconf = ap_get_module_config(s->module_config, &core_module);
+            if (c && !c->log_id) {
+                add_log_id(c, NULL);
+                if (sconf->error_log_conn && sconf->error_log_conn->nelts > 0)
+                    log_conn_info = 1;
+            }
+            if (r) {
+                if (r->main)
+                    rmain = r->main;
+                else
+                    rmain = r;
+
+                if (!rmain->log_id) {
+                    /* XXX: do we need separate log ids for subrequests? */
+                    if (sconf->error_log_req && sconf->error_log_req->nelts > 0)
+                        log_req_info = 1;
+                    /*
+                     * XXX: potential optimization: only create log id if %L is
+                     * XXX: actually used
+                     */
+                    add_log_id(c, rmain);
+                }
+            }
+        }
+    }
+
+    info.s             = s;
+    info.c             = c;
+    info.file          = NULL;
+    info.line          = 0;
+    info.status        = 0;
+    info.using_syslog  = (logf == NULL);
+    info.startup       = ((level & APLOG_STARTUP) == APLOG_STARTUP);
+
+
+    while (!done) {
+        apr_array_header_t *log_format;
+        int len = 0, errstr_start = 0, errstr_end = 0;
+        /* XXX: potential optimization: format common prefixes only once */
+        if (log_conn_info) {
+            /* once-per-connection info */
+            if (line_number == 0) {
+                lines = (apr_array_header_t **)sconf->error_log_conn->elts;
+                info.r = NULL;
+                info.rmain = NULL;
+                info.level = -1;
+                info.module_index = APLOG_NO_MODULE;
+            }
+
+            log_format = lines[line_number++];
+
+            if (line_number == sconf->error_log_conn->nelts) {
+                /* this is the last line of once-per-connection info */
+                line_number = 0;
+                log_conn_info = 0;
+            }
+        }
+        else if (log_req_info) {
+            /* once-per-request info */
+            if (line_number == 0) {
+                lines = (apr_array_header_t **)sconf->error_log_req->elts;
+                info.r = rmain;
+                info.rmain = rmain;
+                info.level = -1;
+                info.module_index = APLOG_NO_MODULE;
+            }
+
+            log_format = lines[line_number++];
+
+            if (line_number == sconf->error_log_req->nelts) {
+                /* this is the last line of once-per-request info */
+                line_number = 0;
+                log_req_info = 0;
+            }
+        }
+        else {
+            /* the actual error message */
+            info.r            = r;
+            info.rmain        = rmain;
+            info.level        = level_and_mask;
+            info.module_index = module_index;
+            info.file         = file;
+            info.line         = line;
+            info.status       = status;
+            log_format = sconf ? sconf->error_log_format : NULL;
+            done = 1;
+        }
+
+        /*
+         * prepare and log one line
+         */
+
+        if (log_format) {
+            len += do_errorlog_format(log_format, &info, errstr + len,
+                                      MAX_STRING_LEN - len,
+                                      &errstr_start, &errstr_end, fmt, args);
+        }
+        else {
+            len += do_errorlog_default(&info, errstr + len, MAX_STRING_LEN - len,
+                                       &errstr_start, &errstr_end, fmt, args);
+        }
+
+        if (!*errstr)
+        {
+            /*
+             * Don't log empty lines. This can happen with once-per-conn/req
+             * info if an item with AP_ERRORLOG_FLAG_REQUIRED is NULL.
+             */
+            continue;
+        }
+        write_logline(errstr, len, logf, level_and_mask);
+
+        if (!log_format) {
+            /* only pass the real error string to the hook */
+            errstr[errstr_end] = '\0';
+            ap_run_error_log(file, line, module_index, level, status, s, r, pool,
+                             errstr + errstr_start);
+        }
+
+        *errstr = '\0';
+    }
 }
 
-AP_DECLARE(void) ap_log_error(const char *file, int line, int level,
-                              apr_status_t status, const server_rec *s,
-                              const char *fmt, ...)
+AP_DECLARE(void) ap_log_error_(const char *file, int line, int module_index,
+                               int level, apr_status_t status,
+                               const server_rec *s, const char *fmt, ...)
 {
     va_list args;
 
     va_start(args, fmt);
-    log_error_core(file, line, level, status, s, NULL, NULL, NULL, fmt, args);
+    log_error_core(file, line, module_index, level, status, s, NULL, NULL,
+                   NULL, fmt, args);
     va_end(args);
 }
 
-AP_DECLARE(void) ap_log_perror(const char *file, int line, int level,
-                               apr_status_t status, apr_pool_t *p,
-                               const char *fmt, ...)
+AP_DECLARE(void) ap_log_perror_(const char *file, int line, int module_index,
+                                int level, apr_status_t status, apr_pool_t *p,
+                                const char *fmt, ...)
 {
     va_list args;
 
     va_start(args, fmt);
-    log_error_core(file, line, level, status, NULL, NULL, NULL, p, fmt, args);
+    log_error_core(file, line, module_index, level, status, NULL, NULL, NULL,
+                   p, fmt, args);
     va_end(args);
 }
 
-AP_DECLARE(void) ap_log_rerror(const char *file, int line, int level,
-                               apr_status_t status, const request_rec *r,
-                               const char *fmt, ...)
+AP_DECLARE(void) ap_log_rerror_(const char *file, int line, int module_index,
+                                int level, apr_status_t status,
+                                const request_rec *r, const char *fmt, ...)
 {
     va_list args;
 
     va_start(args, fmt);
-    log_error_core(file, line, level, status, r->server, NULL, r, NULL, fmt,
-                   args);
+    log_error_core(file, line, module_index, level, status, r->server, NULL, r,
+                   NULL, fmt, args);
 
     /*
      * IF APLOG_TOCLIENT is set,
@@ -772,18 +1309,77 @@ AP_DECLARE(void) ap_log_rerror(const char *file, int line, int level,
     va_end(args);
 }
 
-AP_DECLARE(void) ap_log_cerror(const char *file, int line, int level,
-                               apr_status_t status, const conn_rec *c,
-                               const char *fmt, ...)
+AP_DECLARE(void) ap_log_cserror_(const char *file, int line, int module_index,
+                                 int level, apr_status_t status,
+                                 const conn_rec *c, const server_rec *s,
+                                 const char *fmt, ...)
 {
     va_list args;
 
     va_start(args, fmt);
-    log_error_core(file, line, level, status, c->base_server, c, NULL, NULL,
-                   fmt, args);
+    log_error_core(file, line, module_index, level, status, s, c,
+                   NULL, NULL, fmt, args);
     va_end(args);
 }
 
+AP_DECLARE(void) ap_log_cerror_(const char *file, int line, int module_index,
+                                int level, apr_status_t status,
+                                const conn_rec *c, const char *fmt, ...)
+{
+    va_list args;
+
+    va_start(args, fmt);
+    log_error_core(file, line, module_index, level, status, c->base_server, c,
+                   NULL, NULL, fmt, args);
+    va_end(args);
+}
+
+AP_DECLARE(void) ap_log_command_line(apr_pool_t *plog, server_rec *s)
+{
+    int i;
+    process_rec *process = s->process;
+    char *result;
+    int len_needed = 0;
+    
+    /* Piece together the command line from the pieces
+     * in process->argv, with spaces in between.
+     */
+    for (i = 0; i < process->argc; i++) {
+        len_needed += strlen(process->argv[i]) + 1;
+    }
+
+    result = (char *) apr_palloc(plog, len_needed);
+    *result = '\0';
+
+    for (i = 0; i < process->argc; i++) {
+        strcat(result, process->argv[i]);
+        if ((i+1)< process->argc) {
+            strcat(result, " ");
+        }
+    }
+    ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
+                 "Command line: '%s'", result);
+}
+
+AP_DECLARE(void) ap_remove_pid(apr_pool_t *p, const char *rel_fname)
+{
+    apr_status_t rv;
+    const char *fname = ap_server_root_relative(p, rel_fname);
+
+    if (fname != NULL) {
+        rv = apr_file_remove(fname, p);
+        if (rv != APR_SUCCESS) {
+            ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
+                         "failed to remove PID file %s", fname);
+        }
+        else {
+            ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
+                         "removed PID file %s (pid=%" APR_PID_T_FMT ")",
+                         fname, getpid());
+        }
+    }
+}
+
 AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
 {
     apr_file_t *pid_file = NULL;
@@ -831,7 +1427,7 @@ AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
                      ap_server_argv0, fname);
         exit(1);
     }
-    apr_file_printf(pid_file, "%ld" APR_EOL_STR, (long)mypid);
+    apr_file_printf(pid_file, "%" APR_PID_T_FMT APR_EOL_STR, mypid);
     apr_file_close(pid_file);
     saved_pid = mypid;
 }
@@ -1127,9 +1723,33 @@ AP_DECLARE(void) ap_close_piped_log(piped_log *pl)
     apr_pool_cleanup_run(pl->p, pl, piped_log_cleanup);
 }
 
+AP_DECLARE(const char *) ap_parse_log_level(const char *str, int *val)
+{
+    char *err = "Log level keyword must be one of emerg/alert/crit/error/warn/"
+                "notice/info/debug/trace1/.../trace8";
+    int i = 0;
+
+    if (str == NULL)
+        return err;
+
+    while (priorities[i].t_name != NULL) {
+        if (!strcasecmp(str, priorities[i].t_name)) {
+            *val = priorities[i].t_val;
+            return NULL;
+        }
+        i++;
+    }
+    return err;
+}
+
 AP_IMPLEMENT_HOOK_VOID(error_log,
-                       (const char *file, int line, int level,
+                       (const char *file, int line, int module_index, int level,
                         apr_status_t status, const server_rec *s,
                         const request_rec *r, apr_pool_t *pool,
-                        const char *errstr), (file, line, level,
+                        const char *errstr), (file, line, module_index, level,
                         status, s, r, pool, errstr))
+
+AP_IMPLEMENT_HOOK_RUN_FIRST(int, generate_log_id,
+                            (const conn_rec *c, const request_rec *r,
+                             const char **id),
+                            (c, r, id), DECLINED)