upstream nginx-0.7.39
[nginx.git] / nginx / src / core / ngx_log.c
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         p = ngx_snprintf(p, last - p, ((unsigned) err < 0x80000000)
131                                            ? " (%d: " : " (%Xd: ", err);
132 #else
133         p = ngx_snprintf(p, last - p, " (%d: ", err);
134 #endif
135
136         p = ngx_strerror_r(err, p, last - p);
137
138         if (p < last) {
139             *p++ = ')';
140         }
141     }
142
143     if (level != NGX_LOG_DEBUG && log->handler) {
144         p = log->handler(log, p, last - p);
145     }
146
147     if (p > last - NGX_LINEFEED_SIZE) {
148         p = last - NGX_LINEFEED_SIZE;
149     }
150
151     ngx_linefeed(p);
152
153     (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
154 }
155
156
157 #if !(NGX_HAVE_VARIADIC_MACROS)
158
159 void ngx_cdecl
160 ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
161     const char *fmt, ...)
162 {
163     va_list  args;
164
165     if (log->log_level >= level) {
166         va_start(args, fmt);
167         ngx_log_error_core(level, log, err, fmt, args);
168         va_end(args);
169     }
170 }
171
172
173 void ngx_cdecl
174 ngx_log_debug_core(ngx_log_t *log, ngx_err_t err, const char *fmt, ...)
175 {
176     va_list  args;
177
178     va_start(args, fmt);
179     ngx_log_error_core(NGX_LOG_DEBUG, log, err, fmt, args);
180     va_end(args);
181 }
182
183 #endif
184
185
186 void
187 ngx_log_abort(ngx_err_t err, const char *text)
188 {
189     ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err, text);
190 }
191
192
193 ngx_log_t *
194 ngx_log_init(void)
195 {
196     ngx_log.file = &ngx_stderr;
197     ngx_log.log_level = NGX_LOG_NOTICE;
198
199 #if (NGX_WIN32)
200
201     ngx_stderr_fileno = GetStdHandle(STD_ERROR_HANDLE);
202
203     ngx_stderr.fd = ngx_open_file(NGX_ERROR_LOG_PATH, NGX_FILE_RDWR,
204                                   NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND, 0);
205
206     if (ngx_stderr.fd == NGX_INVALID_FILE) {
207         ngx_message_box("nginx", MB_OK, ngx_errno,
208                         "Could not open error log file: "
209                         ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed");
210         return NULL;
211     }
212
213     if (ngx_file_append_mode(ngx_stderr.fd) == NGX_ERROR) {
214         ngx_message_box("nginx", MB_OK, ngx_errno,
215                         "Could not open error log file: "
216                         ngx_file_append_mode_n " \"" NGX_ERROR_LOG_PATH
217                         "\" failed");
218         return NULL;
219     }
220
221 #else
222
223     ngx_stderr.fd = STDERR_FILENO;
224
225 #endif
226
227     return &ngx_log;
228 }
229
230
231 ngx_log_t *
232 ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_array_t *args)
233 {
234     ngx_log_t  *log;
235     ngx_str_t  *value, *name;
236
237     if (args) {
238         value = args->elts;
239         name = &value[1];
240
241     } else {
242         name = NULL;
243     }
244
245     log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
246     if (log == NULL) {
247         return NULL;
248     }
249
250     log->file = ngx_conf_open_file(cycle, name);
251     if (log->file == NULL) {
252         return NULL;
253     }
254
255     return log;
256 }
257
258
259 char *
260 ngx_set_error_log_levels(ngx_conf_t *cf, ngx_log_t *log)
261 {
262     ngx_uint_t   i, n, d;
263     ngx_str_t   *value;
264
265     value = cf->args->elts;
266
267     for (i = 2; i < cf->args->nelts; i++) {
268
269         for (n = 1; n <= NGX_LOG_DEBUG; n++) {
270             if (ngx_strcmp(value[i].data, err_levels[n]) == 0) {
271
272                 if (log->log_level != 0) {
273                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
274                                        "duplicate log level \"%s\"",
275                                        value[i].data);
276                     return NGX_CONF_ERROR;
277                 }
278
279                 log->log_level = n;
280                 continue;
281             }
282         }
283
284         for (n = 0, d = NGX_LOG_DEBUG_FIRST; d <= NGX_LOG_DEBUG_LAST; d <<= 1) {
285             if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) {
286                 if (log->log_level & ~NGX_LOG_DEBUG_ALL) {
287                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
288                                        "invalid log level \"%s\"",
289                                        value[i].data);
290                     return NGX_CONF_ERROR;
291                 }
292
293                 log->log_level |= d;
294             }
295         }
296
297
298         if (log->log_level == 0) {
299             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
300                                "invalid log level \"%s\"", value[i].data);
301             return NGX_CONF_ERROR;
302         }
303     }
304
305     if (log->log_level == 0) {
306         log->log_level = NGX_LOG_ERR;
307
308     } else if (log->log_level == NGX_LOG_DEBUG) {
309         log->log_level = NGX_LOG_DEBUG_ALL;
310     }
311
312     return NGX_CONF_OK;
313 }
314
315
316 static char *
317 ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
318 {
319     ngx_str_t  *value;
320
321     value = cf->args->elts;
322
323     if (value[1].len == 6 && ngx_strcmp(value[1].data, "stderr") == 0) {
324         cf->cycle->new_log->file->fd = ngx_stderr.fd;
325         cf->cycle->new_log->file->name.len = 0;
326         cf->cycle->new_log->file->name.data = NULL;
327
328     } else {
329         cf->cycle->new_log->file->name = value[1];
330
331         if (ngx_conf_full_name(cf->cycle, &cf->cycle->new_log->file->name, 0)
332             != NGX_OK)
333         {
334             return NGX_CONF_ERROR;
335         }
336     }
337
338     return ngx_set_error_log_levels(cf, cf->cycle->new_log);
339 }