blob: e129c2d12f7aaa3895266463c876cf84453a71d2 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreauc8f24f82007-11-30 18:38:35 +010013#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020014#include <stdarg.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <syslog.h>
19#include <time.h>
20#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010021#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022
23#include <sys/time.h>
24
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020026#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020027#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020028#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010031#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020032
33#include <proto/log.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010034#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreaubaaee002006-06-26 02:48:02 +020036const char *log_facilities[NB_LOG_FACILITIES] = {
37 "kern", "user", "mail", "daemon",
38 "auth", "syslog", "lpr", "news",
39 "uucp", "cron", "auth2", "ftp",
40 "ntp", "audit", "alert", "cron2",
41 "local0", "local1", "local2", "local3",
42 "local4", "local5", "local6", "local7"
43};
44
45
46const char *log_levels[NB_LOG_LEVELS] = {
47 "emerg", "alert", "crit", "err",
48 "warning", "notice", "info", "debug"
49};
50
Willy Tarreaua2a64e92011-09-07 23:01:56 +020051const char sess_term_cond[10] = "-cCsSPRIDK"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed */
Willy Tarreaub8750a82006-09-03 09:56:00 +020052const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
William Lallemand723b73a2012-02-08 16:37:49 +010054
55/* log_format */
56struct logformat_type {
57 char *name;
58 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010059 int mode;
William Lallemandb7ff6a32012-03-02 14:35:21 +010060 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
William Lallemand723b73a2012-02-08 16:37:49 +010061};
62
William Lallemandb7ff6a32012-03-02 14:35:21 +010063int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
64
William Lallemand723b73a2012-02-08 16:37:49 +010065/* log_format variable names */
66static const struct logformat_type logformat_keywords[] = {
William Lallemandb7ff6a32012-03-02 14:35:21 +010067 { "o", LOG_GLOBAL, PR_MODE_TCP, NULL }, /* global option */
68 { "Ci", LOG_CLIENTIP, PR_MODE_TCP, NULL }, /* client ip */
69 { "Cp", LOG_CLIENTPORT, PR_MODE_TCP, NULL }, /* client port */
70 { "Bp", LOG_SOURCEPORT, PR_MODE_TCP, prepare_addrsource }, /* backend source port */
71 { "Bi", LOG_SOURCEIP, PR_MODE_TCP, prepare_addrsource }, /* backend source ip */
72 { "t", LOG_DATE, PR_MODE_TCP, NULL }, /* date */
73 { "T", LOG_DATEGMT, PR_MODE_TCP, NULL }, /* date GMT */
74 { "ms", LOG_MS, PR_MODE_TCP, NULL }, /* accept date millisecond */
75 { "f", LOG_FRONTEND, PR_MODE_TCP, NULL }, /* frontend */
76 { "b", LOG_BACKEND, PR_MODE_TCP, NULL }, /* backend */
77 { "s", LOG_SERVER, PR_MODE_TCP, NULL }, /* server */
78 { "B", LOG_BYTES, PR_MODE_TCP, NULL }, /* bytes read */
79 { "Tq", LOG_TQ, PR_MODE_HTTP, NULL }, /* Tq */
80 { "Tw", LOG_TW, PR_MODE_TCP, NULL }, /* Tw */
81 { "Tc", LOG_TC, PR_MODE_TCP, NULL }, /* Tc */
82 { "Tr", LOG_TR, PR_MODE_HTTP, NULL }, /* Tr */
83 { "Tt", LOG_TT, PR_MODE_TCP, NULL }, /* Tt */
84 { "st", LOG_STATUS, PR_MODE_HTTP, NULL }, /* status code */
85 { "cc", LOG_CCLIENT, PR_MODE_HTTP, NULL }, /* client cookie */
86 { "cs", LOG_CSERVER, PR_MODE_HTTP, NULL }, /* server cookie */
87 { "ts", LOG_TERMSTATE, PR_MODE_TCP, NULL },/* terminaison state */
88 { "ac", LOG_ACTCONN, PR_MODE_TCP, NULL }, /* actconn */
89 { "fc", LOG_FECONN, PR_MODE_TCP, NULL }, /* feconn */
90 { "bc", LOG_BECONN, PR_MODE_TCP, NULL }, /* beconn */
91 { "sc", LOG_SRVCONN, PR_MODE_TCP, NULL }, /* srv_conn */
92 { "rc", LOG_RETRIES, PR_MODE_TCP, NULL }, /* retries */
93 { "sq", LOG_SRVQUEUE, PR_MODE_TCP, NULL }, /* srv_queue */
94 { "bq", LOG_BCKQUEUE, PR_MODE_TCP, NULL }, /* backend_queue */
95 { "hr", LOG_HDRREQUEST, PR_MODE_HTTP, NULL }, /* header request */
96 { "hs", LOG_HDRRESPONS, PR_MODE_HTTP, NULL }, /* header response */
97 { "hrl", LOG_HDRREQUESTLIST, PR_MODE_HTTP, NULL }, /* header request list */
98 { "hsl", LOG_HDRRESPONSLIST, PR_MODE_HTTP, NULL }, /* header response list */
99 { "r", LOG_REQ, PR_MODE_HTTP, NULL }, /* request */
100 { 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100101};
102
103char default_http_log_format[] = "%Ci:%Cp [%t] %f %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %st %B %cc %cs %ts %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
104char clf_http_log_format[] = "%{+Q}o %{-Q}Ci - - [%T] %r %st %B \"\" \"\" %Cp %ms %f %b %s %Tq %Tw %Tc %Tr %Tt %ts %ac %fc %bc %sc %rc %sq %bq %cc %cs %hrl %hsl";
William Lallemandbddd4fd2012-02-27 11:23:10 +0100105char default_tcp_log_format[] = "%Ci:%Cp [%t] %f %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq";
William Lallemand723b73a2012-02-08 16:37:49 +0100106char *log_format = NULL;
107
108struct logformat_var_args {
109 char *name;
110 int mask;
111};
112
113struct logformat_var_args var_args_list[] = {
114// global
115 { "M", LOG_OPT_MANDATORY },
116 { "Q", LOG_OPT_QUOTE },
117 { 0, 0 }
118};
119
120/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100121 * callback used to configure addr source retrieval
122 */
123int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
124{
125 curproxy->options2 |= PR_O2_SRC_ADDR;
126
127 return 0;
128}
129
130
131/*
William Lallemand723b73a2012-02-08 16:37:49 +0100132 * Parse args in a logformat_var
133 */
134int parse_logformat_var_args(char *args, struct logformat_node *node)
135{
136 int i = 0;
137 int end = 0;
138 int flags = 0; // 1 = + 2 = -
139 char *sp = NULL; // start pointer
140
141 if (args == NULL)
142 return 1;
143
144 while (1) {
145 if (*args == '\0')
146 end = 1;
147
148 if (*args == '+') {
149 // add flag
150 sp = args + 1;
151 flags = 1;
152 }
153 if (*args == '-') {
154 // delete flag
155 sp = args + 1;
156 flags = 2;
157 }
158
159 if (*args == '\0' || *args == ',') {
160 *args = '\0';
161 for (i = 0; var_args_list[i].name; i++) {
162 if (strcmp(sp, var_args_list[i].name) == 0) {
163 if (flags == 1) {
164 node->options |= var_args_list[i].mask;
165 break;
166 } else if (flags == 2) {
167 node->options &= ~var_args_list[i].mask;
168 break;
169 }
170 }
171 }
172 sp = NULL;
173 if (end)
174 break;
175 }
176 args++;
177 }
178 return 0;
179}
180
181/*
182 * Parse a variable '%varname' or '%{args}varname' in logformat
183 *
184 */
185int parse_logformat_var(char *str, size_t len, struct proxy *curproxy)
186{
187 int i, j;
188 char *arg = NULL; // arguments
189 int fparam = 0;
190 char *name = NULL;
191 struct logformat_node *node = NULL;
192 char varname[255] = { 0 }; // variable name
193 int logformat_options = 0x00000000;
194
William Lallemand723b73a2012-02-08 16:37:49 +0100195 for (i = 1; i < len; i++) { // escape first char %
196 if (!arg && str[i] == '{') {
197 arg = str + i;
198 fparam = 1;
199 } else if (arg && str[i] == '}') {
200 char *tmp = arg;
201 arg = calloc(str + i - tmp, 1); // without {}
202 strncpy(arg, tmp + 1, str + i - tmp - 1); // copy without { and }
203 arg[str + i - tmp - 1] = '\0';
204 fparam = 0;
205 } else if (!name && !fparam) {
206 strncpy(varname, str + i, len - i + 1);
207 varname[len - i] = '\0';
208 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
209 if (strcmp(varname, logformat_keywords[j].name) == 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100210 if (!((logformat_keywords[j].mode == PR_MODE_HTTP) && (curproxy->mode == PR_MODE_TCP))) {
211 node = calloc(1, sizeof(struct logformat_node));
212 node->type = logformat_keywords[j].type;
213 node->options = logformat_options;
214 node->arg = arg;
215 parse_logformat_var_args(node->arg, node);
216 if (node->type == LOG_GLOBAL) {
217 logformat_options = node->options;
218 free(node);
219 } else {
William Lallemandb7ff6a32012-03-02 14:35:21 +0100220 if (logformat_keywords[j].config_callback != NULL) {
221 if (logformat_keywords[j].config_callback(node, curproxy) != 0) {
222 return -1;
223 }
224 }
William Lallemandbddd4fd2012-02-27 11:23:10 +0100225 LIST_ADDQ(&curproxy->logformat, &node->list);
226 }
227 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100228 } else {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100229 Warning("Warning: No such variable name '%s' in this log mode\n", varname);
230 if (arg)
231 free(arg);
232 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100233 }
William Lallemand723b73a2012-02-08 16:37:49 +0100234 }
235 }
236 Warning("Warning: No such variable name '%s' in logformat\n", varname);
237 if (arg)
238 free(arg);
239 return -1;
240 }
241 }
242 return -1;
243}
244
245/*
246 * push to the logformat linked list
247 *
248 * start: start pointer
249 * end: end text pointer
250 * type: string type
251 *
252 * LOG_TEXT: copy chars from start to end excluding end.
253 *
254*/
255void add_to_logformat_list(char *start, char *end, int type, struct proxy *curproxy)
256{
257 char *str;
258
259 if (type == LOG_TEXT) { /* type text */
260 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
261
262 str = calloc(end - start + 1, 1);
263 strncpy(str, start, end - start);
264
265 str[end - start] = '\0';
266 node->arg = str;
267 node->type = LOG_TEXT; // type string
268 LIST_ADDQ(&curproxy->logformat, &node->list);
269 } else if (type == LOG_VARIABLE) { /* type variable */
270 parse_logformat_var(start, end - start, curproxy);
271 } else if (type == LOG_SEPARATOR) {
272 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
273 node->type = LOG_SEPARATOR;
274 LIST_ADDQ(&curproxy->logformat, &node->list);
275 }
276}
277
278/*
279 * Parse the log_format string and fill a linked list.
280 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
281 * You can set arguments using { } : %{many arguments}varname
282 */
283void parse_logformat_string(char *str, struct proxy *curproxy)
284{
285 char *sp = str; /* start pointer */
286 int cformat = -1; /* current token format : LOG_TEXT, LOG_SEPARATOR, LOG_VARIABLE */
287 int pformat = -1; /* previous token format */
288 struct logformat_node *tmplf, *back;
289
290 /* flush the list first. */
291 list_for_each_entry_safe(tmplf, back, &curproxy->logformat, list) {
292 LIST_DEL(&tmplf->list);
293 free(tmplf);
294 }
295
296 while (1) {
297
298 // push the variable only if formats are different, not
299 // within a variable, and not the first iteration
300 if ((cformat != pformat && cformat != -1 && pformat != -1) || *str == '\0') {
301 if (((pformat != LF_STARTVAR && cformat != LF_VAR) &&
302 (pformat != LF_STARTVAR && cformat != LF_STARG) &&
303 (pformat != LF_STARG && cformat != LF_VAR)) || *str == '\0') {
304 if (pformat > LF_VAR) // unfinished string
305 pformat = LF_TEXT;
306 add_to_logformat_list(sp, str, pformat, curproxy);
307 sp = str;
308 if (*str == '\0')
309 break;
310 }
311 }
312
313 if (cformat != -1)
314 str++; // consume the string, except on the first tour
315
316 pformat = cformat;
317
318 if (*str == '\0') {
319 cformat = LF_STARTVAR; // for breaking in all cases
320 continue;
321 }
322
323 if (pformat == LF_STARTVAR) { // after a %
324 if ( (*str >= 'a' && *str <= 'z') || // parse varname
325 (*str >= 'A' && *str <= 'Z') ||
326 (*str >= '0' && *str <= '9')) {
327 cformat = LF_VAR; // varname
328 continue;
329 } else if (*str == '{') {
330 cformat = LF_STARG; // variable arguments
331 continue;
332 } else { // another unexpected token
333 pformat = LF_TEXT; // redefine the format of the previous token to TEXT
334 cformat = LF_TEXT;
335 continue;
336 }
337
338 } else if (pformat == LF_VAR) { // after a varname
339 if ( (*str >= 'a' && *str <= 'z') || // parse varname
340 (*str >= 'A' && *str <= 'Z') ||
341 (*str >= '0' && *str <= '9')) {
342 cformat = LF_VAR;
343 continue;
344 }
345 } else if (pformat == LF_STARG) { // inside variable arguments
346 if (*str == '}') { // end of varname
347 cformat = LF_EDARG;
348 continue;
349 } else { // all tokens are acceptable within { }
350 cformat = LF_STARG;
351 continue;
352 }
353 } else if (pformat == LF_EDARG) { // after arguments
354 if ( (*str >= 'a' && *str <= 'z') || // parse a varname
355 (*str >= 'A' && *str <= 'Z') ||
356 (*str >= '0' && *str <= '9')) {
357 cformat = LF_VAR;
358 continue;
359 } else { // if no varname after arguments, transform in TEXT
360 pformat = LF_TEXT;
361 cformat = LF_TEXT;
362 }
363 }
364
365 // others tokens that don't match previous conditions
366 if (*str == '%') {
367 cformat = LF_STARTVAR;
368 } else if (*str == ' ') {
369 cformat = LF_SEPARATOR;
370 } else {
371 cformat = LF_TEXT;
372 }
373 }
374}
375
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376/*
377 * Displays the message on stderr with the date and pid. Overrides the quiet
378 * mode during startup.
379 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200380void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381{
382 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200383 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200384
385 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
386 va_start(argp, fmt);
387
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200388 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200390 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200391 vfprintf(stderr, fmt, argp);
392 fflush(stderr);
393 va_end(argp);
394 }
395}
396
397
398/*
399 * Displays the message on stderr with the date and pid.
400 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200401void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402{
403 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200404 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405
406 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
407 va_start(argp, fmt);
408
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200409 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200411 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412 vfprintf(stderr, fmt, argp);
413 fflush(stderr);
414 va_end(argp);
415 }
416}
417
418/*
419 * Displays the message on <out> only if quiet mode is not set.
420 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200421void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422{
423 va_list argp;
424
425 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
426 va_start(argp, fmt);
427 vfprintf(out, fmt, argp);
428 fflush(out);
429 va_end(argp);
430 }
431}
432
433/*
434 * returns log level for <lev> or -1 if not found.
435 */
436int get_log_level(const char *lev)
437{
438 int level;
439
440 level = NB_LOG_LEVELS - 1;
441 while (level >= 0 && strcmp(log_levels[level], lev))
442 level--;
443
444 return level;
445}
446
447
448/*
449 * returns log facility for <fac> or -1 if not found.
450 */
451int get_log_facility(const char *fac)
452{
453 int facility;
454
455 facility = NB_LOG_FACILITIES - 1;
456 while (facility >= 0 && strcmp(log_facilities[facility], fac))
457 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100458
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459 return facility;
460}
461
William Lallemanda1cc3812012-02-08 16:38:44 +0100462/*
463 * Write a string in the log string
464 * Take cares of mandatory and quote options
465 *
466 * Return the adress of the \0 character, or NULL on error
467 */
468char *logformat_write_string(char *dst, char *src, size_t size, struct logformat_node *node)
469{
William Lallemandbddd4fd2012-02-27 11:23:10 +0100470 char *orig = dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100471
William Lallemandbddd4fd2012-02-27 11:23:10 +0100472 if (src == NULL || *src == '\0') {
473 if (node->options & LOG_OPT_QUOTE) {
474 if (size > 2) {
475 *(dst++) = '"';
476 *(dst++) = '"';
477 *dst = '\0';
478 node->options |= LOG_OPT_WRITTEN;
479 } else {
480 dst = NULL;
481 return dst;
482 }
483 } else {
484 if (size > 1) {
485 *(dst++) = '-';
486 *dst = '\0';
487 node->options |= LOG_OPT_WRITTEN;
488 } else { // error no space available
489 dst = NULL;
490 return dst;
491 }
492 }
493 } else {
494 if (node->options & LOG_OPT_QUOTE) {
495 if (size-- > 1 ) {
496 *(dst++) = '"';
497 } else {
498 dst = NULL;
499 return NULL;
500 }
501 dst += strlcpy2(dst, src, size);
502 size -= orig - dst + 1;
503 if (size > 1) {
504 *(dst++) = '"';
505 *dst = '\0';
506 } else {
507 dst = NULL;
508 }
509 } else {
510 dst += strlcpy2(dst, src, size);
511 }
512 }
513 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100514}
515
William Lallemand2a4a44f2012-02-06 16:00:33 +0100516/* generate the syslog header once a second */
517char *hdr_log(char *dst)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200518{
William Lallemand2a4a44f2012-02-06 16:00:33 +0100519 int hdr_len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520 static long tvsec = -1; /* to force the string to be initialized */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521 static char *dataptr = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200523 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200525 struct tm tm;
526
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200527 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200528 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529
William Lallemand2a4a44f2012-02-06 16:00:33 +0100530 hdr_len = snprintf(dst, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100531 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200532 monthname[tm.tm_mon],
533 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100534 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000535 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200536 /* WARNING: depending upon implementations, snprintf may return
537 * either -1 or the number of bytes that would be needed to store
538 * the total message. In both cases, we must adjust it.
539 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100540 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
541 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542
William Lallemand2a4a44f2012-02-06 16:00:33 +0100543 dataptr = dst + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544 }
545
William Lallemand2a4a44f2012-02-06 16:00:33 +0100546 return dataptr;
547}
548
549/*
550 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100551 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100552 */
553void send_log(struct proxy *p, int level, const char *format, ...)
554{
555 va_list argp;
556 static char logmsg[MAX_SYSLOG_LEN];
557 static char *dataptr = NULL;
558 int data_len = 0;
559
560 if (level < 0 || format == NULL)
561 return;
562
563 dataptr = hdr_log(logmsg); /* create header */
564 data_len = dataptr - logmsg;
565
566 va_start(argp, format);
567 data_len += vsnprintf(dataptr, MAX_SYSLOG_LEN, format, argp);
568 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
569 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100571
572 __send_log(p, level, logmsg, data_len);
573}
574
575/*
576 * This function sends a syslog message.
577 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100578 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100579 */
580void __send_log(struct proxy *p, int level, char *message, size_t size)
581{
582 static int logfdunix = -1; /* syslog to AF_UNIX socket */
583 static int logfdinet = -1; /* syslog to AF_INET socket */
584 static char *dataptr = NULL;
585 int fac_level;
586 struct list *logsrvs = NULL;
587 struct logsrv *tmp = NULL;
588 int nblogger;
589 char *log_ptr;
590
591 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200592
593 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200594 if (!LIST_ISEMPTY(&global.logsrvs)) {
595 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200596 }
597 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200598 if (!LIST_ISEMPTY(&p->logsrvs)) {
599 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200600 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100601 }
602
William Lallemand0f99e342011-10-12 17:50:54 +0200603 if (!logsrvs)
604 return;
605
William Lallemand2a4a44f2012-02-06 16:00:33 +0100606 message[size - 1] = '\n';
607
Robert Tsai81ae1952007-12-05 10:47:29 +0100608 /* Lazily set up syslog sockets for protocol families of configured
609 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200610 nblogger = 0;
611 list_for_each_entry(tmp, logsrvs, list) {
612 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100613 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200614
David du Colombier11bcb6c2011-03-24 12:23:00 +0100615 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100616 proto = 0;
617 plogfd = &logfdunix;
618 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100619 proto = IPPROTO_UDP;
620 plogfd = &logfdinet;
621 }
622 if (*plogfd >= 0) {
623 /* socket already created. */
624 continue;
625 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100626 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100627 proto)) < 0) {
628 Alert("socket for logger #%d failed: %s (errno=%d)\n",
629 nblogger + 1, strerror(errno), errno);
630 return;
631 }
632 /* we don't want to receive anything on this socket */
633 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
634 /* does nothing under Linux, maybe needed for others */
635 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200636 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637 }
638
Robert Tsai81ae1952007-12-05 10:47:29 +0100639 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200640 nblogger = 0;
641 list_for_each_entry(tmp, logsrvs, list) {
642 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100643 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100644 &logfdunix : &logfdinet;
645 int sent;
646
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200648 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100650
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651 /* For each target, we may have a different facility.
652 * We can also have a different log level for each message.
653 * This induces variations in the message header length.
654 * Since we don't want to recompute it each time, nor copy it every
655 * time, we only change the facility in the pre-computed header,
656 * and we change the pointer to the header accordingly.
657 */
William Lallemand0f99e342011-10-12 17:50:54 +0200658 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100659 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660 do {
661 *log_ptr = '0' + fac_level % 10;
662 fac_level /= 10;
663 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100664 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100666
667 sent = sendto(*plogfd, log_ptr, size,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200668 MSG_DONTWAIT | MSG_NOSIGNAL,
669 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100670 if (sent < 0) {
671 Alert("sendto logger #%d failed: %s (errno=%d)\n",
672 nblogger, strerror(errno), errno);
673 }
William Lallemand0f99e342011-10-12 17:50:54 +0200674 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 }
676}
677
William Lallemandbddd4fd2012-02-27 11:23:10 +0100678extern fd_set hdr_encode_map[];
679extern fd_set url_encode_map[];
680
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681
William Lallemandbddd4fd2012-02-27 11:23:10 +0100682const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
683const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
684 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
685 Set-cookie Updated, unknown, unknown */
686
687#define LOGCHAR(x) do { \
688 if (MAX_SYSLOG_LEN - (tmplog - logline) > 1) { \
689 *(tmplog++) = (x); \
690 } else { \
691 goto out; \
692 } \
693 } while(0)
694
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695/*
William Lallemandbddd4fd2012-02-27 11:23:10 +0100696 * send a log for the session when we have enough info about it.
697 * Will not log if the frontend has no log defined.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200698 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100699void sess_log(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100701 char pn[INET6_ADDRSTRLEN];
William Lallemandb7ff6a32012-03-02 14:35:21 +0100702 char sn[INET6_ADDRSTRLEN];
Willy Tarreau73de9892006-11-30 11:40:23 +0100703 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100704 struct proxy *be = s->be;
705 struct proxy *prx_log;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100706 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200707 int tolog, level, err;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100708 char *uri;
709 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200710 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100711 int t_request;
712 int hdr;
713 int last_isspace = 1;
714 static char logline[MAX_SYSLOG_LEN] = { 0 };
715 static char *tmplog;
716 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200718 /* if we don't want to log normal traffic, return now */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100719 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
720 (s->req->cons->conn_retries != be->conn_retries) ||
721 ((s->fe->mode == PR_MODE_HTTP) && txn->status >= 500);
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200722 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
723 return;
724
William Lallemandbddd4fd2012-02-27 11:23:10 +0100725 if (LIST_ISEMPTY(&fe->logsrvs))
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200726 return;
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200727 prx_log = fe;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100728
729 if (addr_to_str(&s->req->prod->addr.from, pn, sizeof(pn)) == AF_UNIX)
730 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
731
William Lallemandb7ff6a32012-03-02 14:35:21 +0100732 if (be->options2 & PR_O2_SRC_ADDR) {
733 if (addr_to_str(&s->req->cons->addr.from, sn, sizeof(sn)) == AF_UNIX)
734 snprintf(sn, sizeof(sn), "unix:%d", s->listener->luid);
735 }
736
William Lallemandbddd4fd2012-02-27 11:23:10 +0100737 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreau42250582007-04-01 01:30:43 +0200738 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100739
740 if (!(tolog & LW_SVID))
741 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200742 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100743 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200744 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100745 break;
746 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200747 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100748 break;
749 default:
750 svid = "<NOSRV>";
751 break;
752 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753
William Lallemandbddd4fd2012-02-27 11:23:10 +0100754 t_request = -1;
755 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
756 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
757
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200758 level = LOG_INFO;
759 if (err && (fe->options2 & PR_O2_LOGERRORS))
760 level = LOG_ERR;
761
William Lallemandbddd4fd2012-02-27 11:23:10 +0100762 /* fill logbuffer */
763
764 tmplog = logline;
765 tmplog = hdr_log(tmplog);
766
767 list_for_each_entry(tmp, &fe->logformat, list) {
768 char *src = NULL;
769 switch (tmp->type) {
770
771 case LOG_SEPARATOR:
772 if (!last_isspace) {
773 LOGCHAR(' ');
774 last_isspace = 1;
775 *tmplog = '\0';
776 }
777 break;
778
779 case LOG_TEXT: // text
780 src = tmp->arg;
781 tmplog += strlcpy2(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline));
782 if (!tmplog)
783 goto out;
784 last_isspace = 0;
785 break;
786
787 case LOG_CLIENTIP: // %Ci
788 src = (s->req->prod->addr.from.ss_family == AF_UNIX) ? "unix" : pn;
789 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
790
791 if (!tmplog)
792 goto out;
793 last_isspace = 0;
794 break;
795
796 case LOG_CLIENTPORT: // %Cp
797 tmplog = ltoa_o((s->req->prod->addr.from.ss_family == AF_UNIX) ? s->listener->luid : get_host_port(&s->req->prod->addr.from),
798 tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
799 if (!tmplog)
800 goto out;
801 last_isspace = 0;
802 break;
803
William Lallemandb7ff6a32012-03-02 14:35:21 +0100804 case LOG_SOURCEIP: // Bi
805 src = (s->req->cons->addr.from.ss_family == AF_UNIX) ? "unix" : sn;
806 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
807
808 if (!tmplog)
809 goto out;
810 last_isspace = 0;
811 break;
812
813 case LOG_SOURCEPORT: // %Bp
814 tmplog = ltoa_o((s->req->cons->addr.from.ss_family == AF_UNIX) ? s->listener->luid : get_host_port(&s->req->cons->addr.from),
815 tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
816 if (!tmplog)
817 goto out;
818 last_isspace = 0;
819 break;
820
William Lallemandbddd4fd2012-02-27 11:23:10 +0100821 case LOG_DATE: // %t
822 get_localtime(s->logs.accept_date.tv_sec, &tm);
823 tmplog = date2str_log(tmplog, &tm, &(s->logs.accept_date), MAX_SYSLOG_LEN - (tmplog - logline));
824 if (!tmplog)
825 goto out;
826 last_isspace = 0;
827 break;
828
829 case LOG_DATEGMT: // %T
830 get_gmtime(s->logs.accept_date.tv_sec, &tm);
831 tmplog = gmt2str_log(tmplog, &tm, MAX_SYSLOG_LEN - (tmplog - logline));
832 if (!tmplog)
833 goto out;
834 last_isspace = 0;
835 break;
836
837 case LOG_MS: // %ms
838 if ((MAX_SYSLOG_LEN - (tmplog - logline)) < 4) {
839 tmplog = NULL;
840 goto out;
841 }
842 tmplog = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
843 tmplog, 4);
844 last_isspace = 0;
845
846 break;
847
848 case LOG_FRONTEND: // %f
849 src = fe->id;
850 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
851 if (!tmplog)
852 goto out;
853 last_isspace = 0 ;
854 break;
855
856 case LOG_BACKEND: // %b
857 src = be->id;
858 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
859 if (!tmplog)
860 goto out;
861 last_isspace = 0 ;
862 break;
863
864 case LOG_SERVER: // %s
865 src = (char *)svid;
866 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
867 if (!tmplog)
868 goto out;
869 last_isspace = 0;
870 break;
871
872 case LOG_TQ: // %Tq
873 tmplog = ltoa_o(t_request, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
874 if (!tmplog)
875 goto out;
876 last_isspace = 0;
877 break;
878
879 case LOG_TW: // %Tw
880 tmplog = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
881 if (!tmplog)
882 goto out;
883 last_isspace = 0;
884 break;
885
886 case LOG_TC: // %Tc
887 tmplog = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
888 if (!tmplog)
889 goto out;
890 last_isspace = 0;
891 break;
892
893 case LOG_TR: // %Tr
894 tmplog = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
895 if (!tmplog)
896 goto out;
897 last_isspace = 0;
898 break;
899
900 case LOG_TT: // %Tt
901 if (!(tolog & LW_BYTES))
902 *(tmplog++) = '+';
903 tmplog = ltoa_o(s->logs.t_close, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
904 if (!tmplog)
905 goto out;
906 last_isspace = 0;
907 break;
908
909 case LOG_STATUS: // %st
910 tmplog = ultoa_o(txn->status, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
911 if (!tmplog)
912 goto out;
913 last_isspace = 0;
914 break;
915
916 case LOG_BYTES: // %B
917 if (!(tolog & LW_BYTES))
918 *(tmplog++) = '+';
919 tmplog = lltoa(s->logs.bytes_out, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
920 if (!tmplog)
921 goto out;
922 last_isspace = 0;
923 break;
924
925 case LOG_CCLIENT: // %cc
926 src = txn->cli_cookie;
927 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
928 last_isspace = 0;
929 break;
930
931 case LOG_CSERVER: // %cs
932 src = txn->srv_cookie;
933 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
934 last_isspace = 0;
935 break;
936
937 case LOG_TERMSTATE: // %ts
938
939 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
940 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
941 if (fe->mode == PR_MODE_HTTP) {
942 LOGCHAR((be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
943 LOGCHAR((be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-');
944 }
945 *tmplog = '\0';
946 last_isspace = 0;
947 break;
948
949 case LOG_ACTCONN: // %ac
950 tmplog = ltoa_o(actconn, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
951 if (!tmplog)
952 goto out;
953 last_isspace = 0;
954 break;
955
956 case LOG_FECONN: // %fc
957 tmplog = ltoa_o(fe->feconn, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
958 if (!tmplog)
959 goto out;
960 last_isspace = 0;
961 break;
962
963 case LOG_BECONN: // %bc
964 tmplog = ltoa_o(be->beconn, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
965 if (!tmplog)
966 goto out;
967 last_isspace = 0;
968 break;
969
970 case LOG_SRVCONN: // %sc
971 tmplog = ultoa_o(target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
972 if (!tmplog)
973 goto out;
974 last_isspace = 0;
975 break;
976
977 case LOG_RETRIES: // %rq
978 if (s->flags & SN_REDISP)
979 *(tmplog++) = '+';
980 tmplog = ltoa_o((s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
981 last_isspace = 0;
982 break;
983
984 case LOG_SRVQUEUE: // %sq
985 tmplog = ltoa_o(s->logs.srv_queue_size, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
986 if (!tmplog)
987 goto out;
988 last_isspace = 0;
989 break;
990
991 case LOG_BCKQUEUE: // %bq
992 tmplog = ltoa_o(s->logs.prx_queue_size, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
993 if (!tmplog)
994 goto out;
995 last_isspace = 0;
996 break;
997
998 case LOG_HDRREQUEST: // %hr
999 /* request header */
1000 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1001 if (tmp->options & LOG_OPT_QUOTE)
1002 LOGCHAR('"');
1003 LOGCHAR('{');
1004 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1005 if (hdr)
1006 LOGCHAR('|');
1007 if (txn->req.cap[hdr] != NULL)
1008 tmplog = encode_string(tmplog, logline + MAX_SYSLOG_LEN,
1009 '#', hdr_encode_map, txn->req.cap[hdr]);
1010 }
1011 LOGCHAR('}');
1012 last_isspace = 0;
1013 }
1014 *tmplog = '\0';
1015 break;
1016
1017 case LOG_HDRREQUESTLIST: // %hrl
1018 /* request header list */
1019 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1020 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1021 if (hdr > 0)
1022 LOGCHAR(' ');
1023 if (tmp->options & LOG_OPT_QUOTE)
1024 LOGCHAR('"');
1025 if (txn->req.cap[hdr] != NULL) {
1026 tmplog = encode_string(tmplog, logline + MAX_SYSLOG_LEN,
1027 '#', hdr_encode_map, txn->req.cap[hdr]);
1028 } else if (!(tmp->options & LOG_OPT_QUOTE))
1029 LOGCHAR('-');
1030 if (tmp->options & LOG_OPT_QUOTE)
1031 LOGCHAR('"');
1032 *tmplog = '\0';
1033 last_isspace = 0;
1034 }
1035 }
1036 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001037
William Lallemandbddd4fd2012-02-27 11:23:10 +01001038 case LOG_HDRRESPONS: // %hs
1039 /* response header */
1040 if (fe->to_log & LW_RSPHDR &&
1041 txn->rsp.cap) {
1042 if (tmp->options & LOG_OPT_QUOTE)
1043 LOGCHAR('"');
1044 LOGCHAR('{');
1045 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1046 if (hdr)
1047 LOGCHAR('|');
1048 if (txn->rsp.cap[hdr] != NULL)
1049 tmplog = encode_string(tmplog, logline + MAX_SYSLOG_LEN,
1050 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1051 }
1052 LOGCHAR('}');
1053 last_isspace = 0;
1054 if (tmp->options & LOG_OPT_QUOTE)
1055 LOGCHAR('"');
1056 }
1057 *tmplog = '\0';
1058 break;
1059
1060 case LOG_HDRRESPONSLIST: // %hsl
1061 /* response header list */
1062 if (fe->to_log & LW_RSPHDR && txn->rsp.cap) {
1063 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1064 if (hdr > 0)
1065 LOGCHAR(' ');
1066 if (tmp->options & LOG_OPT_QUOTE)
1067 LOGCHAR('"');
1068 if (txn->rsp.cap[hdr] != NULL) {
1069 tmplog = encode_string(tmplog, logline + MAX_SYSLOG_LEN,
1070 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1071 } else if (!(tmp->options & LOG_OPT_QUOTE))
1072 LOGCHAR('-');
1073 if (tmp->options & LOG_OPT_QUOTE)
1074 LOGCHAR('"');
1075 *tmplog = '\0';
1076 last_isspace = 0;
1077 }
1078 }
1079 break;
1080
1081 case LOG_REQ: // %r
1082 /* Request */
1083 if (tmp->options & LOG_OPT_QUOTE)
1084 LOGCHAR('"');
1085 uri = txn->uri ? txn->uri : "<BADREQ>";
1086 tmplog = encode_string(tmplog, logline + MAX_SYSLOG_LEN,
1087 '#', url_encode_map, uri);
1088 if (tmp->options & LOG_OPT_QUOTE)
1089 LOGCHAR('"');
1090 *tmplog = '\0';
1091 last_isspace = 0;
1092 break;
1093 }
1094 }
1095
1096out:
1097
1098 if (tmplog == NULL) // if previous error
1099 tmplog = logline + MAX_SYSLOG_LEN - 1;
1100
1101 __send_log(prx_log, level, logline, tmplog - logline + 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001102 s->logs.logwait = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001103
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104}
1105
Willy Tarreaubaaee002006-06-26 02:48:02 +02001106
William Lallemandbddd4fd2012-02-27 11:23:10 +01001107
1108
1109
Willy Tarreaubaaee002006-06-26 02:48:02 +02001110/*
1111 * Local variables:
1112 * c-indent-level: 8
1113 * c-basic-offset: 8
1114 * End:
1115 */