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_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_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_log_file;
52 ngx_uint_t ngx_use_stderr = 1;
53
54
55 static ngx_str_t err_levels[] = {
56 ngx_null_string,
57 ngx_string("emerg"),
58 ngx_string("alert"),
59 ngx_string("crit"),
60 ngx_string("error"),
61 ngx_string("warn"),
62 ngx_string("notice"),
63 ngx_string("info"),
64 ngx_string("debug")
65 };
66
67 static const char *debug_levels[] = {
68 "debug_core", "debug_alloc", "debug_mutex", "debug_event",
69 "debug_http", "debug_mail", "debug_mysql"
70 };
71
72
73 #if (NGX_HAVE_VARIADIC_MACROS)
74
75 void
76 ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
77 const char *fmt, ...)
78
79 #else
80
81 void
82 ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
83 const char *fmt, va_list args)
84
85 #endif
86 {
87 #if (NGX_HAVE_VARIADIC_MACROS)
88 va_list args;
89 #endif
90 u_char *p, *last, *msg;
91 u_char errstr[NGX_MAX_ERROR_STR];
92
93 if (log->file->fd == NGX_INVALID_FILE) {
94 return;
95 }
96
97 last = errstr + NGX_MAX_ERROR_STR;
98
99 ngx_memcpy(errstr, ngx_cached_err_log_time.data,
100 ngx_cached_err_log_time.len);
101
102 p = errstr + ngx_cached_err_log_time.len;
103
104 p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]);
105
106 /* pid#tid */
107 p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ",
108 ngx_log_pid, ngx_log_tid);
109
110 if (log->connection) {
111 p = ngx_slprintf(p, last, "*%uA ", log->connection);
112 }
113
114 msg = p;
115
116 #if (NGX_HAVE_VARIADIC_MACROS)
117
118 va_start(args, fmt);
119 p = ngx_vslprintf(p, last, fmt, args);
120 va_end(args);
121
122 #else
123
124 p = ngx_vslprintf(p, last, fmt, args);
125
126 #endif
127
128 if (err) {
129 p = ngx_log_errno(p, last, err);
130 }
131
132 if (level != NGX_LOG_DEBUG && log->handler) {
133 p = log->handler(log, p, last - p);
134 }
135
136 if (p > last - NGX_LINEFEED_SIZE) {
137 p = last - NGX_LINEFEED_SIZE;
138 }
139
140 ngx_linefeed(p);
141
142 (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
143
144 if (!ngx_use_stderr
145 || level > NGX_LOG_WARN
146 || log->file->fd == ngx_stderr)
147 {
148 return;
149 }
150
151 msg -= (err_levels[level].len + 4);
152
153 (void) ngx_sprintf(msg, "[%V]: ", &err_levels[level]);
154
155 (void) ngx_write_console(ngx_stderr, msg, p - msg);
156 }
157
158
159 #if !(NGX_HAVE_VARIADIC_MACROS)
160
161 void ngx_cdecl
162 ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
163 const char *fmt, ...)
164 {
165 va_list args;
166
167 if (log->log_level >= level) {
168 va_start(args, fmt);
169 ngx_log_error_core(level, log, err, fmt, args);
170 va_end(args);
171 }
172 }
173
174
175 void ngx_cdecl
176 ngx_log_debug_core(ngx_log_t *log, ngx_err_t err, const char *fmt, ...)
177 {
178 va_list args;
179
180 va_start(args, fmt);
181 ngx_log_error_core(NGX_LOG_DEBUG, log, err, fmt, args);
182 va_end(args);
183 }
184
185 #endif
186
187
188 void ngx_cdecl
189 ngx_log_abort(ngx_err_t err, const char *fmt, ...)
190 {
191 u_char *p;
192 va_list args;
193 u_char errstr[NGX_MAX_CONF_ERRSTR];
194
195 va_start(args, fmt);
196 p = ngx_vsnprintf(errstr, sizeof(errstr) - 1, fmt, args);
197 va_end(args);
198
199 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
200 "%*s", p - errstr, errstr);
201 }
202
203
204 void ngx_cdecl
205 ngx_log_stderr(ngx_err_t err, const char *fmt, ...)
206 {
207 u_char *p, *last;
208 va_list args;
209 u_char errstr[NGX_MAX_ERROR_STR];
210
211 last = errstr + NGX_MAX_ERROR_STR;
212
213 va_start(args, fmt);
214 p = ngx_vslprintf(errstr, last, fmt, args);
215 va_end(args);
216
217 if (err) {
218 p = ngx_log_errno(p, last, err);
219 }
220
221 if (p > last - NGX_LINEFEED_SIZE) {
222 p = last - NGX_LINEFEED_SIZE;
223 }
224
225 ngx_linefeed(p);
226
227 (void) ngx_write_console(ngx_stderr, errstr, p - errstr);
228 }
229
230
231 u_char *
232 ngx_log_errno(u_char *buf, u_char *last, ngx_err_t err)
233 {
234 if (buf > last - 50) {
235
236 /* leave a space for an error code */
237
238 buf = last - 50;
239 *buf++ = '.';
240 *buf++ = '.';
241 *buf++ = '.';
242 }
243
244 #if (NGX_WIN32)
245 buf = ngx_slprintf(buf, last, ((unsigned) err < 0x80000000)
246 ? " (%d: " : " (%Xd: ", err);
247 #else
248 buf = ngx_slprintf(buf, last, " (%d: ", err);
249 #endif
250
251 buf = ngx_strerror_r(err, buf, last - buf);
252
253 if (buf < last) {
254 *buf++ = ')';
255 }
256
257 return buf;
258 }
259
260
261 ngx_log_t *
262 ngx_log_init(u_char *prefix)
263 {
264 u_char *p, *name;
265 size_t nlen, plen;
266
267 ngx_log.file = &ngx_log_file;
268 ngx_log.log_level = NGX_LOG_NOTICE;
269
270 name = (u_char *) NGX_ERROR_LOG_PATH;
271
272 /*
273 * we use ngx_strlen() here since BCC warns about
274 * condition is always false and unreachable code
275 */
276
277 nlen = ngx_strlen(name);
278
279 if (nlen == 0) {
280 ngx_log_file.fd = ngx_stderr;
281 return &ngx_log;
282 }
283
284 p = NULL;
285
286 #if (NGX_WIN32)
287 if (name[1] != ':') {
288 #else
289 if (name[0] != '/') {
290 #endif
291
292 if (prefix) {
293 plen = ngx_strlen(prefix);
294
295 } else {
296 #ifdef NGX_PREFIX
297 prefix = (u_char *) NGX_PREFIX;
298 plen = ngx_strlen(prefix);
299 #else
300 plen = 0;
301 #endif
302 }
303
304 if (plen) {
305 name = malloc(plen + nlen + 2);
306 if (name == NULL) {
307 return NULL;
308 }
309
310 p = ngx_cpymem(name, prefix, plen);
311
312 if (!ngx_path_separator(*(p - 1))) {
313 *p++ = '/';
314 }
315
316 ngx_cpystrn(p, (u_char *) NGX_ERROR_LOG_PATH, nlen + 1);
317
318 p = name;
319 }
320 }
321
322 ngx_log_file.fd = ngx_open_file(name, NGX_FILE_APPEND,
323 NGX_FILE_CREATE_OR_OPEN,
324 NGX_FILE_DEFAULT_ACCESS);
325
326 if (ngx_log_file.fd == NGX_INVALID_FILE) {
327 ngx_log_stderr(ngx_errno,
328 "[alert]: could not open error log file: "
329 ngx_open_file_n " \"%s\" failed", name);
330 #if (NGX_WIN32)
331 ngx_event_log(ngx_errno,
332 "could not open error log file: "
333 ngx_open_file_n " \"%s\" failed", name);
334 #endif
335
336 ngx_log_file.fd = ngx_stderr;
337 }
338
339 if (p) {
340 ngx_free(p);
341 }
342
343 return &ngx_log;
344 }
345
346
347 ngx_log_t *
348 ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name)
349 {
350 ngx_log_t *log;
351
352 log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
353 if (log == NULL) {
354 return NULL;
355 }
356
357 log->file = ngx_conf_open_file(cycle, name);
358 if (log->file == NULL) {
359 return NULL;
360 }
361
362 return log;
363 }
364
365
366 char *
367 ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)
368 {
369 ngx_uint_t i, n, d;
370 ngx_str_t *value;
371
372 value = cf->args->elts;
373
374 for (i = 2; i < cf->args->nelts; i++) {
375
376 for (n = 1; n <= NGX_LOG_DEBUG; n++) {
377 if (ngx_strcmp(value[i].data, err_levels[n].data) == 0) {
378
379 if (log->log_level != 0) {
380 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
381 "duplicate log level \"%V\"",
382 &value[i]);
383 return NGX_CONF_ERROR;
384 }
385
386 log->log_level = n;
387 continue;
388 }
389 }
390
391 for (n = 0, d = NGX_LOG_DEBUG_FIRST; d <= NGX_LOG_DEBUG_LAST; d <<= 1) {
392 if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) {
393 if (log->log_level & ~NGX_LOG_DEBUG_ALL) {
394 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
395 "invalid log level \"%V\"",
396 &value[i]);
397 return NGX_CONF_ERROR;
398 }
399
400 log->log_level |= d;
401 }
402 }
403
404
405 if (log->log_level == 0) {
406 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
407 "invalid log level \"%V\"", &value[i]);
408 return NGX_CONF_ERROR;
409 }
410 }
411
412 if (log->log_level == NGX_LOG_DEBUG) {
413 log->log_level = NGX_LOG_DEBUG_ALL;
414 }
415
416 return NGX_CONF_OK;
417 }
418
419
420 static char *
421 ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
422 {
423 ngx_str_t *value, name;
424
425 if (cf->cycle->new_log.file) {
426 return "is duplicate";
427 }
428
429 value = cf->args->elts;
430
431 if (ngx_strcmp(value[1].data, "stderr") == 0) {
432 name.len = 0;
433 name.data = NULL;
434
435 } else {
436 name = value[1];
437 }
438
439 cf->cycle->new_log.file = ngx_conf_open_file(cf->cycle, &name);
440 if (cf->cycle->new_log.file == NULL) {
441 return NULL;
442 }
443
444 if (cf->args->nelts == 2) {
445 cf->cycle->new_log.log_level = NGX_LOG_ERR;
446 return NGX_CONF_OK;
447 }
448
449 cf->cycle->new_log.log_level = 0;
450
451 return ngx_log_set_levels(cf, &cf->cycle->new_log);
452 }
453
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.