~ubuntu-branches/ubuntu/trusty/nginx/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/ngx_log.c

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry
  • Date: 2013-04-25 12:51:45 UTC
  • mfrom: (1.3.28)
  • mto: (1.3.29) (15.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 64.
  • Revision ID: package-import@ubuntu.com-20130425125145-ugl0wor6bq0u5eae
Tags: upstream-1.4.0
ImportĀ upstreamĀ versionĀ 1.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/*
3
3
 * Copyright (C) Igor Sysoev
 
4
 * Copyright (C) Nginx, Inc.
4
5
 */
5
6
 
6
7
 
8
9
#include <ngx_core.h>
9
10
 
10
11
 
11
 
static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 
12
static char *ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
12
13
 
13
14
 
14
15
static ngx_command_t  ngx_errlog_commands[] = {
15
16
 
16
17
    {ngx_string("error_log"),
17
18
     NGX_MAIN_CONF|NGX_CONF_1MORE,
18
 
     ngx_set_error_log,
 
19
     ngx_error_log,
19
20
     0,
20
21
     0,
21
22
     NULL},
48
49
 
49
50
 
50
51
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"
 
52
static ngx_open_file_t  ngx_log_file;
 
53
ngx_uint_t              ngx_use_stderr = 1;
 
54
 
 
55
 
 
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")
57
66
};
58
67
 
59
68
static const char *debug_levels[] = {
79
88
#if (NGX_HAVE_VARIADIC_MACROS)
80
89
    va_list  args;
81
90
#endif
82
 
    u_char   errstr[NGX_MAX_ERROR_STR], *p, *last;
 
91
    u_char  *p, *last, *msg;
 
92
    u_char   errstr[NGX_MAX_ERROR_STR];
83
93
 
84
94
    if (log->file->fd == NGX_INVALID_FILE) {
85
95
        return;
92
102
 
93
103
    p = errstr + ngx_cached_err_log_time.len;
94
104
 
95
 
    p = ngx_snprintf(p, last - p, " [%s] ", err_levels[level]);
 
105
    p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]);
96
106
 
97
107
    /* pid#tid */
98
 
    p = ngx_snprintf(p, last - p, "%P#" NGX_TID_T_FMT ": ",
 
108
    p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ",
99
109
                    ngx_log_pid, ngx_log_tid);
100
110
 
101
111
    if (log->connection) {
102
 
        p = ngx_snprintf(p, last - p, "*%uA ", log->connection);
 
112
        p = ngx_slprintf(p, last, "*%uA ", log->connection);
103
113
    }
104
114
 
 
115
    msg = p;
 
116
 
105
117
#if (NGX_HAVE_VARIADIC_MACROS)
106
118
 
107
119
    va_start(args, fmt);
108
 
    p = ngx_vsnprintf(p, last - p, fmt, args);
 
120
    p = ngx_vslprintf(p, last, fmt, args);
109
121
    va_end(args);
110
122
 
111
123
#else
112
124
 
113
 
    p = ngx_vsnprintf(p, last - p, fmt, args);
 
125
    p = ngx_vslprintf(p, last, fmt, args);
114
126
 
115
127
#endif
116
128
 
117
129
    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
 
        }
 
130
        p = ngx_log_errno(p, last, err);
149
131
    }
150
132
 
151
133
    if (level != NGX_LOG_DEBUG && log->handler) {
159
141
    ngx_linefeed(p);
160
142
 
161
143
    (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
 
144
 
 
145
    if (!ngx_use_stderr
 
146
        || level > NGX_LOG_WARN
 
147
        || log->file->fd == ngx_stderr)
 
148
    {
 
149
        return;
 
150
    }
 
151
 
 
152
    msg -= (7 + err_levels[level].len + 3);
 
153
 
 
154
    (void) ngx_sprintf(msg, "nginx: [%V] ", &err_levels[level]);
 
155
 
 
156
    (void) ngx_write_console(ngx_stderr, msg, p - msg);
162
157
}
163
158
 
164
159
 
191
186
#endif
192
187
 
193
188
 
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);
 
189
void ngx_cdecl
 
190
ngx_log_abort(ngx_err_t err, const char *fmt, ...)
 
191
{
 
192
    u_char   *p;
 
193
    va_list   args;
 
194
    u_char    errstr[NGX_MAX_CONF_ERRSTR];
 
195
 
 
196
    va_start(args, fmt);
 
197
    p = ngx_vsnprintf(errstr, sizeof(errstr) - 1, fmt, args);
 
198
    va_end(args);
 
199
 
 
200
    ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
 
201
                  "%*s", p - errstr, errstr);
 
202
}
 
203
 
 
204
 
 
205
void ngx_cdecl
 
206
ngx_log_stderr(ngx_err_t err, const char *fmt, ...)
 
207
{
 
208
    u_char   *p, *last;
 
209
    va_list   args;
 
210
    u_char    errstr[NGX_MAX_ERROR_STR];
 
211
 
 
212
    last = errstr + NGX_MAX_ERROR_STR;
 
213
    p = errstr + 7;
 
214
 
 
215
    ngx_memcpy(errstr, "nginx: ", 7);
 
216
 
 
217
    va_start(args, fmt);
 
218
    p = ngx_vslprintf(p, last, fmt, args);
 
219
    va_end(args);
 
220
 
 
221
    if (err) {
 
222
        p = ngx_log_errno(p, last, err);
 
223
    }
 
224
 
 
225
    if (p > last - NGX_LINEFEED_SIZE) {
 
226
        p = last - NGX_LINEFEED_SIZE;
 
227
    }
 
228
 
 
229
    ngx_linefeed(p);
 
230
 
 
231
    (void) ngx_write_console(ngx_stderr, errstr, p - errstr);
 
232
}
 
233
 
 
234
 
 
235
u_char *
 
236
ngx_log_errno(u_char *buf, u_char *last, ngx_err_t err)
 
237
{
 
238
    if (buf > last - 50) {
 
239
 
 
240
        /* leave a space for an error code */
 
241
 
 
242
        buf = last - 50;
 
243
        *buf++ = '.';
 
244
        *buf++ = '.';
 
245
        *buf++ = '.';
 
246
    }
 
247
 
 
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
 
254
 
 
255
    buf = ngx_strerror(err, buf, last - buf);
 
256
 
 
257
    if (buf < last) {
 
258
        *buf++ = ')';
 
259
    }
 
260
 
 
261
    return buf;
198
262
}
199
263
 
200
264
 
201
265
ngx_log_t *
202
 
ngx_log_init(void)
 
266
ngx_log_init(u_char *prefix)
203
267
{
204
 
    ngx_log.file = &ngx_stderr;
 
268
    u_char  *p, *name;
 
269
    size_t   nlen, plen;
 
270
 
 
271
    ngx_log.file = &ngx_log_file;
205
272
    ngx_log.log_level = NGX_LOG_NOTICE;
206
273
 
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
 
274
    name = (u_char *) NGX_ERROR_LOG_PATH;
 
275
 
 
276
    /*
 
277
     * we use ngx_strlen() here since BCC warns about
 
278
     * condition is always false and unreachable code
 
279
     */
 
280
 
 
281
    nlen = ngx_strlen(name);
 
282
 
 
283
    if (nlen == 0) {
 
284
        ngx_log_file.fd = ngx_stderr;
 
285
        return &ngx_log;
 
286
    }
 
287
 
 
288
    p = NULL;
 
289
 
 
290
#if (NGX_WIN32)
 
291
    if (name[1] != ':') {
 
292
#else
 
293
    if (name[0] != '/') {
 
294
#endif
 
295
 
 
296
        if (prefix) {
 
297
            plen = ngx_strlen(prefix);
 
298
 
 
299
        } else {
 
300
#ifdef NGX_PREFIX
 
301
            prefix = (u_char *) NGX_PREFIX;
 
302
            plen = ngx_strlen(prefix);
 
303
#else
 
304
            plen = 0;
 
305
#endif
 
306
        }
 
307
 
 
308
        if (plen) {
 
309
            name = malloc(plen + nlen + 2);
 
310
            if (name == NULL) {
 
311
                return NULL;
 
312
            }
 
313
 
 
314
            p = ngx_cpymem(name, prefix, plen);
 
315
 
 
316
            if (!ngx_path_separator(*(p - 1))) {
 
317
                *p++ = '/';
 
318
            }
 
319
 
 
320
            ngx_cpystrn(p, (u_char *) NGX_ERROR_LOG_PATH, nlen + 1);
 
321
 
 
322
            p = name;
 
323
        }
 
324
    }
 
325
 
 
326
    ngx_log_file.fd = ngx_open_file(name, NGX_FILE_APPEND,
 
327
                                    NGX_FILE_CREATE_OR_OPEN,
 
328
                                    NGX_FILE_DEFAULT_ACCESS);
 
329
 
 
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
 
339
 
 
340
        ngx_log_file.fd = ngx_stderr;
 
341
    }
 
342
 
 
343
    if (p) {
 
344
        ngx_free(p);
 
345
    }
234
346
 
235
347
    return &ngx_log;
236
348
}
237
349
 
238
350
 
239
351
ngx_log_t *
240
 
ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_array_t *args)
 
352
ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name)
241
353
{
242
354
    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
355
 
253
356
    log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
254
357
    if (log == NULL) {
265
368
 
266
369
 
267
370
char *
268
 
ngx_set_error_log_levels(ngx_conf_t *cf, ngx_log_t *log)
 
371
ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)
269
372
{
270
 
    ngx_uint_t   i, n, d;
 
373
    ngx_uint_t   i, n, d, found;
271
374
    ngx_str_t   *value;
272
375
 
273
376
    value = cf->args->elts;
274
377
 
275
378
    for (i = 2; i < cf->args->nelts; i++) {
 
379
        found = 0;
276
380
 
277
381
        for (n = 1; n <= NGX_LOG_DEBUG; n++) {
278
 
            if (ngx_strcmp(value[i].data, err_levels[n]) == 0) {
 
382
            if (ngx_strcmp(value[i].data, err_levels[n].data) == 0) {
279
383
 
280
384
                if (log->log_level != 0) {
281
385
                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
282
 
                                       "duplicate log level \"%s\"",
283
 
                                       value[i].data);
 
386
                                       "duplicate log level \"%V\"",
 
387
                                       &value[i]);
284
388
                    return NGX_CONF_ERROR;
285
389
                }
286
390
 
287
391
                log->log_level = n;
288
 
                continue;
 
392
                found = 1;
 
393
                break;
289
394
            }
290
395
        }
291
396
 
293
398
            if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) {
294
399
                if (log->log_level & ~NGX_LOG_DEBUG_ALL) {
295
400
                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
296
 
                                       "invalid log level \"%s\"",
297
 
                                       value[i].data);
 
401
                                       "invalid log level \"%V\"",
 
402
                                       &value[i]);
298
403
                    return NGX_CONF_ERROR;
299
404
                }
300
405
 
301
406
                log->log_level |= d;
 
407
                found = 1;
 
408
                break;
302
409
            }
303
410
        }
304
411
 
305
412
 
306
 
        if (log->log_level == 0) {
 
413
        if (!found) {
307
414
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
308
 
                               "invalid log level \"%s\"", value[i].data);
 
415
                               "invalid log level \"%V\"", &value[i]);
309
416
            return NGX_CONF_ERROR;
310
417
        }
311
418
    }
319
426
 
320
427
 
321
428
static char *
322
 
ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
429
ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
323
430
{
324
 
    ngx_str_t  *value;
 
431
    ngx_str_t  *value, name;
 
432
 
 
433
    if (cf->cycle->new_log.file) {
 
434
        return "is duplicate";
 
435
    }
325
436
 
326
437
    value = cf->args->elts;
327
438
 
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;
 
439
    if (ngx_strcmp(value[1].data, "stderr") == 0) {
 
440
        ngx_str_null(&name);
332
441
 
333
442
    } 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);
 
443
        name = value[1];
 
444
    }
 
445
 
 
446
    cf->cycle->new_log.file = ngx_conf_open_file(cf->cycle, &name);
 
447
    if (cf->cycle->new_log.file == NULL) {
 
448
        return NULL;
 
449
    }
 
450
 
 
451
    if (cf->args->nelts == 2) {
 
452
        cf->cycle->new_log.log_level = NGX_LOG_ERR;
 
453
        return NGX_CONF_OK;
 
454
    }
 
455
 
 
456
    cf->cycle->new_log.log_level = 0;
 
457
 
 
458
    return ngx_log_set_levels(cf, &cf->cycle->new_log);
344
459
}