blob: bdb0060d548405f4cf1b257076800e91641b7b2c [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 Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreaubaaee002006-06-26 02:48:02 +020037const char *log_facilities[NB_LOG_FACILITIES] = {
38 "kern", "user", "mail", "daemon",
39 "auth", "syslog", "lpr", "news",
40 "uucp", "cron", "auth2", "ftp",
41 "ntp", "audit", "alert", "cron2",
42 "local0", "local1", "local2", "local3",
43 "local4", "local5", "local6", "local7"
44};
45
46
47const char *log_levels[NB_LOG_LEVELS] = {
48 "emerg", "alert", "crit", "err",
49 "warning", "notice", "info", "debug"
50};
51
Justin Karnegeseb2c24a2012-05-24 15:28:52 -070052const char sess_term_cond[16] = "-cCsSPRIDKUIIIII"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +020053const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
William Lallemand723b73a2012-02-08 16:37:49 +010055
56/* log_format */
57struct logformat_type {
58 char *name;
59 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010060 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020061 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010062 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
William Lallemand723b73a2012-02-08 16:37:49 +010063};
64
William Lallemandb7ff6a32012-03-02 14:35:21 +010065int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
66
William Lallemand723b73a2012-02-08 16:37:49 +010067/* log_format variable names */
68static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020069 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
70 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
71 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
72 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
73 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
74 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
75 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
76 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
77 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
78 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
79 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
80 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
81 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
82 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
83 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
84 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
85 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes read */
86 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
87 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
88 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
89 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
90 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
91 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL }, /* status code */
92 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
93 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
94 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
95 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
96 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
97 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
98 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
99 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
100 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
101 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
102 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
103 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request */
104 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response */
105 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request list */
106 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response list */
107 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
108 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
109 { "rt", LOG_FMT_COUNTER, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP request counter */
110 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
111 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
112 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100113};
114
Willy Tarreau6580c062012-03-12 15:09:42 +0100115char default_http_log_format[] = "%Ci:%Cp [%t] %f %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %st %B %cc %cs %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
116char clf_http_log_format[] = "%{+Q}o %{-Q}Ci - - [%T] %r %st %B \"\" \"\" %Cp %ms %f %b %s %Tq %Tw %Tc %Tr %Tt %tsc %ac %fc %bc %sc %rc %sq %bq %cc %cs %hrl %hsl";
William Lallemandbddd4fd2012-02-27 11:23:10 +0100117char default_tcp_log_format[] = "%Ci:%Cp [%t] %f %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq";
William Lallemand723b73a2012-02-08 16:37:49 +0100118char *log_format = NULL;
119
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100120/* This is a global syslog line, common to all outgoing messages. It begins
121 * with the syslog tag and the date that are updated by update_log_hdr().
122 */
123static char logline[MAX_SYSLOG_LEN];
124
William Lallemand723b73a2012-02-08 16:37:49 +0100125struct logformat_var_args {
126 char *name;
127 int mask;
128};
129
130struct logformat_var_args var_args_list[] = {
131// global
132 { "M", LOG_OPT_MANDATORY },
133 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200134 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100135 { 0, 0 }
136};
137
138/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100139 * callback used to configure addr source retrieval
140 */
141int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
142{
143 curproxy->options2 |= PR_O2_SRC_ADDR;
144
145 return 0;
146}
147
148
149/*
William Lallemand723b73a2012-02-08 16:37:49 +0100150 * Parse args in a logformat_var
151 */
152int parse_logformat_var_args(char *args, struct logformat_node *node)
153{
154 int i = 0;
155 int end = 0;
156 int flags = 0; // 1 = + 2 = -
157 char *sp = NULL; // start pointer
158
159 if (args == NULL)
160 return 1;
161
162 while (1) {
163 if (*args == '\0')
164 end = 1;
165
166 if (*args == '+') {
167 // add flag
168 sp = args + 1;
169 flags = 1;
170 }
171 if (*args == '-') {
172 // delete flag
173 sp = args + 1;
174 flags = 2;
175 }
176
177 if (*args == '\0' || *args == ',') {
178 *args = '\0';
179 for (i = 0; var_args_list[i].name; i++) {
180 if (strcmp(sp, var_args_list[i].name) == 0) {
181 if (flags == 1) {
182 node->options |= var_args_list[i].mask;
183 break;
184 } else if (flags == 2) {
185 node->options &= ~var_args_list[i].mask;
186 break;
187 }
188 }
189 }
190 sp = NULL;
191 if (end)
192 break;
193 }
194 args++;
195 }
196 return 0;
197}
198
199/*
200 * Parse a variable '%varname' or '%{args}varname' in logformat
201 *
202 */
William Lallemand1d705562012-03-12 12:46:41 +0100203int parse_logformat_var(char *str, size_t len, struct proxy *curproxy, struct list *list_format, int *defoptions)
William Lallemand723b73a2012-02-08 16:37:49 +0100204{
205 int i, j;
206 char *arg = NULL; // arguments
207 int fparam = 0;
208 char *name = NULL;
209 struct logformat_node *node = NULL;
210 char varname[255] = { 0 }; // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100211
William Lallemand723b73a2012-02-08 16:37:49 +0100212 for (i = 1; i < len; i++) { // escape first char %
213 if (!arg && str[i] == '{') {
214 arg = str + i;
215 fparam = 1;
216 } else if (arg && str[i] == '}') {
217 char *tmp = arg;
218 arg = calloc(str + i - tmp, 1); // without {}
219 strncpy(arg, tmp + 1, str + i - tmp - 1); // copy without { and }
220 arg[str + i - tmp - 1] = '\0';
221 fparam = 0;
222 } else if (!name && !fparam) {
223 strncpy(varname, str + i, len - i + 1);
224 varname[len - i] = '\0';
225 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
226 if (strcmp(varname, logformat_keywords[j].name) == 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100227 if (!((logformat_keywords[j].mode == PR_MODE_HTTP) && (curproxy->mode == PR_MODE_TCP))) {
228 node = calloc(1, sizeof(struct logformat_node));
229 node->type = logformat_keywords[j].type;
William Lallemand81f51172012-03-12 11:03:47 +0100230 node->options = *defoptions;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100231 node->arg = arg;
232 parse_logformat_var_args(node->arg, node);
William Lallemand1d705562012-03-12 12:46:41 +0100233 if (node->type == LOG_FMT_GLOBAL) {
William Lallemand81f51172012-03-12 11:03:47 +0100234 *defoptions = node->options;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100235 free(node);
236 } else {
William Lallemandb7ff6a32012-03-02 14:35:21 +0100237 if (logformat_keywords[j].config_callback != NULL) {
238 if (logformat_keywords[j].config_callback(node, curproxy) != 0) {
239 return -1;
240 }
241 }
William Lallemand5e19a282012-04-02 16:22:10 +0200242 curproxy->to_log |= logformat_keywords[j].lw;
William Lallemand1d705562012-03-12 12:46:41 +0100243 LIST_ADDQ(list_format, &node->list);
William Lallemandbddd4fd2012-02-27 11:23:10 +0100244 }
245 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100246 } else {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100247 Warning("Warning: No such variable name '%s' in this log mode\n", varname);
248 if (arg)
249 free(arg);
250 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100251 }
William Lallemand723b73a2012-02-08 16:37:49 +0100252 }
253 }
254 Warning("Warning: No such variable name '%s' in logformat\n", varname);
255 if (arg)
256 free(arg);
257 return -1;
258 }
259 }
260 return -1;
261}
262
263/*
264 * push to the logformat linked list
265 *
266 * start: start pointer
267 * end: end text pointer
268 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100269 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100270 *
271 * LOG_TEXT: copy chars from start to end excluding end.
272 *
273*/
William Lallemand1d705562012-03-12 12:46:41 +0100274void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100275{
276 char *str;
277
William Lallemand1d705562012-03-12 12:46:41 +0100278 if (type == LOG_FMT_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100279 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100280 str = calloc(end - start + 1, 1);
281 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100282 str[end - start] = '\0';
283 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100284 node->type = LOG_FMT_TEXT; // type string
285 LIST_ADDQ(list_format, &node->list);
286 } else if (type == LOG_FMT_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100287 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100288 node->type = LOG_FMT_SEPARATOR;
289 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100290 }
291}
292
293/*
294 * Parse the log_format string and fill a linked list.
295 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
296 * You can set arguments using { } : %{many arguments}varname
William Lallemand1d705562012-03-12 12:46:41 +0100297 *
298 * str: the string to parse
299 * curproxy: the proxy affected
300 * list_format: the destination list
301 * capabilities: PR_MODE_TCP_ | PR_MODE_HTTP
William Lallemand723b73a2012-02-08 16:37:49 +0100302 */
William Lallemand1d705562012-03-12 12:46:41 +0100303void parse_logformat_string(char *str, struct proxy *curproxy, struct list *list_format, int capabilities)
William Lallemand723b73a2012-02-08 16:37:49 +0100304{
305 char *sp = str; /* start pointer */
306 int cformat = -1; /* current token format : LOG_TEXT, LOG_SEPARATOR, LOG_VARIABLE */
307 int pformat = -1; /* previous token format */
308 struct logformat_node *tmplf, *back;
William Lallemand81f51172012-03-12 11:03:47 +0100309 int options = 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100310
William Lallemand1dc00ef2012-08-09 16:41:35 +0200311 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200312
William Lallemand723b73a2012-02-08 16:37:49 +0100313 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100314 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100315 LIST_DEL(&tmplf->list);
316 free(tmplf);
317 }
318
319 while (1) {
320
321 // push the variable only if formats are different, not
322 // within a variable, and not the first iteration
323 if ((cformat != pformat && cformat != -1 && pformat != -1) || *str == '\0') {
324 if (((pformat != LF_STARTVAR && cformat != LF_VAR) &&
325 (pformat != LF_STARTVAR && cformat != LF_STARG) &&
326 (pformat != LF_STARG && cformat != LF_VAR)) || *str == '\0') {
327 if (pformat > LF_VAR) // unfinished string
328 pformat = LF_TEXT;
William Lallemand81f51172012-03-12 11:03:47 +0100329 if (pformat == LF_VAR)
William Lallemand1d705562012-03-12 12:46:41 +0100330 parse_logformat_var(sp, str - sp, curproxy, list_format, &options);
William Lallemand81f51172012-03-12 11:03:47 +0100331 else
William Lallemand1d705562012-03-12 12:46:41 +0100332 add_to_logformat_list(sp, str, pformat, list_format);
William Lallemand723b73a2012-02-08 16:37:49 +0100333 sp = str;
334 if (*str == '\0')
335 break;
336 }
337 }
338
339 if (cformat != -1)
340 str++; // consume the string, except on the first tour
341
342 pformat = cformat;
343
344 if (*str == '\0') {
345 cformat = LF_STARTVAR; // for breaking in all cases
346 continue;
347 }
348
349 if (pformat == LF_STARTVAR) { // after a %
350 if ( (*str >= 'a' && *str <= 'z') || // parse varname
351 (*str >= 'A' && *str <= 'Z') ||
352 (*str >= '0' && *str <= '9')) {
353 cformat = LF_VAR; // varname
354 continue;
355 } else if (*str == '{') {
356 cformat = LF_STARG; // variable arguments
357 continue;
358 } else { // another unexpected token
359 pformat = LF_TEXT; // redefine the format of the previous token to TEXT
360 cformat = LF_TEXT;
361 continue;
362 }
363
364 } else if (pformat == LF_VAR) { // after a varname
365 if ( (*str >= 'a' && *str <= 'z') || // parse varname
366 (*str >= 'A' && *str <= 'Z') ||
367 (*str >= '0' && *str <= '9')) {
368 cformat = LF_VAR;
369 continue;
370 }
371 } else if (pformat == LF_STARG) { // inside variable arguments
372 if (*str == '}') { // end of varname
373 cformat = LF_EDARG;
374 continue;
375 } else { // all tokens are acceptable within { }
376 cformat = LF_STARG;
377 continue;
378 }
379 } else if (pformat == LF_EDARG) { // after arguments
380 if ( (*str >= 'a' && *str <= 'z') || // parse a varname
381 (*str >= 'A' && *str <= 'Z') ||
382 (*str >= '0' && *str <= '9')) {
383 cformat = LF_VAR;
384 continue;
385 } else { // if no varname after arguments, transform in TEXT
386 pformat = LF_TEXT;
387 cformat = LF_TEXT;
388 }
389 }
390
391 // others tokens that don't match previous conditions
392 if (*str == '%') {
393 cformat = LF_STARTVAR;
394 } else if (*str == ' ') {
395 cformat = LF_SEPARATOR;
396 } else {
397 cformat = LF_TEXT;
398 }
399 }
400}
401
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402/*
403 * Displays the message on stderr with the date and pid. Overrides the quiet
404 * mode during startup.
405 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200406void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407{
408 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200409 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410
411 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
412 va_start(argp, fmt);
413
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200414 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200416 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200417 vfprintf(stderr, fmt, argp);
418 fflush(stderr);
419 va_end(argp);
420 }
421}
422
423
424/*
425 * Displays the message on stderr with the date and pid.
426 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200427void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428{
429 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200430 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200431
432 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
433 va_start(argp, fmt);
434
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200435 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200437 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438 vfprintf(stderr, fmt, argp);
439 fflush(stderr);
440 va_end(argp);
441 }
442}
443
444/*
445 * Displays the message on <out> only if quiet mode is not set.
446 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200447void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448{
449 va_list argp;
450
451 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
452 va_start(argp, fmt);
453 vfprintf(out, fmt, argp);
454 fflush(out);
455 va_end(argp);
456 }
457}
458
459/*
460 * returns log level for <lev> or -1 if not found.
461 */
462int get_log_level(const char *lev)
463{
464 int level;
465
466 level = NB_LOG_LEVELS - 1;
467 while (level >= 0 && strcmp(log_levels[level], lev))
468 level--;
469
470 return level;
471}
472
473
474/*
475 * returns log facility for <fac> or -1 if not found.
476 */
477int get_log_facility(const char *fac)
478{
479 int facility;
480
481 facility = NB_LOG_FACILITIES - 1;
482 while (facility >= 0 && strcmp(log_facilities[facility], fac))
483 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100484
Willy Tarreaubaaee002006-06-26 02:48:02 +0200485 return facility;
486}
487
William Lallemanda1cc3812012-02-08 16:38:44 +0100488/*
489 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200490 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100491 *
492 * Return the adress of the \0 character, or NULL on error
493 */
William Lallemand5f232402012-04-05 18:02:55 +0200494char *lf_text(char *dst, char *src, size_t size, struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +0100495{
William Lallemand51b5dca2012-03-26 17:52:55 +0200496 int n;
William Lallemanda1cc3812012-02-08 16:38:44 +0100497
William Lallemandbddd4fd2012-02-27 11:23:10 +0100498 if (src == NULL || *src == '\0') {
499 if (node->options & LOG_OPT_QUOTE) {
500 if (size > 2) {
501 *(dst++) = '"';
502 *(dst++) = '"';
503 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100504 } else {
505 dst = NULL;
506 return dst;
507 }
508 } else {
509 if (size > 1) {
510 *(dst++) = '-';
511 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100512 } else { // error no space available
513 dst = NULL;
514 return dst;
515 }
516 }
517 } else {
518 if (node->options & LOG_OPT_QUOTE) {
519 if (size-- > 1 ) {
520 *(dst++) = '"';
521 } else {
522 dst = NULL;
523 return NULL;
524 }
William Lallemand51b5dca2012-03-26 17:52:55 +0200525 n = strlcpy2(dst, src, size);
526 size -= n;
527 dst += n;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100528 if (size > 1) {
529 *(dst++) = '"';
530 *dst = '\0';
531 } else {
532 dst = NULL;
533 }
534 } else {
535 dst += strlcpy2(dst, src, size);
536 }
537 }
538 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100539}
540
William Lallemand5f232402012-04-05 18:02:55 +0200541/*
542 * Write a IP adress to the log string
543 * +X option write in hexadecimal notation, most signifant byte on the left
544 */
545char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
546{
547 char *ret = dst;
548 int iret;
549 char pn[INET6_ADDRSTRLEN];
550
551 if (node->options & LOG_OPT_HEXA) {
552 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
553 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
554 if (iret < 0 || iret > size)
555 return NULL;
556 ret += iret;
557 } else {
558 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
559 ret = lf_text(dst, pn, size, node);
560 if (ret == NULL)
561 return NULL;
562 }
563 return ret;
564}
565
566/*
567 * Write a port to the log
568 * +X option write in hexadecimal notation, most signifant byte on the left
569 */
570char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
571{
572 char *ret = dst;
573 int iret;
574
575 if (node->options & LOG_OPT_HEXA) {
576 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
577 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
578 if (iret < 0 || iret > size)
579 return NULL;
580 ret += iret;
581 } else {
582 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
583 if (ret == NULL)
584 return NULL;
585 }
586 return ret;
587}
588
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100589/* Re-generate the syslog header at the beginning of logline once a second and
590 * return the pointer to the first character after the header.
591 */
592static char *update_log_hdr()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200593{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100594 static long tvsec;
595 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200596
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200597 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200599 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100600 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200601
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200602 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200603 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200604
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100605 hdr_len = snprintf(logline, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100606 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200607 monthname[tm.tm_mon],
608 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100609 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000610 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611 /* WARNING: depending upon implementations, snprintf may return
612 * either -1 or the number of bytes that would be needed to store
613 * the total message. In both cases, we must adjust it.
614 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100615 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
616 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200617
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100618 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200619 }
620
William Lallemand2a4a44f2012-02-06 16:00:33 +0100621 return dataptr;
622}
623
624/*
625 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100626 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100627 */
628void send_log(struct proxy *p, int level, const char *format, ...)
629{
630 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100631 char *dataptr;
632 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100633
634 if (level < 0 || format == NULL)
635 return;
636
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100637 dataptr = update_log_hdr(); /* update log header and skip it */
638 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100639
640 va_start(argp, format);
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100641 data_len += vsnprintf(dataptr, logline + sizeof(logline) - dataptr, format, argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100642 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
643 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200644 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100645
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100646 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100647}
648
649/*
650 * This function sends a syslog message.
651 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100652 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100653 */
654void __send_log(struct proxy *p, int level, char *message, size_t size)
655{
656 static int logfdunix = -1; /* syslog to AF_UNIX socket */
657 static int logfdinet = -1; /* syslog to AF_INET socket */
658 static char *dataptr = NULL;
659 int fac_level;
660 struct list *logsrvs = NULL;
661 struct logsrv *tmp = NULL;
662 int nblogger;
663 char *log_ptr;
664
665 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200666
667 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200668 if (!LIST_ISEMPTY(&global.logsrvs)) {
669 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670 }
671 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200672 if (!LIST_ISEMPTY(&p->logsrvs)) {
673 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100675 }
676
William Lallemand0f99e342011-10-12 17:50:54 +0200677 if (!logsrvs)
678 return;
679
William Lallemand2a4a44f2012-02-06 16:00:33 +0100680 message[size - 1] = '\n';
681
Robert Tsai81ae1952007-12-05 10:47:29 +0100682 /* Lazily set up syslog sockets for protocol families of configured
683 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200684 nblogger = 0;
685 list_for_each_entry(tmp, logsrvs, list) {
686 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100687 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200688
David du Colombier11bcb6c2011-03-24 12:23:00 +0100689 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100690 proto = 0;
691 plogfd = &logfdunix;
692 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100693 proto = IPPROTO_UDP;
694 plogfd = &logfdinet;
695 }
696 if (*plogfd >= 0) {
697 /* socket already created. */
698 continue;
699 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100700 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100701 proto)) < 0) {
702 Alert("socket for logger #%d failed: %s (errno=%d)\n",
703 nblogger + 1, strerror(errno), errno);
704 return;
705 }
706 /* we don't want to receive anything on this socket */
707 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
708 /* does nothing under Linux, maybe needed for others */
709 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200710 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711 }
712
Robert Tsai81ae1952007-12-05 10:47:29 +0100713 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200714 nblogger = 0;
715 list_for_each_entry(tmp, logsrvs, list) {
716 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100717 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100718 &logfdunix : &logfdinet;
719 int sent;
720
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200722 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100724
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725 /* For each target, we may have a different facility.
726 * We can also have a different log level for each message.
727 * This induces variations in the message header length.
728 * Since we don't want to recompute it each time, nor copy it every
729 * time, we only change the facility in the pre-computed header,
730 * and we change the pointer to the header accordingly.
731 */
William Lallemand0f99e342011-10-12 17:50:54 +0200732 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100733 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200734 do {
735 *log_ptr = '0' + fac_level % 10;
736 fac_level /= 10;
737 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100738 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200739 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100740
William Lallemandbfb099c2012-03-19 16:15:12 +0100741 sent = sendto(*plogfd, log_ptr, size + log_ptr - dataptr,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200742 MSG_DONTWAIT | MSG_NOSIGNAL,
743 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100744 if (sent < 0) {
745 Alert("sendto logger #%d failed: %s (errno=%d)\n",
746 nblogger, strerror(errno), errno);
747 }
William Lallemand0f99e342011-10-12 17:50:54 +0200748 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200749 }
750}
751
William Lallemandbddd4fd2012-02-27 11:23:10 +0100752extern fd_set hdr_encode_map[];
753extern fd_set url_encode_map[];
754
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200756const 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 +0100757const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
758 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
759 Set-cookie Updated, unknown, unknown */
760
William Lallemand1d705562012-03-12 12:46:41 +0100761/*
762 * try to write a character if there is enough space, or goto out
763 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100764#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100765 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100766 *(tmplog++) = (x); \
767 } else { \
768 goto out; \
769 } \
770 } while(0)
771
William Lallemand1d705562012-03-12 12:46:41 +0100772
773
774int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775{
Willy Tarreau73de9892006-11-30 11:40:23 +0100776 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100777 struct proxy *be = s->be;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100778 struct http_txn *txn = &s->txn;
William Lallemand1d705562012-03-12 12:46:41 +0100779 int tolog;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100780 char *uri;
781 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200782 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100783 int t_request;
784 int hdr;
785 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100786 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100787 char *ret;
788 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100789 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200790
William Lallemandbddd4fd2012-02-27 11:23:10 +0100791 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreau42250582007-04-01 01:30:43 +0200792 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100793
794 if (!(tolog & LW_SVID))
795 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200796 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100797 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200798 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100799 break;
800 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200801 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100802 break;
803 default:
804 svid = "<NOSRV>";
805 break;
806 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807
William Lallemandbddd4fd2012-02-27 11:23:10 +0100808 t_request = -1;
809 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
810 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
811
William Lallemand1d705562012-03-12 12:46:41 +0100812 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200813
William Lallemandbddd4fd2012-02-27 11:23:10 +0100814 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100815 if (LIST_ISEMPTY(list_format))
816 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100817
William Lallemand1d705562012-03-12 12:46:41 +0100818 list_for_each_entry(tmp, list_format, list) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100819 char *src = NULL;
820 switch (tmp->type) {
821
William Lallemand1d705562012-03-12 12:46:41 +0100822 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100823 if (!last_isspace) {
824 LOGCHAR(' ');
825 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100826 }
827 break;
828
William Lallemand1d705562012-03-12 12:46:41 +0100829 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100830 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200831 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100832 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100833 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100834 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100835 last_isspace = 0;
836 break;
837
William Lallemand1d705562012-03-12 12:46:41 +0100838 case LOG_FMT_CLIENTIP: // %Ci
William Lallemand5f232402012-04-05 18:02:55 +0200839 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->prod->addr.from,
840 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100841 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100842 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100843 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100844 last_isspace = 0;
845 break;
846
William Lallemand1d705562012-03-12 12:46:41 +0100847 case LOG_FMT_CLIENTPORT: // %Cp
William Lallemand5f232402012-04-05 18:02:55 +0200848 if (s->req->prod->addr.from.ss_family == AF_UNIX) {
849 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
850 } else {
851 ret = lf_port(tmplog, (struct sockaddr *)&s->req->prod->addr.from,
852 dst + maxsize - tmplog, tmp);
853 }
854 if (ret == NULL)
855 goto out;
856 tmplog = ret;
857 last_isspace = 0;
858 break;
859
860 case LOG_FMT_FRONTENDIP: // %Fi
Willy Tarreau59b94792012-05-11 16:16:40 +0200861 si_get_to_addr(s->req->prod);
William Lallemand5f232402012-04-05 18:02:55 +0200862 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->prod->addr.to,
863 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100864 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100865 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100866 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100867 last_isspace = 0;
868 break;
869
William Lallemand5f232402012-04-05 18:02:55 +0200870 case LOG_FMT_FRONTENDPORT: // %Fp
Willy Tarreau59b94792012-05-11 16:16:40 +0200871 si_get_to_addr(s->req->prod);
William Lallemand5f232402012-04-05 18:02:55 +0200872 if (s->req->prod->addr.to.ss_family == AF_UNIX) {
873 ret = ltoa_o(s->listener->luid,
874 tmplog, dst + maxsize - tmplog);
875 } else {
876 ret = lf_port(tmplog, (struct sockaddr *)&s->req->prod->addr.to,
877 dst + maxsize - tmplog, tmp);
878 }
879 if (ret == NULL)
880 goto out;
881 tmplog = ret;
882 last_isspace = 0;
883 break;
884
William Lallemand1d705562012-03-12 12:46:41 +0100885 case LOG_FMT_BACKENDIP: // %Bi
William Lallemand5f232402012-04-05 18:02:55 +0200886 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->cons->addr.from,
887 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100888 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +0100889 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100890 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +0100891 last_isspace = 0;
892 break;
893
William Lallemand1d705562012-03-12 12:46:41 +0100894 case LOG_FMT_BACKENDPORT: // %Bp
William Lallemand5f232402012-04-05 18:02:55 +0200895 ret = lf_port(tmplog, (struct sockaddr *)&s->req->cons->addr.from,
896 dst + maxsize - tmplog, tmp);
897 if (ret == NULL)
898 goto out;
899 tmplog = ret;
900 last_isspace = 0;
901 break;
902
903 case LOG_FMT_SERVERIP: // %Si
904 ret = lf_ip(tmplog, (struct sockaddr *)&s->req->cons->addr.to,
905 dst + maxsize - tmplog, tmp);
906 if (ret == NULL)
907 goto out;
908 tmplog = ret;
909 last_isspace = 0;
910 break;
911
912 case LOG_FMT_SERVERPORT: // %Sp
913 ret = lf_port(tmplog, (struct sockaddr *)&s->req->cons->addr.to,
914 dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100915 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +0100916 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100917 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +0100918 last_isspace = 0;
919 break;
920
William Lallemand1d705562012-03-12 12:46:41 +0100921 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +0100922 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +0200923 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
924 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100925 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100926 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100927 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100928 last_isspace = 0;
929 break;
930
William Lallemand1d705562012-03-12 12:46:41 +0100931 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +0100932 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +0200933 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100934 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100935 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100936 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100937 last_isspace = 0;
938 break;
939
William Lallemand5f232402012-04-05 18:02:55 +0200940 case LOG_FMT_TS: // %Ts
941 get_gmtime(s->logs.accept_date.tv_sec, &tm);
942 if (tmp->options & LOG_OPT_HEXA) {
943 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
944 if (iret < 0 || iret > dst + maxsize - tmplog)
945 goto out;
946 last_isspace = 0;
947 tmplog += iret;
948 } else {
949 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
950 if (ret == NULL)
951 goto out;
952 tmplog = ret;
953 last_isspace = 0;
954 }
955 break;
956
William Lallemand1d705562012-03-12 12:46:41 +0100957 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +0200958 if (tmp->options & LOG_OPT_HEXA) {
959 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
960 if (iret < 0 || iret > dst + maxsize - tmplog)
961 goto out;
962 last_isspace = 0;
963 tmplog += iret;
964 } else {
965 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100966 goto out;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100967 tmplog = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
968 tmplog, 4);
William Lallemand51b5dca2012-03-26 17:52:55 +0200969 if (!tmplog)
970 goto out;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100971 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +0200972 }
973 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100974
William Lallemand1d705562012-03-12 12:46:41 +0100975 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +0100976 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +0200977 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100978 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100979 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100980 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +0200981 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100982 break;
983
William Lallemand1d705562012-03-12 12:46:41 +0100984 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +0100985 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +0200986 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100987 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100988 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100989 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +0200990 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100991 break;
992
William Lallemand1d705562012-03-12 12:46:41 +0100993 case LOG_FMT_SERVER: // %s
William Lallemandbddd4fd2012-02-27 11:23:10 +0100994 src = (char *)svid;
William Lallemand5f232402012-04-05 18:02:55 +0200995 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100996 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100997 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100998 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100999 last_isspace = 0;
1000 break;
1001
William Lallemand1d705562012-03-12 12:46:41 +01001002 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001003 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001004 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001005 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001006 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001007 last_isspace = 0;
1008 break;
1009
William Lallemand1d705562012-03-12 12:46:41 +01001010 case LOG_FMT_TW: // %Tw
1011 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001012 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001013 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001014 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001015 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001016 last_isspace = 0;
1017 break;
1018
William Lallemand1d705562012-03-12 12:46:41 +01001019 case LOG_FMT_TC: // %Tc
1020 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001021 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001022 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001023 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001024 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001025 last_isspace = 0;
1026 break;
1027
William Lallemand1d705562012-03-12 12:46:41 +01001028 case LOG_FMT_TR: // %Tr
1029 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001030 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001031 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001032 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001033 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001034 last_isspace = 0;
1035 break;
1036
William Lallemand1d705562012-03-12 12:46:41 +01001037 case LOG_FMT_TT: // %Tt
William Lallemandbddd4fd2012-02-27 11:23:10 +01001038 if (!(tolog & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001039 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001040 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
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_STATUS: // %st
William Lallemand5f232402012-04-05 18:02:55 +02001048 ret = ltoa_o(txn->status, 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_BYTES: // %B
William Lallemandbddd4fd2012-02-27 11:23:10 +01001056 if (!(tolog & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001057 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001058 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001059 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001060 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001061 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001062 last_isspace = 0;
1063 break;
1064
William Lallemand1d705562012-03-12 12:46:41 +01001065 case LOG_FMT_CCLIENT: // %cc
William Lallemandbddd4fd2012-02-27 11:23:10 +01001066 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001067 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001068 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001069 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001070 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001071 last_isspace = 0;
1072 break;
1073
William Lallemand1d705562012-03-12 12:46:41 +01001074 case LOG_FMT_CSERVER: // %cs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001075 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001076 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001077 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001078 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001079 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001080 last_isspace = 0;
1081 break;
1082
William Lallemand1d705562012-03-12 12:46:41 +01001083 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +01001084 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1085 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
1086 *tmplog = '\0';
1087 last_isspace = 0;
1088 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001089
William Lallemand1d705562012-03-12 12:46:41 +01001090 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001091 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1092 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001093 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1094 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 +01001095 last_isspace = 0;
1096 break;
1097
William Lallemand1d705562012-03-12 12:46:41 +01001098 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001099 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001100 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001101 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001102 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001103 last_isspace = 0;
1104 break;
1105
William Lallemand1d705562012-03-12 12:46:41 +01001106 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001107 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001108 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001109 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001110 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001111 last_isspace = 0;
1112 break;
1113
William Lallemand1d705562012-03-12 12:46:41 +01001114 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001115 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001116 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001117 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001118 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001119 last_isspace = 0;
1120 break;
1121
William Lallemand1d705562012-03-12 12:46:41 +01001122 case LOG_FMT_SRVCONN: // %sc
1123 ret = ultoa_o(target_srv(&s->target) ?
1124 target_srv(&s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001125 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001126 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001127 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001128 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001129 last_isspace = 0;
1130 break;
1131
William Lallemand1d705562012-03-12 12:46:41 +01001132 case LOG_FMT_RETRIES: // %rq
William Lallemandbddd4fd2012-02-27 11:23:10 +01001133 if (s->flags & SN_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001134 LOGCHAR('+');
1135 ret = ltoa_o((s->req->cons->conn_retries>0) ?
1136 (be->conn_retries - s->req->cons->conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001137 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001138 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001139 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001140 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001141 last_isspace = 0;
1142 break;
1143
William Lallemand1d705562012-03-12 12:46:41 +01001144 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001145 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001146 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001147 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001148 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001149 last_isspace = 0;
1150 break;
1151
William Lallemand1d705562012-03-12 12:46:41 +01001152 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001153 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001154 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001155 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001156 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001157 last_isspace = 0;
1158 break;
1159
William Lallemand1d705562012-03-12 12:46:41 +01001160 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001161 /* request header */
1162 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1163 if (tmp->options & LOG_OPT_QUOTE)
1164 LOGCHAR('"');
1165 LOGCHAR('{');
1166 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1167 if (hdr)
1168 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001169 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001170 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001171 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001172 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001173 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001174 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001175 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001176 }
1177 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001178 if (tmp->options & LOG_OPT_QUOTE)
1179 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001180 last_isspace = 0;
William Lallemand1d705562012-03-12 12:46:41 +01001181 if (tmp->options & LOG_OPT_QUOTE)
1182 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001183 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001184 break;
1185
William Lallemand1d705562012-03-12 12:46:41 +01001186 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001187 /* request header list */
1188 if (fe->to_log & LW_REQHDR && txn->req.cap) {
1189 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1190 if (hdr > 0)
1191 LOGCHAR(' ');
1192 if (tmp->options & LOG_OPT_QUOTE)
1193 LOGCHAR('"');
1194 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001195 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001196 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001197 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001198 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001199 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001200 } else if (!(tmp->options & LOG_OPT_QUOTE))
1201 LOGCHAR('-');
1202 if (tmp->options & LOG_OPT_QUOTE)
1203 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001204 last_isspace = 0;
1205 }
1206 }
1207 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001208
William Lallemand1d705562012-03-12 12:46:41 +01001209
1210 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001211 /* response header */
1212 if (fe->to_log & LW_RSPHDR &&
1213 txn->rsp.cap) {
1214 if (tmp->options & LOG_OPT_QUOTE)
1215 LOGCHAR('"');
1216 LOGCHAR('{');
1217 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1218 if (hdr)
1219 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001220 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001221 ret = encode_string(tmplog, dst + maxsize,
1222 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1223 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001224 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001225 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001226 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001227 }
1228 LOGCHAR('}');
1229 last_isspace = 0;
1230 if (tmp->options & LOG_OPT_QUOTE)
1231 LOGCHAR('"');
1232 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001233 break;
1234
William Lallemand1d705562012-03-12 12:46:41 +01001235 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001236 /* response header list */
1237 if (fe->to_log & LW_RSPHDR && txn->rsp.cap) {
1238 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1239 if (hdr > 0)
1240 LOGCHAR(' ');
1241 if (tmp->options & LOG_OPT_QUOTE)
1242 LOGCHAR('"');
1243 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001244 ret = encode_string(tmplog, dst + maxsize,
1245 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1246 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001247 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001248 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001249 } else if (!(tmp->options & LOG_OPT_QUOTE))
1250 LOGCHAR('-');
1251 if (tmp->options & LOG_OPT_QUOTE)
1252 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001253 last_isspace = 0;
1254 }
1255 }
1256 break;
1257
William Lallemand1d705562012-03-12 12:46:41 +01001258 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001259 /* Request */
1260 if (tmp->options & LOG_OPT_QUOTE)
1261 LOGCHAR('"');
1262 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001263 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001264 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001265 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001266 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001267 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001268 if (tmp->options & LOG_OPT_QUOTE)
1269 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001270 last_isspace = 0;
1271 break;
William Lallemand5f232402012-04-05 18:02:55 +02001272
1273 case LOG_FMT_COUNTER: // %rt
1274 if (tmp->options & LOG_OPT_HEXA) {
1275 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", global.req_count);
1276 if (iret < 0 || iret > dst + maxsize - tmplog)
1277 goto out;
1278 last_isspace = 0;
1279 tmplog += iret;
1280 } else {
1281 ret = ltoa_o(global.req_count, tmplog, dst + maxsize - tmplog);
1282 if (ret == NULL)
1283 goto out;
1284 tmplog = ret;
1285 last_isspace = 0;
1286 }
1287 break;
1288
1289 case LOG_FMT_HOSTNAME: // %H
1290 src = hostname;
1291 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1292 if (ret == NULL)
1293 goto out;
1294 tmplog = ret;
1295 last_isspace = 0;
1296 break;
1297
1298 case LOG_FMT_PID: // %pid
1299 if (tmp->options & LOG_OPT_HEXA) {
1300 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1301 if (iret < 0 || iret > dst + maxsize - tmplog)
1302 goto out;
1303 last_isspace = 0;
1304 tmplog += iret;
1305 } else {
1306 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1307 if (ret == NULL)
1308 goto out;
1309 tmplog = ret;
1310 last_isspace = 0;
1311 }
1312 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001313
1314 case LOG_FMT_UNIQUEID: // %ID
1315 src = s->unique_id;
1316 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
1317 if (ret == NULL)
1318 goto out;
1319 tmplog = ret;
1320 last_isspace = 0;
1321 break;
1322
William Lallemandbddd4fd2012-02-27 11:23:10 +01001323 }
1324 }
1325
1326out:
William Lallemand1d705562012-03-12 12:46:41 +01001327 /* *tmplog is a unused character */
1328 *tmplog = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +01001329
William Lallemand1d705562012-03-12 12:46:41 +01001330 return tmplog - dst + 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001331
Willy Tarreaubaaee002006-06-26 02:48:02 +02001332}
1333
William Lallemand1d705562012-03-12 12:46:41 +01001334/*
1335 * send a log for the session when we have enough info about it.
1336 * Will not log if the frontend has no log defined.
1337 */
1338void sess_log(struct session *s)
1339{
1340 char *tmplog;
1341 int size, err, level;
1342
1343 /* if we don't want to log normal traffic, return now */
1344 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1345 (s->req->cons->conn_retries != s->be->conn_retries) ||
1346 ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001347
William Lallemand1d705562012-03-12 12:46:41 +01001348 if (!err && (s->fe->options2 & PR_O2_NOLOGNORM))
1349 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001350
William Lallemand1d705562012-03-12 12:46:41 +01001351 if (LIST_ISEMPTY(&s->fe->logsrvs))
1352 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001353
William Lallemand1d705562012-03-12 12:46:41 +01001354 level = LOG_INFO;
1355 if (err && (s->fe->options2 & PR_O2_LOGERRORS))
1356 level = LOG_ERR;
1357
1358 tmplog = update_log_hdr();
1359 size = tmplog - logline;
1360 size += build_logline(s, tmplog, sizeof(logline) - size, &s->fe->logformat);
1361 if (size > 0) {
1362 __send_log(s->fe, level, logline, size);
1363 s->logs.logwait = 0;
1364 }
1365}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001366
Willy Tarreaubaaee002006-06-26 02:48:02 +02001367/*
1368 * Local variables:
1369 * c-indent-level: 8
1370 * c-basic-offset: 8
1371 * End:
1372 */