src/core/ngx_log.h - nginx

Data types defined

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #ifndef _NGX_LOG_H_INCLUDED_
  6. #define _NGX_LOG_H_INCLUDED_


  7. #include <ngx_config.h>
  8. #include <ngx_core.h>


  9. #define NGX_LOG_STDERR            0
  10. #define NGX_LOG_EMERG             1
  11. #define NGX_LOG_ALERT             2
  12. #define NGX_LOG_CRIT              3
  13. #define NGX_LOG_ERR               4
  14. #define NGX_LOG_WARN              5
  15. #define NGX_LOG_NOTICE            6
  16. #define NGX_LOG_INFO              7
  17. #define NGX_LOG_DEBUG             8

  18. #define NGX_LOG_DEBUG_CORE        0x010
  19. #define NGX_LOG_DEBUG_ALLOC       0x020
  20. #define NGX_LOG_DEBUG_MUTEX       0x040
  21. #define NGX_LOG_DEBUG_EVENT       0x080
  22. #define NGX_LOG_DEBUG_HTTP        0x100
  23. #define NGX_LOG_DEBUG_MAIL        0x200
  24. #define NGX_LOG_DEBUG_STREAM      0x400

  25. /*
  26. * do not forget to update debug_levels[] in src/core/ngx_log.c
  27. * after the adding a new debug level
  28. */

  29. #define NGX_LOG_DEBUG_FIRST       NGX_LOG_DEBUG_CORE
  30. #define NGX_LOG_DEBUG_LAST        NGX_LOG_DEBUG_STREAM
  31. #define NGX_LOG_DEBUG_CONNECTION  0x80000000
  32. #define NGX_LOG_DEBUG_ALL         0x7ffffff0


  33. typedef u_char *(*ngx_log_handler_pt) (ngx_log_t *log, u_char *buf, size_t len);
  34. typedef void (*ngx_log_writer_pt) (ngx_log_t *log, ngx_uint_t level,
  35.     u_char *buf, size_t len);


  36. struct ngx_log_s {
  37.     ngx_uint_t           log_level;
  38.     ngx_open_file_t     *file;

  39.     ngx_atomic_uint_t    connection;

  40.     time_t               disk_full_time;

  41.     ngx_log_handler_pt   handler;
  42.     void                *data;

  43.     ngx_log_writer_pt    writer;
  44.     void                *wdata;

  45.     /*
  46.      * we declare "action" as "char *" because the actions are usually
  47.      * the static strings and in the "u_char *" case we have to override
  48.      * their types all the time
  49.      */

  50.     char                *action;

  51.     ngx_log_t           *next;

  52.     NGX_COMPAT_BEGIN(5)
  53.     NGX_COMPAT_END
  54. };


  55. #define NGX_MAX_ERROR_STR   2048


  56. /*********************************/

  57. #if (NGX_HAVE_C99_VARIADIC_MACROS)

  58. #define NGX_HAVE_VARIADIC_MACROS  1

  59. #define ngx_log_error(level, log, ...)                                        \
  60.     if ((log)->log_level >= level) ngx_log_error_core(level, log, __VA_ARGS__)

  61. void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
  62.     const char *fmt, ...);

  63. #define ngx_log_debug(level, log, ...)                                        \
  64.     if ((log)->log_level & level)                                             \
  65.         ngx_log_error_core(NGX_LOG_DEBUG, log, __VA_ARGS__)

  66. /*********************************/

  67. #elif (NGX_HAVE_GCC_VARIADIC_MACROS)

  68. #define NGX_HAVE_VARIADIC_MACROS  1

  69. #define ngx_log_error(level, log, args...)                                    \
  70.     if ((log)->log_level >= level) ngx_log_error_core(level, log, args)

  71. void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
  72.     const char *fmt, ...);

  73. #define ngx_log_debug(level, log, args...)                                    \
  74.     if ((log)->log_level & level)                                             \
  75.         ngx_log_error_core(NGX_LOG_DEBUG, log, args)

  76. /*********************************/

  77. #else /* no variadic macros */

  78. #define NGX_HAVE_VARIADIC_MACROS  0

  79. void ngx_cdecl ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
  80.     const char *fmt, ...);
  81. void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
  82.     const char *fmt, va_list args);
  83. void ngx_cdecl ngx_log_debug_core(ngx_log_t *log, ngx_err_t err,
  84.     const char *fmt, ...);


  85. #endif /* variadic macros */


  86. /*********************************/

  87. #if (NGX_DEBUG)

  88. #if (NGX_HAVE_VARIADIC_MACROS)

  89. #define ngx_log_debug0(level, log, err, fmt)                                  \
  90.         ngx_log_debug(level, log, err, fmt)

  91. #define ngx_log_debug1(level, log, err, fmt, arg1)                            \
  92.         ngx_log_debug(level, log, err, fmt, arg1)

  93. #define ngx_log_debug2(level, log, err, fmt, arg1, arg2)                      \
  94.         ngx_log_debug(level, log, err, fmt, arg1, arg2)

  95. #define ngx_log_debug3(level, log, err, fmt, arg1, arg2, arg3)                \
  96.         ngx_log_debug(level, log, err, fmt, arg1, arg2, arg3)

  97. #define ngx_log_debug4(level, log, err, fmt, arg1, arg2, arg3, arg4)          \
  98.         ngx_log_debug(level, log, err, fmt, arg1, arg2, arg3, arg4)

  99. #define ngx_log_debug5(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5)    \
  100.         ngx_log_debug(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5)

  101. #define ngx_log_debug6(level, log, err, fmt,                                  \
  102.                        arg1, arg2, arg3, arg4, arg5, arg6)                    \
  103.         ngx_log_debug(level, log, err, fmt,                                   \
  104.                        arg1, arg2, arg3, arg4, arg5, arg6)

  105. #define ngx_log_debug7(level, log, err, fmt,                                  \
  106.                        arg1, arg2, arg3, arg4, arg5, arg6, arg7)              \
  107.         ngx_log_debug(level, log, err, fmt,                                   \
  108.                        arg1, arg2, arg3, arg4, arg5, arg6, arg7)

  109. #define ngx_log_debug8(level, log, err, fmt,                                  \
  110.                        arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)        \
  111.         ngx_log_debug(level, log, err, fmt,                                   \
  112.                        arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)


  113. #else /* no variadic macros */

  114. #define ngx_log_debug0(level, log, err, fmt)                                  \
  115.     if ((log)->log_level & level)                                             \
  116.         ngx_log_debug_core(log, err, fmt)

  117. #define ngx_log_debug1(level, log, err, fmt, arg1)                            \
  118.     if ((log)->log_level & level)                                             \
  119.         ngx_log_debug_core(log, err, fmt, arg1)

  120. #define ngx_log_debug2(level, log, err, fmt, arg1, arg2)                      \
  121.     if ((log)->log_level & level)                                             \
  122.         ngx_log_debug_core(log, err, fmt, arg1, arg2)

  123. #define ngx_log_debug3(level, log, err, fmt, arg1, arg2, arg3)                \
  124.     if ((log)->log_level & level)                                             \
  125.         ngx_log_debug_core(log, err, fmt, arg1, arg2, arg3)

  126. #define ngx_log_debug4(level, log, err, fmt, arg1, arg2, arg3, arg4)          \
  127.     if ((log)->log_level & level)                                             \
  128.         ngx_log_debug_core(log, err, fmt, arg1, arg2, arg3, arg4)

  129. #define ngx_log_debug5(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5)    \
  130.     if ((log)->log_level & level)                                             \
  131.         ngx_log_debug_core(log, err, fmt, arg1, arg2, arg3, arg4, arg5)

  132. #define ngx_log_debug6(level, log, err, fmt,                                  \
  133.                        arg1, arg2, arg3, arg4, arg5, arg6)                    \
  134.     if ((log)->log_level & level)                                             \
  135.         ngx_log_debug_core(log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6)

  136. #define ngx_log_debug7(level, log, err, fmt,                                  \
  137.                        arg1, arg2, arg3, arg4, arg5, arg6, arg7)              \
  138.     if ((log)->log_level & level)                                             \
  139.         ngx_log_debug_core(log, err, fmt,                                     \
  140.                        arg1, arg2, arg3, arg4, arg5, arg6, arg7)

  141. #define ngx_log_debug8(level, log, err, fmt,                                  \
  142.                        arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)        \
  143.     if ((log)->log_level & level)                                             \
  144.         ngx_log_debug_core(log, err, fmt,                                     \
  145.                        arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)

  146. #endif

  147. #else /* !NGX_DEBUG */

  148. #define ngx_log_debug0(level, log, err, fmt)
  149. #define ngx_log_debug1(level, log, err, fmt, arg1)
  150. #define ngx_log_debug2(level, log, err, fmt, arg1, arg2)
  151. #define ngx_log_debug3(level, log, err, fmt, arg1, arg2, arg3)
  152. #define ngx_log_debug4(level, log, err, fmt, arg1, arg2, arg3, arg4)
  153. #define ngx_log_debug5(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5)
  154. #define ngx_log_debug6(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5, arg6)
  155. #define ngx_log_debug7(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5,    \
  156.                        arg6, arg7)
  157. #define ngx_log_debug8(level, log, err, fmt, arg1, arg2, arg3, arg4, arg5,    \
  158.                        arg6, arg7, arg8)

  159. #endif

  160. /*********************************/

  161. ngx_log_t *ngx_log_init(u_char *prefix, u_char *error_log);
  162. void ngx_cdecl ngx_log_abort(ngx_err_t err, const char *fmt, ...);
  163. void ngx_cdecl ngx_log_stderr(ngx_err_t err, const char *fmt, ...);
  164. u_char *ngx_log_errno(u_char *buf, u_char *last, ngx_err_t err);
  165. ngx_int_t ngx_log_open_default(ngx_cycle_t *cycle);
  166. ngx_int_t ngx_log_redirect_stderr(ngx_cycle_t *cycle);
  167. ngx_log_t *ngx_log_get_file_log(ngx_log_t *head);
  168. char *ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head);


  169. /*
  170. * ngx_write_stderr() cannot be implemented as macro, since
  171. * MSVC does not allow to use #ifdef inside macro parameters.
  172. *
  173. * ngx_write_fd() is used instead of ngx_write_console(), since
  174. * CharToOemBuff() inside ngx_write_console() cannot be used with
  175. * read only buffer as destination and CharToOemBuff() is not needed
  176. * for ngx_write_stderr() anyway.
  177. */
  178. static ngx_inline void
  179. ngx_write_stderr(char *text)
  180. {
  181.     (void) ngx_write_fd(ngx_stderr, text, ngx_strlen(text));
  182. }


  183. static ngx_inline void
  184. ngx_write_stdout(char *text)
  185. {
  186.     (void) ngx_write_fd(ngx_stdout, text, ngx_strlen(text));
  187. }


  188. extern ngx_module_t  ngx_errlog_module;
  189. extern ngx_uint_t    ngx_use_stderr;


  190. #endif /* _NGX_LOG_H_INCLUDED_ */