blob: 130c378ca7e336f2ed1a8999c3a003332a9e0f2f [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 */
83 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
84 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
85 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
Willy Tarreau773d65f2012-10-12 14:56:11 +020086 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
William Lallemand5e19a282012-04-02 16:22:10 +020087 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
88 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
89 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes read */
90 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
91 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
92 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
93 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
94 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
95 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL }, /* status code */
96 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
97 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
98 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
99 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
100 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
101 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
102 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
103 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
104 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
105 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
106 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
107 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request */
108 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response */
109 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request list */
110 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response list */
111 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
112 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
113 { "rt", LOG_FMT_COUNTER, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP request counter */
114 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
115 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200116 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
117 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
William Lallemand5e19a282012-04-02 16:22:10 +0200118 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100119};
120
Willy Tarreau773d65f2012-10-12 14:56:11 +0200121char 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
122char 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";
123char 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 +0100124char *log_format = NULL;
125
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100126/* This is a global syslog line, common to all outgoing messages. It begins
127 * with the syslog tag and the date that are updated by update_log_hdr().
128 */
129static char logline[MAX_SYSLOG_LEN];
130
William Lallemand723b73a2012-02-08 16:37:49 +0100131struct logformat_var_args {
132 char *name;
133 int mask;
134};
135
136struct logformat_var_args var_args_list[] = {
137// global
138 { "M", LOG_OPT_MANDATORY },
139 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200140 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100141 { 0, 0 }
142};
143
144/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100145 * callback used to configure addr source retrieval
146 */
147int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
148{
149 curproxy->options2 |= PR_O2_SRC_ADDR;
150
151 return 0;
152}
153
154
155/*
William Lallemand723b73a2012-02-08 16:37:49 +0100156 * Parse args in a logformat_var
157 */
158int parse_logformat_var_args(char *args, struct logformat_node *node)
159{
160 int i = 0;
161 int end = 0;
162 int flags = 0; // 1 = + 2 = -
163 char *sp = NULL; // start pointer
164
165 if (args == NULL)
166 return 1;
167
168 while (1) {
169 if (*args == '\0')
170 end = 1;
171
172 if (*args == '+') {
173 // add flag
174 sp = args + 1;
175 flags = 1;
176 }
177 if (*args == '-') {
178 // delete flag
179 sp = args + 1;
180 flags = 2;
181 }
182
183 if (*args == '\0' || *args == ',') {
184 *args = '\0';
185 for (i = 0; var_args_list[i].name; i++) {
186 if (strcmp(sp, var_args_list[i].name) == 0) {
187 if (flags == 1) {
188 node->options |= var_args_list[i].mask;
189 break;
190 } else if (flags == 2) {
191 node->options &= ~var_args_list[i].mask;
192 break;
193 }
194 }
195 }
196 sp = NULL;
197 if (end)
198 break;
199 }
200 args++;
201 }
202 return 0;
203}
204
205/*
206 * Parse a variable '%varname' or '%{args}varname' in logformat
207 *
208 */
William Lallemand1d705562012-03-12 12:46:41 +0100209int parse_logformat_var(char *str, size_t len, struct proxy *curproxy, struct list *list_format, int *defoptions)
William Lallemand723b73a2012-02-08 16:37:49 +0100210{
211 int i, j;
212 char *arg = NULL; // arguments
213 int fparam = 0;
214 char *name = NULL;
215 struct logformat_node *node = NULL;
216 char varname[255] = { 0 }; // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100217
William Lallemand723b73a2012-02-08 16:37:49 +0100218 for (i = 1; i < len; i++) { // escape first char %
219 if (!arg && str[i] == '{') {
220 arg = str + i;
221 fparam = 1;
222 } else if (arg && str[i] == '}') {
223 char *tmp = arg;
224 arg = calloc(str + i - tmp, 1); // without {}
225 strncpy(arg, tmp + 1, str + i - tmp - 1); // copy without { and }
226 arg[str + i - tmp - 1] = '\0';
227 fparam = 0;
228 } else if (!name && !fparam) {
229 strncpy(varname, str + i, len - i + 1);
230 varname[len - i] = '\0';
231 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
232 if (strcmp(varname, logformat_keywords[j].name) == 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100233 if (!((logformat_keywords[j].mode == PR_MODE_HTTP) && (curproxy->mode == PR_MODE_TCP))) {
234 node = calloc(1, sizeof(struct logformat_node));
235 node->type = logformat_keywords[j].type;
William Lallemand81f51172012-03-12 11:03:47 +0100236 node->options = *defoptions;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100237 node->arg = arg;
238 parse_logformat_var_args(node->arg, node);
William Lallemand1d705562012-03-12 12:46:41 +0100239 if (node->type == LOG_FMT_GLOBAL) {
William Lallemand81f51172012-03-12 11:03:47 +0100240 *defoptions = node->options;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100241 free(node);
242 } else {
William Lallemandb7ff6a32012-03-02 14:35:21 +0100243 if (logformat_keywords[j].config_callback != NULL) {
244 if (logformat_keywords[j].config_callback(node, curproxy) != 0) {
245 return -1;
246 }
247 }
William Lallemand5e19a282012-04-02 16:22:10 +0200248 curproxy->to_log |= logformat_keywords[j].lw;
William Lallemand1d705562012-03-12 12:46:41 +0100249 LIST_ADDQ(list_format, &node->list);
William Lallemandbddd4fd2012-02-27 11:23:10 +0100250 }
251 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100252 } else {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100253 Warning("Warning: No such variable name '%s' in this log mode\n", varname);
254 if (arg)
255 free(arg);
256 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100257 }
William Lallemand723b73a2012-02-08 16:37:49 +0100258 }
259 }
260 Warning("Warning: No such variable name '%s' in logformat\n", varname);
261 if (arg)
262 free(arg);
263 return -1;
264 }
265 }
266 return -1;
267}
268
269/*
270 * push to the logformat linked list
271 *
272 * start: start pointer
273 * end: end text pointer
274 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100275 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100276 *
277 * LOG_TEXT: copy chars from start to end excluding end.
278 *
279*/
William Lallemand1d705562012-03-12 12:46:41 +0100280void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100281{
282 char *str;
283
William Lallemand1d705562012-03-12 12:46:41 +0100284 if (type == LOG_FMT_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100285 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100286 str = calloc(end - start + 1, 1);
287 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100288 str[end - start] = '\0';
289 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100290 node->type = LOG_FMT_TEXT; // type string
291 LIST_ADDQ(list_format, &node->list);
292 } else if (type == LOG_FMT_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100293 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100294 node->type = LOG_FMT_SEPARATOR;
295 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100296 }
297}
298
299/*
300 * Parse the log_format string and fill a linked list.
301 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
302 * You can set arguments using { } : %{many arguments}varname
William Lallemand1d705562012-03-12 12:46:41 +0100303 *
304 * str: the string to parse
305 * curproxy: the proxy affected
306 * list_format: the destination list
307 * capabilities: PR_MODE_TCP_ | PR_MODE_HTTP
William Lallemand723b73a2012-02-08 16:37:49 +0100308 */
William Lallemand1d705562012-03-12 12:46:41 +0100309void parse_logformat_string(char *str, struct proxy *curproxy, struct list *list_format, int capabilities)
William Lallemand723b73a2012-02-08 16:37:49 +0100310{
311 char *sp = str; /* start pointer */
312 int cformat = -1; /* current token format : LOG_TEXT, LOG_SEPARATOR, LOG_VARIABLE */
313 int pformat = -1; /* previous token format */
314 struct logformat_node *tmplf, *back;
William Lallemand81f51172012-03-12 11:03:47 +0100315 int options = 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100316
William Lallemand1dc00ef2012-08-09 16:41:35 +0200317 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200318
William Lallemand723b73a2012-02-08 16:37:49 +0100319 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100320 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100321 LIST_DEL(&tmplf->list);
322 free(tmplf);
323 }
324
325 while (1) {
326
327 // push the variable only if formats are different, not
328 // within a variable, and not the first iteration
329 if ((cformat != pformat && cformat != -1 && pformat != -1) || *str == '\0') {
330 if (((pformat != LF_STARTVAR && cformat != LF_VAR) &&
331 (pformat != LF_STARTVAR && cformat != LF_STARG) &&
332 (pformat != LF_STARG && cformat != LF_VAR)) || *str == '\0') {
333 if (pformat > LF_VAR) // unfinished string
334 pformat = LF_TEXT;
William Lallemand81f51172012-03-12 11:03:47 +0100335 if (pformat == LF_VAR)
William Lallemand1d705562012-03-12 12:46:41 +0100336 parse_logformat_var(sp, str - sp, curproxy, list_format, &options);
William Lallemand81f51172012-03-12 11:03:47 +0100337 else
William Lallemand1d705562012-03-12 12:46:41 +0100338 add_to_logformat_list(sp, str, pformat, list_format);
William Lallemand723b73a2012-02-08 16:37:49 +0100339 sp = str;
340 if (*str == '\0')
341 break;
342 }
343 }
344
345 if (cformat != -1)
346 str++; // consume the string, except on the first tour
347
348 pformat = cformat;
349
350 if (*str == '\0') {
351 cformat = LF_STARTVAR; // for breaking in all cases
352 continue;
353 }
354
355 if (pformat == LF_STARTVAR) { // after a %
356 if ( (*str >= 'a' && *str <= 'z') || // parse varname
357 (*str >= 'A' && *str <= 'Z') ||
358 (*str >= '0' && *str <= '9')) {
359 cformat = LF_VAR; // varname
360 continue;
361 } else if (*str == '{') {
362 cformat = LF_STARG; // variable arguments
363 continue;
364 } else { // another unexpected token
365 pformat = LF_TEXT; // redefine the format of the previous token to TEXT
366 cformat = LF_TEXT;
367 continue;
368 }
369
370 } else if (pformat == LF_VAR) { // after a varname
371 if ( (*str >= 'a' && *str <= 'z') || // parse varname
372 (*str >= 'A' && *str <= 'Z') ||
373 (*str >= '0' && *str <= '9')) {
374 cformat = LF_VAR;
375 continue;
376 }
377 } else if (pformat == LF_STARG) { // inside variable arguments
378 if (*str == '}') { // end of varname
379 cformat = LF_EDARG;
380 continue;
381 } else { // all tokens are acceptable within { }
382 cformat = LF_STARG;
383 continue;
384 }
385 } else if (pformat == LF_EDARG) { // after arguments
386 if ( (*str >= 'a' && *str <= 'z') || // parse a varname
387 (*str >= 'A' && *str <= 'Z') ||
388 (*str >= '0' && *str <= '9')) {
389 cformat = LF_VAR;
390 continue;
391 } else { // if no varname after arguments, transform in TEXT
392 pformat = LF_TEXT;
393 cformat = LF_TEXT;
394 }
395 }
396
397 // others tokens that don't match previous conditions
398 if (*str == '%') {
399 cformat = LF_STARTVAR;
400 } else if (*str == ' ') {
401 cformat = LF_SEPARATOR;
402 } else {
403 cformat = LF_TEXT;
404 }
405 }
406}
407
Willy Tarreaubaaee002006-06-26 02:48:02 +0200408/*
409 * Displays the message on stderr with the date and pid. Overrides the quiet
410 * mode during startup.
411 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200412void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413{
414 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200415 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416
417 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
418 va_start(argp, fmt);
419
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200420 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200421 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200422 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423 vfprintf(stderr, fmt, argp);
424 fflush(stderr);
425 va_end(argp);
426 }
427}
428
429
430/*
431 * Displays the message on stderr with the date and pid.
432 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200433void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434{
435 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200436 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437
438 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
439 va_start(argp, fmt);
440
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200441 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200443 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200444 vfprintf(stderr, fmt, argp);
445 fflush(stderr);
446 va_end(argp);
447 }
448}
449
450/*
451 * Displays the message on <out> only if quiet mode is not set.
452 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200453void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454{
455 va_list argp;
456
457 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
458 va_start(argp, fmt);
459 vfprintf(out, fmt, argp);
460 fflush(out);
461 va_end(argp);
462 }
463}
464
465/*
466 * returns log level for <lev> or -1 if not found.
467 */
468int get_log_level(const char *lev)
469{
470 int level;
471
472 level = NB_LOG_LEVELS - 1;
473 while (level >= 0 && strcmp(log_levels[level], lev))
474 level--;
475
476 return level;
477}
478
479
480/*
481 * returns log facility for <fac> or -1 if not found.
482 */
483int get_log_facility(const char *fac)
484{
485 int facility;
486
487 facility = NB_LOG_FACILITIES - 1;
488 while (facility >= 0 && strcmp(log_facilities[facility], fac))
489 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100490
Willy Tarreaubaaee002006-06-26 02:48:02 +0200491 return facility;
492}
493
William Lallemanda1cc3812012-02-08 16:38:44 +0100494/*
495 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200496 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100497 *
498 * Return the adress of the \0 character, or NULL on error
499 */
Willy Tarreau4f653562012-10-12 19:48:16 +0200500char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +0100501{
William Lallemand51b5dca2012-03-26 17:52:55 +0200502 int n;
William Lallemanda1cc3812012-02-08 16:38:44 +0100503
William Lallemandbddd4fd2012-02-27 11:23:10 +0100504 if (src == NULL || *src == '\0') {
505 if (node->options & LOG_OPT_QUOTE) {
506 if (size > 2) {
507 *(dst++) = '"';
508 *(dst++) = '"';
509 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100510 } else {
511 dst = NULL;
512 return dst;
513 }
514 } else {
515 if (size > 1) {
516 *(dst++) = '-';
517 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100518 } else { // error no space available
519 dst = NULL;
520 return dst;
521 }
522 }
523 } else {
524 if (node->options & LOG_OPT_QUOTE) {
525 if (size-- > 1 ) {
526 *(dst++) = '"';
527 } else {
528 dst = NULL;
529 return NULL;
530 }
William Lallemand51b5dca2012-03-26 17:52:55 +0200531 n = strlcpy2(dst, src, size);
532 size -= n;
533 dst += n;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100534 if (size > 1) {
535 *(dst++) = '"';
536 *dst = '\0';
537 } else {
538 dst = NULL;
539 }
540 } else {
541 dst += strlcpy2(dst, src, size);
542 }
543 }
544 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100545}
546
William Lallemand5f232402012-04-05 18:02:55 +0200547/*
548 * Write a IP adress to the log string
549 * +X option write in hexadecimal notation, most signifant byte on the left
550 */
551char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
552{
553 char *ret = dst;
554 int iret;
555 char pn[INET6_ADDRSTRLEN];
556
557 if (node->options & LOG_OPT_HEXA) {
558 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
559 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
560 if (iret < 0 || iret > size)
561 return NULL;
562 ret += iret;
563 } else {
564 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
565 ret = lf_text(dst, pn, size, node);
566 if (ret == NULL)
567 return NULL;
568 }
569 return ret;
570}
571
572/*
573 * Write a port to the log
574 * +X option write in hexadecimal notation, most signifant byte on the left
575 */
576char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
577{
578 char *ret = dst;
579 int iret;
580
581 if (node->options & LOG_OPT_HEXA) {
582 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
583 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
584 if (iret < 0 || iret > size)
585 return NULL;
586 ret += iret;
587 } else {
588 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
589 if (ret == NULL)
590 return NULL;
591 }
592 return ret;
593}
594
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100595/* Re-generate the syslog header at the beginning of logline once a second and
596 * return the pointer to the first character after the header.
597 */
598static char *update_log_hdr()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200599{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100600 static long tvsec;
601 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200602
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200603 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200604 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200605 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100606 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200607
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200608 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200609 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100611 hdr_len = snprintf(logline, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100612 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200613 monthname[tm.tm_mon],
614 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100615 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000616 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200617 /* WARNING: depending upon implementations, snprintf may return
618 * either -1 or the number of bytes that would be needed to store
619 * the total message. In both cases, we must adjust it.
620 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100621 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
622 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200623
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100624 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625 }
626
William Lallemand2a4a44f2012-02-06 16:00:33 +0100627 return dataptr;
628}
629
630/*
631 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100632 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100633 */
634void send_log(struct proxy *p, int level, const char *format, ...)
635{
636 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100637 char *dataptr;
638 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100639
640 if (level < 0 || format == NULL)
641 return;
642
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100643 dataptr = update_log_hdr(); /* update log header and skip it */
644 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100645
646 va_start(argp, format);
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100647 data_len += vsnprintf(dataptr, logline + sizeof(logline) - dataptr, format, argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100648 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
649 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100651
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100652 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100653}
654
655/*
656 * This function sends a syslog message.
657 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100658 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100659 */
660void __send_log(struct proxy *p, int level, char *message, size_t size)
661{
662 static int logfdunix = -1; /* syslog to AF_UNIX socket */
663 static int logfdinet = -1; /* syslog to AF_INET socket */
664 static char *dataptr = NULL;
665 int fac_level;
666 struct list *logsrvs = NULL;
667 struct logsrv *tmp = NULL;
668 int nblogger;
669 char *log_ptr;
670
671 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672
673 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200674 if (!LIST_ISEMPTY(&global.logsrvs)) {
675 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 }
677 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200678 if (!LIST_ISEMPTY(&p->logsrvs)) {
679 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100681 }
682
William Lallemand0f99e342011-10-12 17:50:54 +0200683 if (!logsrvs)
684 return;
685
William Lallemand2a4a44f2012-02-06 16:00:33 +0100686 message[size - 1] = '\n';
687
Robert Tsai81ae1952007-12-05 10:47:29 +0100688 /* Lazily set up syslog sockets for protocol families of configured
689 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200690 nblogger = 0;
691 list_for_each_entry(tmp, logsrvs, list) {
692 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100693 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200694
David du Colombier11bcb6c2011-03-24 12:23:00 +0100695 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100696 proto = 0;
697 plogfd = &logfdunix;
698 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100699 proto = IPPROTO_UDP;
700 plogfd = &logfdinet;
701 }
702 if (*plogfd >= 0) {
703 /* socket already created. */
704 continue;
705 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100706 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100707 proto)) < 0) {
708 Alert("socket for logger #%d failed: %s (errno=%d)\n",
709 nblogger + 1, strerror(errno), errno);
710 return;
711 }
712 /* we don't want to receive anything on this socket */
713 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
714 /* does nothing under Linux, maybe needed for others */
715 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200716 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717 }
718
Robert Tsai81ae1952007-12-05 10:47:29 +0100719 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200720 nblogger = 0;
721 list_for_each_entry(tmp, logsrvs, list) {
722 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100723 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100724 &logfdunix : &logfdinet;
725 int sent;
726
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200728 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100730
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731 /* For each target, we may have a different facility.
732 * We can also have a different log level for each message.
733 * This induces variations in the message header length.
734 * Since we don't want to recompute it each time, nor copy it every
735 * time, we only change the facility in the pre-computed header,
736 * and we change the pointer to the header accordingly.
737 */
William Lallemand0f99e342011-10-12 17:50:54 +0200738 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100739 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740 do {
741 *log_ptr = '0' + fac_level % 10;
742 fac_level /= 10;
743 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100744 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100746
William Lallemandbfb099c2012-03-19 16:15:12 +0100747 sent = sendto(*plogfd, log_ptr, size + log_ptr - dataptr,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200748 MSG_DONTWAIT | MSG_NOSIGNAL,
749 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100750 if (sent < 0) {
751 Alert("sendto logger #%d failed: %s (errno=%d)\n",
752 nblogger, strerror(errno), errno);
753 }
William Lallemand0f99e342011-10-12 17:50:54 +0200754 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755 }
756}
757
William Lallemandbddd4fd2012-02-27 11:23:10 +0100758extern fd_set hdr_encode_map[];
759extern fd_set url_encode_map[];
760
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200762const 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 +0100763const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
764 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
765 Set-cookie Updated, unknown, unknown */
766
William Lallemand1d705562012-03-12 12:46:41 +0100767/*
768 * try to write a character if there is enough space, or goto out
769 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100770#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100771 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100772 *(tmplog++) = (x); \
773 } else { \
774 goto out; \
775 } \
776 } while(0)
777
William Lallemand1d705562012-03-12 12:46:41 +0100778
779
780int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200781{
Willy Tarreau73de9892006-11-30 11:40:23 +0100782 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100783 struct proxy *be = s->be;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100784 struct http_txn *txn = &s->txn;
William Lallemand1d705562012-03-12 12:46:41 +0100785 int tolog;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100786 char *uri;
787 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200788 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100789 int t_request;
790 int hdr;
791 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100792 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100793 char *ret;
794 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100795 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796
William Lallemandbddd4fd2012-02-27 11:23:10 +0100797 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreau42250582007-04-01 01:30:43 +0200798 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100799
800 if (!(tolog & LW_SVID))
801 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200802 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100803 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200804 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100805 break;
806 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200807 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100808 break;
809 default:
810 svid = "<NOSRV>";
811 break;
812 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813
William Lallemandbddd4fd2012-02-27 11:23:10 +0100814 t_request = -1;
815 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
816 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
817
William Lallemand1d705562012-03-12 12:46:41 +0100818 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200819
William Lallemandbddd4fd2012-02-27 11:23:10 +0100820 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100821 if (LIST_ISEMPTY(list_format))
822 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100823
William Lallemand1d705562012-03-12 12:46:41 +0100824 list_for_each_entry(tmp, list_format, list) {
Willy Tarreau4f653562012-10-12 19:48:16 +0200825 const char *src = NULL;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100826 switch (tmp->type) {
827
William Lallemand1d705562012-03-12 12:46:41 +0100828 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100829 if (!last_isspace) {
830 LOGCHAR(' ');
831 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100832 }
833 break;
834
William Lallemand1d705562012-03-12 12:46:41 +0100835 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100836 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200837 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100838 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100839 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100840 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100841 last_isspace = 0;
842 break;
843
William Lallemand1d705562012-03-12 12:46:41 +0100844 case LOG_FMT_CLIENTIP: // %Ci
Willy Tarreau986a9d22012-08-30 21:11:38 +0200845 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->prod->conn.addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200846 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100847 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100848 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100849 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100850 last_isspace = 0;
851 break;
852
William Lallemand1d705562012-03-12 12:46:41 +0100853 case LOG_FMT_CLIENTPORT: // %Cp
Willy Tarreau986a9d22012-08-30 21:11:38 +0200854 if (s->req->prod->conn.addr.from.ss_family == AF_UNIX) {
William Lallemand5f232402012-04-05 18:02:55 +0200855 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
856 } else {
Willy Tarreau986a9d22012-08-30 21:11:38 +0200857 ret = lf_port(tmplog, (struct sockaddr *)&s->req->prod->conn.addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200858 dst + maxsize - tmplog, tmp);
859 }
860 if (ret == NULL)
861 goto out;
862 tmplog = ret;
863 last_isspace = 0;
864 break;
865
866 case LOG_FMT_FRONTENDIP: // %Fi
Willy Tarreau986a9d22012-08-30 21:11:38 +0200867 conn_get_to_addr(&s->req->prod->conn);
868 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->prod->conn.addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200869 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100870 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100871 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100872 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100873 last_isspace = 0;
874 break;
875
William Lallemand5f232402012-04-05 18:02:55 +0200876 case LOG_FMT_FRONTENDPORT: // %Fp
Willy Tarreau986a9d22012-08-30 21:11:38 +0200877 conn_get_to_addr(&s->req->prod->conn);
878 if (s->req->prod->conn.addr.to.ss_family == AF_UNIX) {
William Lallemand5f232402012-04-05 18:02:55 +0200879 ret = ltoa_o(s->listener->luid,
880 tmplog, dst + maxsize - tmplog);
881 } else {
Willy Tarreau986a9d22012-08-30 21:11:38 +0200882 ret = lf_port(tmplog, (struct sockaddr *)&s->req->prod->conn.addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200883 dst + maxsize - tmplog, tmp);
884 }
885 if (ret == NULL)
886 goto out;
887 tmplog = ret;
888 last_isspace = 0;
889 break;
890
William Lallemand1d705562012-03-12 12:46:41 +0100891 case LOG_FMT_BACKENDIP: // %Bi
Willy Tarreau986a9d22012-08-30 21:11:38 +0200892 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->cons->conn.addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200893 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100894 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +0100895 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100896 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +0100897 last_isspace = 0;
898 break;
899
William Lallemand1d705562012-03-12 12:46:41 +0100900 case LOG_FMT_BACKENDPORT: // %Bp
Willy Tarreau986a9d22012-08-30 21:11:38 +0200901 ret = lf_port(tmplog, (struct sockaddr *)&s->req->cons->conn.addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200902 dst + maxsize - tmplog, tmp);
903 if (ret == NULL)
904 goto out;
905 tmplog = ret;
906 last_isspace = 0;
907 break;
908
909 case LOG_FMT_SERVERIP: // %Si
Willy Tarreau986a9d22012-08-30 21:11:38 +0200910 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->cons->conn.addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200911 dst + maxsize - tmplog, tmp);
912 if (ret == NULL)
913 goto out;
914 tmplog = ret;
915 last_isspace = 0;
916 break;
917
918 case LOG_FMT_SERVERPORT: // %Sp
Willy Tarreau986a9d22012-08-30 21:11:38 +0200919 ret = lf_port(tmplog, (struct sockaddr *)&s->req->cons->conn.addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200920 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100921 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +0100922 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100923 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +0100924 last_isspace = 0;
925 break;
926
William Lallemand1d705562012-03-12 12:46:41 +0100927 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +0100928 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +0200929 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
930 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100931 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100932 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100933 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100934 last_isspace = 0;
935 break;
936
William Lallemand1d705562012-03-12 12:46:41 +0100937 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +0100938 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +0200939 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100940 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100941 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100942 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100943 last_isspace = 0;
944 break;
945
William Lallemand5f232402012-04-05 18:02:55 +0200946 case LOG_FMT_TS: // %Ts
947 get_gmtime(s->logs.accept_date.tv_sec, &tm);
948 if (tmp->options & LOG_OPT_HEXA) {
949 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
950 if (iret < 0 || iret > dst + maxsize - tmplog)
951 goto out;
952 last_isspace = 0;
953 tmplog += iret;
954 } else {
955 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
956 if (ret == NULL)
957 goto out;
958 tmplog = ret;
959 last_isspace = 0;
960 }
961 break;
962
William Lallemand1d705562012-03-12 12:46:41 +0100963 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +0200964 if (tmp->options & LOG_OPT_HEXA) {
965 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
966 if (iret < 0 || iret > dst + maxsize - tmplog)
967 goto out;
968 last_isspace = 0;
969 tmplog += iret;
970 } else {
971 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100972 goto out;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100973 tmplog = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
974 tmplog, 4);
William Lallemand51b5dca2012-03-26 17:52:55 +0200975 if (!tmplog)
976 goto out;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100977 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +0200978 }
979 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100980
William Lallemand1d705562012-03-12 12:46:41 +0100981 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +0100982 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +0200983 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100984 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100985 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100986 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +0200987 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100988 break;
989
Willy Tarreau773d65f2012-10-12 14:56:11 +0200990 case LOG_FMT_FRONTEND_XPRT: // %ft
991 src = fe->id;
992 if (tmp->options & LOG_OPT_QUOTE)
993 LOGCHAR('"');
994 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
995 if (iret == 0)
996 goto out;
997 tmplog += iret;
998#ifdef USE_OPENSSL
999 if (s->listener->xprt == &ssl_sock)
1000 LOGCHAR('~');
1001#endif
1002 if (tmp->options & LOG_OPT_QUOTE)
1003 LOGCHAR('"');
1004 last_isspace = 0;
1005 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001006#ifdef USE_OPENSSL
1007 case LOG_FMT_SSL_CIPHER: // %sslc
1008 src = NULL;
1009 if (s->listener->xprt == &ssl_sock)
1010 src = ssl_sock_get_cipher_name(&s->si[0].conn);
1011 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1012 if (ret == NULL)
1013 goto out;
1014 tmplog = ret;
1015 last_isspace = 0;
1016 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001017
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001018 case LOG_FMT_SSL_VERSION: // %sslv
1019 src = NULL;
1020 if (s->listener->xprt == &ssl_sock)
1021 src = ssl_sock_get_proto_version(&s->si[0].conn);
1022 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1023 if (ret == NULL)
1024 goto out;
1025 tmplog = ret;
1026 last_isspace = 0;
1027 break;
1028#endif
William Lallemand1d705562012-03-12 12:46:41 +01001029 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001030 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001031 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
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 Lallemand51b5dca2012-03-26 17:52:55 +02001035 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001036 break;
1037
William Lallemand1d705562012-03-12 12:46:41 +01001038 case LOG_FMT_SERVER: // %s
William Lallemandbddd4fd2012-02-27 11:23:10 +01001039 src = (char *)svid;
William Lallemand5f232402012-04-05 18:02:55 +02001040 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001041 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001042 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001043 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001044 last_isspace = 0;
1045 break;
1046
William Lallemand1d705562012-03-12 12:46:41 +01001047 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001048 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001049 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001050 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001051 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001052 last_isspace = 0;
1053 break;
1054
William Lallemand1d705562012-03-12 12:46:41 +01001055 case LOG_FMT_TW: // %Tw
1056 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001057 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001058 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001059 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001060 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001061 last_isspace = 0;
1062 break;
1063
William Lallemand1d705562012-03-12 12:46:41 +01001064 case LOG_FMT_TC: // %Tc
1065 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001066 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001067 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001068 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001069 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001070 last_isspace = 0;
1071 break;
1072
William Lallemand1d705562012-03-12 12:46:41 +01001073 case LOG_FMT_TR: // %Tr
1074 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001075 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001076 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001077 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001078 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001079 last_isspace = 0;
1080 break;
1081
William Lallemand1d705562012-03-12 12:46:41 +01001082 case LOG_FMT_TT: // %Tt
William Lallemandbddd4fd2012-02-27 11:23:10 +01001083 if (!(tolog & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001084 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001085 ret = ltoa_o(s->logs.t_close, 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_STATUS: // %st
William Lallemand5f232402012-04-05 18:02:55 +02001093 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001094 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001095 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001096 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001097 last_isspace = 0;
1098 break;
1099
William Lallemand1d705562012-03-12 12:46:41 +01001100 case LOG_FMT_BYTES: // %B
William Lallemandbddd4fd2012-02-27 11:23:10 +01001101 if (!(tolog & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001102 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001103 ret = lltoa(s->logs.bytes_out, 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_CCLIENT: // %cc
William Lallemandbddd4fd2012-02-27 11:23:10 +01001111 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001112 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001113 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001114 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001115 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001116 last_isspace = 0;
1117 break;
1118
William Lallemand1d705562012-03-12 12:46:41 +01001119 case LOG_FMT_CSERVER: // %cs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001120 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001121 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001122 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001123 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001124 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001125 last_isspace = 0;
1126 break;
1127
William Lallemand1d705562012-03-12 12:46:41 +01001128 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +01001129 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1130 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
1131 *tmplog = '\0';
1132 last_isspace = 0;
1133 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001134
William Lallemand1d705562012-03-12 12:46:41 +01001135 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001136 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1137 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001138 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1139 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 +01001140 last_isspace = 0;
1141 break;
1142
William Lallemand1d705562012-03-12 12:46:41 +01001143 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001144 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001145 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001146 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001147 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001148 last_isspace = 0;
1149 break;
1150
William Lallemand1d705562012-03-12 12:46:41 +01001151 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001152 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001153 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001154 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001155 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001156 last_isspace = 0;
1157 break;
1158
William Lallemand1d705562012-03-12 12:46:41 +01001159 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001160 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001161 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001162 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001163 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001164 last_isspace = 0;
1165 break;
1166
William Lallemand1d705562012-03-12 12:46:41 +01001167 case LOG_FMT_SRVCONN: // %sc
1168 ret = ultoa_o(target_srv(&s->target) ?
1169 target_srv(&s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001170 0, 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_RETRIES: // %rq
William Lallemandbddd4fd2012-02-27 11:23:10 +01001178 if (s->flags & SN_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001179 LOGCHAR('+');
1180 ret = ltoa_o((s->req->cons->conn_retries>0) ?
1181 (be->conn_retries - s->req->cons->conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001182 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001183 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001184 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001185 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001186 last_isspace = 0;
1187 break;
1188
William Lallemand1d705562012-03-12 12:46:41 +01001189 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001190 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001191 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001192 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001193 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001194 last_isspace = 0;
1195 break;
1196
William Lallemand1d705562012-03-12 12:46:41 +01001197 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001198 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001199 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001200 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001201 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001202 last_isspace = 0;
1203 break;
1204
William Lallemand1d705562012-03-12 12:46:41 +01001205 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001206 /* request header */
1207 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1208 if (tmp->options & LOG_OPT_QUOTE)
1209 LOGCHAR('"');
1210 LOGCHAR('{');
1211 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1212 if (hdr)
1213 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001214 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001215 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001216 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001217 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001218 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001219 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001220 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001221 }
1222 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001223 if (tmp->options & LOG_OPT_QUOTE)
1224 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001225 last_isspace = 0;
William Lallemand1d705562012-03-12 12:46:41 +01001226 if (tmp->options & LOG_OPT_QUOTE)
1227 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001228 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001229 break;
1230
William Lallemand1d705562012-03-12 12:46:41 +01001231 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001232 /* request header list */
1233 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1234 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1235 if (hdr > 0)
1236 LOGCHAR(' ');
1237 if (tmp->options & LOG_OPT_QUOTE)
1238 LOGCHAR('"');
1239 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001240 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001241 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001242 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001243 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001244 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001245 } else if (!(tmp->options & LOG_OPT_QUOTE))
1246 LOGCHAR('-');
1247 if (tmp->options & LOG_OPT_QUOTE)
1248 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001249 last_isspace = 0;
1250 }
1251 }
1252 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001253
William Lallemand1d705562012-03-12 12:46:41 +01001254
1255 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001256 /* response header */
1257 if (fe->to_log & LW_RSPHDR &&
1258 txn->rsp.cap) {
1259 if (tmp->options & LOG_OPT_QUOTE)
1260 LOGCHAR('"');
1261 LOGCHAR('{');
1262 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1263 if (hdr)
1264 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001265 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001266 ret = encode_string(tmplog, dst + maxsize,
1267 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1268 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001269 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001270 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001271 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001272 }
1273 LOGCHAR('}');
1274 last_isspace = 0;
1275 if (tmp->options & LOG_OPT_QUOTE)
1276 LOGCHAR('"');
1277 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001278 break;
1279
William Lallemand1d705562012-03-12 12:46:41 +01001280 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001281 /* response header list */
1282 if (fe->to_log & LW_RSPHDR && txn->rsp.cap) {
1283 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1284 if (hdr > 0)
1285 LOGCHAR(' ');
1286 if (tmp->options & LOG_OPT_QUOTE)
1287 LOGCHAR('"');
1288 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001289 ret = encode_string(tmplog, dst + maxsize,
1290 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1291 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001292 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001293 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001294 } else if (!(tmp->options & LOG_OPT_QUOTE))
1295 LOGCHAR('-');
1296 if (tmp->options & LOG_OPT_QUOTE)
1297 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001298 last_isspace = 0;
1299 }
1300 }
1301 break;
1302
William Lallemand1d705562012-03-12 12:46:41 +01001303 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001304 /* Request */
1305 if (tmp->options & LOG_OPT_QUOTE)
1306 LOGCHAR('"');
1307 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001308 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001309 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001310 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001311 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001312 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001313 if (tmp->options & LOG_OPT_QUOTE)
1314 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001315 last_isspace = 0;
1316 break;
William Lallemand5f232402012-04-05 18:02:55 +02001317
1318 case LOG_FMT_COUNTER: // %rt
1319 if (tmp->options & LOG_OPT_HEXA) {
1320 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", global.req_count);
1321 if (iret < 0 || iret > dst + maxsize - tmplog)
1322 goto out;
1323 last_isspace = 0;
1324 tmplog += iret;
1325 } else {
1326 ret = ltoa_o(global.req_count, tmplog, dst + maxsize - tmplog);
1327 if (ret == NULL)
1328 goto out;
1329 tmplog = ret;
1330 last_isspace = 0;
1331 }
1332 break;
1333
1334 case LOG_FMT_HOSTNAME: // %H
1335 src = hostname;
1336 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1337 if (ret == NULL)
1338 goto out;
1339 tmplog = ret;
1340 last_isspace = 0;
1341 break;
1342
1343 case LOG_FMT_PID: // %pid
1344 if (tmp->options & LOG_OPT_HEXA) {
1345 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1346 if (iret < 0 || iret > dst + maxsize - tmplog)
1347 goto out;
1348 last_isspace = 0;
1349 tmplog += iret;
1350 } else {
1351 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1352 if (ret == NULL)
1353 goto out;
1354 tmplog = ret;
1355 last_isspace = 0;
1356 }
1357 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001358
1359 case LOG_FMT_UNIQUEID: // %ID
1360 src = s->unique_id;
1361 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
1362 if (ret == NULL)
1363 goto out;
1364 tmplog = ret;
1365 last_isspace = 0;
1366 break;
1367
William Lallemandbddd4fd2012-02-27 11:23:10 +01001368 }
1369 }
1370
1371out:
William Lallemand1d705562012-03-12 12:46:41 +01001372 /* *tmplog is a unused character */
1373 *tmplog = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001374
William Lallemand1d705562012-03-12 12:46:41 +01001375 return tmplog - dst + 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001376
Willy Tarreaubaaee002006-06-26 02:48:02 +02001377}
1378
William Lallemand1d705562012-03-12 12:46:41 +01001379/*
1380 * send a log for the session when we have enough info about it.
1381 * Will not log if the frontend has no log defined.
1382 */
1383void sess_log(struct session *s)
1384{
1385 char *tmplog;
1386 int size, err, level;
1387
1388 /* if we don't want to log normal traffic, return now */
1389 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1390 (s->req->cons->conn_retries != s->be->conn_retries) ||
1391 ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001392
William Lallemand1d705562012-03-12 12:46:41 +01001393 if (!err && (s->fe->options2 & PR_O2_NOLOGNORM))
1394 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001395
William Lallemand1d705562012-03-12 12:46:41 +01001396 if (LIST_ISEMPTY(&s->fe->logsrvs))
1397 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001398
William Lallemand1d705562012-03-12 12:46:41 +01001399 level = LOG_INFO;
1400 if (err && (s->fe->options2 & PR_O2_LOGERRORS))
1401 level = LOG_ERR;
1402
1403 tmplog = update_log_hdr();
1404 size = tmplog - logline;
1405 size += build_logline(s, tmplog, sizeof(logline) - size, &s->fe->logformat);
1406 if (size > 0) {
1407 __send_log(s->fe, level, logline, size);
1408 s->logs.logwait = 0;
1409 }
1410}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001411
Willy Tarreaubaaee002006-06-26 02:48:02 +02001412/*
1413 * Local variables:
1414 * c-indent-level: 8
1415 * c-basic-offset: 8
1416 * End:
1417 */