blob: 4ef51e1e9abfa671272d72c59080a788c07237a0 [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 */
Willy Tarreau6580c062012-03-12 15:09:42 +010088 { "tsc", LOG_TERMSTATE_CK, PR_MODE_HTTP, NULL },/* terminaison state with cookie status */
William Lallemandb7ff6a32012-03-02 14:35:21 +010089 { "ac", LOG_ACTCONN, PR_MODE_TCP, NULL }, /* actconn */
90 { "fc", LOG_FECONN, PR_MODE_TCP, NULL }, /* feconn */
91 { "bc", LOG_BECONN, PR_MODE_TCP, NULL }, /* beconn */
92 { "sc", LOG_SRVCONN, PR_MODE_TCP, NULL }, /* srv_conn */
93 { "rc", LOG_RETRIES, PR_MODE_TCP, NULL }, /* retries */
94 { "sq", LOG_SRVQUEUE, PR_MODE_TCP, NULL }, /* srv_queue */
95 { "bq", LOG_BCKQUEUE, PR_MODE_TCP, NULL }, /* backend_queue */
96 { "hr", LOG_HDRREQUEST, PR_MODE_HTTP, NULL }, /* header request */
97 { "hs", LOG_HDRRESPONS, PR_MODE_HTTP, NULL }, /* header response */
98 { "hrl", LOG_HDRREQUESTLIST, PR_MODE_HTTP, NULL }, /* header request list */
99 { "hsl", LOG_HDRRESPONSLIST, PR_MODE_HTTP, NULL }, /* header response list */
100 { "r", LOG_REQ, PR_MODE_HTTP, NULL }, /* request */
101 { 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100102};
103
Willy Tarreau6580c062012-03-12 15:09:42 +0100104char default_http_log_format[] = "%Ci:%Cp [%t] %f %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %st %B %cc %cs %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
105char clf_http_log_format[] = "%{+Q}o %{-Q}Ci - - [%T] %r %st %B \"\" \"\" %Cp %ms %f %b %s %Tq %Tw %Tc %Tr %Tt %tsc %ac %fc %bc %sc %rc %sq %bq %cc %cs %hrl %hsl";
William Lallemandbddd4fd2012-02-27 11:23:10 +0100106char 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 +0100107char *log_format = NULL;
108
109struct logformat_var_args {
110 char *name;
111 int mask;
112};
113
114struct logformat_var_args var_args_list[] = {
115// global
116 { "M", LOG_OPT_MANDATORY },
117 { "Q", LOG_OPT_QUOTE },
118 { 0, 0 }
119};
120
121/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100122 * callback used to configure addr source retrieval
123 */
124int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
125{
126 curproxy->options2 |= PR_O2_SRC_ADDR;
127
128 return 0;
129}
130
131
132/*
William Lallemand723b73a2012-02-08 16:37:49 +0100133 * Parse args in a logformat_var
134 */
135int parse_logformat_var_args(char *args, struct logformat_node *node)
136{
137 int i = 0;
138 int end = 0;
139 int flags = 0; // 1 = + 2 = -
140 char *sp = NULL; // start pointer
141
142 if (args == NULL)
143 return 1;
144
145 while (1) {
146 if (*args == '\0')
147 end = 1;
148
149 if (*args == '+') {
150 // add flag
151 sp = args + 1;
152 flags = 1;
153 }
154 if (*args == '-') {
155 // delete flag
156 sp = args + 1;
157 flags = 2;
158 }
159
160 if (*args == '\0' || *args == ',') {
161 *args = '\0';
162 for (i = 0; var_args_list[i].name; i++) {
163 if (strcmp(sp, var_args_list[i].name) == 0) {
164 if (flags == 1) {
165 node->options |= var_args_list[i].mask;
166 break;
167 } else if (flags == 2) {
168 node->options &= ~var_args_list[i].mask;
169 break;
170 }
171 }
172 }
173 sp = NULL;
174 if (end)
175 break;
176 }
177 args++;
178 }
179 return 0;
180}
181
182/*
183 * Parse a variable '%varname' or '%{args}varname' in logformat
184 *
185 */
William Lallemand81f51172012-03-12 11:03:47 +0100186int parse_logformat_var(char *str, size_t len, struct proxy *curproxy, int *defoptions)
William Lallemand723b73a2012-02-08 16:37:49 +0100187{
188 int i, j;
189 char *arg = NULL; // arguments
190 int fparam = 0;
191 char *name = NULL;
192 struct logformat_node *node = NULL;
193 char varname[255] = { 0 }; // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100194
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;
William Lallemand81f51172012-03-12 11:03:47 +0100213 node->options = *defoptions;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100214 node->arg = arg;
215 parse_logformat_var_args(node->arg, node);
216 if (node->type == LOG_GLOBAL) {
William Lallemand81f51172012-03-12 11:03:47 +0100217 *defoptions = node->options;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100218 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);
William Lallemand723b73a2012-02-08 16:37:49 +0100269 } else if (type == LOG_SEPARATOR) {
270 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
271 node->type = LOG_SEPARATOR;
272 LIST_ADDQ(&curproxy->logformat, &node->list);
273 }
274}
275
276/*
277 * Parse the log_format string and fill a linked list.
278 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
279 * You can set arguments using { } : %{many arguments}varname
280 */
281void parse_logformat_string(char *str, struct proxy *curproxy)
282{
283 char *sp = str; /* start pointer */
284 int cformat = -1; /* current token format : LOG_TEXT, LOG_SEPARATOR, LOG_VARIABLE */
285 int pformat = -1; /* previous token format */
286 struct logformat_node *tmplf, *back;
William Lallemand81f51172012-03-12 11:03:47 +0100287 int options = 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100288
289 /* flush the list first. */
290 list_for_each_entry_safe(tmplf, back, &curproxy->logformat, list) {
291 LIST_DEL(&tmplf->list);
292 free(tmplf);
293 }
294
295 while (1) {
296
297 // push the variable only if formats are different, not
298 // within a variable, and not the first iteration
299 if ((cformat != pformat && cformat != -1 && pformat != -1) || *str == '\0') {
300 if (((pformat != LF_STARTVAR && cformat != LF_VAR) &&
301 (pformat != LF_STARTVAR && cformat != LF_STARG) &&
302 (pformat != LF_STARG && cformat != LF_VAR)) || *str == '\0') {
303 if (pformat > LF_VAR) // unfinished string
304 pformat = LF_TEXT;
William Lallemand81f51172012-03-12 11:03:47 +0100305 if (pformat == LF_VAR)
306 parse_logformat_var(sp, str - sp, curproxy, &options);
307 else
308 add_to_logformat_list(sp, str, pformat, curproxy);
William Lallemand723b73a2012-02-08 16:37:49 +0100309 sp = str;
310 if (*str == '\0')
311 break;
312 }
313 }
314
315 if (cformat != -1)
316 str++; // consume the string, except on the first tour
317
318 pformat = cformat;
319
320 if (*str == '\0') {
321 cformat = LF_STARTVAR; // for breaking in all cases
322 continue;
323 }
324
325 if (pformat == LF_STARTVAR) { // after a %
326 if ( (*str >= 'a' && *str <= 'z') || // parse varname
327 (*str >= 'A' && *str <= 'Z') ||
328 (*str >= '0' && *str <= '9')) {
329 cformat = LF_VAR; // varname
330 continue;
331 } else if (*str == '{') {
332 cformat = LF_STARG; // variable arguments
333 continue;
334 } else { // another unexpected token
335 pformat = LF_TEXT; // redefine the format of the previous token to TEXT
336 cformat = LF_TEXT;
337 continue;
338 }
339
340 } else if (pformat == LF_VAR) { // after a varname
341 if ( (*str >= 'a' && *str <= 'z') || // parse varname
342 (*str >= 'A' && *str <= 'Z') ||
343 (*str >= '0' && *str <= '9')) {
344 cformat = LF_VAR;
345 continue;
346 }
347 } else if (pformat == LF_STARG) { // inside variable arguments
348 if (*str == '}') { // end of varname
349 cformat = LF_EDARG;
350 continue;
351 } else { // all tokens are acceptable within { }
352 cformat = LF_STARG;
353 continue;
354 }
355 } else if (pformat == LF_EDARG) { // after arguments
356 if ( (*str >= 'a' && *str <= 'z') || // parse a varname
357 (*str >= 'A' && *str <= 'Z') ||
358 (*str >= '0' && *str <= '9')) {
359 cformat = LF_VAR;
360 continue;
361 } else { // if no varname after arguments, transform in TEXT
362 pformat = LF_TEXT;
363 cformat = LF_TEXT;
364 }
365 }
366
367 // others tokens that don't match previous conditions
368 if (*str == '%') {
369 cformat = LF_STARTVAR;
370 } else if (*str == ' ') {
371 cformat = LF_SEPARATOR;
372 } else {
373 cformat = LF_TEXT;
374 }
375 }
376}
377
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378/*
379 * Displays the message on stderr with the date and pid. Overrides the quiet
380 * mode during startup.
381 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200382void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200383{
384 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200385 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386
387 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
388 va_start(argp, fmt);
389
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200390 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200391 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200392 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200393 vfprintf(stderr, fmt, argp);
394 fflush(stderr);
395 va_end(argp);
396 }
397}
398
399
400/*
401 * Displays the message on stderr with the date and pid.
402 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200403void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404{
405 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200406 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407
408 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
409 va_start(argp, fmt);
410
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200411 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200413 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414 vfprintf(stderr, fmt, argp);
415 fflush(stderr);
416 va_end(argp);
417 }
418}
419
420/*
421 * Displays the message on <out> only if quiet mode is not set.
422 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200423void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200424{
425 va_list argp;
426
427 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
428 va_start(argp, fmt);
429 vfprintf(out, fmt, argp);
430 fflush(out);
431 va_end(argp);
432 }
433}
434
435/*
436 * returns log level for <lev> or -1 if not found.
437 */
438int get_log_level(const char *lev)
439{
440 int level;
441
442 level = NB_LOG_LEVELS - 1;
443 while (level >= 0 && strcmp(log_levels[level], lev))
444 level--;
445
446 return level;
447}
448
449
450/*
451 * returns log facility for <fac> or -1 if not found.
452 */
453int get_log_facility(const char *fac)
454{
455 int facility;
456
457 facility = NB_LOG_FACILITIES - 1;
458 while (facility >= 0 && strcmp(log_facilities[facility], fac))
459 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100460
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461 return facility;
462}
463
William Lallemanda1cc3812012-02-08 16:38:44 +0100464/*
465 * Write a string in the log string
466 * Take cares of mandatory and quote options
467 *
468 * Return the adress of the \0 character, or NULL on error
469 */
470char *logformat_write_string(char *dst, char *src, size_t size, struct logformat_node *node)
471{
William Lallemandbddd4fd2012-02-27 11:23:10 +0100472 char *orig = dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100473
William Lallemandbddd4fd2012-02-27 11:23:10 +0100474 if (src == NULL || *src == '\0') {
475 if (node->options & LOG_OPT_QUOTE) {
476 if (size > 2) {
477 *(dst++) = '"';
478 *(dst++) = '"';
479 *dst = '\0';
480 node->options |= LOG_OPT_WRITTEN;
481 } else {
482 dst = NULL;
483 return dst;
484 }
485 } else {
486 if (size > 1) {
487 *(dst++) = '-';
488 *dst = '\0';
489 node->options |= LOG_OPT_WRITTEN;
490 } else { // error no space available
491 dst = NULL;
492 return dst;
493 }
494 }
495 } else {
496 if (node->options & LOG_OPT_QUOTE) {
497 if (size-- > 1 ) {
498 *(dst++) = '"';
499 } else {
500 dst = NULL;
501 return NULL;
502 }
503 dst += strlcpy2(dst, src, size);
504 size -= orig - dst + 1;
505 if (size > 1) {
506 *(dst++) = '"';
507 *dst = '\0';
508 } else {
509 dst = NULL;
510 }
511 } else {
512 dst += strlcpy2(dst, src, size);
513 }
514 }
515 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100516}
517
William Lallemand2a4a44f2012-02-06 16:00:33 +0100518/* generate the syslog header once a second */
519char *hdr_log(char *dst)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520{
William Lallemand2a4a44f2012-02-06 16:00:33 +0100521 int hdr_len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522 static long tvsec = -1; /* to force the string to be initialized */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523 static char *dataptr = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200525 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200527 struct tm tm;
528
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200529 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200530 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531
William Lallemand2a4a44f2012-02-06 16:00:33 +0100532 hdr_len = snprintf(dst, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100533 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200534 monthname[tm.tm_mon],
535 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100536 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000537 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538 /* WARNING: depending upon implementations, snprintf may return
539 * either -1 or the number of bytes that would be needed to store
540 * the total message. In both cases, we must adjust it.
541 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100542 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
543 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544
William Lallemand2a4a44f2012-02-06 16:00:33 +0100545 dataptr = dst + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546 }
547
William Lallemand2a4a44f2012-02-06 16:00:33 +0100548 return dataptr;
549}
550
551/*
552 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100553 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100554 */
555void send_log(struct proxy *p, int level, const char *format, ...)
556{
557 va_list argp;
558 static char logmsg[MAX_SYSLOG_LEN];
559 static char *dataptr = NULL;
560 int data_len = 0;
561
562 if (level < 0 || format == NULL)
563 return;
564
565 dataptr = hdr_log(logmsg); /* create header */
566 data_len = dataptr - logmsg;
567
568 va_start(argp, format);
569 data_len += vsnprintf(dataptr, MAX_SYSLOG_LEN, format, argp);
570 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
571 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100573
574 __send_log(p, level, logmsg, data_len);
575}
576
577/*
578 * This function sends a syslog message.
579 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100580 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100581 */
582void __send_log(struct proxy *p, int level, char *message, size_t size)
583{
584 static int logfdunix = -1; /* syslog to AF_UNIX socket */
585 static int logfdinet = -1; /* syslog to AF_INET socket */
586 static char *dataptr = NULL;
587 int fac_level;
588 struct list *logsrvs = NULL;
589 struct logsrv *tmp = NULL;
590 int nblogger;
591 char *log_ptr;
592
593 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200594
595 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200596 if (!LIST_ISEMPTY(&global.logsrvs)) {
597 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598 }
599 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200600 if (!LIST_ISEMPTY(&p->logsrvs)) {
601 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200602 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100603 }
604
William Lallemand0f99e342011-10-12 17:50:54 +0200605 if (!logsrvs)
606 return;
607
William Lallemand2a4a44f2012-02-06 16:00:33 +0100608 message[size - 1] = '\n';
609
Robert Tsai81ae1952007-12-05 10:47:29 +0100610 /* Lazily set up syslog sockets for protocol families of configured
611 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200612 nblogger = 0;
613 list_for_each_entry(tmp, logsrvs, list) {
614 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100615 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200616
David du Colombier11bcb6c2011-03-24 12:23:00 +0100617 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100618 proto = 0;
619 plogfd = &logfdunix;
620 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100621 proto = IPPROTO_UDP;
622 plogfd = &logfdinet;
623 }
624 if (*plogfd >= 0) {
625 /* socket already created. */
626 continue;
627 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100628 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100629 proto)) < 0) {
630 Alert("socket for logger #%d failed: %s (errno=%d)\n",
631 nblogger + 1, strerror(errno), errno);
632 return;
633 }
634 /* we don't want to receive anything on this socket */
635 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
636 /* does nothing under Linux, maybe needed for others */
637 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200638 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639 }
640
Robert Tsai81ae1952007-12-05 10:47:29 +0100641 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200642 nblogger = 0;
643 list_for_each_entry(tmp, logsrvs, list) {
644 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100645 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100646 &logfdunix : &logfdinet;
647 int sent;
648
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200650 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100652
Willy Tarreaubaaee002006-06-26 02:48:02 +0200653 /* For each target, we may have a different facility.
654 * We can also have a different log level for each message.
655 * This induces variations in the message header length.
656 * Since we don't want to recompute it each time, nor copy it every
657 * time, we only change the facility in the pre-computed header,
658 * and we change the pointer to the header accordingly.
659 */
William Lallemand0f99e342011-10-12 17:50:54 +0200660 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100661 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 do {
663 *log_ptr = '0' + fac_level % 10;
664 fac_level /= 10;
665 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100666 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100668
669 sent = sendto(*plogfd, log_ptr, size,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200670 MSG_DONTWAIT | MSG_NOSIGNAL,
671 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100672 if (sent < 0) {
673 Alert("sendto logger #%d failed: %s (errno=%d)\n",
674 nblogger, strerror(errno), errno);
675 }
William Lallemand0f99e342011-10-12 17:50:54 +0200676 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 }
678}
679
William Lallemandbddd4fd2012-02-27 11:23:10 +0100680extern fd_set hdr_encode_map[];
681extern fd_set url_encode_map[];
682
Willy Tarreaubaaee002006-06-26 02:48:02 +0200683
William Lallemandbddd4fd2012-02-27 11:23:10 +0100684const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
685const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
686 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
687 Set-cookie Updated, unknown, unknown */
688
689#define LOGCHAR(x) do { \
690 if (MAX_SYSLOG_LEN - (tmplog - logline) > 1) { \
691 *(tmplog++) = (x); \
692 } else { \
693 goto out; \
694 } \
695 } while(0)
696
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697/*
William Lallemandbddd4fd2012-02-27 11:23:10 +0100698 * send a log for the session when we have enough info about it.
699 * Will not log if the frontend has no log defined.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100701void sess_log(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200702{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100703 char pn[INET6_ADDRSTRLEN];
William Lallemandb7ff6a32012-03-02 14:35:21 +0100704 char sn[INET6_ADDRSTRLEN];
Willy Tarreau73de9892006-11-30 11:40:23 +0100705 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100706 struct proxy *be = s->be;
707 struct proxy *prx_log;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100708 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200709 int tolog, level, err;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100710 char *uri;
711 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200712 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100713 int t_request;
714 int hdr;
715 int last_isspace = 1;
716 static char logline[MAX_SYSLOG_LEN] = { 0 };
717 static char *tmplog;
718 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200720 /* if we don't want to log normal traffic, return now */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100721 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
722 (s->req->cons->conn_retries != be->conn_retries) ||
723 ((s->fe->mode == PR_MODE_HTTP) && txn->status >= 500);
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200724 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
725 return;
726
William Lallemandbddd4fd2012-02-27 11:23:10 +0100727 if (LIST_ISEMPTY(&fe->logsrvs))
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200728 return;
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200729 prx_log = fe;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100730
731 if (addr_to_str(&s->req->prod->addr.from, pn, sizeof(pn)) == AF_UNIX)
732 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
733
William Lallemandb7ff6a32012-03-02 14:35:21 +0100734 if (be->options2 & PR_O2_SRC_ADDR) {
735 if (addr_to_str(&s->req->cons->addr.from, sn, sizeof(sn)) == AF_UNIX)
736 snprintf(sn, sizeof(sn), "unix:%d", s->listener->luid);
737 }
738
William Lallemandbddd4fd2012-02-27 11:23:10 +0100739 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreau42250582007-04-01 01:30:43 +0200740 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100741
742 if (!(tolog & LW_SVID))
743 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200744 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100745 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200746 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100747 break;
748 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200749 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100750 break;
751 default:
752 svid = "<NOSRV>";
753 break;
754 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755
William Lallemandbddd4fd2012-02-27 11:23:10 +0100756 t_request = -1;
757 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
758 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
759
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200760 level = LOG_INFO;
761 if (err && (fe->options2 & PR_O2_LOGERRORS))
762 level = LOG_ERR;
763
William Lallemandbddd4fd2012-02-27 11:23:10 +0100764 /* fill logbuffer */
765
766 tmplog = logline;
767 tmplog = hdr_log(tmplog);
768
769 list_for_each_entry(tmp, &fe->logformat, list) {
770 char *src = NULL;
771 switch (tmp->type) {
772
773 case LOG_SEPARATOR:
774 if (!last_isspace) {
775 LOGCHAR(' ');
776 last_isspace = 1;
777 *tmplog = '\0';
778 }
779 break;
780
781 case LOG_TEXT: // text
782 src = tmp->arg;
783 tmplog += strlcpy2(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline));
784 if (!tmplog)
785 goto out;
786 last_isspace = 0;
787 break;
788
789 case LOG_CLIENTIP: // %Ci
790 src = (s->req->prod->addr.from.ss_family == AF_UNIX) ? "unix" : pn;
791 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
792
793 if (!tmplog)
794 goto out;
795 last_isspace = 0;
796 break;
797
798 case LOG_CLIENTPORT: // %Cp
799 tmplog = ltoa_o((s->req->prod->addr.from.ss_family == AF_UNIX) ? s->listener->luid : get_host_port(&s->req->prod->addr.from),
800 tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
801 if (!tmplog)
802 goto out;
803 last_isspace = 0;
804 break;
805
William Lallemandb7ff6a32012-03-02 14:35:21 +0100806 case LOG_SOURCEIP: // Bi
807 src = (s->req->cons->addr.from.ss_family == AF_UNIX) ? "unix" : sn;
808 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
809
810 if (!tmplog)
811 goto out;
812 last_isspace = 0;
813 break;
814
815 case LOG_SOURCEPORT: // %Bp
816 tmplog = ltoa_o((s->req->cons->addr.from.ss_family == AF_UNIX) ? s->listener->luid : get_host_port(&s->req->cons->addr.from),
817 tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
818 if (!tmplog)
819 goto out;
820 last_isspace = 0;
821 break;
822
William Lallemandbddd4fd2012-02-27 11:23:10 +0100823 case LOG_DATE: // %t
824 get_localtime(s->logs.accept_date.tv_sec, &tm);
825 tmplog = date2str_log(tmplog, &tm, &(s->logs.accept_date), MAX_SYSLOG_LEN - (tmplog - logline));
826 if (!tmplog)
827 goto out;
828 last_isspace = 0;
829 break;
830
831 case LOG_DATEGMT: // %T
832 get_gmtime(s->logs.accept_date.tv_sec, &tm);
833 tmplog = gmt2str_log(tmplog, &tm, MAX_SYSLOG_LEN - (tmplog - logline));
834 if (!tmplog)
835 goto out;
836 last_isspace = 0;
837 break;
838
839 case LOG_MS: // %ms
840 if ((MAX_SYSLOG_LEN - (tmplog - logline)) < 4) {
841 tmplog = NULL;
842 goto out;
843 }
844 tmplog = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
845 tmplog, 4);
846 last_isspace = 0;
847
848 break;
849
850 case LOG_FRONTEND: // %f
851 src = fe->id;
852 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
853 if (!tmplog)
854 goto out;
855 last_isspace = 0 ;
856 break;
857
858 case LOG_BACKEND: // %b
859 src = be->id;
860 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
861 if (!tmplog)
862 goto out;
863 last_isspace = 0 ;
864 break;
865
866 case LOG_SERVER: // %s
867 src = (char *)svid;
868 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
869 if (!tmplog)
870 goto out;
871 last_isspace = 0;
872 break;
873
874 case LOG_TQ: // %Tq
875 tmplog = ltoa_o(t_request, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
876 if (!tmplog)
877 goto out;
878 last_isspace = 0;
879 break;
880
881 case LOG_TW: // %Tw
882 tmplog = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
883 if (!tmplog)
884 goto out;
885 last_isspace = 0;
886 break;
887
888 case LOG_TC: // %Tc
889 tmplog = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
890 if (!tmplog)
891 goto out;
892 last_isspace = 0;
893 break;
894
895 case LOG_TR: // %Tr
896 tmplog = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
897 if (!tmplog)
898 goto out;
899 last_isspace = 0;
900 break;
901
902 case LOG_TT: // %Tt
903 if (!(tolog & LW_BYTES))
904 *(tmplog++) = '+';
905 tmplog = ltoa_o(s->logs.t_close, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
906 if (!tmplog)
907 goto out;
908 last_isspace = 0;
909 break;
910
911 case LOG_STATUS: // %st
912 tmplog = ultoa_o(txn->status, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
913 if (!tmplog)
914 goto out;
915 last_isspace = 0;
916 break;
917
918 case LOG_BYTES: // %B
919 if (!(tolog & LW_BYTES))
920 *(tmplog++) = '+';
921 tmplog = lltoa(s->logs.bytes_out, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
922 if (!tmplog)
923 goto out;
924 last_isspace = 0;
925 break;
926
927 case LOG_CCLIENT: // %cc
928 src = txn->cli_cookie;
929 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
930 last_isspace = 0;
931 break;
932
933 case LOG_CSERVER: // %cs
934 src = txn->srv_cookie;
935 tmplog = logformat_write_string(tmplog, src, MAX_SYSLOG_LEN - (tmplog - logline), tmp);
936 last_isspace = 0;
937 break;
938
939 case LOG_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +0100940 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
941 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
942 *tmplog = '\0';
943 last_isspace = 0;
944 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100945
Willy Tarreau6580c062012-03-12 15:09:42 +0100946 case LOG_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100947 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
948 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +0100949 LOGCHAR((be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
950 LOGCHAR((be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-');
William Lallemandbddd4fd2012-02-27 11:23:10 +0100951 *tmplog = '\0';
952 last_isspace = 0;
953 break;
954
955 case LOG_ACTCONN: // %ac
956 tmplog = ltoa_o(actconn, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
957 if (!tmplog)
958 goto out;
959 last_isspace = 0;
960 break;
961
962 case LOG_FECONN: // %fc
963 tmplog = ltoa_o(fe->feconn, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
964 if (!tmplog)
965 goto out;
966 last_isspace = 0;
967 break;
968
969 case LOG_BECONN: // %bc
970 tmplog = ltoa_o(be->beconn, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
971 if (!tmplog)
972 goto out;
973 last_isspace = 0;
974 break;
975
976 case LOG_SRVCONN: // %sc
977 tmplog = ultoa_o(target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
978 if (!tmplog)
979 goto out;
980 last_isspace = 0;
981 break;
982
983 case LOG_RETRIES: // %rq
984 if (s->flags & SN_REDISP)
985 *(tmplog++) = '+';
986 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));
987 last_isspace = 0;
988 break;
989
990 case LOG_SRVQUEUE: // %sq
991 tmplog = ltoa_o(s->logs.srv_queue_size, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
992 if (!tmplog)
993 goto out;
994 last_isspace = 0;
995 break;
996
997 case LOG_BCKQUEUE: // %bq
998 tmplog = ltoa_o(s->logs.prx_queue_size, tmplog, MAX_SYSLOG_LEN - (tmplog - logline));
999 if (!tmplog)
1000 goto out;
1001 last_isspace = 0;
1002 break;
1003
1004 case LOG_HDRREQUEST: // %hr
1005 /* request header */
1006 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1007 if (tmp->options & LOG_OPT_QUOTE)
1008 LOGCHAR('"');
1009 LOGCHAR('{');
1010 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1011 if (hdr)
1012 LOGCHAR('|');
1013 if (txn->req.cap[hdr] != NULL)
1014 tmplog = encode_string(tmplog, logline + MAX_SYSLOG_LEN,
1015 '#', hdr_encode_map, txn->req.cap[hdr]);
1016 }
1017 LOGCHAR('}');
1018 last_isspace = 0;
1019 }
1020 *tmplog = '\0';
1021 break;
1022
1023 case LOG_HDRREQUESTLIST: // %hrl
1024 /* request header list */
1025 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1026 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1027 if (hdr > 0)
1028 LOGCHAR(' ');
1029 if (tmp->options & LOG_OPT_QUOTE)
1030 LOGCHAR('"');
1031 if (txn->req.cap[hdr] != NULL) {
1032 tmplog = encode_string(tmplog, logline + MAX_SYSLOG_LEN,
1033 '#', hdr_encode_map, txn->req.cap[hdr]);
1034 } else if (!(tmp->options & LOG_OPT_QUOTE))
1035 LOGCHAR('-');
1036 if (tmp->options & LOG_OPT_QUOTE)
1037 LOGCHAR('"');
1038 *tmplog = '\0';
1039 last_isspace = 0;
1040 }
1041 }
1042 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043
William Lallemandbddd4fd2012-02-27 11:23:10 +01001044 case LOG_HDRRESPONS: // %hs
1045 /* response header */
1046 if (fe->to_log & LW_RSPHDR &&
1047 txn->rsp.cap) {
1048 if (tmp->options & LOG_OPT_QUOTE)
1049 LOGCHAR('"');
1050 LOGCHAR('{');
1051 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1052 if (hdr)
1053 LOGCHAR('|');
1054 if (txn->rsp.cap[hdr] != NULL)
1055 tmplog = encode_string(tmplog, logline + MAX_SYSLOG_LEN,
1056 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1057 }
1058 LOGCHAR('}');
1059 last_isspace = 0;
1060 if (tmp->options & LOG_OPT_QUOTE)
1061 LOGCHAR('"');
1062 }
1063 *tmplog = '\0';
1064 break;
1065
1066 case LOG_HDRRESPONSLIST: // %hsl
1067 /* response header list */
1068 if (fe->to_log & LW_RSPHDR && txn->rsp.cap) {
1069 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1070 if (hdr > 0)
1071 LOGCHAR(' ');
1072 if (tmp->options & LOG_OPT_QUOTE)
1073 LOGCHAR('"');
1074 if (txn->rsp.cap[hdr] != NULL) {
1075 tmplog = encode_string(tmplog, logline + MAX_SYSLOG_LEN,
1076 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1077 } else if (!(tmp->options & LOG_OPT_QUOTE))
1078 LOGCHAR('-');
1079 if (tmp->options & LOG_OPT_QUOTE)
1080 LOGCHAR('"');
1081 *tmplog = '\0';
1082 last_isspace = 0;
1083 }
1084 }
1085 break;
1086
1087 case LOG_REQ: // %r
1088 /* Request */
1089 if (tmp->options & LOG_OPT_QUOTE)
1090 LOGCHAR('"');
1091 uri = txn->uri ? txn->uri : "<BADREQ>";
1092 tmplog = encode_string(tmplog, logline + MAX_SYSLOG_LEN,
1093 '#', url_encode_map, uri);
1094 if (tmp->options & LOG_OPT_QUOTE)
1095 LOGCHAR('"');
1096 *tmplog = '\0';
1097 last_isspace = 0;
1098 break;
1099 }
1100 }
1101
1102out:
1103
1104 if (tmplog == NULL) // if previous error
1105 tmplog = logline + MAX_SYSLOG_LEN - 1;
1106
1107 __send_log(prx_log, level, logline, tmplog - logline + 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001108 s->logs.logwait = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001109
Willy Tarreaubaaee002006-06-26 02:48:02 +02001110}
1111
Willy Tarreaubaaee002006-06-26 02:48:02 +02001112
William Lallemandbddd4fd2012-02-27 11:23:10 +01001113
1114
1115
Willy Tarreaubaaee002006-06-26 02:48:02 +02001116/*
1117 * Local variables:
1118 * c-indent-level: 8
1119 * c-basic-offset: 8
1120 * End:
1121 */