118
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
260
|
12 typedef struct ngx_http_log_op_s ngx_http_log_op_t;
|
|
13
|
|
14 typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
|
|
15 ngx_http_log_op_t *op);
|
|
16
|
|
17 typedef size_t (*ngx_http_log_op_getlen_pt) (ngx_http_request_t *r,
|
|
18 uintptr_t data);
|
|
19
|
|
20
|
|
21 struct ngx_http_log_op_s {
|
|
22 size_t len;
|
|
23 ngx_http_log_op_getlen_pt getlen;
|
|
24 ngx_http_log_op_run_pt run;
|
|
25 uintptr_t data;
|
|
26 };
|
|
27
|
122
|
28
|
|
29 typedef struct {
|
|
30 ngx_str_t name;
|
440
|
31 ngx_array_t *flushes;
|
122
|
32 ngx_array_t *ops; /* array of ngx_http_log_op_t */
|
|
33 } ngx_http_log_fmt_t;
|
|
34
|
206
|
35
|
122
|
36 typedef struct {
|
|
37 ngx_array_t formats; /* array of ngx_http_log_fmt_t */
|
|
38 ngx_uint_t combined_used; /* unsigned combined_used:1 */
|
|
39 } ngx_http_log_main_conf_t;
|
|
40
|
206
|
41
|
122
|
42 typedef struct {
|
384
|
43 ngx_array_t *lengths;
|
|
44 ngx_array_t *values;
|
|
45 } ngx_http_log_script_t;
|
|
46
|
|
47
|
|
48 typedef struct {
|
122
|
49 ngx_open_file_t *file;
|
384
|
50 ngx_http_log_script_t *script;
|
182
|
51 time_t disk_full_time;
|
278
|
52 time_t error_log_time;
|
440
|
53 ngx_http_log_fmt_t *format;
|
122
|
54 } ngx_http_log_t;
|
|
55
|
206
|
56
|
122
|
57 typedef struct {
|
|
58 ngx_array_t *logs; /* array of ngx_http_log_t */
|
384
|
59
|
|
60 ngx_open_file_cache_t *open_file_cache;
|
|
61 time_t open_file_cache_valid;
|
|
62 ngx_uint_t open_file_cache_min_uses;
|
|
63
|
122
|
64 ngx_uint_t off; /* unsigned off:1 */
|
|
65 } ngx_http_log_loc_conf_t;
|
|
66
|
|
67
|
|
68 typedef struct {
|
|
69 ngx_str_t name;
|
|
70 size_t len;
|
|
71 ngx_http_log_op_run_pt run;
|
|
72 } ngx_http_log_var_t;
|
|
73
|
|
74
|
278
|
75 static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log,
|
|
76 u_char *buf, size_t len);
|
384
|
77 static ssize_t ngx_http_log_script_write(ngx_http_request_t *r,
|
|
78 ngx_http_log_script_t *script, u_char **name, u_char *buf, size_t len);
|
278
|
79
|
118
|
80 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
|
|
81 ngx_http_log_op_t *op);
|
|
82 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
|
|
83 ngx_http_log_op_t *op);
|
|
84 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
|
|
85 ngx_http_log_op_t *op);
|
|
86 static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
|
|
87 ngx_http_log_op_t *op);
|
|
88 static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
|
|
89 ngx_http_log_op_t *op);
|
|
90 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
|
|
91 ngx_http_log_op_t *op);
|
122
|
92 static u_char *ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
118
|
93 ngx_http_log_op_t *op);
|
126
|
94 static u_char *ngx_http_log_body_bytes_sent(ngx_http_request_t *r,
|
122
|
95 u_char *buf, ngx_http_log_op_t *op);
|
118
|
96 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
|
|
97 ngx_http_log_op_t *op);
|
|
98
|
|
99 static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
|
|
100 ngx_http_log_op_t *op, ngx_str_t *value);
|
|
101 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
|
|
102 uintptr_t data);
|
|
103 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
|
|
104 ngx_http_log_op_t *op);
|
376
|
105 static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size);
|
118
|
106
|
|
107
|
|
108 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
|
|
109 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
|
|
110 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
|
|
111 void *child);
|
|
112 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
113 void *conf);
|
|
114 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
115 void *conf);
|
122
|
116 static char *ngx_http_log_compile_format(ngx_conf_t *cf,
|
440
|
117 ngx_array_t *flushes, ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s);
|
384
|
118 static char *ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
119 void *conf);
|
122
|
120 static ngx_int_t ngx_http_log_init(ngx_conf_t *cf);
|
118
|
121
|
|
122
|
|
123 static ngx_command_t ngx_http_log_commands[] = {
|
|
124
|
|
125 { ngx_string("log_format"),
|
|
126 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
|
|
127 ngx_http_log_set_format,
|
|
128 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
129 0,
|
|
130 NULL },
|
|
131
|
|
132 { ngx_string("access_log"),
|
194
|
133 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
340
|
134 |NGX_HTTP_LMT_CONF|NGX_CONF_TAKE123,
|
118
|
135 ngx_http_log_set_log,
|
|
136 NGX_HTTP_LOC_CONF_OFFSET,
|
|
137 0,
|
|
138 NULL },
|
|
139
|
384
|
140 { ngx_string("open_log_file_cache"),
|
|
141 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
|
|
142 ngx_http_log_open_file_cache,
|
|
143 NGX_HTTP_LOC_CONF_OFFSET,
|
|
144 0,
|
|
145 NULL },
|
|
146
|
118
|
147 ngx_null_command
|
|
148 };
|
|
149
|
|
150
|
216
|
151 static ngx_http_module_t ngx_http_log_module_ctx = {
|
260
|
152 NULL, /* preconfiguration */
|
122
|
153 ngx_http_log_init, /* postconfiguration */
|
118
|
154
|
|
155 ngx_http_log_create_main_conf, /* create main configuration */
|
|
156 NULL, /* init main configuration */
|
|
157
|
|
158 NULL, /* create server configuration */
|
|
159 NULL, /* merge server configuration */
|
|
160
|
|
161 ngx_http_log_create_loc_conf, /* create location configration */
|
|
162 ngx_http_log_merge_loc_conf /* merge location configration */
|
|
163 };
|
|
164
|
|
165
|
|
166 ngx_module_t ngx_http_log_module = {
|
|
167 NGX_MODULE_V1,
|
|
168 &ngx_http_log_module_ctx, /* module context */
|
|
169 ngx_http_log_commands, /* module directives */
|
|
170 NGX_HTTP_MODULE, /* module type */
|
|
171 NULL, /* init master */
|
122
|
172 NULL, /* init module */
|
118
|
173 NULL, /* init process */
|
|
174 NULL, /* init thread */
|
|
175 NULL, /* exit thread */
|
|
176 NULL, /* exit process */
|
|
177 NULL, /* exit master */
|
|
178 NGX_MODULE_V1_PADDING
|
|
179 };
|
|
180
|
|
181
|
278
|
182 static ngx_str_t ngx_http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
|
122
|
183
|
|
184
|
|
185 static ngx_str_t ngx_http_combined_fmt =
|
130
|
186 ngx_string("$remote_addr - $remote_user [$time_local] "
|
126
|
187 "\"$request\" $status $body_bytes_sent "
|
122
|
188 "\"$http_referer\" \"$http_user_agent\"");
|
118
|
189
|
|
190
|
122
|
191 static ngx_http_log_var_t ngx_http_log_vars[] = {
|
|
192 { ngx_string("connection"), NGX_ATOMIC_T_LEN, ngx_http_log_connection },
|
|
193 { ngx_string("pipe"), 1, ngx_http_log_pipe },
|
130
|
194 { ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
|
122
|
195 ngx_http_log_time },
|
|
196 { ngx_string("msec"), NGX_TIME_T_LEN + 4, ngx_http_log_msec },
|
298
|
197 { ngx_string("request_time"), NGX_TIME_T_LEN + 4,
|
|
198 ngx_http_log_request_time },
|
122
|
199 { ngx_string("status"), 3, ngx_http_log_status },
|
|
200 { ngx_string("bytes_sent"), NGX_OFF_T_LEN, ngx_http_log_bytes_sent },
|
126
|
201 { ngx_string("body_bytes_sent"), NGX_OFF_T_LEN,
|
|
202 ngx_http_log_body_bytes_sent },
|
122
|
203 { ngx_string("apache_bytes_sent"), NGX_OFF_T_LEN,
|
126
|
204 ngx_http_log_body_bytes_sent },
|
122
|
205 { ngx_string("request_length"), NGX_SIZE_T_LEN,
|
|
206 ngx_http_log_request_length },
|
|
207
|
|
208 { ngx_null_string, 0, NULL }
|
|
209 };
|
118
|
210
|
|
211
|
|
212 ngx_int_t
|
|
213 ngx_http_log_handler(ngx_http_request_t *r)
|
|
214 {
|
|
215 u_char *line, *p;
|
|
216 size_t len;
|
278
|
217 ngx_uint_t i, l;
|
118
|
218 ngx_http_log_t *log;
|
120
|
219 ngx_open_file_t *file;
|
118
|
220 ngx_http_log_op_t *op;
|
|
221 ngx_http_log_loc_conf_t *lcf;
|
|
222
|
|
223 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
224 "http log handler");
|
|
225
|
|
226 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
|
|
227
|
|
228 if (lcf->off) {
|
|
229 return NGX_OK;
|
|
230 }
|
|
231
|
|
232 log = lcf->logs->elts;
|
|
233 for (l = 0; l < lcf->logs->nelts; l++) {
|
|
234
|
182
|
235 if (ngx_time() == log[l].disk_full_time) {
|
|
236
|
|
237 /*
|
278
|
238 * on FreeBSD writing to a full filesystem with enabled softupdates
|
182
|
239 * may block process for much longer time than writing to non-full
|
278
|
240 * filesystem, so we skip writing to a log for one second
|
182
|
241 */
|
|
242
|
|
243 continue;
|
|
244 }
|
|
245
|
440
|
246 ngx_http_script_flush_no_cacheable_variables(r, log[l].format->flushes);
|
|
247
|
118
|
248 len = 0;
|
440
|
249 op = log[l].format->ops->elts;
|
|
250 for (i = 0; i < log[l].format->ops->nelts; i++) {
|
118
|
251 if (op[i].len == 0) {
|
|
252 len += op[i].getlen(r, op[i].data);
|
|
253
|
|
254 } else {
|
|
255 len += op[i].len;
|
|
256 }
|
|
257 }
|
|
258
|
120
|
259 len += NGX_LINEFEED_SIZE;
|
|
260
|
|
261 file = log[l].file;
|
|
262
|
384
|
263 if (file && file->buffer) {
|
120
|
264
|
|
265 if (len > (size_t) (file->last - file->pos)) {
|
|
266
|
278
|
267 ngx_http_log_write(r, &log[l], file->buffer,
|
|
268 file->pos - file->buffer);
|
120
|
269
|
|
270 file->pos = file->buffer;
|
|
271 }
|
|
272
|
|
273 if (len <= (size_t) (file->last - file->pos)) {
|
|
274
|
|
275 p = file->pos;
|
|
276
|
440
|
277 for (i = 0; i < log[l].format->ops->nelts; i++) {
|
120
|
278 p = op[i].run(r, p, &op[i]);
|
|
279 }
|
|
280
|
|
281 ngx_linefeed(p);
|
|
282
|
|
283 file->pos = p;
|
|
284
|
|
285 continue;
|
|
286 }
|
|
287 }
|
118
|
288
|
382
|
289 line = ngx_pnalloc(r->pool, len);
|
118
|
290 if (line == NULL) {
|
|
291 return NGX_ERROR;
|
|
292 }
|
|
293
|
|
294 p = line;
|
|
295
|
440
|
296 for (i = 0; i < log[l].format->ops->nelts; i++) {
|
118
|
297 p = op[i].run(r, p, &op[i]);
|
|
298 }
|
|
299
|
120
|
300 ngx_linefeed(p);
|
|
301
|
278
|
302 ngx_http_log_write(r, &log[l], line, p - line);
|
118
|
303 }
|
|
304
|
|
305 return NGX_OK;
|
|
306 }
|
|
307
|
|
308
|
278
|
309 static void
|
|
310 ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log, u_char *buf,
|
|
311 size_t len)
|
|
312 {
|
384
|
313 u_char *name;
|
|
314 time_t now;
|
|
315 ssize_t n;
|
|
316 ngx_err_t err;
|
278
|
317
|
384
|
318 if (log->script == NULL) {
|
|
319 name = log->file->name.data;
|
|
320 n = ngx_write_fd(log->file->fd, buf, len);
|
|
321
|
|
322 } else {
|
|
323 name = NULL;
|
|
324 n = ngx_http_log_script_write(r, log->script, &name, buf, len);
|
|
325 }
|
278
|
326
|
|
327 if (n == (ssize_t) len) {
|
|
328 return;
|
|
329 }
|
|
330
|
|
331 now = ngx_time();
|
|
332
|
|
333 if (n == -1) {
|
|
334 err = ngx_errno;
|
|
335
|
|
336 if (err == NGX_ENOSPC) {
|
|
337 log->disk_full_time = now;
|
|
338 }
|
|
339
|
296
|
340 if (now - log->error_log_time > 59) {
|
278
|
341 ngx_log_error(NGX_LOG_ALERT, r->connection->log, err,
|
384
|
342 ngx_write_fd_n " to \"%s\" failed", name);
|
278
|
343
|
|
344 log->error_log_time = now;
|
|
345 }
|
|
346
|
|
347 return;
|
|
348 }
|
|
349
|
296
|
350 if (now - log->error_log_time > 59) {
|
278
|
351 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
384
|
352 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
|
|
353 name, n, len);
|
278
|
354
|
|
355 log->error_log_time = now;
|
|
356 }
|
|
357 }
|
|
358
|
|
359
|
384
|
360 static ssize_t
|
|
361 ngx_http_log_script_write(ngx_http_request_t *r, ngx_http_log_script_t *script,
|
|
362 u_char **name, u_char *buf, size_t len)
|
|
363 {
|
|
364 size_t root;
|
|
365 ssize_t n;
|
|
366 ngx_str_t log, path;
|
|
367 ngx_open_file_info_t of;
|
|
368 ngx_http_log_loc_conf_t *llcf;
|
|
369 ngx_http_core_loc_conf_t *clcf;
|
|
370
|
388
|
371 if (!r->root_tested) {
|
384
|
372
|
|
373 /* test root directory existance */
|
|
374
|
|
375 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
|
|
376 /* simulate successfull logging */
|
|
377 return len;
|
|
378 }
|
|
379
|
|
380 path.data[root] = '\0';
|
|
381
|
|
382 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
383
|
|
384 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
385
|
|
386 of.valid = clcf->open_file_cache_valid;
|
|
387 of.min_uses = clcf->open_file_cache_min_uses;
|
522
|
388 of.test_dir = 1;
|
|
389 of.test_only = 1;
|
384
|
390 of.errors = clcf->open_file_cache_errors;
|
|
391 of.events = clcf->open_file_cache_events;
|
|
392
|
|
393 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
|
388
|
394 != NGX_OK)
|
384
|
395 {
|
388
|
396 if (of.err == 0) {
|
|
397 /* simulate successfull logging */
|
|
398 return len;
|
|
399 }
|
|
400
|
|
401 ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
|
|
402 "testing \"%s\" existence failed", path.data);
|
|
403
|
|
404 /* simulate successfull logging */
|
|
405 return len;
|
|
406 }
|
|
407
|
|
408 if (!of.is_dir) {
|
|
409 ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ENOTDIR,
|
|
410 "testing \"%s\" existence failed", path.data);
|
|
411
|
|
412 /* simulate successfull logging */
|
384
|
413 return len;
|
|
414 }
|
|
415 }
|
|
416
|
|
417 if (ngx_http_script_run(r, &log, script->lengths->elts, 1,
|
|
418 script->values->elts)
|
|
419 == NULL)
|
|
420 {
|
|
421 /* simulate successfull logging */
|
|
422 return len;
|
|
423 }
|
|
424
|
|
425 log.data[log.len - 1] = '\0';
|
|
426 *name = log.data;
|
|
427
|
|
428 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
429 "http log \"%s\"", log.data);
|
|
430
|
|
431 llcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
|
|
432
|
|
433 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
434
|
|
435 of.log = 1;
|
|
436 of.valid = llcf->open_file_cache_valid;
|
|
437 of.min_uses = llcf->open_file_cache_min_uses;
|
445
|
438 of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
|
384
|
439
|
|
440 if (ngx_open_cached_file(llcf->open_file_cache, &log, &of, r->pool)
|
|
441 != NGX_OK)
|
|
442 {
|
|
443 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
522
|
444 "%s \"%s\" failed", of.failed, log.data);
|
386
|
445 /* simulate successfull logging */
|
|
446 return len;
|
384
|
447 }
|
|
448
|
|
449 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
450 "http log #%d", of.fd);
|
|
451
|
|
452 n = ngx_write_fd(of.fd, buf, len);
|
|
453
|
|
454 return n;
|
|
455 }
|
|
456
|
|
457
|
118
|
458 static u_char *
|
|
459 ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
|
|
460 ngx_http_log_op_t *op)
|
|
461 {
|
|
462 size_t len;
|
|
463 uintptr_t data;
|
|
464
|
|
465 len = op->len;
|
|
466 data = op->data;
|
|
467
|
|
468 while (len--) {
|
|
469 *buf++ = (u_char) (data & 0xff);
|
|
470 data >>= 8;
|
|
471 }
|
|
472
|
|
473 return buf;
|
|
474 }
|
|
475
|
|
476
|
|
477 static u_char *
|
|
478 ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
|
|
479 ngx_http_log_op_t *op)
|
|
480 {
|
|
481 return ngx_cpymem(buf, (u_char *) op->data, op->len);
|
|
482 }
|
|
483
|
|
484
|
|
485 static u_char *
|
|
486 ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
|
|
487 ngx_http_log_op_t *op)
|
|
488 {
|
|
489 return ngx_sprintf(buf, "%ui", r->connection->number);
|
|
490 }
|
|
491
|
|
492
|
|
493 static u_char *
|
|
494 ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
495 {
|
|
496 if (r->pipeline) {
|
|
497 *buf = 'p';
|
|
498 } else {
|
|
499 *buf = '.';
|
|
500 }
|
|
501
|
|
502 return buf + 1;
|
|
503 }
|
|
504
|
|
505
|
|
506 static u_char *
|
|
507 ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
508 {
|
|
509 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
|
|
510 ngx_cached_http_log_time.len);
|
|
511 }
|
|
512
|
|
513
|
|
514 static u_char *
|
|
515 ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
516 {
|
|
517 ngx_time_t *tp;
|
|
518
|
|
519 tp = ngx_timeofday();
|
|
520
|
|
521 return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
|
|
522 }
|
|
523
|
|
524
|
|
525 static u_char *
|
|
526 ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
|
|
527 ngx_http_log_op_t *op)
|
|
528 {
|
298
|
529 ngx_time_t *tp;
|
|
530 ngx_msec_int_t ms;
|
|
531
|
|
532 tp = ngx_timeofday();
|
118
|
533
|
346
|
534 ms = (ngx_msec_int_t)
|
|
535 ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
|
298
|
536 ms = (ms >= 0) ? ms : 0;
|
118
|
537
|
298
|
538 return ngx_sprintf(buf, "%T.%03M", ms / 1000, ms % 1000);
|
118
|
539 }
|
|
540
|
|
541
|
|
542 static u_char *
|
|
543 ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
544 {
|
|
545 return ngx_sprintf(buf, "%ui",
|
|
546 r->err_status ? r->err_status : r->headers_out.status);
|
|
547 }
|
|
548
|
|
549
|
|
550 static u_char *
|
122
|
551 ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
|
552 ngx_http_log_op_t *op)
|
118
|
553 {
|
|
554 return ngx_sprintf(buf, "%O", r->connection->sent);
|
|
555 }
|
|
556
|
|
557
|
316
|
558 /*
|
|
559 * although there is a real $body_bytes_sent variable,
|
|
560 * this log operation code function is more optimized for logging
|
|
561 */
|
|
562
|
118
|
563 static u_char *
|
126
|
564 ngx_http_log_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
118
|
565 ngx_http_log_op_t *op)
|
|
566 {
|
|
567 off_t length;
|
|
568
|
|
569 length = r->connection->sent - r->header_size;
|
|
570
|
|
571 if (length > 0) {
|
|
572 return ngx_sprintf(buf, "%O", length);
|
|
573 }
|
|
574
|
|
575 *buf = '0';
|
|
576
|
|
577 return buf + 1;
|
|
578 }
|
|
579
|
|
580
|
|
581 static u_char *
|
|
582 ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
|
|
583 ngx_http_log_op_t *op)
|
|
584 {
|
212
|
585 return ngx_sprintf(buf, "%O", r->request_length);
|
118
|
586 }
|
|
587
|
|
588
|
|
589 static ngx_int_t
|
|
590 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
|
|
591 ngx_str_t *value)
|
|
592 {
|
|
593 ngx_int_t index;
|
|
594
|
|
595 index = ngx_http_get_variable_index(cf, value);
|
|
596 if (index == NGX_ERROR) {
|
|
597 return NGX_ERROR;
|
|
598 }
|
|
599
|
|
600 op->len = 0;
|
|
601 op->getlen = ngx_http_log_variable_getlen;
|
|
602 op->run = ngx_http_log_variable;
|
|
603 op->data = index;
|
|
604
|
|
605 return NGX_OK;
|
|
606 }
|
|
607
|
|
608
|
|
609 static size_t
|
|
610 ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
|
|
611 {
|
376
|
612 uintptr_t len;
|
118
|
613 ngx_http_variable_value_t *value;
|
|
614
|
|
615 value = ngx_http_get_indexed_variable(r, data);
|
|
616
|
122
|
617 if (value == NULL || value->not_found) {
|
118
|
618 return 1;
|
|
619 }
|
|
620
|
376
|
621 len = ngx_http_log_escape(NULL, value->data, value->len);
|
|
622
|
|
623 value->escape = len ? 1 : 0;
|
|
624
|
|
625 return value->len + len * 3;
|
118
|
626 }
|
|
627
|
|
628
|
|
629 static u_char *
|
|
630 ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
631 {
|
|
632 ngx_http_variable_value_t *value;
|
|
633
|
|
634 value = ngx_http_get_indexed_variable(r, op->data);
|
|
635
|
122
|
636 if (value == NULL || value->not_found) {
|
118
|
637 *buf = '-';
|
|
638 return buf + 1;
|
|
639 }
|
|
640
|
376
|
641 if (value->escape == 0) {
|
|
642 return ngx_cpymem(buf, value->data, value->len);
|
|
643
|
|
644 } else {
|
|
645 return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
|
|
646 }
|
|
647 }
|
|
648
|
|
649
|
|
650 static uintptr_t
|
|
651 ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
|
|
652 {
|
|
653 ngx_uint_t i, n;
|
|
654 static u_char hex[] = "0123456789ABCDEF";
|
|
655
|
|
656 static uint32_t escape[] = {
|
|
657 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
658
|
|
659 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
660 0x00000004, /* 0000 0000 0000 0000 0000 0000 0000 0100 */
|
|
661
|
|
662 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
663 0x10000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */
|
|
664
|
|
665 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
666 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
667
|
|
668 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
669 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
670 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
671 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
672 };
|
|
673
|
|
674
|
|
675 if (dst == NULL) {
|
|
676
|
|
677 /* find the number of the characters to be escaped */
|
|
678
|
|
679 n = 0;
|
|
680
|
|
681 for (i = 0; i < size; i++) {
|
|
682 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
683 n++;
|
|
684 }
|
|
685 src++;
|
|
686 }
|
|
687
|
|
688 return (uintptr_t) n;
|
|
689 }
|
|
690
|
|
691 for (i = 0; i < size; i++) {
|
|
692 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
693 *dst++ = '\\';
|
|
694 *dst++ = 'x';
|
|
695 *dst++ = hex[*src >> 4];
|
|
696 *dst++ = hex[*src & 0xf];
|
|
697 src++;
|
|
698
|
|
699 } else {
|
|
700 *dst++ = *src++;
|
|
701 }
|
|
702 }
|
|
703
|
|
704 return (uintptr_t) dst;
|
118
|
705 }
|
|
706
|
|
707
|
|
708 static void *
|
|
709 ngx_http_log_create_main_conf(ngx_conf_t *cf)
|
|
710 {
|
|
711 ngx_http_log_main_conf_t *conf;
|
|
712
|
122
|
713 ngx_http_log_fmt_t *fmt;
|
118
|
714
|
|
715 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
|
|
716 if (conf == NULL) {
|
|
717 return NGX_CONF_ERROR;
|
|
718 }
|
|
719
|
|
720 if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
|
122
|
721 != NGX_OK)
|
118
|
722 {
|
|
723 return NGX_CONF_ERROR;
|
|
724 }
|
|
725
|
122
|
726 fmt = ngx_array_push(&conf->formats);
|
|
727 if (fmt == NULL) {
|
118
|
728 return NGX_CONF_ERROR;
|
|
729 }
|
|
730
|
122
|
731 fmt->name.len = sizeof("combined") - 1;
|
|
732 fmt->name.data = (u_char *) "combined";
|
118
|
733
|
440
|
734 fmt->flushes = NULL;
|
|
735
|
122
|
736 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
|
|
737 if (fmt->ops == NULL) {
|
118
|
738 return NGX_CONF_ERROR;
|
|
739 }
|
|
740
|
|
741 return conf;
|
|
742 }
|
|
743
|
|
744
|
|
745 static void *
|
|
746 ngx_http_log_create_loc_conf(ngx_conf_t *cf)
|
|
747 {
|
|
748 ngx_http_log_loc_conf_t *conf;
|
|
749
|
|
750 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
|
|
751 if (conf == NULL) {
|
|
752 return NGX_CONF_ERROR;
|
|
753 }
|
|
754
|
384
|
755 conf->open_file_cache = NGX_CONF_UNSET_PTR;
|
|
756
|
118
|
757 return conf;
|
|
758 }
|
|
759
|
|
760
|
|
761 static char *
|
|
762 ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
763 {
|
|
764 ngx_http_log_loc_conf_t *prev = parent;
|
|
765 ngx_http_log_loc_conf_t *conf = child;
|
|
766
|
|
767 ngx_http_log_t *log;
|
|
768 ngx_http_log_fmt_t *fmt;
|
|
769 ngx_http_log_main_conf_t *lmcf;
|
|
770
|
384
|
771 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
|
|
772
|
|
773 conf->open_file_cache = prev->open_file_cache;
|
|
774 conf->open_file_cache_valid = prev->open_file_cache_valid;
|
|
775 conf->open_file_cache_min_uses = prev->open_file_cache_min_uses;
|
|
776
|
|
777 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
|
|
778 conf->open_file_cache = NULL;
|
|
779 }
|
|
780 }
|
|
781
|
278
|
782 if (conf->logs || conf->off) {
|
|
783 return NGX_CONF_OK;
|
|
784 }
|
118
|
785
|
386
|
786 conf->logs = prev->logs;
|
|
787 conf->off = prev->off;
|
118
|
788
|
278
|
789 if (conf->logs || conf->off) {
|
|
790 return NGX_CONF_OK;
|
|
791 }
|
118
|
792
|
278
|
793 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
794 if (conf->logs == NULL) {
|
|
795 return NGX_CONF_ERROR;
|
|
796 }
|
118
|
797
|
278
|
798 log = ngx_array_push(conf->logs);
|
|
799 if (log == NULL) {
|
|
800 return NGX_CONF_ERROR;
|
|
801 }
|
118
|
802
|
278
|
803 log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);
|
|
804 if (log->file == NULL) {
|
|
805 return NGX_CONF_ERROR;
|
|
806 }
|
118
|
807
|
386
|
808 log->script = NULL;
|
278
|
809 log->disk_full_time = 0;
|
|
810 log->error_log_time = 0;
|
118
|
811
|
278
|
812 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
813 fmt = lmcf->formats.elts;
|
|
814
|
|
815 /* the default "combined" format */
|
440
|
816 log->format = &fmt[0];
|
278
|
817 lmcf->combined_used = 1;
|
118
|
818
|
|
819 return NGX_CONF_OK;
|
|
820 }
|
|
821
|
|
822
|
|
823 static char *
|
|
824 ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
825 {
|
|
826 ngx_http_log_loc_conf_t *llcf = conf;
|
|
827
|
384
|
828 ssize_t buf;
|
|
829 ngx_uint_t i, n;
|
|
830 ngx_str_t *value, name;
|
|
831 ngx_http_log_t *log;
|
|
832 ngx_http_log_fmt_t *fmt;
|
|
833 ngx_http_log_main_conf_t *lmcf;
|
|
834 ngx_http_script_compile_t sc;
|
118
|
835
|
|
836 value = cf->args->elts;
|
|
837
|
|
838 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
839 llcf->off = 1;
|
|
840 return NGX_CONF_OK;
|
|
841 }
|
|
842
|
|
843 if (llcf->logs == NULL) {
|
|
844 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
845 if (llcf->logs == NULL) {
|
|
846 return NGX_CONF_ERROR;
|
|
847 }
|
|
848 }
|
|
849
|
|
850 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
851
|
|
852 log = ngx_array_push(llcf->logs);
|
|
853 if (log == NULL) {
|
|
854 return NGX_CONF_ERROR;
|
|
855 }
|
|
856
|
384
|
857 ngx_memzero(log, sizeof(ngx_http_log_t));
|
|
858
|
|
859 n = ngx_http_script_variables_count(&value[1]);
|
|
860
|
|
861 if (n == 0) {
|
|
862 log->file = ngx_conf_open_file(cf->cycle, &value[1]);
|
|
863 if (log->file == NULL) {
|
|
864 return NGX_CONF_ERROR;
|
|
865 }
|
|
866
|
|
867 } else {
|
493
|
868 if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {
|
384
|
869 return NGX_CONF_ERROR;
|
|
870 }
|
|
871
|
|
872 log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));
|
|
873 if (log->script == NULL) {
|
|
874 return NGX_CONF_ERROR;
|
|
875 }
|
|
876
|
|
877 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
878
|
|
879 sc.cf = cf;
|
|
880 sc.source = &value[1];
|
|
881 sc.lengths = &log->script->lengths;
|
|
882 sc.values = &log->script->values;
|
|
883 sc.variables = n;
|
|
884 sc.complete_lengths = 1;
|
|
885 sc.complete_values = 1;
|
|
886
|
|
887 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
888 return NGX_CONF_ERROR;
|
|
889 }
|
118
|
890 }
|
|
891
|
120
|
892 if (cf->args->nelts >= 3) {
|
118
|
893 name = value[2];
|
130
|
894
|
|
895 if (ngx_strcmp(name.data, "combined") == 0) {
|
|
896 lmcf->combined_used = 1;
|
|
897 }
|
|
898
|
118
|
899 } else {
|
|
900 name.len = sizeof("combined") - 1;
|
|
901 name.data = (u_char *) "combined";
|
122
|
902 lmcf->combined_used = 1;
|
118
|
903 }
|
|
904
|
|
905 fmt = lmcf->formats.elts;
|
|
906 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
907 if (fmt[i].name.len == name.len
|
|
908 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
|
|
909 {
|
440
|
910 log->format = &fmt[i];
|
120
|
911 goto buffer;
|
118
|
912 }
|
|
913 }
|
|
914
|
|
915 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
916 "unknown log format \"%V\"", &name);
|
120
|
917 return NGX_CONF_ERROR;
|
118
|
918
|
120
|
919 buffer:
|
|
920
|
|
921 if (cf->args->nelts == 4) {
|
|
922 if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {
|
|
923 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
924 "invalid parameter \"%V\"", &value[3]);
|
|
925 return NGX_CONF_ERROR;
|
|
926 }
|
|
927
|
384
|
928 if (log->script) {
|
|
929 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
930 "buffered logs can not have variables in name");
|
|
931 return NGX_CONF_ERROR;
|
|
932 }
|
|
933
|
120
|
934 name.len = value[3].len - 7;
|
|
935 name.data = value[3].data + 7;
|
|
936
|
|
937 buf = ngx_parse_size(&name);
|
|
938
|
|
939 if (buf == NGX_ERROR) {
|
|
940 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
941 "invalid parameter \"%V\"", &value[3]);
|
|
942 return NGX_CONF_ERROR;
|
|
943 }
|
|
944
|
|
945 if (log->file->buffer && log->file->last - log->file->pos != buf) {
|
|
946 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
947 "access_log \"%V\" already defined "
|
|
948 "with different buffer size", &value[1]);
|
|
949 return NGX_CONF_ERROR;
|
|
950 }
|
|
951
|
|
952 log->file->buffer = ngx_palloc(cf->pool, buf);
|
|
953 if (log->file->buffer == NULL) {
|
|
954 return NGX_CONF_ERROR;
|
|
955 }
|
|
956
|
|
957 log->file->pos = log->file->buffer;
|
|
958 log->file->last = log->file->buffer + buf;
|
|
959 }
|
|
960
|
|
961 return NGX_CONF_OK;
|
118
|
962 }
|
|
963
|
|
964
|
|
965 static char *
|
|
966 ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
967 {
|
|
968 ngx_http_log_main_conf_t *lmcf = conf;
|
|
969
|
122
|
970 ngx_str_t *value;
|
|
971 ngx_uint_t i;
|
|
972 ngx_http_log_fmt_t *fmt;
|
118
|
973
|
|
974 value = cf->args->elts;
|
|
975
|
|
976 fmt = lmcf->formats.elts;
|
122
|
977 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
978 if (fmt[i].name.len == value[1].len
|
|
979 && ngx_strcmp(fmt[i].name.data, value[1].data) == 0)
|
118
|
980 {
|
380
|
981 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
982 "duplicate \"log_format\" name \"%V\"",
|
|
983 &value[1]);
|
|
984 return NGX_CONF_ERROR;
|
118
|
985 }
|
|
986 }
|
|
987
|
|
988 fmt = ngx_array_push(&lmcf->formats);
|
|
989 if (fmt == NULL) {
|
|
990 return NGX_CONF_ERROR;
|
|
991 }
|
|
992
|
|
993 fmt->name = value[1];
|
|
994
|
440
|
995 fmt->flushes = ngx_array_create(cf->pool, 4, sizeof(ngx_int_t));
|
|
996 if (fmt->flushes == NULL) {
|
|
997 return NGX_CONF_ERROR;
|
|
998 }
|
|
999
|
122
|
1000 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
|
118
|
1001 if (fmt->ops == NULL) {
|
|
1002 return NGX_CONF_ERROR;
|
|
1003 }
|
|
1004
|
440
|
1005 return ngx_http_log_compile_format(cf, fmt->flushes, fmt->ops, cf->args, 2);
|
122
|
1006 }
|
|
1007
|
118
|
1008
|
122
|
1009 static char *
|
440
|
1010 ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,
|
|
1011 ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s)
|
122
|
1012 {
|
260
|
1013 u_char *data, *p, ch;
|
|
1014 size_t i, len;
|
|
1015 ngx_str_t *value, var;
|
440
|
1016 ngx_int_t *flush;
|
260
|
1017 ngx_uint_t bracket;
|
|
1018 ngx_http_log_op_t *op;
|
|
1019 ngx_http_log_var_t *v;
|
122
|
1020
|
|
1021 value = args->elts;
|
|
1022
|
|
1023 for ( /* void */ ; s < args->nelts; s++) {
|
118
|
1024
|
260
|
1025 for (i = 0; i < value[s].len; i++) {
|
|
1026 if (value[s].data[i] != '%') {
|
|
1027 continue;
|
|
1028 }
|
|
1029
|
|
1030 ch = value[s].data[i + 1];
|
|
1031
|
|
1032 if ((ch >= 'A' && ch <= 'Z')
|
|
1033 || (ch >= 'a' && ch <= 'z')
|
|
1034 || ch == '{')
|
|
1035 {
|
|
1036 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1037 "the parameters in the \"%%name\" form are not supported, "
|
|
1038 "use the \"$variable\" instead");
|
|
1039
|
|
1040 return NGX_CONF_ERROR;
|
|
1041 }
|
|
1042 }
|
|
1043
|
118
|
1044 i = 0;
|
|
1045
|
|
1046 while (i < value[s].len) {
|
|
1047
|
122
|
1048 op = ngx_array_push(ops);
|
118
|
1049 if (op == NULL) {
|
|
1050 return NGX_CONF_ERROR;
|
|
1051 }
|
|
1052
|
|
1053 data = &value[s].data[i];
|
|
1054
|
260
|
1055 if (value[s].data[i] == '$') {
|
118
|
1056
|
|
1057 if (++i == value[s].len) {
|
|
1058 goto invalid;
|
|
1059 }
|
|
1060
|
|
1061 if (value[s].data[i] == '{') {
|
|
1062 bracket = 1;
|
|
1063
|
|
1064 if (++i == value[s].len) {
|
|
1065 goto invalid;
|
|
1066 }
|
|
1067
|
|
1068 var.data = &value[s].data[i];
|
|
1069
|
|
1070 } else {
|
|
1071 bracket = 0;
|
|
1072 var.data = &value[s].data[i];
|
|
1073 }
|
|
1074
|
|
1075 for (var.len = 0; i < value[s].len; i++, var.len++) {
|
|
1076 ch = value[s].data[i];
|
|
1077
|
|
1078 if (ch == '}' && bracket) {
|
|
1079 i++;
|
|
1080 bracket = 0;
|
|
1081 break;
|
|
1082 }
|
|
1083
|
|
1084 if ((ch >= 'A' && ch <= 'Z')
|
|
1085 || (ch >= 'a' && ch <= 'z')
|
|
1086 || (ch >= '0' && ch <= '9')
|
|
1087 || ch == '_')
|
|
1088 {
|
|
1089 continue;
|
|
1090 }
|
|
1091
|
|
1092 break;
|
|
1093 }
|
|
1094
|
|
1095 if (bracket) {
|
|
1096 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1097 "the closing bracket in \"%V\" "
|
|
1098 "variable is missing", &var);
|
|
1099 return NGX_CONF_ERROR;
|
|
1100 }
|
|
1101
|
|
1102 if (var.len == 0) {
|
|
1103 goto invalid;
|
|
1104 }
|
|
1105
|
126
|
1106 if (ngx_strncmp(var.data, "apache_bytes_sent", 17) == 0) {
|
|
1107 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1108 "use \"$body_bytes_sent\" instead of "
|
|
1109 "\"$apache_bytes_sent\"");
|
|
1110 }
|
|
1111
|
122
|
1112 for (v = ngx_http_log_vars; v->name.len; v++) {
|
|
1113
|
|
1114 if (v->name.len == var.len
|
|
1115 && ngx_strncmp(v->name.data, var.data, var.len) == 0)
|
|
1116 {
|
|
1117 op->len = v->len;
|
|
1118 op->getlen = NULL;
|
|
1119 op->run = v->run;
|
|
1120 op->data = 0;
|
|
1121
|
|
1122 goto found;
|
|
1123 }
|
|
1124 }
|
|
1125
|
118
|
1126 if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
|
|
1127 return NGX_CONF_ERROR;
|
|
1128 }
|
|
1129
|
440
|
1130 if (flushes) {
|
|
1131
|
|
1132 flush = ngx_array_push(flushes);
|
|
1133 if (flush == NULL) {
|
|
1134 return NGX_CONF_ERROR;
|
|
1135 }
|
|
1136
|
|
1137 *flush = op->data; /* variable index */
|
|
1138 }
|
|
1139
|
122
|
1140 found:
|
|
1141
|
|
1142 continue;
|
|
1143 }
|
|
1144
|
|
1145 i++;
|
118
|
1146
|
260
|
1147 while (i < value[s].len && value[s].data[i] != '$') {
|
122
|
1148 i++;
|
|
1149 }
|
118
|
1150
|
122
|
1151 len = &value[s].data[i] - data;
|
118
|
1152
|
122
|
1153 if (len) {
|
118
|
1154
|
122
|
1155 op->len = len;
|
|
1156 op->getlen = NULL;
|
118
|
1157
|
122
|
1158 if (len <= sizeof(uintptr_t)) {
|
|
1159 op->run = ngx_http_log_copy_short;
|
|
1160 op->data = 0;
|
118
|
1161
|
122
|
1162 while (len--) {
|
|
1163 op->data <<= 8;
|
|
1164 op->data |= data[len];
|
|
1165 }
|
|
1166
|
|
1167 } else {
|
|
1168 op->run = ngx_http_log_copy_long;
|
118
|
1169
|
382
|
1170 p = ngx_pnalloc(cf->pool, len);
|
122
|
1171 if (p == NULL) {
|
|
1172 return NGX_CONF_ERROR;
|
|
1173 }
|
118
|
1174
|
122
|
1175 ngx_memcpy(p, data, len);
|
|
1176 op->data = (uintptr_t) p;
|
118
|
1177 }
|
|
1178 }
|
|
1179 }
|
|
1180 }
|
|
1181
|
|
1182 return NGX_CONF_OK;
|
|
1183
|
|
1184 invalid:
|
|
1185
|
|
1186 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
|
|
1187
|
|
1188 return NGX_CONF_ERROR;
|
|
1189 }
|
|
1190
|
|
1191
|
384
|
1192 static char *
|
|
1193 ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1194 {
|
|
1195 ngx_http_log_loc_conf_t *llcf = conf;
|
|
1196
|
|
1197 time_t inactive, valid;
|
|
1198 ngx_str_t *value, s;
|
|
1199 ngx_int_t max, min_uses;
|
|
1200 ngx_uint_t i;
|
|
1201
|
|
1202 if (llcf->open_file_cache != NGX_CONF_UNSET_PTR) {
|
|
1203 return "is duplicate";
|
|
1204 }
|
|
1205
|
|
1206 value = cf->args->elts;
|
|
1207
|
|
1208 max = 0;
|
|
1209 inactive = 10;
|
|
1210 valid = 60;
|
|
1211 min_uses = 1;
|
|
1212
|
|
1213 for (i = 1; i < cf->args->nelts; i++) {
|
|
1214
|
|
1215 if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
|
|
1216
|
|
1217 max = ngx_atoi(value[i].data + 4, value[i].len - 4);
|
|
1218 if (max == NGX_ERROR) {
|
|
1219 goto failed;
|
|
1220 }
|
|
1221
|
|
1222 continue;
|
|
1223 }
|
|
1224
|
|
1225 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
|
|
1226
|
|
1227 s.len = value[i].len - 9;
|
|
1228 s.data = value[i].data + 9;
|
|
1229
|
|
1230 inactive = ngx_parse_time(&s, 1);
|
|
1231 if (inactive < 0) {
|
|
1232 goto failed;
|
|
1233 }
|
|
1234
|
|
1235 continue;
|
|
1236 }
|
|
1237
|
|
1238 if (ngx_strncmp(value[i].data, "min_uses=", 9) == 0) {
|
|
1239
|
|
1240 min_uses = ngx_atoi(value[i].data + 9, value[i].len - 9);
|
|
1241 if (min_uses == NGX_ERROR) {
|
|
1242 goto failed;
|
|
1243 }
|
|
1244
|
|
1245 continue;
|
|
1246 }
|
|
1247
|
|
1248 if (ngx_strncmp(value[i].data, "valid=", 6) == 0) {
|
|
1249
|
|
1250 s.len = value[i].len - 6;
|
|
1251 s.data = value[i].data + 6;
|
|
1252
|
|
1253 valid = ngx_parse_time(&s, 1);
|
|
1254 if (valid < 0) {
|
|
1255 goto failed;
|
|
1256 }
|
|
1257
|
|
1258 continue;
|
|
1259 }
|
|
1260
|
|
1261 if (ngx_strcmp(value[i].data, "off") == 0) {
|
|
1262
|
|
1263 llcf->open_file_cache = NULL;
|
|
1264
|
|
1265 continue;
|
|
1266 }
|
|
1267
|
|
1268 failed:
|
|
1269
|
|
1270 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1271 "invalid \"open_log_file_cache\" parameter \"%V\"",
|
|
1272 &value[i]);
|
|
1273 return NGX_CONF_ERROR;
|
|
1274 }
|
|
1275
|
|
1276 if (llcf->open_file_cache == NULL) {
|
|
1277 return NGX_CONF_OK;
|
|
1278 }
|
|
1279
|
|
1280 if (max == 0) {
|
|
1281 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1282 "\"open_log_file_cache\" must have \"max\" parameter");
|
|
1283 return NGX_CONF_ERROR;
|
|
1284 }
|
|
1285
|
|
1286 llcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
|
|
1287
|
|
1288 if (llcf->open_file_cache) {
|
|
1289
|
|
1290 llcf->open_file_cache_valid = valid;
|
|
1291 llcf->open_file_cache_min_uses = min_uses;
|
|
1292
|
|
1293 return NGX_CONF_OK;
|
|
1294 }
|
|
1295
|
|
1296 return NGX_CONF_ERROR;
|
|
1297 }
|
|
1298
|
|
1299
|
118
|
1300 static ngx_int_t
|
122
|
1301 ngx_http_log_init(ngx_conf_t *cf)
|
118
|
1302 {
|
122
|
1303 ngx_str_t *value;
|
|
1304 ngx_array_t a;
|
130
|
1305 ngx_http_handler_pt *h;
|
122
|
1306 ngx_http_log_fmt_t *fmt;
|
|
1307 ngx_http_log_main_conf_t *lmcf;
|
118
|
1308 ngx_http_core_main_conf_t *cmcf;
|
|
1309
|
122
|
1310 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
1311
|
|
1312 if (lmcf->combined_used) {
|
|
1313 if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {
|
|
1314 return NGX_ERROR;
|
|
1315 }
|
|
1316
|
|
1317 value = ngx_array_push(&a);
|
|
1318 if (value == NULL) {
|
|
1319 return NGX_ERROR;
|
|
1320 }
|
|
1321
|
|
1322 *value = ngx_http_combined_fmt;
|
|
1323 fmt = lmcf->formats.elts;
|
|
1324
|
440
|
1325 if (ngx_http_log_compile_format(cf, NULL, fmt->ops, &a, 0)
|
122
|
1326 != NGX_CONF_OK)
|
|
1327 {
|
|
1328 return NGX_ERROR;
|
|
1329 }
|
|
1330 }
|
|
1331
|
|
1332 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
118
|
1333
|
130
|
1334 h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
|
|
1335 if (h == NULL) {
|
|
1336 return NGX_ERROR;
|
|
1337 }
|
|
1338
|
|
1339 *h = ngx_http_log_handler;
|
118
|
1340
|
|
1341 return NGX_OK;
|
|
1342 }
|