Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / core / ngx_log.c
blobd7830fb4b1b4f9b50bee9d162f6c1d4d0c4dc1df
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
8 #include <ngx_config.h>
9 #include <ngx_core.h>
12 static char *ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
15 static ngx_command_t ngx_errlog_commands[] = {
17 {ngx_string("error_log"),
18 NGX_MAIN_CONF|NGX_CONF_1MORE,
19 ngx_error_log,
22 NULL},
24 ngx_null_command
28 static ngx_core_module_t ngx_errlog_module_ctx = {
29 ngx_string("errlog"),
30 NULL,
31 NULL
35 ngx_module_t ngx_errlog_module = {
36 NGX_MODULE_V1,
37 &ngx_errlog_module_ctx, /* module context */
38 ngx_errlog_commands, /* module directives */
39 NGX_CORE_MODULE, /* module type */
40 NULL, /* init master */
41 NULL, /* init module */
42 NULL, /* init process */
43 NULL, /* init thread */
44 NULL, /* exit thread */
45 NULL, /* exit process */
46 NULL, /* exit master */
47 NGX_MODULE_V1_PADDING
51 static ngx_log_t ngx_log;
52 static ngx_open_file_t ngx_log_file;
53 ngx_uint_t ngx_use_stderr = 1;
56 static ngx_str_t err_levels[] = {
57 ngx_null_string,
58 ngx_string("emerg"),
59 ngx_string("alert"),
60 ngx_string("crit"),
61 ngx_string("error"),
62 ngx_string("warn"),
63 ngx_string("notice"),
64 ngx_string("info"),
65 ngx_string("debug")
68 static const char *debug_levels[] = {
69 "debug_core", "debug_alloc", "debug_mutex", "debug_event",
70 "debug_http", "debug_mail", "debug_mysql"
74 #if (NGX_HAVE_VARIADIC_MACROS)
76 void
77 ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
78 const char *fmt, ...)
80 #else
82 void
83 ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
84 const char *fmt, va_list args)
86 #endif
88 #if (NGX_HAVE_VARIADIC_MACROS)
89 va_list args;
90 #endif
91 u_char *p, *last, *msg;
92 u_char errstr[NGX_MAX_ERROR_STR];
94 if (log->file->fd == NGX_INVALID_FILE) {
95 return;
98 last = errstr + NGX_MAX_ERROR_STR;
100 ngx_memcpy(errstr, ngx_cached_err_log_time.data,
101 ngx_cached_err_log_time.len);
103 p = errstr + ngx_cached_err_log_time.len;
105 p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]);
107 /* pid#tid */
108 p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ",
109 ngx_log_pid, ngx_log_tid);
111 if (log->connection) {
112 p = ngx_slprintf(p, last, "*%uA ", log->connection);
115 msg = p;
117 #if (NGX_HAVE_VARIADIC_MACROS)
119 va_start(args, fmt);
120 p = ngx_vslprintf(p, last, fmt, args);
121 va_end(args);
123 #else
125 p = ngx_vslprintf(p, last, fmt, args);
127 #endif
129 if (err) {
130 p = ngx_log_errno(p, last, err);
133 if (level != NGX_LOG_DEBUG && log->handler) {
134 p = log->handler(log, p, last - p);
137 if (p > last - NGX_LINEFEED_SIZE) {
138 p = last - NGX_LINEFEED_SIZE;
141 ngx_linefeed(p);
143 (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
145 if (!ngx_use_stderr
146 || level > NGX_LOG_WARN
147 || log->file->fd == ngx_stderr)
149 return;
152 msg -= (7 + err_levels[level].len + 3);
154 (void) ngx_sprintf(msg, "nginx: [%V] ", &err_levels[level]);
156 (void) ngx_write_console(ngx_stderr, msg, p - msg);
160 #if !(NGX_HAVE_VARIADIC_MACROS)
162 void ngx_cdecl
163 ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
164 const char *fmt, ...)
166 va_list args;
168 if (log->log_level >= level) {
169 va_start(args, fmt);
170 ngx_log_error_core(level, log, err, fmt, args);
171 va_end(args);
176 void ngx_cdecl
177 ngx_log_debug_core(ngx_log_t *log, ngx_err_t err, const char *fmt, ...)
179 va_list args;
181 va_start(args, fmt);
182 ngx_log_error_core(NGX_LOG_DEBUG, log, err, fmt, args);
183 va_end(args);
186 #endif
189 void ngx_cdecl
190 ngx_log_abort(ngx_err_t err, const char *fmt, ...)
192 u_char *p;
193 va_list args;
194 u_char errstr[NGX_MAX_CONF_ERRSTR];
196 va_start(args, fmt);
197 p = ngx_vsnprintf(errstr, sizeof(errstr) - 1, fmt, args);
198 va_end(args);
200 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
201 "%*s", p - errstr, errstr);
205 void ngx_cdecl
206 ngx_log_stderr(ngx_err_t err, const char *fmt, ...)
208 u_char *p, *last;
209 va_list args;
210 u_char errstr[NGX_MAX_ERROR_STR];
212 last = errstr + NGX_MAX_ERROR_STR;
213 p = errstr + 7;
215 ngx_memcpy(errstr, "nginx: ", 7);
217 va_start(args, fmt);
218 p = ngx_vslprintf(p, last, fmt, args);
219 va_end(args);
221 if (err) {
222 p = ngx_log_errno(p, last, err);
225 if (p > last - NGX_LINEFEED_SIZE) {
226 p = last - NGX_LINEFEED_SIZE;
229 ngx_linefeed(p);
231 (void) ngx_write_console(ngx_stderr, errstr, p - errstr);
235 u_char *
236 ngx_log_errno(u_char *buf, u_char *last, ngx_err_t err)
238 if (buf > last - 50) {
240 /* leave a space for an error code */
242 buf = last - 50;
243 *buf++ = '.';
244 *buf++ = '.';
245 *buf++ = '.';
248 #if (NGX_WIN32)
249 buf = ngx_slprintf(buf, last, ((unsigned) err < 0x80000000)
250 ? " (%d: " : " (%Xd: ", err);
251 #else
252 buf = ngx_slprintf(buf, last, " (%d: ", err);
253 #endif
255 buf = ngx_strerror(err, buf, last - buf);
257 if (buf < last) {
258 *buf++ = ')';
261 return buf;
265 ngx_log_t *
266 ngx_log_init(u_char *prefix)
268 u_char *p, *name;
269 size_t nlen, plen;
271 ngx_log.file = &ngx_log_file;
272 ngx_log.log_level = NGX_LOG_NOTICE;
274 name = (u_char *) NGX_ERROR_LOG_PATH;
277 * we use ngx_strlen() here since BCC warns about
278 * condition is always false and unreachable code
281 nlen = ngx_strlen(name);
283 if (nlen == 0) {
284 ngx_log_file.fd = ngx_stderr;
285 return &ngx_log;
288 p = NULL;
290 #if (NGX_WIN32)
291 if (name[1] != ':') {
292 #else
293 if (name[0] != '/') {
294 #endif
296 if (prefix) {
297 plen = ngx_strlen(prefix);
299 } else {
300 #ifdef NGX_PREFIX
301 prefix = (u_char *) NGX_PREFIX;
302 plen = ngx_strlen(prefix);
303 #else
304 plen = 0;
305 #endif
308 if (plen) {
309 name = malloc(plen + nlen + 2);
310 if (name == NULL) {
311 return NULL;
314 p = ngx_cpymem(name, prefix, plen);
316 if (!ngx_path_separator(*(p - 1))) {
317 *p++ = '/';
320 ngx_cpystrn(p, (u_char *) NGX_ERROR_LOG_PATH, nlen + 1);
322 p = name;
326 ngx_log_file.fd = ngx_open_file(name, NGX_FILE_APPEND,
327 NGX_FILE_CREATE_OR_OPEN,
328 NGX_FILE_DEFAULT_ACCESS);
330 if (ngx_log_file.fd == NGX_INVALID_FILE) {
331 ngx_log_stderr(ngx_errno,
332 "[alert] could not open error log file: "
333 ngx_open_file_n " \"%s\" failed", name);
334 #if (NGX_WIN32)
335 ngx_event_log(ngx_errno,
336 "could not open error log file: "
337 ngx_open_file_n " \"%s\" failed", name);
338 #endif
340 ngx_log_file.fd = ngx_stderr;
343 if (p) {
344 ngx_free(p);
347 return &ngx_log;
351 ngx_log_t *
352 ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name)
354 ngx_log_t *log;
356 log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
357 if (log == NULL) {
358 return NULL;
361 log->file = ngx_conf_open_file(cycle, name);
362 if (log->file == NULL) {
363 return NULL;
366 return log;
370 char *
371 ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)
373 ngx_uint_t i, n, d, found;
374 ngx_str_t *value;
376 value = cf->args->elts;
378 for (i = 2; i < cf->args->nelts; i++) {
379 found = 0;
381 for (n = 1; n <= NGX_LOG_DEBUG; n++) {
382 if (ngx_strcmp(value[i].data, err_levels[n].data) == 0) {
384 if (log->log_level != 0) {
385 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
386 "duplicate log level \"%V\"",
387 &value[i]);
388 return NGX_CONF_ERROR;
391 log->log_level = n;
392 found = 1;
393 break;
397 for (n = 0, d = NGX_LOG_DEBUG_FIRST; d <= NGX_LOG_DEBUG_LAST; d <<= 1) {
398 if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) {
399 if (log->log_level & ~NGX_LOG_DEBUG_ALL) {
400 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
401 "invalid log level \"%V\"",
402 &value[i]);
403 return NGX_CONF_ERROR;
406 log->log_level |= d;
407 found = 1;
408 break;
413 if (!found) {
414 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
415 "invalid log level \"%V\"", &value[i]);
416 return NGX_CONF_ERROR;
420 if (log->log_level == NGX_LOG_DEBUG) {
421 log->log_level = NGX_LOG_DEBUG_ALL;
424 return NGX_CONF_OK;
428 static char *
429 ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
431 ngx_str_t *value, name;
433 if (cf->cycle->new_log.file) {
434 return "is duplicate";
437 value = cf->args->elts;
439 if (ngx_strcmp(value[1].data, "stderr") == 0) {
440 ngx_str_null(&name);
442 } else {
443 name = value[1];
446 cf->cycle->new_log.file = ngx_conf_open_file(cf->cycle, &name);
447 if (cf->cycle->new_log.file == NULL) {
448 return NULL;
451 if (cf->args->nelts == 2) {
452 cf->cycle->new_log.log_level = NGX_LOG_ERR;
453 return NGX_CONF_OK;
456 cf->cycle->new_log.log_level = 0;
458 return ngx_log_set_levels(cf, &cf->cycle->new_log);