Patch for greater logging information

Brian Moran bmoran at onehub.com
Fri Jan 15 04:12:54 MSK 2010


When tracking down some potential issues in the nginx constellation,
we've found it useful to understand where particular error messages
are coming from, since many of the same messages are repeated in
various places. This patch will write the source file from which the
message originated, the function name, and the line number if you're
using GCC to compile nginx. Here's an example:

Old Output:
2010/01/12 14:43:10 [notice] 67772#0: nginx/0.7.64
2010/01/12 14:43:10 [notice] 67772#0: built by gcc 4.0.1 (Apple Inc. build 5490)
2010/01/12 14:43:10 [notice] 67772#0: OS: Darwin 9.8.0
2010/01/12 14:43:10 [notice] 67772#0: hw.ncpu: 2
2010/01/12 14:43:10 [notice] 67772#0: net.inet.tcp.sendspace: 65536
2010/01/12 14:43:10 [notice] 67772#0: kern.ipc.somaxconn: 128
2010/01/12 14:43:10 [notice] 67772#0: getrlimit(RLIMIT_NOFILE):
256:9223372036854775807
2010/01/12 14:43:10 [notice] 67772#0: start worker processes
2010/01/12 14:43:10 [notice] 67772#0: start worker process 67785
2010/01/12 14:43:16 [notice] 67772#0: signal 20 (SIGCHLD) received

New Output:
2010/01/14 16:35:09 [notice] 27241#0: src/os/unix/ngx_posix_init.c
ngx_os_status(   80) nginx/0.7.64
2010/01/14 16:35:09 [notice] 27241#0: src/os/unix/ngx_posix_init.c
ngx_os_status(   83) built by gcc 4.0.1 (Apple Inc. build 5490)
2010/01/14 16:35:09 [notice] 27241#0: src/os/unix/ngx_darwin_init.c
ngx_os_specific_status(  153) OS: Darwin 9.8.0
2010/01/14 16:35:09 [notice] 27241#0: src/os/unix/ngx_darwin_init.c
ngx_os_specific_status(  166) hw.ncpu: 2
2010/01/14 16:35:09 [notice] 27241#0: src/os/unix/ngx_darwin_init.c
ngx_os_specific_status(  166) net.inet.tcp.sendspace: 65536
2010/01/14 16:35:09 [notice] 27241#0: src/os/unix/ngx_darwin_init.c
ngx_os_specific_status(  166) kern.ipc.somaxconn: 128
2010/01/14 16:35:09 [notice] 27241#0: src/os/unix/ngx_posix_init.c
ngx_os_status(   92) getrlimit(RLIMIT_NOFILE):
2560:9223372036854775807
2010/01/14 16:35:09 [notice] 27241#0: src/os/unix/ngx_process_cycle.c
ngx_start_worker_processes(  337) start worker processes
2010/01/14 16:35:09 [notice] 27241#0: src/os/unix/ngx_process.c
ngx_spawn_process(  201) start worker process 27254
2010/01/14 16:35:14 [notice] 27241#0: src/os/unix/ngx_process.c
ngx_signal_handler(  420) signal 20 (SIGCHLD) received

Formatting the filename and function name fields into fixed-width
fields would be nicer, however that would require further changes in
src/core/ngx_string.c

diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c
index 770a590..0ff8789 100644
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -72,16 +72,15 @@ static const char *debug_levels[] = {

 #if (NGX_HAVE_VARIADIC_MACROS)

+#ifndef EXTENDED_LOGGING
 void
 ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
     const char *fmt, ...)
-
 #else
-
 void
-ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
-    const char *fmt, va_list args)
-
+ngx_log_error_core_extended(ngx_uint_t level, ngx_log_t *log,
+                           const char *file, int line, const char
*func, ngx_err_t err, const char *fmt,  ...)
+#endif
 #endif
 {
 #if (NGX_HAVE_VARIADIC_MACROS)
@@ -107,6 +106,12 @@ ngx_log_error_core(ngx_uint_t level, ngx_log_t
*log, ngx_err_t err,
     p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ",
                     ngx_log_pid, ngx_log_tid);

+#ifdef EXTENDED_LOGGING
+    /*extended logging */
+
+    p = ngx_slprintf(p, last, "%s %s(%5d)  ",
+                    file,func,line);
+#endif
     if (log->connection) {
         p = ngx_slprintf(p, last, "*%uA ", log->connection);
     }
diff --git a/src/core/ngx_log.h b/src/core/ngx_log.h
index b736aa2..d0fc173 100644
--- a/src/core/ngx_log.h
+++ b/src/core/ngx_log.h
@@ -71,7 +71,8 @@ struct ngx_log_s {
 #if (NGX_HAVE_GCC_VARIADIC_MACROS)

 #define NGX_HAVE_VARIADIC_MACROS  1
-
+#define EXTENDED_LOGGING 1
+#ifndef EXTENDED_LOGGING
 #define ngx_log_error(level, log, args...)                                    \
     if ((log)->log_level >= level) ngx_log_error_core(level, log, args)

@@ -81,6 +82,18 @@ void ngx_log_error_core(ngx_uint_t level, ngx_log_t
*log, ngx_err_t err,
 #define ngx_log_debug(level, log, args...)                                    \
     if ((log)->log_level & level)                                             \
         ngx_log_error_core(NGX_LOG_DEBUG, log, args)
+#else
+#define ngx_log_error(level, log, args...)                                    \
+  if ((log)->log_level >= level) ngx_log_error_core_extended(level,
log, __FILE__, __LINE__, __func__, args)
+
+void ngx_log_error_core_extended(ngx_uint_t level, ngx_log_t *log,
const char *file, int line,
+                                const char *func, ngx_err_t err,
const char *fmt, ...);
+
+#define ngx_log_debug(level, log, args...)                                    \
+    if ((log)->log_level & level)                                             \
+      ngx_log_error_core_extended(NGX_LOG_DEBUG, log, __FILE__,
__LINE__ , __func__, args)
+
+#endif

 /*********************************/



More information about the nginx-devel mailing list