~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Nginx/core/ngx_log.c

Version: ~ [ nginx-0.6.26 ] ~ [ nginx-0.5.35 ] ~ [ nginx-0.5.20 ] ~ [ nginx-0.5.19 ] ~

  1 
  2 /*
  3  * Copyright (C) Igor Sysoev
  4  */
  5 
  6 
  7 #include <ngx_config.h>
  8 #include <ngx_core.h>
  9 
 10 
 11 static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 12 
 13 
 14 static ngx_command_t  ngx_errlog_commands[] = {
 15 
 16     {ngx_string("error_log"),
 17      NGX_MAIN_CONF|NGX_CONF_1MORE,
 18      ngx_set_error_log,
 19      0,
 20      0,
 21      NULL},
 22 
 23     ngx_null_command
 24 };
 25 
 26 
 27 static ngx_core_module_t  ngx_errlog_module_ctx = {
 28     ngx_string("errlog"),
 29     NULL,
 30     NULL
 31 };
 32 
 33 
 34 ngx_module_t  ngx_errlog_module = {
 35     NGX_MODULE_V1,
 36     &ngx_errlog_module_ctx,                /* module context */
 37     ngx_errlog_commands,                   /* module directives */
 38     NGX_CORE_MODULE,                       /* module type */
 39     NULL,                                  /* init master */
 40     NULL,                                  /* init module */
 41     NULL,                                  /* init process */
 42     NULL,                                  /* init thread */
 43     NULL,                                  /* exit thread */
 44     NULL,                                  /* exit process */
 45     NULL,                                  /* exit master */
 46     NGX_MODULE_V1_PADDING
 47 };
 48 
 49 
 50 static ngx_log_t        ngx_log;
 51 static ngx_open_file_t  ngx_stderr;
 52 
 53 
 54 static const char *err_levels[] = {
 55     "stderr", "emerg", "alert", "crit", "error",
 56     "warn", "notice", "info", "debug"
 57 };
 58 
 59 static const char *debug_levels[] = {
 60     "debug_core", "debug_alloc", "debug_mutex", "debug_event",
 61     "debug_http", "debug_mail", "debug_mysql"
 62 };
 63 
 64 
 65 #if (NGX_HAVE_VARIADIC_MACROS)
 66 
 67 void
 68 ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
 69     const char *fmt, ...)
 70 
 71 #else
 72 
 73 void
 74 ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
 75     const char *fmt, va_list args)
 76 
 77 #endif
 78 {
 79 #if (NGX_HAVE_VARIADIC_MACROS)
 80     va_list  args;
 81 #endif
 82     u_char   errstr[NGX_MAX_ERROR_STR], *p, *last;
 83 
 84     if (log->file->fd == NGX_INVALID_FILE) {
 85         return;
 86     }
 87 
 88     last = errstr + NGX_MAX_ERROR_STR;
 89 
 90     ngx_memcpy(errstr, ngx_cached_err_log_time.data,
 91                ngx_cached_err_log_time.len);
 92 
 93     p = errstr + ngx_cached_err_log_time.len;
 94 
 95     p = ngx_snprintf(p, last - p, " [%s] ", err_levels[level]);
 96 
 97     /* pid#tid */
 98     p = ngx_snprintf(p, last - p, "%P#" NGX_TID_T_FMT ": ",
 99                     ngx_log_pid, ngx_log_tid);
100 
101     if (log->connection) {
102         p = ngx_snprintf(p, last - p, "*%uA ", log->connection);
103     }
104 
105 #if (NGX_HAVE_VARIADIC_MACROS)
106 
107     va_start(args, fmt);
108     p = ngx_vsnprintf(p, last - p, fmt, args);
109     va_end(args);
110 
111 #else
112 
113     p = ngx_vsnprintf(p, last - p, fmt, args);
114 
115 #endif
116 
117     if (err) {
118 
119         if (p > last - 50) {
120 
121             /* leave a space for an error code */
122 
123             p = last - 50;
124             *p++ = '.';
125             *p++ = '.';
126             *p++ = '.';
127         }
128 
129 #if (NGX_WIN32)
130 
131         if ((unsigned) err >= 0x80000000) {
132             p = ngx_snprintf(p, last - p, " (%Xd: ", err);
133 
134         } else {
135             p = ngx_snprintf(p, last - p, " (%d: ", err);
136         }
137 
138 #else
139 
140         p = ngx_snprintf(p, last - p, " (%d: ", err);
141 
142 #endif
143 
144         p = ngx_strerror_r(err, p, last - p);
145 
146         if (p < last) {
147             *p++ = ')';
148         }
149     }
150 
151     if (level != NGX_LOG_DEBUG && log->handler) {
152         p = log->handler(log, p, last - p);
153     }
154 
155     if (p > last - NGX_LINEFEED_SIZE) {
156         p = last - NGX_LINEFEED_SIZE;
157     }
158 
159     ngx_linefeed(p);
160 
161     ngx_write_fd(log->file->fd, errstr, p - errstr);
162 }
163 
164 
165 #if !(NGX_HAVE_VARIADIC_MACROS)
166 
167 void ngx_cdecl
168 ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
169     const char *fmt, ...)
170 {
171     va_list  args;
172 
173     if (log->log_level >= level) {
174         va_start(args, fmt);
175         ngx_log_error_core(level, log, err, fmt, args);
176         va_end(args);
177     }
178 }
179 
180 
181 void ngx_cdecl
182 ngx_log_debug_core(ngx_log_t *log, ngx_err_t err, const char *fmt, ...)
183 {
184     va_list  args;
185 
186     va_start(args, fmt);
187     ngx_log_error_core(NGX_LOG_DEBUG, log, err, fmt, args);
188     va_end(args);
189 }
190 
191 #endif
192 
193 
194 void
195 ngx_log_abort(ngx_err_t err, const char *text)
196 {
197     ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err, text);
198 }
199 
200 
201 ngx_log_t *
202 ngx_log_init(void)
203 {
204     ngx_log.file = &ngx_stderr;
205     ngx_log.log_level = NGX_LOG_NOTICE;
206 
207 #if (NGX_WIN32)
208 
209     ngx_stderr_fileno = GetStdHandle(STD_ERROR_HANDLE);
210 
211     ngx_stderr.fd = ngx_open_file(NGX_ERROR_LOG_PATH, NGX_FILE_RDWR,
212                                   NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND, 0);
213 
214     if (ngx_stderr.fd == NGX_INVALID_FILE) {
215         ngx_message_box("nginx", MB_OK, ngx_errno,
216                         "Could not open error log file: "
217                         ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed");
218         return NULL;
219     }
220 
221     if (ngx_file_append_mode(ngx_stderr.fd) == NGX_ERROR) {
222         ngx_message_box("nginx", MB_OK, ngx_errno,
223                         "Could not open error log file: "
224                         ngx_file_append_mode_n " \"" NGX_ERROR_LOG_PATH
225                         "\" failed");
226         return NULL;
227     }
228 
229 #else
230 
231     ngx_stderr.fd = STDERR_FILENO;
232 
233 #endif
234 
235     return &ngx_log;
236 }
237 
238 
239 ngx_log_t *
240 ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_array_t *args)
241 {
242     ngx_log_t  *log;
243     ngx_str_t  *value, *name;
244 
245     if (args) {
246         value = args->elts;
247         name = &value[1];
248 
249     } else {
250         name = NULL;
251     }
252 
253     log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
254     if (log == NULL) {
255         return NULL;
256     }
257 
258     log->file = ngx_conf_open_file(cycle, name);
259     if (log->file == NULL) {
260         return NULL;
261     }
262 
263     return log;
264 }
265 
266 
267 char *
268 ngx_set_error_log_levels(ngx_conf_t *cf, ngx_log_t *log)
269 {
270     ngx_uint_t   i, n, d;
271     ngx_str_t   *value;
272 
273     value = cf->args->elts;
274 
275     for (i = 2; i < cf->args->nelts; i++) {
276 
277         for (n = 1; n <= NGX_LOG_DEBUG; n++) {
278             if (ngx_strcmp(value[i].data, err_levels[n]) == 0) {
279 
280                 if (log->log_level != 0) {
281                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
282                                        "duplicate log level \"%s\"",
283                                        value[i].data);
284                     return NGX_CONF_ERROR;
285                 }
286 
287                 log->log_level = n;
288                 continue;
289             }
290         }
291 
292         for (n = 0, d = NGX_LOG_DEBUG_FIRST; d <= NGX_LOG_DEBUG_LAST; d <<= 1) {
293             if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) {
294                 if (log->log_level & ~NGX_LOG_DEBUG_ALL) {
295                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
296                                        "invalid log level \"%s\"",
297                                        value[i].data);
298                     return NGX_CONF_ERROR;
299                 }
300 
301                 log->log_level |= d;
302             }
303         }
304 
305 
306         if (log->log_level == 0) {
307             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
308                                "invalid log level \"%s\"", value[i].data);
309             return NGX_CONF_ERROR;
310         }
311     }
312 
313     if (log->log_level == NGX_LOG_DEBUG) {
314         log->log_level = NGX_LOG_DEBUG_ALL;
315     }
316 
317     return NGX_CONF_OK;
318 }
319 
320 
321 static char *
322 ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
323 {
324     ngx_str_t  *value;
325 
326     value = cf->args->elts;
327 
328     if (value[1].len == 6 && ngx_strcmp(value[1].data, "stderr") == 0) {
329         cf->cycle->new_log->file->fd = ngx_stderr.fd;
330         cf->cycle->new_log->file->name.len = 0;
331         cf->cycle->new_log->file->name.data = NULL;
332 
333     } else {
334         cf->cycle->new_log->file->name = value[1];
335 
336         if (ngx_conf_full_name(cf->cycle, &cf->cycle->new_log->file->name, 0)
337             == NGX_ERROR)
338         {
339             return NGX_CONF_ERROR;
340         }
341     }
342 
343     return ngx_set_error_log_levels(cf, cf->cycle->new_log);
344 }
345 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.