blob: a0493dc9fd8fd3410f4a8bf058714183a6ef1717 [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
William Lallemand5f232402012-04-05 18:02:55 +020033#include <proto/frontend.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020034#include <proto/log.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010035#include <proto/stream_interface.h>
Willy Tarreau9b061e32012-04-07 18:03:52 +020036#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
Willy Tarreaubaaee002006-06-26 02:48:02 +020038const char *log_facilities[NB_LOG_FACILITIES] = {
39 "kern", "user", "mail", "daemon",
40 "auth", "syslog", "lpr", "news",
41 "uucp", "cron", "auth2", "ftp",
42 "ntp", "audit", "alert", "cron2",
43 "local0", "local1", "local2", "local3",
44 "local4", "local5", "local6", "local7"
45};
46
47
48const char *log_levels[NB_LOG_LEVELS] = {
49 "emerg", "alert", "crit", "err",
50 "warning", "notice", "info", "debug"
51};
52
Willy Tarreaua2a64e92011-09-07 23:01:56 +020053const char sess_term_cond[10] = "-cCsSPRIDK"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed */
Willy Tarreaub8750a82006-09-03 09:56:00 +020054const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
William Lallemand723b73a2012-02-08 16:37:49 +010056
57/* log_format */
58struct logformat_type {
59 char *name;
60 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010061 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020062 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010063 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
William Lallemand723b73a2012-02-08 16:37:49 +010064};
65
William Lallemandb7ff6a32012-03-02 14:35:21 +010066int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
67
William Lallemand723b73a2012-02-08 16:37:49 +010068/* log_format variable names */
69static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020070 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
71 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
72 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
73 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
74 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
75 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
76 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
77 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
78 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
79 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
80 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
81 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
82 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
83 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
84 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
85 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
86 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes read */
87 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
88 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
89 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
90 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
91 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
92 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL }, /* status code */
93 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
94 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
95 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
96 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
97 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
98 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
99 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
100 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
101 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
102 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
103 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
104 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request */
105 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response */
106 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request list */
107 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response list */
108 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
109 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
110 { "rt", LOG_FMT_COUNTER, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP request counter */
111 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
112 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
113 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100114};
115
Willy Tarreau6580c062012-03-12 15:09:42 +0100116char 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
117char 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 +0100118char 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 +0100119char *log_format = NULL;
120
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100121/* This is a global syslog line, common to all outgoing messages. It begins
122 * with the syslog tag and the date that are updated by update_log_hdr().
123 */
124static char logline[MAX_SYSLOG_LEN];
125
William Lallemand723b73a2012-02-08 16:37:49 +0100126struct logformat_var_args {
127 char *name;
128 int mask;
129};
130
131struct logformat_var_args var_args_list[] = {
132// global
133 { "M", LOG_OPT_MANDATORY },
134 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200135 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100136 { 0, 0 }
137};
138
139/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100140 * callback used to configure addr source retrieval
141 */
142int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
143{
144 curproxy->options2 |= PR_O2_SRC_ADDR;
145
146 return 0;
147}
148
149
150/*
William Lallemand723b73a2012-02-08 16:37:49 +0100151 * Parse args in a logformat_var
152 */
153int parse_logformat_var_args(char *args, struct logformat_node *node)
154{
155 int i = 0;
156 int end = 0;
157 int flags = 0; // 1 = + 2 = -
158 char *sp = NULL; // start pointer
159
160 if (args == NULL)
161 return 1;
162
163 while (1) {
164 if (*args == '\0')
165 end = 1;
166
167 if (*args == '+') {
168 // add flag
169 sp = args + 1;
170 flags = 1;
171 }
172 if (*args == '-') {
173 // delete flag
174 sp = args + 1;
175 flags = 2;
176 }
177
178 if (*args == '\0' || *args == ',') {
179 *args = '\0';
180 for (i = 0; var_args_list[i].name; i++) {
181 if (strcmp(sp, var_args_list[i].name) == 0) {
182 if (flags == 1) {
183 node->options |= var_args_list[i].mask;
184 break;
185 } else if (flags == 2) {
186 node->options &= ~var_args_list[i].mask;
187 break;
188 }
189 }
190 }
191 sp = NULL;
192 if (end)
193 break;
194 }
195 args++;
196 }
197 return 0;
198}
199
200/*
201 * Parse a variable '%varname' or '%{args}varname' in logformat
202 *
203 */
William Lallemand1d705562012-03-12 12:46:41 +0100204int parse_logformat_var(char *str, size_t len, struct proxy *curproxy, struct list *list_format, int *defoptions)
William Lallemand723b73a2012-02-08 16:37:49 +0100205{
206 int i, j;
207 char *arg = NULL; // arguments
208 int fparam = 0;
209 char *name = NULL;
210 struct logformat_node *node = NULL;
211 char varname[255] = { 0 }; // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100212
William Lallemand723b73a2012-02-08 16:37:49 +0100213 for (i = 1; i < len; i++) { // escape first char %
214 if (!arg && str[i] == '{') {
215 arg = str + i;
216 fparam = 1;
217 } else if (arg && str[i] == '}') {
218 char *tmp = arg;
219 arg = calloc(str + i - tmp, 1); // without {}
220 strncpy(arg, tmp + 1, str + i - tmp - 1); // copy without { and }
221 arg[str + i - tmp - 1] = '\0';
222 fparam = 0;
223 } else if (!name && !fparam) {
224 strncpy(varname, str + i, len - i + 1);
225 varname[len - i] = '\0';
226 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
227 if (strcmp(varname, logformat_keywords[j].name) == 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100228 if (!((logformat_keywords[j].mode == PR_MODE_HTTP) && (curproxy->mode == PR_MODE_TCP))) {
229 node = calloc(1, sizeof(struct logformat_node));
230 node->type = logformat_keywords[j].type;
William Lallemand81f51172012-03-12 11:03:47 +0100231 node->options = *defoptions;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100232 node->arg = arg;
233 parse_logformat_var_args(node->arg, node);
William Lallemand1d705562012-03-12 12:46:41 +0100234 if (node->type == LOG_FMT_GLOBAL) {
William Lallemand81f51172012-03-12 11:03:47 +0100235 *defoptions = node->options;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100236 free(node);
237 } else {
William Lallemandb7ff6a32012-03-02 14:35:21 +0100238 if (logformat_keywords[j].config_callback != NULL) {
239 if (logformat_keywords[j].config_callback(node, curproxy) != 0) {
240 return -1;
241 }
242 }
William Lallemand5e19a282012-04-02 16:22:10 +0200243 curproxy->to_log |= logformat_keywords[j].lw;
William Lallemand1d705562012-03-12 12:46:41 +0100244 LIST_ADDQ(list_format, &node->list);
William Lallemandbddd4fd2012-02-27 11:23:10 +0100245 }
246 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100247 } else {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100248 Warning("Warning: No such variable name '%s' in this log mode\n", varname);
249 if (arg)
250 free(arg);
251 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100252 }
William Lallemand723b73a2012-02-08 16:37:49 +0100253 }
254 }
255 Warning("Warning: No such variable name '%s' in logformat\n", varname);
256 if (arg)
257 free(arg);
258 return -1;
259 }
260 }
261 return -1;
262}
263
264/*
265 * push to the logformat linked list
266 *
267 * start: start pointer
268 * end: end text pointer
269 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100270 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100271 *
272 * LOG_TEXT: copy chars from start to end excluding end.
273 *
274*/
William Lallemand1d705562012-03-12 12:46:41 +0100275void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100276{
277 char *str;
278
William Lallemand1d705562012-03-12 12:46:41 +0100279 if (type == LOG_FMT_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100280 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100281 str = calloc(end - start + 1, 1);
282 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100283 str[end - start] = '\0';
284 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100285 node->type = LOG_FMT_TEXT; // type string
286 LIST_ADDQ(list_format, &node->list);
287 } else if (type == LOG_FMT_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100288 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100289 node->type = LOG_FMT_SEPARATOR;
290 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100291 }
292}
293
294/*
295 * Parse the log_format string and fill a linked list.
296 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
297 * You can set arguments using { } : %{many arguments}varname
William Lallemand1d705562012-03-12 12:46:41 +0100298 *
299 * str: the string to parse
300 * curproxy: the proxy affected
301 * list_format: the destination list
302 * capabilities: PR_MODE_TCP_ | PR_MODE_HTTP
William Lallemand723b73a2012-02-08 16:37:49 +0100303 */
William Lallemand1d705562012-03-12 12:46:41 +0100304void parse_logformat_string(char *str, struct proxy *curproxy, struct list *list_format, int capabilities)
William Lallemand723b73a2012-02-08 16:37:49 +0100305{
306 char *sp = str; /* start pointer */
307 int cformat = -1; /* current token format : LOG_TEXT, LOG_SEPARATOR, LOG_VARIABLE */
308 int pformat = -1; /* previous token format */
309 struct logformat_node *tmplf, *back;
William Lallemand81f51172012-03-12 11:03:47 +0100310 int options = 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100311
William Lallemand5e19a282012-04-02 16:22:10 +0200312 curproxy->to_log = LW_INIT;
313
William Lallemand723b73a2012-02-08 16:37:49 +0100314 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100315 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100316 LIST_DEL(&tmplf->list);
317 free(tmplf);
318 }
319
320 while (1) {
321
322 // push the variable only if formats are different, not
323 // within a variable, and not the first iteration
324 if ((cformat != pformat && cformat != -1 && pformat != -1) || *str == '\0') {
325 if (((pformat != LF_STARTVAR && cformat != LF_VAR) &&
326 (pformat != LF_STARTVAR && cformat != LF_STARG) &&
327 (pformat != LF_STARG && cformat != LF_VAR)) || *str == '\0') {
328 if (pformat > LF_VAR) // unfinished string
329 pformat = LF_TEXT;
William Lallemand81f51172012-03-12 11:03:47 +0100330 if (pformat == LF_VAR)
William Lallemand1d705562012-03-12 12:46:41 +0100331 parse_logformat_var(sp, str - sp, curproxy, list_format, &options);
William Lallemand81f51172012-03-12 11:03:47 +0100332 else
William Lallemand1d705562012-03-12 12:46:41 +0100333 add_to_logformat_list(sp, str, pformat, list_format);
William Lallemand723b73a2012-02-08 16:37:49 +0100334 sp = str;
335 if (*str == '\0')
336 break;
337 }
338 }
339
340 if (cformat != -1)
341 str++; // consume the string, except on the first tour
342
343 pformat = cformat;
344
345 if (*str == '\0') {
346 cformat = LF_STARTVAR; // for breaking in all cases
347 continue;
348 }
349
350 if (pformat == LF_STARTVAR) { // after a %
351 if ( (*str >= 'a' && *str <= 'z') || // parse varname
352 (*str >= 'A' && *str <= 'Z') ||
353 (*str >= '0' && *str <= '9')) {
354 cformat = LF_VAR; // varname
355 continue;
356 } else if (*str == '{') {
357 cformat = LF_STARG; // variable arguments
358 continue;
359 } else { // another unexpected token
360 pformat = LF_TEXT; // redefine the format of the previous token to TEXT
361 cformat = LF_TEXT;
362 continue;
363 }
364
365 } else if (pformat == LF_VAR) { // after a varname
366 if ( (*str >= 'a' && *str <= 'z') || // parse varname
367 (*str >= 'A' && *str <= 'Z') ||
368 (*str >= '0' && *str <= '9')) {
369 cformat = LF_VAR;
370 continue;
371 }
372 } else if (pformat == LF_STARG) { // inside variable arguments
373 if (*str == '}') { // end of varname
374 cformat = LF_EDARG;
375 continue;
376 } else { // all tokens are acceptable within { }
377 cformat = LF_STARG;
378 continue;
379 }
380 } else if (pformat == LF_EDARG) { // after arguments
381 if ( (*str >= 'a' && *str <= 'z') || // parse a varname
382 (*str >= 'A' && *str <= 'Z') ||
383 (*str >= '0' && *str <= '9')) {
384 cformat = LF_VAR;
385 continue;
386 } else { // if no varname after arguments, transform in TEXT
387 pformat = LF_TEXT;
388 cformat = LF_TEXT;
389 }
390 }
391
392 // others tokens that don't match previous conditions
393 if (*str == '%') {
394 cformat = LF_STARTVAR;
395 } else if (*str == ' ') {
396 cformat = LF_SEPARATOR;
397 } else {
398 cformat = LF_TEXT;
399 }
400 }
401}
402
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403/*
404 * Displays the message on stderr with the date and pid. Overrides the quiet
405 * mode during startup.
406 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200407void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200408{
409 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200410 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
412 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
413 va_start(argp, fmt);
414
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200415 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200417 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200418 vfprintf(stderr, fmt, argp);
419 fflush(stderr);
420 va_end(argp);
421 }
422}
423
424
425/*
426 * Displays the message on stderr with the date and pid.
427 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200428void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200429{
430 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200431 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200432
433 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
434 va_start(argp, fmt);
435
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200436 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200438 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439 vfprintf(stderr, fmt, argp);
440 fflush(stderr);
441 va_end(argp);
442 }
443}
444
445/*
446 * Displays the message on <out> only if quiet mode is not set.
447 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200448void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449{
450 va_list argp;
451
452 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
453 va_start(argp, fmt);
454 vfprintf(out, fmt, argp);
455 fflush(out);
456 va_end(argp);
457 }
458}
459
460/*
461 * returns log level for <lev> or -1 if not found.
462 */
463int get_log_level(const char *lev)
464{
465 int level;
466
467 level = NB_LOG_LEVELS - 1;
468 while (level >= 0 && strcmp(log_levels[level], lev))
469 level--;
470
471 return level;
472}
473
474
475/*
476 * returns log facility for <fac> or -1 if not found.
477 */
478int get_log_facility(const char *fac)
479{
480 int facility;
481
482 facility = NB_LOG_FACILITIES - 1;
483 while (facility >= 0 && strcmp(log_facilities[facility], fac))
484 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100485
Willy Tarreaubaaee002006-06-26 02:48:02 +0200486 return facility;
487}
488
William Lallemanda1cc3812012-02-08 16:38:44 +0100489/*
490 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200491 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100492 *
493 * Return the adress of the \0 character, or NULL on error
494 */
William Lallemand5f232402012-04-05 18:02:55 +0200495char *lf_text(char *dst, char *src, size_t size, struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +0100496{
William Lallemand51b5dca2012-03-26 17:52:55 +0200497 int n;
William Lallemanda1cc3812012-02-08 16:38:44 +0100498
William Lallemandbddd4fd2012-02-27 11:23:10 +0100499 if (src == NULL || *src == '\0') {
500 if (node->options & LOG_OPT_QUOTE) {
501 if (size > 2) {
502 *(dst++) = '"';
503 *(dst++) = '"';
504 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100505 } else {
506 dst = NULL;
507 return dst;
508 }
509 } else {
510 if (size > 1) {
511 *(dst++) = '-';
512 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100513 } else { // error no space available
514 dst = NULL;
515 return dst;
516 }
517 }
518 } else {
519 if (node->options & LOG_OPT_QUOTE) {
520 if (size-- > 1 ) {
521 *(dst++) = '"';
522 } else {
523 dst = NULL;
524 return NULL;
525 }
William Lallemand51b5dca2012-03-26 17:52:55 +0200526 n = strlcpy2(dst, src, size);
527 size -= n;
528 dst += n;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100529 if (size > 1) {
530 *(dst++) = '"';
531 *dst = '\0';
532 } else {
533 dst = NULL;
534 }
535 } else {
536 dst += strlcpy2(dst, src, size);
537 }
538 }
539 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100540}
541
William Lallemand5f232402012-04-05 18:02:55 +0200542/*
543 * Write a IP adress to the log string
544 * +X option write in hexadecimal notation, most signifant byte on the left
545 */
546char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
547{
548 char *ret = dst;
549 int iret;
550 char pn[INET6_ADDRSTRLEN];
551
552 if (node->options & LOG_OPT_HEXA) {
553 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
554 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
555 if (iret < 0 || iret > size)
556 return NULL;
557 ret += iret;
558 } else {
559 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
560 ret = lf_text(dst, pn, size, node);
561 if (ret == NULL)
562 return NULL;
563 }
564 return ret;
565}
566
567/*
568 * Write a port to the log
569 * +X option write in hexadecimal notation, most signifant byte on the left
570 */
571char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
572{
573 char *ret = dst;
574 int iret;
575
576 if (node->options & LOG_OPT_HEXA) {
577 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
578 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
579 if (iret < 0 || iret > size)
580 return NULL;
581 ret += iret;
582 } else {
583 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
584 if (ret == NULL)
585 return NULL;
586 }
587 return ret;
588}
589
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100590/* Re-generate the syslog header at the beginning of logline once a second and
591 * return the pointer to the first character after the header.
592 */
593static char *update_log_hdr()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200594{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100595 static long tvsec;
596 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200597
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200598 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200599 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200600 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100601 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200602
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200603 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200604 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200605
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100606 hdr_len = snprintf(logline, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100607 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200608 monthname[tm.tm_mon],
609 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100610 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000611 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200612 /* WARNING: depending upon implementations, snprintf may return
613 * either -1 or the number of bytes that would be needed to store
614 * the total message. In both cases, we must adjust it.
615 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100616 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
617 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100619 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200620 }
621
William Lallemand2a4a44f2012-02-06 16:00:33 +0100622 return dataptr;
623}
624
625/*
626 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100627 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100628 */
629void send_log(struct proxy *p, int level, const char *format, ...)
630{
631 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100632 char *dataptr;
633 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100634
635 if (level < 0 || format == NULL)
636 return;
637
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100638 dataptr = update_log_hdr(); /* update log header and skip it */
639 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100640
641 va_start(argp, format);
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100642 data_len += vsnprintf(dataptr, logline + sizeof(logline) - dataptr, format, argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100643 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
644 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100646
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100647 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100648}
649
650/*
651 * This function sends a syslog message.
652 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100653 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100654 */
655void __send_log(struct proxy *p, int level, char *message, size_t size)
656{
657 static int logfdunix = -1; /* syslog to AF_UNIX socket */
658 static int logfdinet = -1; /* syslog to AF_INET socket */
659 static char *dataptr = NULL;
660 int fac_level;
661 struct list *logsrvs = NULL;
662 struct logsrv *tmp = NULL;
663 int nblogger;
664 char *log_ptr;
665
666 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667
668 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200669 if (!LIST_ISEMPTY(&global.logsrvs)) {
670 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200671 }
672 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200673 if (!LIST_ISEMPTY(&p->logsrvs)) {
674 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100676 }
677
William Lallemand0f99e342011-10-12 17:50:54 +0200678 if (!logsrvs)
679 return;
680
William Lallemand2a4a44f2012-02-06 16:00:33 +0100681 message[size - 1] = '\n';
682
Robert Tsai81ae1952007-12-05 10:47:29 +0100683 /* Lazily set up syslog sockets for protocol families of configured
684 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200685 nblogger = 0;
686 list_for_each_entry(tmp, logsrvs, list) {
687 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100688 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200689
David du Colombier11bcb6c2011-03-24 12:23:00 +0100690 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100691 proto = 0;
692 plogfd = &logfdunix;
693 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100694 proto = IPPROTO_UDP;
695 plogfd = &logfdinet;
696 }
697 if (*plogfd >= 0) {
698 /* socket already created. */
699 continue;
700 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100701 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100702 proto)) < 0) {
703 Alert("socket for logger #%d failed: %s (errno=%d)\n",
704 nblogger + 1, strerror(errno), errno);
705 return;
706 }
707 /* we don't want to receive anything on this socket */
708 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
709 /* does nothing under Linux, maybe needed for others */
710 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200711 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200712 }
713
Robert Tsai81ae1952007-12-05 10:47:29 +0100714 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200715 nblogger = 0;
716 list_for_each_entry(tmp, logsrvs, list) {
717 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100718 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100719 &logfdunix : &logfdinet;
720 int sent;
721
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200723 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100725
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 /* For each target, we may have a different facility.
727 * We can also have a different log level for each message.
728 * This induces variations in the message header length.
729 * Since we don't want to recompute it each time, nor copy it every
730 * time, we only change the facility in the pre-computed header,
731 * and we change the pointer to the header accordingly.
732 */
William Lallemand0f99e342011-10-12 17:50:54 +0200733 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100734 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735 do {
736 *log_ptr = '0' + fac_level % 10;
737 fac_level /= 10;
738 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100739 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100741
William Lallemandbfb099c2012-03-19 16:15:12 +0100742 sent = sendto(*plogfd, log_ptr, size + log_ptr - dataptr,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200743 MSG_DONTWAIT | MSG_NOSIGNAL,
744 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100745 if (sent < 0) {
746 Alert("sendto logger #%d failed: %s (errno=%d)\n",
747 nblogger, strerror(errno), errno);
748 }
William Lallemand0f99e342011-10-12 17:50:54 +0200749 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200750 }
751}
752
William Lallemandbddd4fd2012-02-27 11:23:10 +0100753extern fd_set hdr_encode_map[];
754extern fd_set url_encode_map[];
755
Willy Tarreaubaaee002006-06-26 02:48:02 +0200756
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200757const char sess_cookie[8] = "NIDVEOU7"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, Unused, unknown */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100758const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
759 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
760 Set-cookie Updated, unknown, unknown */
761
William Lallemand1d705562012-03-12 12:46:41 +0100762/*
763 * try to write a character if there is enough space, or goto out
764 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100765#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100766 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100767 *(tmplog++) = (x); \
768 } else { \
769 goto out; \
770 } \
771 } while(0)
772
William Lallemand1d705562012-03-12 12:46:41 +0100773
774
775int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776{
Willy Tarreau73de9892006-11-30 11:40:23 +0100777 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100778 struct proxy *be = s->be;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100779 struct http_txn *txn = &s->txn;
William Lallemand1d705562012-03-12 12:46:41 +0100780 int tolog;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100781 char *uri;
782 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200783 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100784 int t_request;
785 int hdr;
786 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100787 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100788 char *ret;
789 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100790 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791
William Lallemandbddd4fd2012-02-27 11:23:10 +0100792 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreau42250582007-04-01 01:30:43 +0200793 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100794
795 if (!(tolog & LW_SVID))
796 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200797 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100798 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200799 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100800 break;
801 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200802 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100803 break;
804 default:
805 svid = "<NOSRV>";
806 break;
807 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808
William Lallemandbddd4fd2012-02-27 11:23:10 +0100809 t_request = -1;
810 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
811 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
812
William Lallemand1d705562012-03-12 12:46:41 +0100813 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200814
William Lallemandbddd4fd2012-02-27 11:23:10 +0100815 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100816 if (LIST_ISEMPTY(list_format))
817 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100818
William Lallemand1d705562012-03-12 12:46:41 +0100819 list_for_each_entry(tmp, list_format, list) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100820 char *src = NULL;
821 switch (tmp->type) {
822
William Lallemand1d705562012-03-12 12:46:41 +0100823 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100824 if (!last_isspace) {
825 LOGCHAR(' ');
826 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100827 }
828 break;
829
William Lallemand1d705562012-03-12 12:46:41 +0100830 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100831 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200832 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100833 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100834 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100835 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100836 last_isspace = 0;
837 break;
838
William Lallemand1d705562012-03-12 12:46:41 +0100839 case LOG_FMT_CLIENTIP: // %Ci
William Lallemand5f232402012-04-05 18:02:55 +0200840 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->prod->addr.from,
841 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100842 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100843 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100844 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100845 last_isspace = 0;
846 break;
847
William Lallemand1d705562012-03-12 12:46:41 +0100848 case LOG_FMT_CLIENTPORT: // %Cp
William Lallemand5f232402012-04-05 18:02:55 +0200849 if (s->req->prod->addr.from.ss_family == AF_UNIX) {
850 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
851 } else {
852 ret = lf_port(tmplog, (struct sockaddr *)&s->req->prod->addr.from,
853 dst + maxsize - tmplog, tmp);
854 }
855 if (ret == NULL)
856 goto out;
857 tmplog = ret;
858 last_isspace = 0;
859 break;
860
861 case LOG_FMT_FRONTENDIP: // %Fi
Willy Tarreau9b061e32012-04-07 18:03:52 +0200862 stream_sock_get_to_addr(s->req->prod);
William Lallemand5f232402012-04-05 18:02:55 +0200863 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->prod->addr.to,
864 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100865 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100866 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100867 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100868 last_isspace = 0;
869 break;
870
William Lallemand5f232402012-04-05 18:02:55 +0200871 case LOG_FMT_FRONTENDPORT: // %Fp
Willy Tarreau9b061e32012-04-07 18:03:52 +0200872 stream_sock_get_to_addr(s->req->prod);
William Lallemand5f232402012-04-05 18:02:55 +0200873 if (s->req->prod->addr.to.ss_family == AF_UNIX) {
874 ret = ltoa_o(s->listener->luid,
875 tmplog, dst + maxsize - tmplog);
876 } else {
877 ret = lf_port(tmplog, (struct sockaddr *)&s->req->prod->addr.to,
878 dst + maxsize - tmplog, tmp);
879 }
880 if (ret == NULL)
881 goto out;
882 tmplog = ret;
883 last_isspace = 0;
884 break;
885
William Lallemand1d705562012-03-12 12:46:41 +0100886 case LOG_FMT_BACKENDIP: // %Bi
William Lallemand5f232402012-04-05 18:02:55 +0200887 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->cons->addr.from,
888 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100889 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +0100890 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100891 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +0100892 last_isspace = 0;
893 break;
894
William Lallemand1d705562012-03-12 12:46:41 +0100895 case LOG_FMT_BACKENDPORT: // %Bp
William Lallemand5f232402012-04-05 18:02:55 +0200896 ret = lf_port(tmplog, (struct sockaddr *)&s->req->cons->addr.from,
897 dst + maxsize - tmplog, tmp);
898 if (ret == NULL)
899 goto out;
900 tmplog = ret;
901 last_isspace = 0;
902 break;
903
904 case LOG_FMT_SERVERIP: // %Si
905 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->cons->addr.to,
906 dst + maxsize - tmplog, tmp);
907 if (ret == NULL)
908 goto out;
909 tmplog = ret;
910 last_isspace = 0;
911 break;
912
913 case LOG_FMT_SERVERPORT: // %Sp
914 ret = lf_port(tmplog, (struct sockaddr *)&s->req->cons->addr.to,
915 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100916 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +0100917 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100918 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +0100919 last_isspace = 0;
920 break;
921
William Lallemand1d705562012-03-12 12:46:41 +0100922 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +0100923 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +0200924 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
925 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100926 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100927 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100928 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100929 last_isspace = 0;
930 break;
931
William Lallemand1d705562012-03-12 12:46:41 +0100932 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +0100933 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +0200934 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100935 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100936 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100937 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100938 last_isspace = 0;
939 break;
940
William Lallemand5f232402012-04-05 18:02:55 +0200941 case LOG_FMT_TS: // %Ts
942 get_gmtime(s->logs.accept_date.tv_sec, &tm);
943 if (tmp->options & LOG_OPT_HEXA) {
944 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
945 if (iret < 0 || iret > dst + maxsize - tmplog)
946 goto out;
947 last_isspace = 0;
948 tmplog += iret;
949 } else {
950 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
951 if (ret == NULL)
952 goto out;
953 tmplog = ret;
954 last_isspace = 0;
955 }
956 break;
957
William Lallemand1d705562012-03-12 12:46:41 +0100958 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +0200959 if (tmp->options & LOG_OPT_HEXA) {
960 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
961 if (iret < 0 || iret > dst + maxsize - tmplog)
962 goto out;
963 last_isspace = 0;
964 tmplog += iret;
965 } else {
966 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100967 goto out;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100968 tmplog = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
969 tmplog, 4);
William Lallemand51b5dca2012-03-26 17:52:55 +0200970 if (!tmplog)
971 goto out;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100972 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +0200973 }
974 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100975
William Lallemand1d705562012-03-12 12:46:41 +0100976 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +0100977 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +0200978 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100979 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100980 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100981 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +0200982 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100983 break;
984
William Lallemand1d705562012-03-12 12:46:41 +0100985 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +0100986 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +0200987 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100988 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100989 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100990 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +0200991 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100992 break;
993
William Lallemand1d705562012-03-12 12:46:41 +0100994 case LOG_FMT_SERVER: // %s
William Lallemandbddd4fd2012-02-27 11:23:10 +0100995 src = (char *)svid;
William Lallemand5f232402012-04-05 18:02:55 +0200996 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100997 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100998 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100999 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001000 last_isspace = 0;
1001 break;
1002
William Lallemand1d705562012-03-12 12:46:41 +01001003 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001004 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001005 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001006 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001007 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001008 last_isspace = 0;
1009 break;
1010
William Lallemand1d705562012-03-12 12:46:41 +01001011 case LOG_FMT_TW: // %Tw
1012 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001013 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001014 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001015 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001016 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001017 last_isspace = 0;
1018 break;
1019
William Lallemand1d705562012-03-12 12:46:41 +01001020 case LOG_FMT_TC: // %Tc
1021 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001022 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001023 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001024 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001025 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001026 last_isspace = 0;
1027 break;
1028
William Lallemand1d705562012-03-12 12:46:41 +01001029 case LOG_FMT_TR: // %Tr
1030 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001031 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001032 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001033 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001034 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001035 last_isspace = 0;
1036 break;
1037
William Lallemand1d705562012-03-12 12:46:41 +01001038 case LOG_FMT_TT: // %Tt
William Lallemandbddd4fd2012-02-27 11:23:10 +01001039 if (!(tolog & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001040 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001041 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001042 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001043 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001044 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001045 last_isspace = 0;
1046 break;
1047
William Lallemand1d705562012-03-12 12:46:41 +01001048 case LOG_FMT_STATUS: // %st
William Lallemand5f232402012-04-05 18:02:55 +02001049 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001050 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001051 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001052 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001053 last_isspace = 0;
1054 break;
1055
William Lallemand1d705562012-03-12 12:46:41 +01001056 case LOG_FMT_BYTES: // %B
William Lallemandbddd4fd2012-02-27 11:23:10 +01001057 if (!(tolog & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001058 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001059 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001060 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001061 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001062 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001063 last_isspace = 0;
1064 break;
1065
William Lallemand1d705562012-03-12 12:46:41 +01001066 case LOG_FMT_CCLIENT: // %cc
William Lallemandbddd4fd2012-02-27 11:23:10 +01001067 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001068 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001069 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001070 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001071 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001072 last_isspace = 0;
1073 break;
1074
William Lallemand1d705562012-03-12 12:46:41 +01001075 case LOG_FMT_CSERVER: // %cs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001076 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001077 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001078 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001079 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001080 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001081 last_isspace = 0;
1082 break;
1083
William Lallemand1d705562012-03-12 12:46:41 +01001084 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +01001085 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1086 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
1087 *tmplog = '\0';
1088 last_isspace = 0;
1089 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001090
William Lallemand1d705562012-03-12 12:46:41 +01001091 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001092 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1093 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001094 LOGCHAR((be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1095 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 +01001096 last_isspace = 0;
1097 break;
1098
William Lallemand1d705562012-03-12 12:46:41 +01001099 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001100 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001101 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001102 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001103 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001104 last_isspace = 0;
1105 break;
1106
William Lallemand1d705562012-03-12 12:46:41 +01001107 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001108 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001109 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001110 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001111 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001112 last_isspace = 0;
1113 break;
1114
William Lallemand1d705562012-03-12 12:46:41 +01001115 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001116 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001117 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001118 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001119 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001120 last_isspace = 0;
1121 break;
1122
William Lallemand1d705562012-03-12 12:46:41 +01001123 case LOG_FMT_SRVCONN: // %sc
1124 ret = ultoa_o(target_srv(&s->target) ?
1125 target_srv(&s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001126 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001127 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001128 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001129 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001130 last_isspace = 0;
1131 break;
1132
William Lallemand1d705562012-03-12 12:46:41 +01001133 case LOG_FMT_RETRIES: // %rq
William Lallemandbddd4fd2012-02-27 11:23:10 +01001134 if (s->flags & SN_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001135 LOGCHAR('+');
1136 ret = ltoa_o((s->req->cons->conn_retries>0) ?
1137 (be->conn_retries - s->req->cons->conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001138 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001139 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001140 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001141 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001142 last_isspace = 0;
1143 break;
1144
William Lallemand1d705562012-03-12 12:46:41 +01001145 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001146 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001147 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001148 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001149 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001150 last_isspace = 0;
1151 break;
1152
William Lallemand1d705562012-03-12 12:46:41 +01001153 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001154 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001155 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001156 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001157 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001158 last_isspace = 0;
1159 break;
1160
William Lallemand1d705562012-03-12 12:46:41 +01001161 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001162 /* request header */
1163 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1164 if (tmp->options & LOG_OPT_QUOTE)
1165 LOGCHAR('"');
1166 LOGCHAR('{');
1167 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1168 if (hdr)
1169 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001170 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001171 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001172 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001173 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001174 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001175 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001176 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001177 }
1178 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001179 if (tmp->options & LOG_OPT_QUOTE)
1180 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001181 last_isspace = 0;
William Lallemand1d705562012-03-12 12:46:41 +01001182 if (tmp->options & LOG_OPT_QUOTE)
1183 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001184 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001185 break;
1186
William Lallemand1d705562012-03-12 12:46:41 +01001187 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001188 /* request header list */
1189 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1190 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1191 if (hdr > 0)
1192 LOGCHAR(' ');
1193 if (tmp->options & LOG_OPT_QUOTE)
1194 LOGCHAR('"');
1195 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001196 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001197 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001198 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001199 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001200 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001201 } else if (!(tmp->options & LOG_OPT_QUOTE))
1202 LOGCHAR('-');
1203 if (tmp->options & LOG_OPT_QUOTE)
1204 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001205 last_isspace = 0;
1206 }
1207 }
1208 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001209
William Lallemand1d705562012-03-12 12:46:41 +01001210
1211 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001212 /* response header */
1213 if (fe->to_log & LW_RSPHDR &&
1214 txn->rsp.cap) {
1215 if (tmp->options & LOG_OPT_QUOTE)
1216 LOGCHAR('"');
1217 LOGCHAR('{');
1218 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1219 if (hdr)
1220 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001221 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001222 ret = encode_string(tmplog, dst + maxsize,
1223 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1224 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001225 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001226 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001227 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001228 }
1229 LOGCHAR('}');
1230 last_isspace = 0;
1231 if (tmp->options & LOG_OPT_QUOTE)
1232 LOGCHAR('"');
1233 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001234 break;
1235
William Lallemand1d705562012-03-12 12:46:41 +01001236 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001237 /* response header list */
1238 if (fe->to_log & LW_RSPHDR && txn->rsp.cap) {
1239 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1240 if (hdr > 0)
1241 LOGCHAR(' ');
1242 if (tmp->options & LOG_OPT_QUOTE)
1243 LOGCHAR('"');
1244 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001245 ret = encode_string(tmplog, dst + maxsize,
1246 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1247 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001248 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001249 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001250 } else if (!(tmp->options & LOG_OPT_QUOTE))
1251 LOGCHAR('-');
1252 if (tmp->options & LOG_OPT_QUOTE)
1253 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001254 last_isspace = 0;
1255 }
1256 }
1257 break;
1258
William Lallemand1d705562012-03-12 12:46:41 +01001259 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001260 /* Request */
1261 if (tmp->options & LOG_OPT_QUOTE)
1262 LOGCHAR('"');
1263 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001264 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001265 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001266 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001267 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001268 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001269 if (tmp->options & LOG_OPT_QUOTE)
1270 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001271 last_isspace = 0;
1272 break;
William Lallemand5f232402012-04-05 18:02:55 +02001273
1274 case LOG_FMT_COUNTER: // %rt
1275 if (tmp->options & LOG_OPT_HEXA) {
1276 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", global.req_count);
1277 if (iret < 0 || iret > dst + maxsize - tmplog)
1278 goto out;
1279 last_isspace = 0;
1280 tmplog += iret;
1281 } else {
1282 ret = ltoa_o(global.req_count, tmplog, dst + maxsize - tmplog);
1283 if (ret == NULL)
1284 goto out;
1285 tmplog = ret;
1286 last_isspace = 0;
1287 }
1288 break;
1289
1290 case LOG_FMT_HOSTNAME: // %H
1291 src = hostname;
1292 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1293 if (ret == NULL)
1294 goto out;
1295 tmplog = ret;
1296 last_isspace = 0;
1297 break;
1298
1299 case LOG_FMT_PID: // %pid
1300 if (tmp->options & LOG_OPT_HEXA) {
1301 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1302 if (iret < 0 || iret > dst + maxsize - tmplog)
1303 goto out;
1304 last_isspace = 0;
1305 tmplog += iret;
1306 } else {
1307 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1308 if (ret == NULL)
1309 goto out;
1310 tmplog = ret;
1311 last_isspace = 0;
1312 }
1313 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001314
1315 case LOG_FMT_UNIQUEID: // %ID
1316 src = s->unique_id;
1317 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
1318 if (ret == NULL)
1319 goto out;
1320 tmplog = ret;
1321 last_isspace = 0;
1322 break;
1323
William Lallemandbddd4fd2012-02-27 11:23:10 +01001324 }
1325 }
1326
1327out:
William Lallemand1d705562012-03-12 12:46:41 +01001328 /* *tmplog is a unused character */
1329 *tmplog = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001330
William Lallemand1d705562012-03-12 12:46:41 +01001331 return tmplog - dst + 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001332
Willy Tarreaubaaee002006-06-26 02:48:02 +02001333}
1334
William Lallemand1d705562012-03-12 12:46:41 +01001335/*
1336 * send a log for the session when we have enough info about it.
1337 * Will not log if the frontend has no log defined.
1338 */
1339void sess_log(struct session *s)
1340{
1341 char *tmplog;
1342 int size, err, level;
1343
1344 /* if we don't want to log normal traffic, return now */
1345 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1346 (s->req->cons->conn_retries != s->be->conn_retries) ||
1347 ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001348
William Lallemand1d705562012-03-12 12:46:41 +01001349 if (!err && (s->fe->options2 & PR_O2_NOLOGNORM))
1350 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001351
William Lallemand1d705562012-03-12 12:46:41 +01001352 if (LIST_ISEMPTY(&s->fe->logsrvs))
1353 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001354
William Lallemand1d705562012-03-12 12:46:41 +01001355 level = LOG_INFO;
1356 if (err && (s->fe->options2 & PR_O2_LOGERRORS))
1357 level = LOG_ERR;
1358
1359 tmplog = update_log_hdr();
1360 size = tmplog - logline;
1361 size += build_logline(s, tmplog, sizeof(logline) - size, &s->fe->logformat);
1362 if (size > 0) {
1363 __send_log(s->fe, level, logline, size);
1364 s->logs.logwait = 0;
1365 }
1366}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001367
Willy Tarreaubaaee002006-06-26 02:48:02 +02001368/*
1369 * Local variables:
1370 * c-indent-level: 8
1371 * c-basic-offset: 8
1372 * End:
1373 */