blob: ce1b2a6919f8584528497581946c9cd06ecbce04 [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 */
116 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100117};
118
Willy Tarreau773d65f2012-10-12 14:56:11 +0200119char 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
120char 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";
121char 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 +0100122char *log_format = NULL;
123
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100124/* This is a global syslog line, common to all outgoing messages. It begins
125 * with the syslog tag and the date that are updated by update_log_hdr().
126 */
127static char logline[MAX_SYSLOG_LEN];
128
William Lallemand723b73a2012-02-08 16:37:49 +0100129struct logformat_var_args {
130 char *name;
131 int mask;
132};
133
134struct logformat_var_args var_args_list[] = {
135// global
136 { "M", LOG_OPT_MANDATORY },
137 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200138 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100139 { 0, 0 }
140};
141
142/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100143 * callback used to configure addr source retrieval
144 */
145int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
146{
147 curproxy->options2 |= PR_O2_SRC_ADDR;
148
149 return 0;
150}
151
152
153/*
William Lallemand723b73a2012-02-08 16:37:49 +0100154 * Parse args in a logformat_var
155 */
156int parse_logformat_var_args(char *args, struct logformat_node *node)
157{
158 int i = 0;
159 int end = 0;
160 int flags = 0; // 1 = + 2 = -
161 char *sp = NULL; // start pointer
162
163 if (args == NULL)
164 return 1;
165
166 while (1) {
167 if (*args == '\0')
168 end = 1;
169
170 if (*args == '+') {
171 // add flag
172 sp = args + 1;
173 flags = 1;
174 }
175 if (*args == '-') {
176 // delete flag
177 sp = args + 1;
178 flags = 2;
179 }
180
181 if (*args == '\0' || *args == ',') {
182 *args = '\0';
183 for (i = 0; var_args_list[i].name; i++) {
184 if (strcmp(sp, var_args_list[i].name) == 0) {
185 if (flags == 1) {
186 node->options |= var_args_list[i].mask;
187 break;
188 } else if (flags == 2) {
189 node->options &= ~var_args_list[i].mask;
190 break;
191 }
192 }
193 }
194 sp = NULL;
195 if (end)
196 break;
197 }
198 args++;
199 }
200 return 0;
201}
202
203/*
204 * Parse a variable '%varname' or '%{args}varname' in logformat
205 *
206 */
William Lallemand1d705562012-03-12 12:46:41 +0100207int parse_logformat_var(char *str, size_t len, struct proxy *curproxy, struct list *list_format, int *defoptions)
William Lallemand723b73a2012-02-08 16:37:49 +0100208{
209 int i, j;
210 char *arg = NULL; // arguments
211 int fparam = 0;
212 char *name = NULL;
213 struct logformat_node *node = NULL;
214 char varname[255] = { 0 }; // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100215
William Lallemand723b73a2012-02-08 16:37:49 +0100216 for (i = 1; i < len; i++) { // escape first char %
217 if (!arg && str[i] == '{') {
218 arg = str + i;
219 fparam = 1;
220 } else if (arg && str[i] == '}') {
221 char *tmp = arg;
222 arg = calloc(str + i - tmp, 1); // without {}
223 strncpy(arg, tmp + 1, str + i - tmp - 1); // copy without { and }
224 arg[str + i - tmp - 1] = '\0';
225 fparam = 0;
226 } else if (!name && !fparam) {
227 strncpy(varname, str + i, len - i + 1);
228 varname[len - i] = '\0';
229 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
230 if (strcmp(varname, logformat_keywords[j].name) == 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100231 if (!((logformat_keywords[j].mode == PR_MODE_HTTP) && (curproxy->mode == PR_MODE_TCP))) {
232 node = calloc(1, sizeof(struct logformat_node));
233 node->type = logformat_keywords[j].type;
William Lallemand81f51172012-03-12 11:03:47 +0100234 node->options = *defoptions;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100235 node->arg = arg;
236 parse_logformat_var_args(node->arg, node);
William Lallemand1d705562012-03-12 12:46:41 +0100237 if (node->type == LOG_FMT_GLOBAL) {
William Lallemand81f51172012-03-12 11:03:47 +0100238 *defoptions = node->options;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100239 free(node);
240 } else {
William Lallemandb7ff6a32012-03-02 14:35:21 +0100241 if (logformat_keywords[j].config_callback != NULL) {
242 if (logformat_keywords[j].config_callback(node, curproxy) != 0) {
243 return -1;
244 }
245 }
William Lallemand5e19a282012-04-02 16:22:10 +0200246 curproxy->to_log |= logformat_keywords[j].lw;
William Lallemand1d705562012-03-12 12:46:41 +0100247 LIST_ADDQ(list_format, &node->list);
William Lallemandbddd4fd2012-02-27 11:23:10 +0100248 }
249 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100250 } else {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100251 Warning("Warning: No such variable name '%s' in this log mode\n", varname);
252 if (arg)
253 free(arg);
254 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100255 }
William Lallemand723b73a2012-02-08 16:37:49 +0100256 }
257 }
258 Warning("Warning: No such variable name '%s' in logformat\n", varname);
259 if (arg)
260 free(arg);
261 return -1;
262 }
263 }
264 return -1;
265}
266
267/*
268 * push to the logformat linked list
269 *
270 * start: start pointer
271 * end: end text pointer
272 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100273 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100274 *
275 * LOG_TEXT: copy chars from start to end excluding end.
276 *
277*/
William Lallemand1d705562012-03-12 12:46:41 +0100278void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100279{
280 char *str;
281
William Lallemand1d705562012-03-12 12:46:41 +0100282 if (type == LOG_FMT_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100283 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100284 str = calloc(end - start + 1, 1);
285 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100286 str[end - start] = '\0';
287 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100288 node->type = LOG_FMT_TEXT; // type string
289 LIST_ADDQ(list_format, &node->list);
290 } else if (type == LOG_FMT_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100291 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100292 node->type = LOG_FMT_SEPARATOR;
293 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100294 }
295}
296
297/*
298 * Parse the log_format string and fill a linked list.
299 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
300 * You can set arguments using { } : %{many arguments}varname
William Lallemand1d705562012-03-12 12:46:41 +0100301 *
302 * str: the string to parse
303 * curproxy: the proxy affected
304 * list_format: the destination list
305 * capabilities: PR_MODE_TCP_ | PR_MODE_HTTP
William Lallemand723b73a2012-02-08 16:37:49 +0100306 */
William Lallemand1d705562012-03-12 12:46:41 +0100307void parse_logformat_string(char *str, struct proxy *curproxy, struct list *list_format, int capabilities)
William Lallemand723b73a2012-02-08 16:37:49 +0100308{
309 char *sp = str; /* start pointer */
310 int cformat = -1; /* current token format : LOG_TEXT, LOG_SEPARATOR, LOG_VARIABLE */
311 int pformat = -1; /* previous token format */
312 struct logformat_node *tmplf, *back;
William Lallemand81f51172012-03-12 11:03:47 +0100313 int options = 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100314
William Lallemand1dc00ef2012-08-09 16:41:35 +0200315 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200316
William Lallemand723b73a2012-02-08 16:37:49 +0100317 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100318 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100319 LIST_DEL(&tmplf->list);
320 free(tmplf);
321 }
322
323 while (1) {
324
325 // push the variable only if formats are different, not
326 // within a variable, and not the first iteration
327 if ((cformat != pformat && cformat != -1 && pformat != -1) || *str == '\0') {
328 if (((pformat != LF_STARTVAR && cformat != LF_VAR) &&
329 (pformat != LF_STARTVAR && cformat != LF_STARG) &&
330 (pformat != LF_STARG && cformat != LF_VAR)) || *str == '\0') {
331 if (pformat > LF_VAR) // unfinished string
332 pformat = LF_TEXT;
William Lallemand81f51172012-03-12 11:03:47 +0100333 if (pformat == LF_VAR)
William Lallemand1d705562012-03-12 12:46:41 +0100334 parse_logformat_var(sp, str - sp, curproxy, list_format, &options);
William Lallemand81f51172012-03-12 11:03:47 +0100335 else
William Lallemand1d705562012-03-12 12:46:41 +0100336 add_to_logformat_list(sp, str, pformat, list_format);
William Lallemand723b73a2012-02-08 16:37:49 +0100337 sp = str;
338 if (*str == '\0')
339 break;
340 }
341 }
342
343 if (cformat != -1)
344 str++; // consume the string, except on the first tour
345
346 pformat = cformat;
347
348 if (*str == '\0') {
349 cformat = LF_STARTVAR; // for breaking in all cases
350 continue;
351 }
352
353 if (pformat == LF_STARTVAR) { // after a %
354 if ( (*str >= 'a' && *str <= 'z') || // parse varname
355 (*str >= 'A' && *str <= 'Z') ||
356 (*str >= '0' && *str <= '9')) {
357 cformat = LF_VAR; // varname
358 continue;
359 } else if (*str == '{') {
360 cformat = LF_STARG; // variable arguments
361 continue;
362 } else { // another unexpected token
363 pformat = LF_TEXT; // redefine the format of the previous token to TEXT
364 cformat = LF_TEXT;
365 continue;
366 }
367
368 } else if (pformat == LF_VAR) { // after a varname
369 if ( (*str >= 'a' && *str <= 'z') || // parse varname
370 (*str >= 'A' && *str <= 'Z') ||
371 (*str >= '0' && *str <= '9')) {
372 cformat = LF_VAR;
373 continue;
374 }
375 } else if (pformat == LF_STARG) { // inside variable arguments
376 if (*str == '}') { // end of varname
377 cformat = LF_EDARG;
378 continue;
379 } else { // all tokens are acceptable within { }
380 cformat = LF_STARG;
381 continue;
382 }
383 } else if (pformat == LF_EDARG) { // after arguments
384 if ( (*str >= 'a' && *str <= 'z') || // parse a varname
385 (*str >= 'A' && *str <= 'Z') ||
386 (*str >= '0' && *str <= '9')) {
387 cformat = LF_VAR;
388 continue;
389 } else { // if no varname after arguments, transform in TEXT
390 pformat = LF_TEXT;
391 cformat = LF_TEXT;
392 }
393 }
394
395 // others tokens that don't match previous conditions
396 if (*str == '%') {
397 cformat = LF_STARTVAR;
398 } else if (*str == ' ') {
399 cformat = LF_SEPARATOR;
400 } else {
401 cformat = LF_TEXT;
402 }
403 }
404}
405
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406/*
407 * Displays the message on stderr with the date and pid. Overrides the quiet
408 * mode during startup.
409 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200410void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411{
412 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200413 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414
415 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
416 va_start(argp, fmt);
417
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200418 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200419 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200420 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200421 vfprintf(stderr, fmt, argp);
422 fflush(stderr);
423 va_end(argp);
424 }
425}
426
427
428/*
429 * Displays the message on stderr with the date and pid.
430 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200431void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200432{
433 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200434 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435
436 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
437 va_start(argp, fmt);
438
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200439 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200441 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442 vfprintf(stderr, fmt, argp);
443 fflush(stderr);
444 va_end(argp);
445 }
446}
447
448/*
449 * Displays the message on <out> only if quiet mode is not set.
450 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200451void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200452{
453 va_list argp;
454
455 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
456 va_start(argp, fmt);
457 vfprintf(out, fmt, argp);
458 fflush(out);
459 va_end(argp);
460 }
461}
462
463/*
464 * returns log level for <lev> or -1 if not found.
465 */
466int get_log_level(const char *lev)
467{
468 int level;
469
470 level = NB_LOG_LEVELS - 1;
471 while (level >= 0 && strcmp(log_levels[level], lev))
472 level--;
473
474 return level;
475}
476
477
478/*
479 * returns log facility for <fac> or -1 if not found.
480 */
481int get_log_facility(const char *fac)
482{
483 int facility;
484
485 facility = NB_LOG_FACILITIES - 1;
486 while (facility >= 0 && strcmp(log_facilities[facility], fac))
487 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100488
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489 return facility;
490}
491
William Lallemanda1cc3812012-02-08 16:38:44 +0100492/*
493 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200494 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100495 *
496 * Return the adress of the \0 character, or NULL on error
497 */
William Lallemand5f232402012-04-05 18:02:55 +0200498char *lf_text(char *dst, char *src, size_t size, struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +0100499{
William Lallemand51b5dca2012-03-26 17:52:55 +0200500 int n;
William Lallemanda1cc3812012-02-08 16:38:44 +0100501
William Lallemandbddd4fd2012-02-27 11:23:10 +0100502 if (src == NULL || *src == '\0') {
503 if (node->options & LOG_OPT_QUOTE) {
504 if (size > 2) {
505 *(dst++) = '"';
506 *(dst++) = '"';
507 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100508 } else {
509 dst = NULL;
510 return dst;
511 }
512 } else {
513 if (size > 1) {
514 *(dst++) = '-';
515 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100516 } else { // error no space available
517 dst = NULL;
518 return dst;
519 }
520 }
521 } else {
522 if (node->options & LOG_OPT_QUOTE) {
523 if (size-- > 1 ) {
524 *(dst++) = '"';
525 } else {
526 dst = NULL;
527 return NULL;
528 }
William Lallemand51b5dca2012-03-26 17:52:55 +0200529 n = strlcpy2(dst, src, size);
530 size -= n;
531 dst += n;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100532 if (size > 1) {
533 *(dst++) = '"';
534 *dst = '\0';
535 } else {
536 dst = NULL;
537 }
538 } else {
539 dst += strlcpy2(dst, src, size);
540 }
541 }
542 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100543}
544
William Lallemand5f232402012-04-05 18:02:55 +0200545/*
546 * Write a IP adress to the log string
547 * +X option write in hexadecimal notation, most signifant byte on the left
548 */
549char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
550{
551 char *ret = dst;
552 int iret;
553 char pn[INET6_ADDRSTRLEN];
554
555 if (node->options & LOG_OPT_HEXA) {
556 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
557 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
558 if (iret < 0 || iret > size)
559 return NULL;
560 ret += iret;
561 } else {
562 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
563 ret = lf_text(dst, pn, size, node);
564 if (ret == NULL)
565 return NULL;
566 }
567 return ret;
568}
569
570/*
571 * Write a port to the log
572 * +X option write in hexadecimal notation, most signifant byte on the left
573 */
574char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
575{
576 char *ret = dst;
577 int iret;
578
579 if (node->options & LOG_OPT_HEXA) {
580 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
581 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
582 if (iret < 0 || iret > size)
583 return NULL;
584 ret += iret;
585 } else {
586 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
587 if (ret == NULL)
588 return NULL;
589 }
590 return ret;
591}
592
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100593/* Re-generate the syslog header at the beginning of logline once a second and
594 * return the pointer to the first character after the header.
595 */
596static char *update_log_hdr()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200597{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100598 static long tvsec;
599 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200600
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200601 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200602 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200603 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100604 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200605
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200606 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200607 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200608
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100609 hdr_len = snprintf(logline, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100610 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200611 monthname[tm.tm_mon],
612 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100613 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000614 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200615 /* WARNING: depending upon implementations, snprintf may return
616 * either -1 or the number of bytes that would be needed to store
617 * the total message. In both cases, we must adjust it.
618 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100619 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
620 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100622 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200623 }
624
William Lallemand2a4a44f2012-02-06 16:00:33 +0100625 return dataptr;
626}
627
628/*
629 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100630 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100631 */
632void send_log(struct proxy *p, int level, const char *format, ...)
633{
634 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100635 char *dataptr;
636 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100637
638 if (level < 0 || format == NULL)
639 return;
640
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100641 dataptr = update_log_hdr(); /* update log header and skip it */
642 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100643
644 va_start(argp, format);
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100645 data_len += vsnprintf(dataptr, logline + sizeof(logline) - dataptr, format, argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100646 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
647 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100649
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100650 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100651}
652
653/*
654 * This function sends a syslog message.
655 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100656 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100657 */
658void __send_log(struct proxy *p, int level, char *message, size_t size)
659{
660 static int logfdunix = -1; /* syslog to AF_UNIX socket */
661 static int logfdinet = -1; /* syslog to AF_INET socket */
662 static char *dataptr = NULL;
663 int fac_level;
664 struct list *logsrvs = NULL;
665 struct logsrv *tmp = NULL;
666 int nblogger;
667 char *log_ptr;
668
669 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670
671 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200672 if (!LIST_ISEMPTY(&global.logsrvs)) {
673 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 }
675 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200676 if (!LIST_ISEMPTY(&p->logsrvs)) {
677 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100679 }
680
William Lallemand0f99e342011-10-12 17:50:54 +0200681 if (!logsrvs)
682 return;
683
William Lallemand2a4a44f2012-02-06 16:00:33 +0100684 message[size - 1] = '\n';
685
Robert Tsai81ae1952007-12-05 10:47:29 +0100686 /* Lazily set up syslog sockets for protocol families of configured
687 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200688 nblogger = 0;
689 list_for_each_entry(tmp, logsrvs, list) {
690 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100691 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200692
David du Colombier11bcb6c2011-03-24 12:23:00 +0100693 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100694 proto = 0;
695 plogfd = &logfdunix;
696 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100697 proto = IPPROTO_UDP;
698 plogfd = &logfdinet;
699 }
700 if (*plogfd >= 0) {
701 /* socket already created. */
702 continue;
703 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100704 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100705 proto)) < 0) {
706 Alert("socket for logger #%d failed: %s (errno=%d)\n",
707 nblogger + 1, strerror(errno), errno);
708 return;
709 }
710 /* we don't want to receive anything on this socket */
711 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
712 /* does nothing under Linux, maybe needed for others */
713 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200714 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715 }
716
Robert Tsai81ae1952007-12-05 10:47:29 +0100717 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200718 nblogger = 0;
719 list_for_each_entry(tmp, logsrvs, list) {
720 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100721 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100722 &logfdunix : &logfdinet;
723 int sent;
724
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200726 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100728
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 /* For each target, we may have a different facility.
730 * We can also have a different log level for each message.
731 * This induces variations in the message header length.
732 * Since we don't want to recompute it each time, nor copy it every
733 * time, we only change the facility in the pre-computed header,
734 * and we change the pointer to the header accordingly.
735 */
William Lallemand0f99e342011-10-12 17:50:54 +0200736 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100737 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738 do {
739 *log_ptr = '0' + fac_level % 10;
740 fac_level /= 10;
741 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100742 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100744
William Lallemandbfb099c2012-03-19 16:15:12 +0100745 sent = sendto(*plogfd, log_ptr, size + log_ptr - dataptr,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200746 MSG_DONTWAIT | MSG_NOSIGNAL,
747 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100748 if (sent < 0) {
749 Alert("sendto logger #%d failed: %s (errno=%d)\n",
750 nblogger, strerror(errno), errno);
751 }
William Lallemand0f99e342011-10-12 17:50:54 +0200752 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753 }
754}
755
William Lallemandbddd4fd2012-02-27 11:23:10 +0100756extern fd_set hdr_encode_map[];
757extern fd_set url_encode_map[];
758
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200760const 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 +0100761const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
762 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
763 Set-cookie Updated, unknown, unknown */
764
William Lallemand1d705562012-03-12 12:46:41 +0100765/*
766 * try to write a character if there is enough space, or goto out
767 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100768#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100769 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100770 *(tmplog++) = (x); \
771 } else { \
772 goto out; \
773 } \
774 } while(0)
775
William Lallemand1d705562012-03-12 12:46:41 +0100776
777
778int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779{
Willy Tarreau73de9892006-11-30 11:40:23 +0100780 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100781 struct proxy *be = s->be;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100782 struct http_txn *txn = &s->txn;
William Lallemand1d705562012-03-12 12:46:41 +0100783 int tolog;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100784 char *uri;
785 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200786 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100787 int t_request;
788 int hdr;
789 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100790 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100791 char *ret;
792 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100793 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794
William Lallemandbddd4fd2012-02-27 11:23:10 +0100795 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreau42250582007-04-01 01:30:43 +0200796 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100797
798 if (!(tolog & LW_SVID))
799 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200800 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100801 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200802 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100803 break;
804 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200805 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100806 break;
807 default:
808 svid = "<NOSRV>";
809 break;
810 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811
William Lallemandbddd4fd2012-02-27 11:23:10 +0100812 t_request = -1;
813 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
814 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
815
William Lallemand1d705562012-03-12 12:46:41 +0100816 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200817
William Lallemandbddd4fd2012-02-27 11:23:10 +0100818 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100819 if (LIST_ISEMPTY(list_format))
820 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100821
William Lallemand1d705562012-03-12 12:46:41 +0100822 list_for_each_entry(tmp, list_format, list) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100823 char *src = NULL;
824 switch (tmp->type) {
825
William Lallemand1d705562012-03-12 12:46:41 +0100826 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100827 if (!last_isspace) {
828 LOGCHAR(' ');
829 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100830 }
831 break;
832
William Lallemand1d705562012-03-12 12:46:41 +0100833 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100834 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200835 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100836 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100837 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100838 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100839 last_isspace = 0;
840 break;
841
William Lallemand1d705562012-03-12 12:46:41 +0100842 case LOG_FMT_CLIENTIP: // %Ci
Willy Tarreau986a9d22012-08-30 21:11:38 +0200843 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->prod->conn.addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200844 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100845 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100846 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100847 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100848 last_isspace = 0;
849 break;
850
William Lallemand1d705562012-03-12 12:46:41 +0100851 case LOG_FMT_CLIENTPORT: // %Cp
Willy Tarreau986a9d22012-08-30 21:11:38 +0200852 if (s->req->prod->conn.addr.from.ss_family == AF_UNIX) {
William Lallemand5f232402012-04-05 18:02:55 +0200853 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
854 } else {
Willy Tarreau986a9d22012-08-30 21:11:38 +0200855 ret = lf_port(tmplog, (struct sockaddr *)&s->req->prod->conn.addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200856 dst + maxsize - tmplog, tmp);
857 }
858 if (ret == NULL)
859 goto out;
860 tmplog = ret;
861 last_isspace = 0;
862 break;
863
864 case LOG_FMT_FRONTENDIP: // %Fi
Willy Tarreau986a9d22012-08-30 21:11:38 +0200865 conn_get_to_addr(&s->req->prod->conn);
866 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->prod->conn.addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200867 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100868 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100869 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100870 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100871 last_isspace = 0;
872 break;
873
William Lallemand5f232402012-04-05 18:02:55 +0200874 case LOG_FMT_FRONTENDPORT: // %Fp
Willy Tarreau986a9d22012-08-30 21:11:38 +0200875 conn_get_to_addr(&s->req->prod->conn);
876 if (s->req->prod->conn.addr.to.ss_family == AF_UNIX) {
William Lallemand5f232402012-04-05 18:02:55 +0200877 ret = ltoa_o(s->listener->luid,
878 tmplog, dst + maxsize - tmplog);
879 } else {
Willy Tarreau986a9d22012-08-30 21:11:38 +0200880 ret = lf_port(tmplog, (struct sockaddr *)&s->req->prod->conn.addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200881 dst + maxsize - tmplog, tmp);
882 }
883 if (ret == NULL)
884 goto out;
885 tmplog = ret;
886 last_isspace = 0;
887 break;
888
William Lallemand1d705562012-03-12 12:46:41 +0100889 case LOG_FMT_BACKENDIP: // %Bi
Willy Tarreau986a9d22012-08-30 21:11:38 +0200890 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->cons->conn.addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200891 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100892 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +0100893 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100894 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +0100895 last_isspace = 0;
896 break;
897
William Lallemand1d705562012-03-12 12:46:41 +0100898 case LOG_FMT_BACKENDPORT: // %Bp
Willy Tarreau986a9d22012-08-30 21:11:38 +0200899 ret = lf_port(tmplog, (struct sockaddr *)&s->req->cons->conn.addr.from,
William Lallemand5f232402012-04-05 18:02:55 +0200900 dst + maxsize - tmplog, tmp);
901 if (ret == NULL)
902 goto out;
903 tmplog = ret;
904 last_isspace = 0;
905 break;
906
907 case LOG_FMT_SERVERIP: // %Si
Willy Tarreau986a9d22012-08-30 21:11:38 +0200908 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->cons->conn.addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200909 dst + maxsize - tmplog, tmp);
910 if (ret == NULL)
911 goto out;
912 tmplog = ret;
913 last_isspace = 0;
914 break;
915
916 case LOG_FMT_SERVERPORT: // %Sp
Willy Tarreau986a9d22012-08-30 21:11:38 +0200917 ret = lf_port(tmplog, (struct sockaddr *)&s->req->cons->conn.addr.to,
William Lallemand5f232402012-04-05 18:02:55 +0200918 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100919 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +0100920 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100921 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +0100922 last_isspace = 0;
923 break;
924
William Lallemand1d705562012-03-12 12:46:41 +0100925 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +0100926 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +0200927 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
928 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100929 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100930 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100931 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100932 last_isspace = 0;
933 break;
934
William Lallemand1d705562012-03-12 12:46:41 +0100935 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +0100936 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +0200937 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100938 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100939 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100940 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100941 last_isspace = 0;
942 break;
943
William Lallemand5f232402012-04-05 18:02:55 +0200944 case LOG_FMT_TS: // %Ts
945 get_gmtime(s->logs.accept_date.tv_sec, &tm);
946 if (tmp->options & LOG_OPT_HEXA) {
947 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
948 if (iret < 0 || iret > dst + maxsize - tmplog)
949 goto out;
950 last_isspace = 0;
951 tmplog += iret;
952 } else {
953 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
954 if (ret == NULL)
955 goto out;
956 tmplog = ret;
957 last_isspace = 0;
958 }
959 break;
960
William Lallemand1d705562012-03-12 12:46:41 +0100961 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +0200962 if (tmp->options & LOG_OPT_HEXA) {
963 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
964 if (iret < 0 || iret > dst + maxsize - tmplog)
965 goto out;
966 last_isspace = 0;
967 tmplog += iret;
968 } else {
969 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100970 goto out;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100971 tmplog = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
972 tmplog, 4);
William Lallemand51b5dca2012-03-26 17:52:55 +0200973 if (!tmplog)
974 goto out;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100975 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +0200976 }
977 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100978
William Lallemand1d705562012-03-12 12:46:41 +0100979 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +0100980 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +0200981 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100982 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100983 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100984 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +0200985 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100986 break;
987
Willy Tarreau773d65f2012-10-12 14:56:11 +0200988 case LOG_FMT_FRONTEND_XPRT: // %ft
989 src = fe->id;
990 if (tmp->options & LOG_OPT_QUOTE)
991 LOGCHAR('"');
992 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
993 if (iret == 0)
994 goto out;
995 tmplog += iret;
996#ifdef USE_OPENSSL
997 if (s->listener->xprt == &ssl_sock)
998 LOGCHAR('~');
999#endif
1000 if (tmp->options & LOG_OPT_QUOTE)
1001 LOGCHAR('"');
1002 last_isspace = 0;
1003 break;
1004
William Lallemand1d705562012-03-12 12:46:41 +01001005 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001006 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001007 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001008 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001009 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001010 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001011 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001012 break;
1013
William Lallemand1d705562012-03-12 12:46:41 +01001014 case LOG_FMT_SERVER: // %s
William Lallemandbddd4fd2012-02-27 11:23:10 +01001015 src = (char *)svid;
William Lallemand5f232402012-04-05 18:02:55 +02001016 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001017 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001018 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001019 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001020 last_isspace = 0;
1021 break;
1022
William Lallemand1d705562012-03-12 12:46:41 +01001023 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001024 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001025 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001026 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001027 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001028 last_isspace = 0;
1029 break;
1030
William Lallemand1d705562012-03-12 12:46:41 +01001031 case LOG_FMT_TW: // %Tw
1032 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001033 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001034 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001035 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001036 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001037 last_isspace = 0;
1038 break;
1039
William Lallemand1d705562012-03-12 12:46:41 +01001040 case LOG_FMT_TC: // %Tc
1041 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001042 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001043 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001044 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001045 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001046 last_isspace = 0;
1047 break;
1048
William Lallemand1d705562012-03-12 12:46:41 +01001049 case LOG_FMT_TR: // %Tr
1050 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001051 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001052 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001053 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001054 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001055 last_isspace = 0;
1056 break;
1057
William Lallemand1d705562012-03-12 12:46:41 +01001058 case LOG_FMT_TT: // %Tt
William Lallemandbddd4fd2012-02-27 11:23:10 +01001059 if (!(tolog & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001060 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001061 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001062 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001063 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001064 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001065 last_isspace = 0;
1066 break;
1067
William Lallemand1d705562012-03-12 12:46:41 +01001068 case LOG_FMT_STATUS: // %st
William Lallemand5f232402012-04-05 18:02:55 +02001069 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001070 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001071 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001072 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001073 last_isspace = 0;
1074 break;
1075
William Lallemand1d705562012-03-12 12:46:41 +01001076 case LOG_FMT_BYTES: // %B
William Lallemandbddd4fd2012-02-27 11:23:10 +01001077 if (!(tolog & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001078 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001079 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001080 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001081 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001082 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001083 last_isspace = 0;
1084 break;
1085
William Lallemand1d705562012-03-12 12:46:41 +01001086 case LOG_FMT_CCLIENT: // %cc
William Lallemandbddd4fd2012-02-27 11:23:10 +01001087 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001088 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001089 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001090 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001091 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001092 last_isspace = 0;
1093 break;
1094
William Lallemand1d705562012-03-12 12:46:41 +01001095 case LOG_FMT_CSERVER: // %cs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001096 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001097 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001098 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001099 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001100 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001101 last_isspace = 0;
1102 break;
1103
William Lallemand1d705562012-03-12 12:46:41 +01001104 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +01001105 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1106 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
1107 *tmplog = '\0';
1108 last_isspace = 0;
1109 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001110
William Lallemand1d705562012-03-12 12:46:41 +01001111 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001112 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1113 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001114 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1115 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 +01001116 last_isspace = 0;
1117 break;
1118
William Lallemand1d705562012-03-12 12:46:41 +01001119 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001120 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001121 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001122 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001123 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001124 last_isspace = 0;
1125 break;
1126
William Lallemand1d705562012-03-12 12:46:41 +01001127 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001128 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001129 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001130 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001131 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001132 last_isspace = 0;
1133 break;
1134
William Lallemand1d705562012-03-12 12:46:41 +01001135 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001136 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001137 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001138 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001139 tmplog = ret;
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_SRVCONN: // %sc
1144 ret = ultoa_o(target_srv(&s->target) ?
1145 target_srv(&s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001146 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001147 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001148 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001149 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001150 last_isspace = 0;
1151 break;
1152
William Lallemand1d705562012-03-12 12:46:41 +01001153 case LOG_FMT_RETRIES: // %rq
William Lallemandbddd4fd2012-02-27 11:23:10 +01001154 if (s->flags & SN_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001155 LOGCHAR('+');
1156 ret = ltoa_o((s->req->cons->conn_retries>0) ?
1157 (be->conn_retries - s->req->cons->conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001158 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001159 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001160 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001161 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001162 last_isspace = 0;
1163 break;
1164
William Lallemand1d705562012-03-12 12:46:41 +01001165 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001166 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001167 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001168 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001169 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001170 last_isspace = 0;
1171 break;
1172
William Lallemand1d705562012-03-12 12:46:41 +01001173 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001174 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001175 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001176 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001177 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001178 last_isspace = 0;
1179 break;
1180
William Lallemand1d705562012-03-12 12:46:41 +01001181 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001182 /* request header */
1183 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1184 if (tmp->options & LOG_OPT_QUOTE)
1185 LOGCHAR('"');
1186 LOGCHAR('{');
1187 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1188 if (hdr)
1189 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001190 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001191 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001192 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001193 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001194 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001195 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001196 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001197 }
1198 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001199 if (tmp->options & LOG_OPT_QUOTE)
1200 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001201 last_isspace = 0;
William Lallemand1d705562012-03-12 12:46:41 +01001202 if (tmp->options & LOG_OPT_QUOTE)
1203 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001204 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001205 break;
1206
William Lallemand1d705562012-03-12 12:46:41 +01001207 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001208 /* request header list */
1209 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1210 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1211 if (hdr > 0)
1212 LOGCHAR(' ');
1213 if (tmp->options & LOG_OPT_QUOTE)
1214 LOGCHAR('"');
1215 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001216 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001217 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001218 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001219 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001220 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001221 } else if (!(tmp->options & LOG_OPT_QUOTE))
1222 LOGCHAR('-');
1223 if (tmp->options & LOG_OPT_QUOTE)
1224 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001225 last_isspace = 0;
1226 }
1227 }
1228 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229
William Lallemand1d705562012-03-12 12:46:41 +01001230
1231 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001232 /* response header */
1233 if (fe->to_log & LW_RSPHDR &&
1234 txn->rsp.cap) {
1235 if (tmp->options & LOG_OPT_QUOTE)
1236 LOGCHAR('"');
1237 LOGCHAR('{');
1238 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1239 if (hdr)
1240 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001241 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001242 ret = encode_string(tmplog, dst + maxsize,
1243 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1244 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001245 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001246 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001247 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001248 }
1249 LOGCHAR('}');
1250 last_isspace = 0;
1251 if (tmp->options & LOG_OPT_QUOTE)
1252 LOGCHAR('"');
1253 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001254 break;
1255
William Lallemand1d705562012-03-12 12:46:41 +01001256 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001257 /* response header list */
1258 if (fe->to_log & LW_RSPHDR && txn->rsp.cap) {
1259 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1260 if (hdr > 0)
1261 LOGCHAR(' ');
1262 if (tmp->options & LOG_OPT_QUOTE)
1263 LOGCHAR('"');
1264 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001265 ret = encode_string(tmplog, dst + maxsize,
1266 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1267 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001268 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001269 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001270 } else if (!(tmp->options & LOG_OPT_QUOTE))
1271 LOGCHAR('-');
1272 if (tmp->options & LOG_OPT_QUOTE)
1273 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001274 last_isspace = 0;
1275 }
1276 }
1277 break;
1278
William Lallemand1d705562012-03-12 12:46:41 +01001279 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001280 /* Request */
1281 if (tmp->options & LOG_OPT_QUOTE)
1282 LOGCHAR('"');
1283 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001284 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001285 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001286 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001287 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001288 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001289 if (tmp->options & LOG_OPT_QUOTE)
1290 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001291 last_isspace = 0;
1292 break;
William Lallemand5f232402012-04-05 18:02:55 +02001293
1294 case LOG_FMT_COUNTER: // %rt
1295 if (tmp->options & LOG_OPT_HEXA) {
1296 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", global.req_count);
1297 if (iret < 0 || iret > dst + maxsize - tmplog)
1298 goto out;
1299 last_isspace = 0;
1300 tmplog += iret;
1301 } else {
1302 ret = ltoa_o(global.req_count, tmplog, dst + maxsize - tmplog);
1303 if (ret == NULL)
1304 goto out;
1305 tmplog = ret;
1306 last_isspace = 0;
1307 }
1308 break;
1309
1310 case LOG_FMT_HOSTNAME: // %H
1311 src = hostname;
1312 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1313 if (ret == NULL)
1314 goto out;
1315 tmplog = ret;
1316 last_isspace = 0;
1317 break;
1318
1319 case LOG_FMT_PID: // %pid
1320 if (tmp->options & LOG_OPT_HEXA) {
1321 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1322 if (iret < 0 || iret > dst + maxsize - tmplog)
1323 goto out;
1324 last_isspace = 0;
1325 tmplog += iret;
1326 } else {
1327 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1328 if (ret == NULL)
1329 goto out;
1330 tmplog = ret;
1331 last_isspace = 0;
1332 }
1333 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001334
1335 case LOG_FMT_UNIQUEID: // %ID
1336 src = s->unique_id;
1337 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
1338 if (ret == NULL)
1339 goto out;
1340 tmplog = ret;
1341 last_isspace = 0;
1342 break;
1343
William Lallemandbddd4fd2012-02-27 11:23:10 +01001344 }
1345 }
1346
1347out:
William Lallemand1d705562012-03-12 12:46:41 +01001348 /* *tmplog is a unused character */
1349 *tmplog = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001350
William Lallemand1d705562012-03-12 12:46:41 +01001351 return tmplog - dst + 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001352
Willy Tarreaubaaee002006-06-26 02:48:02 +02001353}
1354
William Lallemand1d705562012-03-12 12:46:41 +01001355/*
1356 * send a log for the session when we have enough info about it.
1357 * Will not log if the frontend has no log defined.
1358 */
1359void sess_log(struct session *s)
1360{
1361 char *tmplog;
1362 int size, err, level;
1363
1364 /* if we don't want to log normal traffic, return now */
1365 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1366 (s->req->cons->conn_retries != s->be->conn_retries) ||
1367 ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001368
William Lallemand1d705562012-03-12 12:46:41 +01001369 if (!err && (s->fe->options2 & PR_O2_NOLOGNORM))
1370 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001371
William Lallemand1d705562012-03-12 12:46:41 +01001372 if (LIST_ISEMPTY(&s->fe->logsrvs))
1373 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001374
William Lallemand1d705562012-03-12 12:46:41 +01001375 level = LOG_INFO;
1376 if (err && (s->fe->options2 & PR_O2_LOGERRORS))
1377 level = LOG_ERR;
1378
1379 tmplog = update_log_hdr();
1380 size = tmplog - logline;
1381 size += build_logline(s, tmplog, sizeof(logline) - size, &s->fe->logformat);
1382 if (size > 0) {
1383 __send_log(s->fe, level, logline, size);
1384 s->logs.logwait = 0;
1385 }
1386}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001387
Willy Tarreaubaaee002006-06-26 02:48:02 +02001388/*
1389 * Local variables:
1390 * c-indent-level: 8
1391 * c-basic-offset: 8
1392 * End:
1393 */