blob: 79158e07b6cd402e56ebf626f6f436aca8ba27a5 [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 Tarreau773d65f2012-10-12 14:56:11 +020036#ifdef USE_OPENSSL
37#include <proto/ssl_sock.h>
38#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
Willy Tarreaubaaee002006-06-26 02:48:02 +020040const char *log_facilities[NB_LOG_FACILITIES] = {
41 "kern", "user", "mail", "daemon",
42 "auth", "syslog", "lpr", "news",
43 "uucp", "cron", "auth2", "ftp",
44 "ntp", "audit", "alert", "cron2",
45 "local0", "local1", "local2", "local3",
46 "local4", "local5", "local6", "local7"
47};
48
49
50const char *log_levels[NB_LOG_LEVELS] = {
51 "emerg", "alert", "crit", "err",
52 "warning", "notice", "info", "debug"
53};
54
Justin Karnegeseb2c24a2012-05-24 15:28:52 -070055const char sess_term_cond[16] = "-cCsSPRIDKUIIIII"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +020056const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
William Lallemand723b73a2012-02-08 16:37:49 +010058
59/* log_format */
60struct logformat_type {
61 char *name;
62 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010063 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020064 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010065 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
William Lallemand723b73a2012-02-08 16:37:49 +010066};
67
William Lallemandb7ff6a32012-03-02 14:35:21 +010068int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
69
William Lallemand723b73a2012-02-08 16:37:49 +010070/* log_format variable names */
71static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020072 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
73 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
74 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
75 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
76 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
77 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
78 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
79 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
80 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
81 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
82 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Yuxans Yao4e25b012012-10-19 10:36:09 +080083 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +020084 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
85 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
86 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
Willy Tarreau773d65f2012-10-12 14:56:11 +020087 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
William Lallemand5e19a282012-04-02 16:22:10 +020088 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
89 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
90 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes read */
91 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
92 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
93 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
94 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
95 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
96 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL }, /* status code */
97 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
98 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
99 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
100 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
101 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
102 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
103 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
104 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
105 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
106 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
107 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
108 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request */
109 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response */
110 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request list */
111 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response list */
112 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
113 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
114 { "rt", LOG_FMT_COUNTER, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP request counter */
115 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
116 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200117 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
118 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
William Lallemand5e19a282012-04-02 16:22:10 +0200119 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100120};
121
Willy Tarreau773d65f2012-10-12 14:56:11 +0200122char default_http_log_format[] = "%Ci:%Cp [%t] %ft %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
123char clf_http_log_format[] = "%{+Q}o %{-Q}Ci - - [%T] %r %st %B \"\" \"\" %Cp %ms %ft %b %s %Tq %Tw %Tc %Tr %Tt %tsc %ac %fc %bc %sc %rc %sq %bq %cc %cs %hrl %hsl";
124char default_tcp_log_format[] = "%Ci:%Cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq";
William Lallemand723b73a2012-02-08 16:37:49 +0100125char *log_format = NULL;
126
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100127/* This is a global syslog line, common to all outgoing messages. It begins
128 * with the syslog tag and the date that are updated by update_log_hdr().
129 */
130static char logline[MAX_SYSLOG_LEN];
131
William Lallemand723b73a2012-02-08 16:37:49 +0100132struct logformat_var_args {
133 char *name;
134 int mask;
135};
136
137struct logformat_var_args var_args_list[] = {
138// global
139 { "M", LOG_OPT_MANDATORY },
140 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200141 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100142 { 0, 0 }
143};
144
145/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100146 * callback used to configure addr source retrieval
147 */
148int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
149{
150 curproxy->options2 |= PR_O2_SRC_ADDR;
151
152 return 0;
153}
154
155
156/*
William Lallemand723b73a2012-02-08 16:37:49 +0100157 * Parse args in a logformat_var
158 */
159int parse_logformat_var_args(char *args, struct logformat_node *node)
160{
161 int i = 0;
162 int end = 0;
163 int flags = 0; // 1 = + 2 = -
164 char *sp = NULL; // start pointer
165
166 if (args == NULL)
167 return 1;
168
169 while (1) {
170 if (*args == '\0')
171 end = 1;
172
173 if (*args == '+') {
174 // add flag
175 sp = args + 1;
176 flags = 1;
177 }
178 if (*args == '-') {
179 // delete flag
180 sp = args + 1;
181 flags = 2;
182 }
183
184 if (*args == '\0' || *args == ',') {
185 *args = '\0';
186 for (i = 0; var_args_list[i].name; i++) {
187 if (strcmp(sp, var_args_list[i].name) == 0) {
188 if (flags == 1) {
189 node->options |= var_args_list[i].mask;
190 break;
191 } else if (flags == 2) {
192 node->options &= ~var_args_list[i].mask;
193 break;
194 }
195 }
196 }
197 sp = NULL;
198 if (end)
199 break;
200 }
201 args++;
202 }
203 return 0;
204}
205
206/*
207 * Parse a variable '%varname' or '%{args}varname' in logformat
208 *
209 */
William Lallemand1d705562012-03-12 12:46:41 +0100210int parse_logformat_var(char *str, size_t len, struct proxy *curproxy, struct list *list_format, int *defoptions)
William Lallemand723b73a2012-02-08 16:37:49 +0100211{
212 int i, j;
213 char *arg = NULL; // arguments
214 int fparam = 0;
215 char *name = NULL;
216 struct logformat_node *node = NULL;
217 char varname[255] = { 0 }; // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100218
William Lallemand723b73a2012-02-08 16:37:49 +0100219 for (i = 1; i < len; i++) { // escape first char %
220 if (!arg && str[i] == '{') {
221 arg = str + i;
222 fparam = 1;
223 } else if (arg && str[i] == '}') {
224 char *tmp = arg;
225 arg = calloc(str + i - tmp, 1); // without {}
226 strncpy(arg, tmp + 1, str + i - tmp - 1); // copy without { and }
227 arg[str + i - tmp - 1] = '\0';
228 fparam = 0;
229 } else if (!name && !fparam) {
230 strncpy(varname, str + i, len - i + 1);
231 varname[len - i] = '\0';
232 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
233 if (strcmp(varname, logformat_keywords[j].name) == 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100234 if (!((logformat_keywords[j].mode == PR_MODE_HTTP) && (curproxy->mode == PR_MODE_TCP))) {
235 node = calloc(1, sizeof(struct logformat_node));
236 node->type = logformat_keywords[j].type;
William Lallemand81f51172012-03-12 11:03:47 +0100237 node->options = *defoptions;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100238 node->arg = arg;
239 parse_logformat_var_args(node->arg, node);
William Lallemand1d705562012-03-12 12:46:41 +0100240 if (node->type == LOG_FMT_GLOBAL) {
William Lallemand81f51172012-03-12 11:03:47 +0100241 *defoptions = node->options;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100242 free(node);
243 } else {
William Lallemandb7ff6a32012-03-02 14:35:21 +0100244 if (logformat_keywords[j].config_callback != NULL) {
245 if (logformat_keywords[j].config_callback(node, curproxy) != 0) {
246 return -1;
247 }
248 }
William Lallemand5e19a282012-04-02 16:22:10 +0200249 curproxy->to_log |= logformat_keywords[j].lw;
William Lallemand1d705562012-03-12 12:46:41 +0100250 LIST_ADDQ(list_format, &node->list);
William Lallemandbddd4fd2012-02-27 11:23:10 +0100251 }
252 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100253 } else {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100254 Warning("Warning: No such variable name '%s' in this log mode\n", varname);
255 if (arg)
256 free(arg);
257 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100258 }
William Lallemand723b73a2012-02-08 16:37:49 +0100259 }
260 }
261 Warning("Warning: No such variable name '%s' in logformat\n", varname);
262 if (arg)
263 free(arg);
264 return -1;
265 }
266 }
267 return -1;
268}
269
270/*
271 * push to the logformat linked list
272 *
273 * start: start pointer
274 * end: end text pointer
275 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100276 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100277 *
278 * LOG_TEXT: copy chars from start to end excluding end.
279 *
280*/
William Lallemand1d705562012-03-12 12:46:41 +0100281void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100282{
283 char *str;
284
William Lallemand1d705562012-03-12 12:46:41 +0100285 if (type == LOG_FMT_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100286 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100287 str = calloc(end - start + 1, 1);
288 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100289 str[end - start] = '\0';
290 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100291 node->type = LOG_FMT_TEXT; // type string
292 LIST_ADDQ(list_format, &node->list);
293 } else if (type == LOG_FMT_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100294 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100295 node->type = LOG_FMT_SEPARATOR;
296 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100297 }
298}
299
300/*
301 * Parse the log_format string and fill a linked list.
302 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
303 * You can set arguments using { } : %{many arguments}varname
William Lallemand1d705562012-03-12 12:46:41 +0100304 *
305 * str: the string to parse
306 * curproxy: the proxy affected
307 * list_format: the destination list
308 * capabilities: PR_MODE_TCP_ | PR_MODE_HTTP
William Lallemand723b73a2012-02-08 16:37:49 +0100309 */
William Lallemand1d705562012-03-12 12:46:41 +0100310void parse_logformat_string(char *str, struct proxy *curproxy, struct list *list_format, int capabilities)
William Lallemand723b73a2012-02-08 16:37:49 +0100311{
312 char *sp = str; /* start pointer */
313 int cformat = -1; /* current token format : LOG_TEXT, LOG_SEPARATOR, LOG_VARIABLE */
314 int pformat = -1; /* previous token format */
315 struct logformat_node *tmplf, *back;
William Lallemand81f51172012-03-12 11:03:47 +0100316 int options = 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100317
William Lallemand1dc00ef2012-08-09 16:41:35 +0200318 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200319
William Lallemand723b73a2012-02-08 16:37:49 +0100320 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100321 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100322 LIST_DEL(&tmplf->list);
323 free(tmplf);
324 }
325
326 while (1) {
327
328 // push the variable only if formats are different, not
329 // within a variable, and not the first iteration
330 if ((cformat != pformat && cformat != -1 && pformat != -1) || *str == '\0') {
331 if (((pformat != LF_STARTVAR && cformat != LF_VAR) &&
332 (pformat != LF_STARTVAR && cformat != LF_STARG) &&
333 (pformat != LF_STARG && cformat != LF_VAR)) || *str == '\0') {
334 if (pformat > LF_VAR) // unfinished string
335 pformat = LF_TEXT;
William Lallemand81f51172012-03-12 11:03:47 +0100336 if (pformat == LF_VAR)
William Lallemand1d705562012-03-12 12:46:41 +0100337 parse_logformat_var(sp, str - sp, curproxy, list_format, &options);
William Lallemand81f51172012-03-12 11:03:47 +0100338 else
William Lallemand1d705562012-03-12 12:46:41 +0100339 add_to_logformat_list(sp, str, pformat, list_format);
William Lallemand723b73a2012-02-08 16:37:49 +0100340 sp = str;
341 if (*str == '\0')
342 break;
343 }
344 }
345
346 if (cformat != -1)
347 str++; // consume the string, except on the first tour
348
349 pformat = cformat;
350
351 if (*str == '\0') {
352 cformat = LF_STARTVAR; // for breaking in all cases
353 continue;
354 }
355
356 if (pformat == LF_STARTVAR) { // after a %
357 if ( (*str >= 'a' && *str <= 'z') || // parse varname
358 (*str >= 'A' && *str <= 'Z') ||
359 (*str >= '0' && *str <= '9')) {
360 cformat = LF_VAR; // varname
361 continue;
362 } else if (*str == '{') {
363 cformat = LF_STARG; // variable arguments
364 continue;
365 } else { // another unexpected token
366 pformat = LF_TEXT; // redefine the format of the previous token to TEXT
367 cformat = LF_TEXT;
368 continue;
369 }
370
371 } else if (pformat == LF_VAR) { // after a varname
372 if ( (*str >= 'a' && *str <= 'z') || // parse varname
373 (*str >= 'A' && *str <= 'Z') ||
374 (*str >= '0' && *str <= '9')) {
375 cformat = LF_VAR;
376 continue;
377 }
378 } else if (pformat == LF_STARG) { // inside variable arguments
379 if (*str == '}') { // end of varname
380 cformat = LF_EDARG;
381 continue;
382 } else { // all tokens are acceptable within { }
383 cformat = LF_STARG;
384 continue;
385 }
386 } else if (pformat == LF_EDARG) { // after arguments
387 if ( (*str >= 'a' && *str <= 'z') || // parse a varname
388 (*str >= 'A' && *str <= 'Z') ||
389 (*str >= '0' && *str <= '9')) {
390 cformat = LF_VAR;
391 continue;
392 } else { // if no varname after arguments, transform in TEXT
393 pformat = LF_TEXT;
394 cformat = LF_TEXT;
395 }
396 }
397
398 // others tokens that don't match previous conditions
399 if (*str == '%') {
400 cformat = LF_STARTVAR;
401 } else if (*str == ' ') {
402 cformat = LF_SEPARATOR;
403 } else {
404 cformat = LF_TEXT;
405 }
406 }
407}
408
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409/*
410 * Displays the message on stderr with the date and pid. Overrides the quiet
411 * mode during startup.
412 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200413void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414{
415 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200416 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200417
418 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
419 va_start(argp, fmt);
420
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200421 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200423 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200424 vfprintf(stderr, fmt, argp);
425 fflush(stderr);
426 va_end(argp);
427 }
428}
429
430
431/*
432 * Displays the message on stderr with the date and pid.
433 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200434void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435{
436 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200437 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438
439 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
440 va_start(argp, fmt);
441
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200442 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200444 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445 vfprintf(stderr, fmt, argp);
446 fflush(stderr);
447 va_end(argp);
448 }
449}
450
451/*
452 * Displays the message on <out> only if quiet mode is not set.
453 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200454void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455{
456 va_list argp;
457
458 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
459 va_start(argp, fmt);
460 vfprintf(out, fmt, argp);
461 fflush(out);
462 va_end(argp);
463 }
464}
465
466/*
467 * returns log level for <lev> or -1 if not found.
468 */
469int get_log_level(const char *lev)
470{
471 int level;
472
473 level = NB_LOG_LEVELS - 1;
474 while (level >= 0 && strcmp(log_levels[level], lev))
475 level--;
476
477 return level;
478}
479
480
481/*
482 * returns log facility for <fac> or -1 if not found.
483 */
484int get_log_facility(const char *fac)
485{
486 int facility;
487
488 facility = NB_LOG_FACILITIES - 1;
489 while (facility >= 0 && strcmp(log_facilities[facility], fac))
490 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100491
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492 return facility;
493}
494
William Lallemanda1cc3812012-02-08 16:38:44 +0100495/*
496 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200497 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100498 *
499 * Return the adress of the \0 character, or NULL on error
500 */
Willy Tarreau4f653562012-10-12 19:48:16 +0200501char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +0100502{
William Lallemand51b5dca2012-03-26 17:52:55 +0200503 int n;
William Lallemanda1cc3812012-02-08 16:38:44 +0100504
William Lallemandbddd4fd2012-02-27 11:23:10 +0100505 if (src == NULL || *src == '\0') {
506 if (node->options & LOG_OPT_QUOTE) {
507 if (size > 2) {
508 *(dst++) = '"';
509 *(dst++) = '"';
510 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100511 } else {
512 dst = NULL;
513 return dst;
514 }
515 } else {
516 if (size > 1) {
517 *(dst++) = '-';
518 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100519 } else { // error no space available
520 dst = NULL;
521 return dst;
522 }
523 }
524 } else {
525 if (node->options & LOG_OPT_QUOTE) {
526 if (size-- > 1 ) {
527 *(dst++) = '"';
528 } else {
529 dst = NULL;
530 return NULL;
531 }
William Lallemand51b5dca2012-03-26 17:52:55 +0200532 n = strlcpy2(dst, src, size);
533 size -= n;
534 dst += n;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100535 if (size > 1) {
536 *(dst++) = '"';
537 *dst = '\0';
538 } else {
539 dst = NULL;
540 }
541 } else {
542 dst += strlcpy2(dst, src, size);
543 }
544 }
545 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100546}
547
William Lallemand5f232402012-04-05 18:02:55 +0200548/*
549 * Write a IP adress to the log string
550 * +X option write in hexadecimal notation, most signifant byte on the left
551 */
552char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
553{
554 char *ret = dst;
555 int iret;
556 char pn[INET6_ADDRSTRLEN];
557
558 if (node->options & LOG_OPT_HEXA) {
559 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
560 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
561 if (iret < 0 || iret > size)
562 return NULL;
563 ret += iret;
564 } else {
565 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
566 ret = lf_text(dst, pn, size, node);
567 if (ret == NULL)
568 return NULL;
569 }
570 return ret;
571}
572
573/*
574 * Write a port to the log
575 * +X option write in hexadecimal notation, most signifant byte on the left
576 */
577char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
578{
579 char *ret = dst;
580 int iret;
581
582 if (node->options & LOG_OPT_HEXA) {
583 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
584 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
585 if (iret < 0 || iret > size)
586 return NULL;
587 ret += iret;
588 } else {
589 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
590 if (ret == NULL)
591 return NULL;
592 }
593 return ret;
594}
595
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100596/* Re-generate the syslog header at the beginning of logline once a second and
597 * return the pointer to the first character after the header.
598 */
599static char *update_log_hdr()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200600{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100601 static long tvsec;
602 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200603
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200604 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200605 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200606 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100607 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200608
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200609 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200610 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100612 hdr_len = snprintf(logline, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100613 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200614 monthname[tm.tm_mon],
615 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100616 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000617 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 /* WARNING: depending upon implementations, snprintf may return
619 * either -1 or the number of bytes that would be needed to store
620 * the total message. In both cases, we must adjust it.
621 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100622 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
623 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100625 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200626 }
627
William Lallemand2a4a44f2012-02-06 16:00:33 +0100628 return dataptr;
629}
630
631/*
632 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100633 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100634 */
635void send_log(struct proxy *p, int level, const char *format, ...)
636{
637 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100638 char *dataptr;
639 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100640
641 if (level < 0 || format == NULL)
642 return;
643
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100644 dataptr = update_log_hdr(); /* update log header and skip it */
645 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100646
647 va_start(argp, format);
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100648 data_len += vsnprintf(dataptr, logline + sizeof(logline) - dataptr, format, argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100649 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
650 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100652
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100653 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100654}
655
656/*
657 * This function sends a syslog message.
658 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100659 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100660 */
661void __send_log(struct proxy *p, int level, char *message, size_t size)
662{
663 static int logfdunix = -1; /* syslog to AF_UNIX socket */
664 static int logfdinet = -1; /* syslog to AF_INET socket */
665 static char *dataptr = NULL;
666 int fac_level;
667 struct list *logsrvs = NULL;
668 struct logsrv *tmp = NULL;
669 int nblogger;
670 char *log_ptr;
671
672 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200673
674 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200675 if (!LIST_ISEMPTY(&global.logsrvs)) {
676 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 }
678 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200679 if (!LIST_ISEMPTY(&p->logsrvs)) {
680 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100682 }
683
William Lallemand0f99e342011-10-12 17:50:54 +0200684 if (!logsrvs)
685 return;
686
William Lallemand2a4a44f2012-02-06 16:00:33 +0100687 message[size - 1] = '\n';
688
Robert Tsai81ae1952007-12-05 10:47:29 +0100689 /* Lazily set up syslog sockets for protocol families of configured
690 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200691 nblogger = 0;
692 list_for_each_entry(tmp, logsrvs, list) {
693 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100694 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200695
David du Colombier11bcb6c2011-03-24 12:23:00 +0100696 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100697 proto = 0;
698 plogfd = &logfdunix;
699 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100700 proto = IPPROTO_UDP;
701 plogfd = &logfdinet;
702 }
703 if (*plogfd >= 0) {
704 /* socket already created. */
705 continue;
706 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100707 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100708 proto)) < 0) {
709 Alert("socket for logger #%d failed: %s (errno=%d)\n",
710 nblogger + 1, strerror(errno), errno);
711 return;
712 }
713 /* we don't want to receive anything on this socket */
714 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
715 /* does nothing under Linux, maybe needed for others */
716 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200717 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718 }
719
Robert Tsai81ae1952007-12-05 10:47:29 +0100720 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200721 nblogger = 0;
722 list_for_each_entry(tmp, logsrvs, list) {
723 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100724 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100725 &logfdunix : &logfdinet;
726 int sent;
727
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200729 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100731
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732 /* For each target, we may have a different facility.
733 * We can also have a different log level for each message.
734 * This induces variations in the message header length.
735 * Since we don't want to recompute it each time, nor copy it every
736 * time, we only change the facility in the pre-computed header,
737 * and we change the pointer to the header accordingly.
738 */
William Lallemand0f99e342011-10-12 17:50:54 +0200739 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100740 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741 do {
742 *log_ptr = '0' + fac_level % 10;
743 fac_level /= 10;
744 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100745 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200746 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100747
William Lallemandbfb099c2012-03-19 16:15:12 +0100748 sent = sendto(*plogfd, log_ptr, size + log_ptr - dataptr,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200749 MSG_DONTWAIT | MSG_NOSIGNAL,
750 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100751 if (sent < 0) {
752 Alert("sendto logger #%d failed: %s (errno=%d)\n",
753 nblogger, strerror(errno), errno);
754 }
William Lallemand0f99e342011-10-12 17:50:54 +0200755 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200756 }
757}
758
William Lallemandbddd4fd2012-02-27 11:23:10 +0100759extern fd_set hdr_encode_map[];
760extern fd_set url_encode_map[];
761
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200763const 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 +0100764const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
765 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
766 Set-cookie Updated, unknown, unknown */
767
William Lallemand1d705562012-03-12 12:46:41 +0100768/*
769 * try to write a character if there is enough space, or goto out
770 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100771#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100772 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100773 *(tmplog++) = (x); \
774 } else { \
775 goto out; \
776 } \
777 } while(0)
778
William Lallemand1d705562012-03-12 12:46:41 +0100779
780
781int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782{
Willy Tarreau73de9892006-11-30 11:40:23 +0100783 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100784 struct proxy *be = s->be;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100785 struct http_txn *txn = &s->txn;
William Lallemand1d705562012-03-12 12:46:41 +0100786 int tolog;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100787 char *uri;
788 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200789 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100790 int t_request;
791 int hdr;
792 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100793 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100794 char *ret;
795 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100796 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797
William Lallemandbddd4fd2012-02-27 11:23:10 +0100798 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreau42250582007-04-01 01:30:43 +0200799 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100800
801 if (!(tolog & LW_SVID))
802 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200803 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100804 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200805 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100806 break;
807 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200808 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100809 break;
810 default:
811 svid = "<NOSRV>";
812 break;
813 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814
William Lallemandbddd4fd2012-02-27 11:23:10 +0100815 t_request = -1;
816 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
817 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
818
William Lallemand1d705562012-03-12 12:46:41 +0100819 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200820
William Lallemandbddd4fd2012-02-27 11:23:10 +0100821 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100822 if (LIST_ISEMPTY(list_format))
823 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100824
William Lallemand1d705562012-03-12 12:46:41 +0100825 list_for_each_entry(tmp, list_format, list) {
Willy Tarreau4f653562012-10-12 19:48:16 +0200826 const char *src = NULL;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100827 switch (tmp->type) {
828
William Lallemand1d705562012-03-12 12:46:41 +0100829 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100830 if (!last_isspace) {
831 LOGCHAR(' ');
832 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100833 }
834 break;
835
William Lallemand1d705562012-03-12 12:46:41 +0100836 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100837 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200838 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100839 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100840 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100841 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100842 last_isspace = 0;
843 break;
844
William Lallemand1d705562012-03-12 12:46:41 +0100845 case LOG_FMT_CLIENTIP: // %Ci
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200846 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->prod->conn->addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200847 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100848 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100849 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100850 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100851 last_isspace = 0;
852 break;
853
William Lallemand1d705562012-03-12 12:46:41 +0100854 case LOG_FMT_CLIENTPORT: // %Cp
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200855 if (s->req->prod->conn->addr.from.ss_family == AF_UNIX) {
William Lallemand5f232402012-04-05 18:02:55 +0200856 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
857 } else {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200858 ret = lf_port(tmplog, (struct sockaddr *)&s->req->prod->conn->addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200859 dst + maxsize - tmplog, tmp);
860 }
861 if (ret == NULL)
862 goto out;
863 tmplog = ret;
864 last_isspace = 0;
865 break;
866
867 case LOG_FMT_FRONTENDIP: // %Fi
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200868 conn_get_to_addr(s->req->prod->conn);
869 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->prod->conn->addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200870 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100871 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100872 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100873 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100874 last_isspace = 0;
875 break;
876
William Lallemand5f232402012-04-05 18:02:55 +0200877 case LOG_FMT_FRONTENDPORT: // %Fp
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200878 conn_get_to_addr(s->req->prod->conn);
879 if (s->req->prod->conn->addr.to.ss_family == AF_UNIX) {
William Lallemand5f232402012-04-05 18:02:55 +0200880 ret = ltoa_o(s->listener->luid,
881 tmplog, dst + maxsize - tmplog);
882 } else {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200883 ret = lf_port(tmplog, (struct sockaddr *)&s->req->prod->conn->addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200884 dst + maxsize - tmplog, tmp);
885 }
886 if (ret == NULL)
887 goto out;
888 tmplog = ret;
889 last_isspace = 0;
890 break;
891
William Lallemand1d705562012-03-12 12:46:41 +0100892 case LOG_FMT_BACKENDIP: // %Bi
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200893 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->cons->conn->addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200894 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100895 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +0100896 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100897 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +0100898 last_isspace = 0;
899 break;
900
William Lallemand1d705562012-03-12 12:46:41 +0100901 case LOG_FMT_BACKENDPORT: // %Bp
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200902 ret = lf_port(tmplog, (struct sockaddr *)&s->req->cons->conn->addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200903 dst + maxsize - tmplog, tmp);
904 if (ret == NULL)
905 goto out;
906 tmplog = ret;
907 last_isspace = 0;
908 break;
909
910 case LOG_FMT_SERVERIP: // %Si
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200911 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->cons->conn->addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200912 dst + maxsize - tmplog, tmp);
913 if (ret == NULL)
914 goto out;
915 tmplog = ret;
916 last_isspace = 0;
917 break;
918
919 case LOG_FMT_SERVERPORT: // %Sp
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200920 ret = lf_port(tmplog, (struct sockaddr *)&s->req->cons->conn->addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200921 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100922 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +0100923 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100924 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +0100925 last_isspace = 0;
926 break;
927
William Lallemand1d705562012-03-12 12:46:41 +0100928 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +0100929 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +0200930 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
931 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100932 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100933 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100934 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100935 last_isspace = 0;
936 break;
937
William Lallemand1d705562012-03-12 12:46:41 +0100938 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +0100939 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +0200940 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100941 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100942 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100943 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100944 last_isspace = 0;
945 break;
946
Yuxans Yao4e25b012012-10-19 10:36:09 +0800947 case LOG_FMT_DATELOCAL: // %Tl
948 get_localtime(s->logs.accept_date.tv_sec, &tm);
949 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
950 if (ret == NULL)
951 goto out;
952 tmplog = ret;
953 last_isspace = 0;
954 break;
955
William Lallemand5f232402012-04-05 18:02:55 +0200956 case LOG_FMT_TS: // %Ts
957 get_gmtime(s->logs.accept_date.tv_sec, &tm);
958 if (tmp->options & LOG_OPT_HEXA) {
959 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
960 if (iret < 0 || iret > dst + maxsize - tmplog)
961 goto out;
962 last_isspace = 0;
963 tmplog += iret;
964 } else {
965 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
966 if (ret == NULL)
967 goto out;
968 tmplog = ret;
969 last_isspace = 0;
970 }
971 break;
972
William Lallemand1d705562012-03-12 12:46:41 +0100973 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +0200974 if (tmp->options & LOG_OPT_HEXA) {
975 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
976 if (iret < 0 || iret > dst + maxsize - tmplog)
977 goto out;
978 last_isspace = 0;
979 tmplog += iret;
980 } else {
981 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100982 goto out;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100983 tmplog = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
984 tmplog, 4);
William Lallemand51b5dca2012-03-26 17:52:55 +0200985 if (!tmplog)
986 goto out;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100987 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +0200988 }
989 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100990
William Lallemand1d705562012-03-12 12:46:41 +0100991 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +0100992 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +0200993 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100994 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100995 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100996 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +0200997 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100998 break;
999
Willy Tarreau773d65f2012-10-12 14:56:11 +02001000 case LOG_FMT_FRONTEND_XPRT: // %ft
1001 src = fe->id;
1002 if (tmp->options & LOG_OPT_QUOTE)
1003 LOGCHAR('"');
1004 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1005 if (iret == 0)
1006 goto out;
1007 tmplog += iret;
1008#ifdef USE_OPENSSL
1009 if (s->listener->xprt == &ssl_sock)
1010 LOGCHAR('~');
1011#endif
1012 if (tmp->options & LOG_OPT_QUOTE)
1013 LOGCHAR('"');
1014 last_isspace = 0;
1015 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001016#ifdef USE_OPENSSL
1017 case LOG_FMT_SSL_CIPHER: // %sslc
1018 src = NULL;
1019 if (s->listener->xprt == &ssl_sock)
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001020 src = ssl_sock_get_cipher_name(s->si[0].conn);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001021 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1022 if (ret == NULL)
1023 goto out;
1024 tmplog = ret;
1025 last_isspace = 0;
1026 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001027
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001028 case LOG_FMT_SSL_VERSION: // %sslv
1029 src = NULL;
1030 if (s->listener->xprt == &ssl_sock)
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001031 src = ssl_sock_get_proto_version(s->si[0].conn);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001032 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1033 if (ret == NULL)
1034 goto out;
1035 tmplog = ret;
1036 last_isspace = 0;
1037 break;
1038#endif
William Lallemand1d705562012-03-12 12:46:41 +01001039 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001040 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001041 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
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 Lallemand51b5dca2012-03-26 17:52:55 +02001045 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001046 break;
1047
William Lallemand1d705562012-03-12 12:46:41 +01001048 case LOG_FMT_SERVER: // %s
William Lallemandbddd4fd2012-02-27 11:23:10 +01001049 src = (char *)svid;
William Lallemand5f232402012-04-05 18:02:55 +02001050 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001051 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001052 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001053 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001054 last_isspace = 0;
1055 break;
1056
William Lallemand1d705562012-03-12 12:46:41 +01001057 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001058 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001059 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001060 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001061 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001062 last_isspace = 0;
1063 break;
1064
William Lallemand1d705562012-03-12 12:46:41 +01001065 case LOG_FMT_TW: // %Tw
1066 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001067 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001068 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001069 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001070 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001071 last_isspace = 0;
1072 break;
1073
William Lallemand1d705562012-03-12 12:46:41 +01001074 case LOG_FMT_TC: // %Tc
1075 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001076 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001077 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001078 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001079 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001080 last_isspace = 0;
1081 break;
1082
William Lallemand1d705562012-03-12 12:46:41 +01001083 case LOG_FMT_TR: // %Tr
1084 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001085 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001086 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001087 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001088 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001089 last_isspace = 0;
1090 break;
1091
William Lallemand1d705562012-03-12 12:46:41 +01001092 case LOG_FMT_TT: // %Tt
William Lallemandbddd4fd2012-02-27 11:23:10 +01001093 if (!(tolog & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001094 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001095 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001096 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001097 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001098 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001099 last_isspace = 0;
1100 break;
1101
William Lallemand1d705562012-03-12 12:46:41 +01001102 case LOG_FMT_STATUS: // %st
William Lallemand5f232402012-04-05 18:02:55 +02001103 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001104 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001105 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001106 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001107 last_isspace = 0;
1108 break;
1109
William Lallemand1d705562012-03-12 12:46:41 +01001110 case LOG_FMT_BYTES: // %B
William Lallemandbddd4fd2012-02-27 11:23:10 +01001111 if (!(tolog & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001112 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001113 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001114 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001115 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001116 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001117 last_isspace = 0;
1118 break;
1119
William Lallemand1d705562012-03-12 12:46:41 +01001120 case LOG_FMT_CCLIENT: // %cc
William Lallemandbddd4fd2012-02-27 11:23:10 +01001121 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001122 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001123 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001124 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001125 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001126 last_isspace = 0;
1127 break;
1128
William Lallemand1d705562012-03-12 12:46:41 +01001129 case LOG_FMT_CSERVER: // %cs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001130 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001131 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001132 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001133 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001134 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001135 last_isspace = 0;
1136 break;
1137
William Lallemand1d705562012-03-12 12:46:41 +01001138 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +01001139 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1140 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
1141 *tmplog = '\0';
1142 last_isspace = 0;
1143 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001144
William Lallemand1d705562012-03-12 12:46:41 +01001145 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001146 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1147 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001148 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1149 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-');
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_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001154 ret = ltoa_o(actconn, 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_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001162 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001163 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001164 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001165 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001166 last_isspace = 0;
1167 break;
1168
William Lallemand1d705562012-03-12 12:46:41 +01001169 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001170 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001171 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001172 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001173 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001174 last_isspace = 0;
1175 break;
1176
William Lallemand1d705562012-03-12 12:46:41 +01001177 case LOG_FMT_SRVCONN: // %sc
1178 ret = ultoa_o(target_srv(&s->target) ?
1179 target_srv(&s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001180 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001181 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001182 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001183 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001184 last_isspace = 0;
1185 break;
1186
William Lallemand1d705562012-03-12 12:46:41 +01001187 case LOG_FMT_RETRIES: // %rq
William Lallemandbddd4fd2012-02-27 11:23:10 +01001188 if (s->flags & SN_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001189 LOGCHAR('+');
1190 ret = ltoa_o((s->req->cons->conn_retries>0) ?
1191 (be->conn_retries - s->req->cons->conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001192 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001193 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001194 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001195 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001196 last_isspace = 0;
1197 break;
1198
William Lallemand1d705562012-03-12 12:46:41 +01001199 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001200 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001201 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001202 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001203 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001204 last_isspace = 0;
1205 break;
1206
William Lallemand1d705562012-03-12 12:46:41 +01001207 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001208 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001209 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001210 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001211 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001212 last_isspace = 0;
1213 break;
1214
William Lallemand1d705562012-03-12 12:46:41 +01001215 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001216 /* request header */
1217 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1218 if (tmp->options & LOG_OPT_QUOTE)
1219 LOGCHAR('"');
1220 LOGCHAR('{');
1221 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1222 if (hdr)
1223 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001224 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001225 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001226 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001227 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001228 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001229 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001230 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001231 }
1232 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001233 if (tmp->options & LOG_OPT_QUOTE)
1234 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001235 last_isspace = 0;
William Lallemand1d705562012-03-12 12:46:41 +01001236 if (tmp->options & LOG_OPT_QUOTE)
1237 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001238 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001239 break;
1240
William Lallemand1d705562012-03-12 12:46:41 +01001241 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001242 /* request header list */
1243 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1244 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1245 if (hdr > 0)
1246 LOGCHAR(' ');
1247 if (tmp->options & LOG_OPT_QUOTE)
1248 LOGCHAR('"');
1249 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001250 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001251 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001252 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001253 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001254 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001255 } else if (!(tmp->options & LOG_OPT_QUOTE))
1256 LOGCHAR('-');
1257 if (tmp->options & LOG_OPT_QUOTE)
1258 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001259 last_isspace = 0;
1260 }
1261 }
1262 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001263
William Lallemand1d705562012-03-12 12:46:41 +01001264
1265 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001266 /* response header */
1267 if (fe->to_log & LW_RSPHDR &&
1268 txn->rsp.cap) {
1269 if (tmp->options & LOG_OPT_QUOTE)
1270 LOGCHAR('"');
1271 LOGCHAR('{');
1272 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1273 if (hdr)
1274 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001275 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001276 ret = encode_string(tmplog, dst + maxsize,
1277 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1278 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001279 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001280 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001281 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001282 }
1283 LOGCHAR('}');
1284 last_isspace = 0;
1285 if (tmp->options & LOG_OPT_QUOTE)
1286 LOGCHAR('"');
1287 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001288 break;
1289
William Lallemand1d705562012-03-12 12:46:41 +01001290 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001291 /* response header list */
1292 if (fe->to_log & LW_RSPHDR && txn->rsp.cap) {
1293 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1294 if (hdr > 0)
1295 LOGCHAR(' ');
1296 if (tmp->options & LOG_OPT_QUOTE)
1297 LOGCHAR('"');
1298 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001299 ret = encode_string(tmplog, dst + maxsize,
1300 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1301 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001302 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001303 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001304 } else if (!(tmp->options & LOG_OPT_QUOTE))
1305 LOGCHAR('-');
1306 if (tmp->options & LOG_OPT_QUOTE)
1307 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001308 last_isspace = 0;
1309 }
1310 }
1311 break;
1312
William Lallemand1d705562012-03-12 12:46:41 +01001313 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001314 /* Request */
1315 if (tmp->options & LOG_OPT_QUOTE)
1316 LOGCHAR('"');
1317 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001318 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001319 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001320 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001321 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001322 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001323 if (tmp->options & LOG_OPT_QUOTE)
1324 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001325 last_isspace = 0;
1326 break;
William Lallemand5f232402012-04-05 18:02:55 +02001327
1328 case LOG_FMT_COUNTER: // %rt
1329 if (tmp->options & LOG_OPT_HEXA) {
1330 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", global.req_count);
1331 if (iret < 0 || iret > dst + maxsize - tmplog)
1332 goto out;
1333 last_isspace = 0;
1334 tmplog += iret;
1335 } else {
1336 ret = ltoa_o(global.req_count, tmplog, dst + maxsize - tmplog);
1337 if (ret == NULL)
1338 goto out;
1339 tmplog = ret;
1340 last_isspace = 0;
1341 }
1342 break;
1343
1344 case LOG_FMT_HOSTNAME: // %H
1345 src = hostname;
1346 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1347 if (ret == NULL)
1348 goto out;
1349 tmplog = ret;
1350 last_isspace = 0;
1351 break;
1352
1353 case LOG_FMT_PID: // %pid
1354 if (tmp->options & LOG_OPT_HEXA) {
1355 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1356 if (iret < 0 || iret > dst + maxsize - tmplog)
1357 goto out;
1358 last_isspace = 0;
1359 tmplog += iret;
1360 } else {
1361 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1362 if (ret == NULL)
1363 goto out;
1364 tmplog = ret;
1365 last_isspace = 0;
1366 }
1367 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001368
1369 case LOG_FMT_UNIQUEID: // %ID
1370 src = s->unique_id;
1371 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
1372 if (ret == NULL)
1373 goto out;
1374 tmplog = ret;
1375 last_isspace = 0;
1376 break;
1377
William Lallemandbddd4fd2012-02-27 11:23:10 +01001378 }
1379 }
1380
1381out:
William Lallemand1d705562012-03-12 12:46:41 +01001382 /* *tmplog is a unused character */
1383 *tmplog = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001384
William Lallemand1d705562012-03-12 12:46:41 +01001385 return tmplog - dst + 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001386
Willy Tarreaubaaee002006-06-26 02:48:02 +02001387}
1388
William Lallemand1d705562012-03-12 12:46:41 +01001389/*
1390 * send a log for the session when we have enough info about it.
1391 * Will not log if the frontend has no log defined.
1392 */
1393void sess_log(struct session *s)
1394{
1395 char *tmplog;
1396 int size, err, level;
1397
1398 /* if we don't want to log normal traffic, return now */
1399 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1400 (s->req->cons->conn_retries != s->be->conn_retries) ||
1401 ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001402
William Lallemand1d705562012-03-12 12:46:41 +01001403 if (!err && (s->fe->options2 & PR_O2_NOLOGNORM))
1404 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001405
William Lallemand1d705562012-03-12 12:46:41 +01001406 if (LIST_ISEMPTY(&s->fe->logsrvs))
1407 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001408
William Lallemand1d705562012-03-12 12:46:41 +01001409 level = LOG_INFO;
1410 if (err && (s->fe->options2 & PR_O2_LOGERRORS))
1411 level = LOG_ERR;
1412
1413 tmplog = update_log_hdr();
1414 size = tmplog - logline;
1415 size += build_logline(s, tmplog, sizeof(logline) - size, &s->fe->logformat);
1416 if (size > 0) {
1417 __send_log(s->fe, level, logline, size);
1418 s->logs.logwait = 0;
1419 }
1420}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001421
Willy Tarreaubaaee002006-06-26 02:48:02 +02001422/*
1423 * Local variables:
1424 * c-indent-level: 8
1425 * c-basic-offset: 8
1426 * End:
1427 */