blob: ef89dd3b4c88a46251bcabd46e74bc077e62591d [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 Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
25
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020027#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020029#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010032#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020033
William Lallemand5f232402012-04-05 18:02:55 +020034#include <proto/frontend.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020035#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010036#include <proto/sample.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010037#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020038#ifdef USE_OPENSSL
39#include <proto/ssl_sock.h>
40#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreaubaaee002006-06-26 02:48:02 +020042const char *log_facilities[NB_LOG_FACILITIES] = {
43 "kern", "user", "mail", "daemon",
44 "auth", "syslog", "lpr", "news",
45 "uucp", "cron", "auth2", "ftp",
46 "ntp", "audit", "alert", "cron2",
47 "local0", "local1", "local2", "local3",
48 "local4", "local5", "local6", "local7"
49};
50
51
52const char *log_levels[NB_LOG_LEVELS] = {
53 "emerg", "alert", "crit", "err",
54 "warning", "notice", "info", "debug"
55};
56
Willy Tarreau570f2212013-06-10 16:42:09 +020057const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +020058const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
William Lallemand723b73a2012-02-08 16:37:49 +010060
61/* log_format */
62struct logformat_type {
63 char *name;
64 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010065 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020066 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010067 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +010068 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +010069};
70
William Lallemandb7ff6a32012-03-02 14:35:21 +010071int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
72
William Lallemand723b73a2012-02-08 16:37:49 +010073/* log_format variable names */
74static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020075 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +010076
77 /* please keep these lines sorted ! */
78 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
79 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
80 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
81 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
82 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
83 { "ST", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +020084 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +010085 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +080086 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +020087 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +020088 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +010089 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +020090 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +010091 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
92 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +020093 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010094 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +020095 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010096 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
97 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +020098 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +010099 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
100 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
101 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
102 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
103 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
104 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
105 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
William Lallemand5e19a282012-04-02 16:22:10 +0200106 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request */
William Lallemand5e19a282012-04-02 16:22:10 +0200107 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request list */
Willy Tarreau2beef582012-12-20 17:22:52 +0100108 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response */
William Lallemand5e19a282012-04-02 16:22:10 +0200109 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response list */
Willy Tarreau2beef582012-12-20 17:22:52 +0100110 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200111 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100112 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
113 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
William Lallemand5e19a282012-04-02 16:22:10 +0200114 { "rt", LOG_FMT_COUNTER, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP request counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100115 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
116 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
117 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
118 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
119 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200120 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
121 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100122 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
123 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
124 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
125
126 /* The following tags are deprecated and will be removed soon */
127 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
128 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
129 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
130 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
131 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
132 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
133 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
134 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
135 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
136 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
137 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200138 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100139};
140
Willy Tarreau2beef582012-12-20 17:22:52 +0100141char 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
142char 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";
143char 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 +0100144char *log_format = NULL;
145
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100146/* This is a global syslog line, common to all outgoing messages. It begins
147 * with the syslog tag and the date that are updated by update_log_hdr().
148 */
149static char logline[MAX_SYSLOG_LEN];
150
William Lallemand723b73a2012-02-08 16:37:49 +0100151struct logformat_var_args {
152 char *name;
153 int mask;
154};
155
156struct logformat_var_args var_args_list[] = {
157// global
158 { "M", LOG_OPT_MANDATORY },
159 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200160 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100161 { 0, 0 }
162};
163
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200164/* return the name of the directive used in the current proxy for which we're
165 * currently parsing a header, when it is known.
166 */
167static inline const char *fmt_directive(const struct proxy *curproxy)
168{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100169 switch (curproxy->conf.args.ctx) {
170 case ARGC_UIF:
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200171 return "unique-id-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100172 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100173 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100174 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100175 return "http-response";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100176 case ARGC_STK:
177 return "stick";
178 case ARGC_TRK:
179 return "track-sc"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100180 case ARGC_RDR:
181 return "redirect"; break;
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100182 case ARGC_ACL:
183 return "acl"; break;
184 default:
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200185 return "log-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100186 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200187}
188
William Lallemand723b73a2012-02-08 16:37:49 +0100189/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100190 * callback used to configure addr source retrieval
191 */
192int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
193{
194 curproxy->options2 |= PR_O2_SRC_ADDR;
195
196 return 0;
197}
198
199
200/*
William Lallemand723b73a2012-02-08 16:37:49 +0100201 * Parse args in a logformat_var
202 */
203int parse_logformat_var_args(char *args, struct logformat_node *node)
204{
205 int i = 0;
206 int end = 0;
207 int flags = 0; // 1 = + 2 = -
208 char *sp = NULL; // start pointer
209
210 if (args == NULL)
211 return 1;
212
213 while (1) {
214 if (*args == '\0')
215 end = 1;
216
217 if (*args == '+') {
218 // add flag
219 sp = args + 1;
220 flags = 1;
221 }
222 if (*args == '-') {
223 // delete flag
224 sp = args + 1;
225 flags = 2;
226 }
227
228 if (*args == '\0' || *args == ',') {
229 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100230 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100231 if (strcmp(sp, var_args_list[i].name) == 0) {
232 if (flags == 1) {
233 node->options |= var_args_list[i].mask;
234 break;
235 } else if (flags == 2) {
236 node->options &= ~var_args_list[i].mask;
237 break;
238 }
239 }
240 }
241 sp = NULL;
242 if (end)
243 break;
244 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100245 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100246 }
247 return 0;
248}
249
250/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100251 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
252 * must pass the args part in the <arg> pointer with its length in <arg_len>,
253 * and varname with its length in <var> and <var_len> respectively. <arg> is
254 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100255 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100256int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions)
William Lallemand723b73a2012-02-08 16:37:49 +0100257{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100258 int j;
259 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100260
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100261 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
262 if (strlen(logformat_keywords[j].name) == var_len &&
263 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
264 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
265 node = calloc(1, sizeof(struct logformat_node));
266 node->type = logformat_keywords[j].type;
267 node->options = *defoptions;
268 if (arg_len) {
269 node->arg = my_strndup(arg, arg_len);
270 parse_logformat_var_args(node->arg, node);
271 }
272 if (node->type == LOG_FMT_GLOBAL) {
273 *defoptions = node->options;
274 free(node->arg);
275 free(node);
276 } else {
277 if (logformat_keywords[j].config_callback &&
278 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100279 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100280 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100281 curproxy->to_log |= logformat_keywords[j].lw;
282 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100283 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100284 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100285 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200286 curproxy->conf.args.file, curproxy->conf.args.line,
287 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100288 return 0;
289 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100290 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200291 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
292 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100293 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100294 }
William Lallemand723b73a2012-02-08 16:37:49 +0100295 }
296 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100297
298 j = var[var_len];
299 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100300 Warning("parsing [%s:%d] : no such format variable '%s' in '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%' in log-format expressions.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200301 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100302 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100303 return -1;
304}
305
306/*
307 * push to the logformat linked list
308 *
309 * start: start pointer
310 * end: end text pointer
311 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100312 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100313 *
314 * LOG_TEXT: copy chars from start to end excluding end.
315 *
316*/
William Lallemand1d705562012-03-12 12:46:41 +0100317void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100318{
319 char *str;
320
Willy Tarreaua3571662012-12-20 21:59:12 +0100321 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100322 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100323 str = calloc(end - start + 1, 1);
324 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100325 str[end - start] = '\0';
326 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100327 node->type = LOG_FMT_TEXT; // type string
328 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100329 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100330 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100331 node->type = LOG_FMT_SEPARATOR;
332 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100333 }
334}
335
336/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100337 * Parse the sample fetch expression <text> and add a node to <list_format> upon
338 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200339 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100340 */
Willy Tarreau434c57c2013-01-08 01:10:24 +0100341void add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap)
Willy Tarreauc8368452012-12-21 00:09:23 +0100342{
343 char *cmd[2];
344 struct sample_expr *expr;
345 struct logformat_node *node;
346 int cmd_arg;
347
348 cmd[0] = text;
349 cmd[1] = "";
350 cmd_arg = 0;
351
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200352 expr = sample_parse_expr(cmd, &cmd_arg, trash.str, trash.size, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100353 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200354 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
355 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
356 text, trash.str);
Willy Tarreauc8368452012-12-21 00:09:23 +0100357 return;
358 }
359
360 node = calloc(1, sizeof(struct logformat_node));
361 node->type = LOG_FMT_EXPR;
362 node->expr = expr;
363 node->options = options;
364
365 if (arg_len) {
366 node->arg = my_strndup(arg, arg_len);
367 parse_logformat_var_args(node->arg, node);
368 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100369 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100370 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
371
Willy Tarreau434c57c2013-01-08 01:10:24 +0100372 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100373 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
374
Willy Tarreau434c57c2013-01-08 01:10:24 +0100375 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200376 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
377 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
378 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100379
Willy Tarreauc8368452012-12-21 00:09:23 +0100380 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
381 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100382 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100383
Willy Tarreau1f31c732013-01-10 16:22:27 +0100384 /* FIXME: temporary workaround for missing LW_XPRT flag needed with some
385 * sample fetches (eg: ssl*). We always set it for now on, but this will
386 * leave with sample capabilities soon.
387 */
388 curpx->to_log |= LW_XPRT;
Willy Tarreauc8368452012-12-21 00:09:23 +0100389 LIST_ADDQ(list_format, &node->list);
390}
391
392/*
William Lallemand723b73a2012-02-08 16:37:49 +0100393 * Parse the log_format string and fill a linked list.
394 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200395 * You can set arguments using { } : %{many arguments}varname.
396 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100397 *
398 * str: the string to parse
399 * curproxy: the proxy affected
400 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100401 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100402 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100403 */
Willy Tarreau434c57c2013-01-08 01:10:24 +0100404void parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap)
William Lallemand723b73a2012-02-08 16:37:49 +0100405{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100406 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100407 char *arg = NULL; /* start pointer for args */
408 char *var = NULL; /* start pointer for vars */
409 int arg_len = 0;
410 int var_len = 0;
411 int cformat; /* current token format */
412 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100413 struct logformat_node *tmplf, *back;
414
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100415 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200416 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200417
William Lallemand723b73a2012-02-08 16:37:49 +0100418 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100419 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100420 LIST_DEL(&tmplf->list);
421 free(tmplf);
422 }
423
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100424 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100425 pformat = cformat;
426
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100427 if (!*str)
428 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100429
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100430 /* The prinicple of the two-step state machine below is to first detect a change, and
431 * second have all common paths processed at one place. The common paths are the ones
432 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
433 * We use the common LF_INIT state to dispatch to the different final states.
434 */
435 switch (pformat) {
436 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100437 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100438 arg_len = var_len = 0;
439 if (*str == '{') { // optional argument
440 cformat = LF_STARG;
441 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100442 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100443 else if (*str == '[') {
444 cformat = LF_STEXPR;
445 var = str + 1; // store expr in variable name
446 }
Willy Tarreau06d97f92013-12-02 17:45:48 +0100447 else if (isalpha((int)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100448 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100449 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100450 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100451 else if (*str == '%')
452 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau9eba36b2013-12-03 00:51:09 +0100453 else if (isdigit(*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100454 /* single '%' followed by blank or digit, send them both */
455 cformat = LF_TEXT;
456 pformat = LF_TEXT; /* finally we include the previous char as well */
457 sp = str - 1; /* send both the '%' and the current char */
458 Warning("parsing [%s:%d] : Fixed missing '%%' before '%c' at position %d in %s line : '%s'. Please use '%%%%' when you need the '%%' character in a log-format expression.\n",
459 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
460
461 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100462 else
463 cformat = LF_INIT; // handle other cases of litterals
464 break;
465
466 case LF_STARG: // text immediately following '%{'
467 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100468 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100469 arg_len = str - arg;
470 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100471 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100472 break;
473
474 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100475 if (*str == '[') {
476 cformat = LF_STEXPR;
477 var = str + 1; // store expr in variable name
478 break;
479 }
480 else if (isalnum((int)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100481 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100482 var = str;
483 break;
484 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200485 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
486 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100487 cformat = LF_INIT;
488 break;
489
Willy Tarreauc8368452012-12-21 00:09:23 +0100490 case LF_STEXPR: // text immediately following '%['
491 if (*str == ']') { // end of arg
492 cformat = LF_EDEXPR;
493 var_len = str - var;
494 *str = 0; // needed for parsing the expression
495 }
496 break;
497
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100498 case LF_VAR: // text part of a variable name
499 var_len = str - var;
500 if (!isalnum((int)*str))
501 cformat = LF_INIT; // not variable name anymore
502 break;
503
Willy Tarreauc8368452012-12-21 00:09:23 +0100504 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100505 cformat = LF_INIT;
506 }
507
508 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
509 switch (*str) {
510 case '%': cformat = LF_STARTVAR; break;
511 case ' ': cformat = LF_SEPARATOR; break;
512 case 0 : cformat = LF_END; break;
513 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100514 }
515 }
516
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100517 if (cformat != pformat || pformat == LF_SEPARATOR) {
518 switch (pformat) {
519 case LF_VAR:
520 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
521 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100522 case LF_STEXPR:
Willy Tarreau434c57c2013-01-08 01:10:24 +0100523 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap);
Willy Tarreauc8368452012-12-21 00:09:23 +0100524 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100525 case LF_TEXT:
526 case LF_SEPARATOR:
527 add_to_logformat_list(sp, str, pformat, list_format);
528 break;
529 }
530 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100531 }
532 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100533
Willy Tarreauc8368452012-12-21 00:09:23 +0100534 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200535 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
536 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
537 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100538
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100539 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100540}
541
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542/*
543 * Displays the message on stderr with the date and pid. Overrides the quiet
544 * mode during startup.
545 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200546void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547{
548 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200549 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550
551 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
552 va_start(argp, fmt);
553
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200554 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200556 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200557 vfprintf(stderr, fmt, argp);
558 fflush(stderr);
559 va_end(argp);
560 }
561}
562
563
564/*
565 * Displays the message on stderr with the date and pid.
566 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200567void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568{
569 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200570 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571
572 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
573 va_start(argp, fmt);
574
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200575 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200576 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200577 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578 vfprintf(stderr, fmt, argp);
579 fflush(stderr);
580 va_end(argp);
581 }
582}
583
584/*
585 * Displays the message on <out> only if quiet mode is not set.
586 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200587void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200588{
589 va_list argp;
590
591 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
592 va_start(argp, fmt);
593 vfprintf(out, fmt, argp);
594 fflush(out);
595 va_end(argp);
596 }
597}
598
599/*
600 * returns log level for <lev> or -1 if not found.
601 */
602int get_log_level(const char *lev)
603{
604 int level;
605
606 level = NB_LOG_LEVELS - 1;
607 while (level >= 0 && strcmp(log_levels[level], lev))
608 level--;
609
610 return level;
611}
612
613
614/*
615 * returns log facility for <fac> or -1 if not found.
616 */
617int get_log_facility(const char *fac)
618{
619 int facility;
620
621 facility = NB_LOG_FACILITIES - 1;
622 while (facility >= 0 && strcmp(log_facilities[facility], fac))
623 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100624
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625 return facility;
626}
627
William Lallemanda1cc3812012-02-08 16:38:44 +0100628/*
629 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200630 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100631 *
632 * Return the adress of the \0 character, or NULL on error
633 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100634char *lf_text_len(char *dst, const char *src, size_t len, size_t size, struct logformat_node *node)
William Lallemanda1cc3812012-02-08 16:38:44 +0100635{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100636 if (size < 2)
637 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100638
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100639 if (node->options & LOG_OPT_QUOTE) {
640 *(dst++) = '"';
641 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100642 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100643
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100644 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100645 if (++len > size)
646 len = size;
647 len = strlcpy2(dst, src, len);
648
649 size -= len;
650 dst += len;
651 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100652 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
653 if (size < 2)
654 return NULL;
655 *(dst++) = '-';
656 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100657
658 if (node->options & LOG_OPT_QUOTE) {
659 if (size < 2)
660 return NULL;
661 *(dst++) = '"';
662 }
663
664 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100665 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100666}
667
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100668static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
669{
670 return lf_text_len(dst, src, size, size, node);
671}
672
William Lallemand5f232402012-04-05 18:02:55 +0200673/*
674 * Write a IP adress to the log string
675 * +X option write in hexadecimal notation, most signifant byte on the left
676 */
677char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
678{
679 char *ret = dst;
680 int iret;
681 char pn[INET6_ADDRSTRLEN];
682
683 if (node->options & LOG_OPT_HEXA) {
684 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
685 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
686 if (iret < 0 || iret > size)
687 return NULL;
688 ret += iret;
689 } else {
690 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
691 ret = lf_text(dst, pn, size, node);
692 if (ret == NULL)
693 return NULL;
694 }
695 return ret;
696}
697
698/*
699 * Write a port to the log
700 * +X option write in hexadecimal notation, most signifant byte on the left
701 */
702char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
703{
704 char *ret = dst;
705 int iret;
706
707 if (node->options & LOG_OPT_HEXA) {
708 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
709 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
710 if (iret < 0 || iret > size)
711 return NULL;
712 ret += iret;
713 } else {
714 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
715 if (ret == NULL)
716 return NULL;
717 }
718 return ret;
719}
720
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100721/* Re-generate the syslog header at the beginning of logline once a second and
722 * return the pointer to the first character after the header.
723 */
724static char *update_log_hdr()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100726 static long tvsec;
727 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200729 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200731 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100732 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200733
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200734 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200735 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200736
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100737 hdr_len = snprintf(logline, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100738 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200739 monthname[tm.tm_mon],
740 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100741 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000742 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 /* WARNING: depending upon implementations, snprintf may return
744 * either -1 or the number of bytes that would be needed to store
745 * the total message. In both cases, we must adjust it.
746 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100747 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
748 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200749
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100750 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751 }
752
William Lallemand2a4a44f2012-02-06 16:00:33 +0100753 return dataptr;
754}
755
756/*
757 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100758 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100759 */
760void send_log(struct proxy *p, int level, const char *format, ...)
761{
762 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100763 char *dataptr;
764 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100765
766 if (level < 0 || format == NULL)
767 return;
768
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100769 dataptr = update_log_hdr(); /* update log header and skip it */
770 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100771
772 va_start(argp, format);
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100773 data_len += vsnprintf(dataptr, logline + sizeof(logline) - dataptr, format, argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100774 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
775 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100777
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100778 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100779}
780
781/*
782 * This function sends a syslog message.
783 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100784 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100785 */
786void __send_log(struct proxy *p, int level, char *message, size_t size)
787{
788 static int logfdunix = -1; /* syslog to AF_UNIX socket */
789 static int logfdinet = -1; /* syslog to AF_INET socket */
790 static char *dataptr = NULL;
791 int fac_level;
792 struct list *logsrvs = NULL;
793 struct logsrv *tmp = NULL;
794 int nblogger;
795 char *log_ptr;
796
797 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200798
799 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200800 if (!LIST_ISEMPTY(&global.logsrvs)) {
801 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802 }
803 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200804 if (!LIST_ISEMPTY(&p->logsrvs)) {
805 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200806 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100807 }
808
William Lallemand0f99e342011-10-12 17:50:54 +0200809 if (!logsrvs)
810 return;
811
William Lallemand2a4a44f2012-02-06 16:00:33 +0100812 message[size - 1] = '\n';
813
Robert Tsai81ae1952007-12-05 10:47:29 +0100814 /* Lazily set up syslog sockets for protocol families of configured
815 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200816 nblogger = 0;
817 list_for_each_entry(tmp, logsrvs, list) {
818 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100819 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200820
David du Colombier11bcb6c2011-03-24 12:23:00 +0100821 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100822 proto = 0;
823 plogfd = &logfdunix;
824 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100825 proto = IPPROTO_UDP;
826 plogfd = &logfdinet;
827 }
828 if (*plogfd >= 0) {
829 /* socket already created. */
830 continue;
831 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100832 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100833 proto)) < 0) {
834 Alert("socket for logger #%d failed: %s (errno=%d)\n",
835 nblogger + 1, strerror(errno), errno);
836 return;
837 }
838 /* we don't want to receive anything on this socket */
839 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
840 /* does nothing under Linux, maybe needed for others */
841 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200842 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843 }
844
Robert Tsai81ae1952007-12-05 10:47:29 +0100845 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200846 nblogger = 0;
847 list_for_each_entry(tmp, logsrvs, list) {
848 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100849 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100850 &logfdunix : &logfdinet;
851 int sent;
852
Willy Tarreaubaaee002006-06-26 02:48:02 +0200853 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200854 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200855 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100856
Willy Tarreaubaaee002006-06-26 02:48:02 +0200857 /* For each target, we may have a different facility.
858 * We can also have a different log level for each message.
859 * This induces variations in the message header length.
860 * Since we don't want to recompute it each time, nor copy it every
861 * time, we only change the facility in the pre-computed header,
862 * and we change the pointer to the header accordingly.
863 */
William Lallemand0f99e342011-10-12 17:50:54 +0200864 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100865 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866 do {
867 *log_ptr = '0' + fac_level % 10;
868 fac_level /= 10;
869 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100870 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200871 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100872
William Lallemandafeb9872013-08-30 14:17:46 +0200873 sent = sendto(*plogfd, log_ptr, size - (log_ptr - dataptr),
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200874 MSG_DONTWAIT | MSG_NOSIGNAL,
875 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100876 if (sent < 0) {
877 Alert("sendto logger #%d failed: %s (errno=%d)\n",
878 nblogger, strerror(errno), errno);
879 }
William Lallemand0f99e342011-10-12 17:50:54 +0200880 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200881 }
882}
883
William Lallemandbddd4fd2012-02-27 11:23:10 +0100884extern fd_set hdr_encode_map[];
885extern fd_set url_encode_map[];
886
Willy Tarreaubaaee002006-06-26 02:48:02 +0200887
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200888const 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 +0100889const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
890 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
891 Set-cookie Updated, unknown, unknown */
892
William Lallemand1d705562012-03-12 12:46:41 +0100893/*
894 * try to write a character if there is enough space, or goto out
895 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100896#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100897 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100898 *(tmplog++) = (x); \
899 } else { \
900 goto out; \
901 } \
902 } while(0)
903
William Lallemand1d705562012-03-12 12:46:41 +0100904
Willy Tarreaudf974472012-12-28 02:44:01 +0100905/* Builds a log line in <dst> based on <list_format>, and stops before reaching
906 * <maxsize> characters. Returns the size of the output string in characters,
907 * not counting the trailing zero which is always added if the resulting size
908 * is not zero.
909 */
William Lallemand1d705562012-03-12 12:46:41 +0100910int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200911{
Willy Tarreau73de9892006-11-30 11:40:23 +0100912 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100913 struct proxy *be = s->be;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100914 struct http_txn *txn = &s->txn;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100915 char *uri;
Willy Tarreaufe944602007-10-25 10:34:16 +0200916 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100917 int t_request;
918 int hdr;
919 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100920 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100921 char *ret;
922 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100923 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924
William Lallemandbddd4fd2012-02-27 11:23:10 +0100925 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926
William Lallemandbddd4fd2012-02-27 11:23:10 +0100927 t_request = -1;
928 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
929 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
930
William Lallemand1d705562012-03-12 12:46:41 +0100931 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200932
William Lallemandbddd4fd2012-02-27 11:23:10 +0100933 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100934 if (LIST_ISEMPTY(list_format))
935 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100936
William Lallemand1d705562012-03-12 12:46:41 +0100937 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200938 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +0200939 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100940 struct sample *key;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100941
Willy Tarreauc8368452012-12-21 00:09:23 +0100942 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +0100943 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100944 if (!last_isspace) {
945 LOGCHAR(' ');
946 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100947 }
948 break;
949
William Lallemand1d705562012-03-12 12:46:41 +0100950 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100951 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200952 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100953 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100954 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100955 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100956 last_isspace = 0;
957 break;
958
Willy Tarreauc8368452012-12-21 00:09:23 +0100959 case LOG_FMT_EXPR: // sample expression, may be request or response
960 key = NULL;
961 if (tmp->options & LOG_OPT_REQ_CAP)
962 key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr);
963 if (!key && (tmp->options & LOG_OPT_RES_CAP))
964 key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr);
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100965 ret = lf_text_len(tmplog, key ? key->data.str.str : NULL, key ? key->data.str.len : 0, dst + maxsize - tmplog, tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +0100966 if (ret == 0)
967 goto out;
968 tmplog = ret;
969 last_isspace = 0;
970 break;
971
Willy Tarreau2beef582012-12-20 17:22:52 +0100972 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200973 conn = objt_conn(s->req->prod->end);
974 if (conn)
975 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
976 else
977 ret = lf_text_len(tmplog, NULL, 0, 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 Lallemandbddd4fd2012-02-27 11:23:10 +0100981 last_isspace = 0;
982 break;
983
Willy Tarreau2beef582012-12-20 17:22:52 +0100984 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200985 conn = objt_conn(s->req->prod->end);
986 if (conn) {
987 if (conn->addr.from.ss_family == AF_UNIX) {
988 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
989 } else {
990 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
991 dst + maxsize - tmplog, tmp);
992 }
William Lallemand5f232402012-04-05 18:02:55 +0200993 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200994 else
995 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
996
William Lallemand5f232402012-04-05 18:02:55 +0200997 if (ret == NULL)
998 goto out;
999 tmplog = ret;
1000 last_isspace = 0;
1001 break;
1002
Willy Tarreau2beef582012-12-20 17:22:52 +01001003 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001004 conn = objt_conn(s->req->prod->end);
1005 if (conn) {
1006 conn_get_to_addr(conn);
1007 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1008 }
1009 else
1010 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1011
William Lallemand1d705562012-03-12 12:46:41 +01001012 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001013 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001014 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001015 last_isspace = 0;
1016 break;
1017
Willy Tarreau2beef582012-12-20 17:22:52 +01001018 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001019 conn = objt_conn(s->req->prod->end);
1020 if (conn) {
1021 conn_get_to_addr(conn);
1022 if (conn->addr.to.ss_family == AF_UNIX)
1023 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
1024 else
1025 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001026 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001027 else
1028 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1029
William Lallemand5f232402012-04-05 18:02:55 +02001030 if (ret == NULL)
1031 goto out;
1032 tmplog = ret;
1033 last_isspace = 0;
1034 break;
1035
Willy Tarreau2beef582012-12-20 17:22:52 +01001036 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001037 conn = objt_conn(s->req->cons->end);
1038 if (conn)
1039 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1040 else
1041 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1042
William Lallemand1d705562012-03-12 12:46:41 +01001043 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001044 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001045 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001046 last_isspace = 0;
1047 break;
1048
Willy Tarreau2beef582012-12-20 17:22:52 +01001049 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001050 conn = objt_conn(s->req->cons->end);
1051 if (conn)
1052 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1053 else
1054 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1055
William Lallemand5f232402012-04-05 18:02:55 +02001056 if (ret == NULL)
1057 goto out;
1058 tmplog = ret;
1059 last_isspace = 0;
1060 break;
1061
Willy Tarreau2beef582012-12-20 17:22:52 +01001062 case LOG_FMT_SERVERIP: // %si
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001063 conn = objt_conn(s->req->cons->end);
1064 if (conn)
1065 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1066 else
1067 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1068
William Lallemand5f232402012-04-05 18:02:55 +02001069 if (ret == NULL)
1070 goto out;
1071 tmplog = ret;
1072 last_isspace = 0;
1073 break;
1074
Willy Tarreau2beef582012-12-20 17:22:52 +01001075 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001076 conn = objt_conn(s->req->cons->end);
1077 if (conn)
1078 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1079 else
1080 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1081
William Lallemand1d705562012-03-12 12:46:41 +01001082 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001083 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001084 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001085 last_isspace = 0;
1086 break;
1087
William Lallemand1d705562012-03-12 12:46:41 +01001088 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001089 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001090 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1091 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001092 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001093 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001094 tmplog = ret;
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_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001099 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001100 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001101 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001102 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001103 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001104 last_isspace = 0;
1105 break;
1106
Yuxans Yao4e25b012012-10-19 10:36:09 +08001107 case LOG_FMT_DATELOCAL: // %Tl
1108 get_localtime(s->logs.accept_date.tv_sec, &tm);
1109 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1110 if (ret == NULL)
1111 goto out;
1112 tmplog = ret;
1113 last_isspace = 0;
1114 break;
1115
William Lallemand5f232402012-04-05 18:02:55 +02001116 case LOG_FMT_TS: // %Ts
1117 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1118 if (tmp->options & LOG_OPT_HEXA) {
1119 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1120 if (iret < 0 || iret > dst + maxsize - tmplog)
1121 goto out;
1122 last_isspace = 0;
1123 tmplog += iret;
1124 } else {
1125 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1126 if (ret == NULL)
1127 goto out;
1128 tmplog = ret;
1129 last_isspace = 0;
1130 }
1131 break;
1132
William Lallemand1d705562012-03-12 12:46:41 +01001133 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001134 if (tmp->options & LOG_OPT_HEXA) {
1135 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1136 if (iret < 0 || iret > dst + maxsize - tmplog)
1137 goto out;
1138 last_isspace = 0;
1139 tmplog += iret;
1140 } else {
1141 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001142 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001143 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1144 tmplog, 4);
1145 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001146 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001147 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001148 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001149 }
1150 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001151
William Lallemand1d705562012-03-12 12:46:41 +01001152 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001153 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001154 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001155 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001156 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001157 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001158 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001159 break;
1160
Willy Tarreau773d65f2012-10-12 14:56:11 +02001161 case LOG_FMT_FRONTEND_XPRT: // %ft
1162 src = fe->id;
1163 if (tmp->options & LOG_OPT_QUOTE)
1164 LOGCHAR('"');
1165 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1166 if (iret == 0)
1167 goto out;
1168 tmplog += iret;
1169#ifdef USE_OPENSSL
1170 if (s->listener->xprt == &ssl_sock)
1171 LOGCHAR('~');
1172#endif
1173 if (tmp->options & LOG_OPT_QUOTE)
1174 LOGCHAR('"');
1175 last_isspace = 0;
1176 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001177#ifdef USE_OPENSSL
1178 case LOG_FMT_SSL_CIPHER: // %sslc
1179 src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001180 conn = objt_conn(s->si[0].end);
1181 if (conn) {
1182 if (s->listener->xprt == &ssl_sock)
1183 src = ssl_sock_get_cipher_name(conn);
1184 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001185 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1186 if (ret == NULL)
1187 goto out;
1188 tmplog = ret;
1189 last_isspace = 0;
1190 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001191
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001192 case LOG_FMT_SSL_VERSION: // %sslv
1193 src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001194 conn = objt_conn(s->si[0].end);
1195 if (conn) {
1196 if (s->listener->xprt == &ssl_sock)
1197 src = ssl_sock_get_proto_version(conn);
1198 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001199 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1200 if (ret == NULL)
1201 goto out;
1202 tmplog = ret;
1203 last_isspace = 0;
1204 break;
1205#endif
William Lallemand1d705562012-03-12 12:46:41 +01001206 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001207 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001208 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001209 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001210 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001211 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001212 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001213 break;
1214
William Lallemand1d705562012-03-12 12:46:41 +01001215 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001216 switch (obj_type(s->target)) {
1217 case OBJ_TYPE_SERVER:
1218 src = objt_server(s->target)->id;
1219 break;
1220 case OBJ_TYPE_APPLET:
1221 src = objt_applet(s->target)->name;
1222 break;
1223 default:
1224 src = "<NOSRV>";
1225 break;
1226 }
William Lallemand5f232402012-04-05 18:02:55 +02001227 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001228 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001229 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001230 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001231 last_isspace = 0;
1232 break;
1233
William Lallemand1d705562012-03-12 12:46:41 +01001234 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001235 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001236 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001237 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001238 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001239 last_isspace = 0;
1240 break;
1241
William Lallemand1d705562012-03-12 12:46:41 +01001242 case LOG_FMT_TW: // %Tw
1243 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001244 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001245 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001246 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001247 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001248 last_isspace = 0;
1249 break;
1250
William Lallemand1d705562012-03-12 12:46:41 +01001251 case LOG_FMT_TC: // %Tc
1252 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001253 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001254 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001255 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001256 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001257 last_isspace = 0;
1258 break;
1259
William Lallemand1d705562012-03-12 12:46:41 +01001260 case LOG_FMT_TR: // %Tr
1261 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001262 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001263 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001264 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001265 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001266 last_isspace = 0;
1267 break;
1268
William Lallemand1d705562012-03-12 12:46:41 +01001269 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001270 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001271 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001272 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001273 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001274 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001275 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001276 last_isspace = 0;
1277 break;
1278
Willy Tarreau2beef582012-12-20 17:22:52 +01001279 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001280 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001281 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001282 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001283 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001284 last_isspace = 0;
1285 break;
1286
William Lallemand1d705562012-03-12 12:46:41 +01001287 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001288 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001289 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001290 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001291 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001292 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001293 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001294 last_isspace = 0;
1295 break;
1296
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001297 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001298 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1299 if (ret == NULL)
1300 goto out;
1301 tmplog = ret;
1302 last_isspace = 0;
1303 break;
1304
Willy Tarreau2beef582012-12-20 17:22:52 +01001305 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001306 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001307 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001308 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001309 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001310 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001311 last_isspace = 0;
1312 break;
1313
Willy Tarreau2beef582012-12-20 17:22:52 +01001314 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001315 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001316 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001317 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001318 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001319 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001320 last_isspace = 0;
1321 break;
1322
William Lallemand1d705562012-03-12 12:46:41 +01001323 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +01001324 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1325 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
1326 *tmplog = '\0';
1327 last_isspace = 0;
1328 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001329
William Lallemand1d705562012-03-12 12:46:41 +01001330 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001331 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1332 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001333 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1334 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 +01001335 last_isspace = 0;
1336 break;
1337
William Lallemand1d705562012-03-12 12:46:41 +01001338 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001339 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001340 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001341 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001342 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001343 last_isspace = 0;
1344 break;
1345
William Lallemand1d705562012-03-12 12:46:41 +01001346 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001347 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001348 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001349 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001350 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001351 last_isspace = 0;
1352 break;
1353
William Lallemand1d705562012-03-12 12:46:41 +01001354 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001355 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001356 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001357 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001358 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001359 last_isspace = 0;
1360 break;
1361
William Lallemand1d705562012-03-12 12:46:41 +01001362 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001363 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001364 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001365 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001366 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001367 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001368 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001369 last_isspace = 0;
1370 break;
1371
William Lallemand1d705562012-03-12 12:46:41 +01001372 case LOG_FMT_RETRIES: // %rq
William Lallemandbddd4fd2012-02-27 11:23:10 +01001373 if (s->flags & SN_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001374 LOGCHAR('+');
1375 ret = ltoa_o((s->req->cons->conn_retries>0) ?
1376 (be->conn_retries - s->req->cons->conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001377 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001378 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001379 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001380 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001381 last_isspace = 0;
1382 break;
1383
William Lallemand1d705562012-03-12 12:46:41 +01001384 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001385 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001386 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001387 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001388 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001389 last_isspace = 0;
1390 break;
1391
William Lallemand1d705562012-03-12 12:46:41 +01001392 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001393 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001394 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001395 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001396 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001397 last_isspace = 0;
1398 break;
1399
William Lallemand1d705562012-03-12 12:46:41 +01001400 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001401 /* request header */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001402 if (fe->nb_req_cap && txn->req.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001403 if (tmp->options & LOG_OPT_QUOTE)
1404 LOGCHAR('"');
1405 LOGCHAR('{');
1406 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1407 if (hdr)
1408 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001409 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001410 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001411 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001412 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001413 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001414 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001415 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001416 }
1417 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001418 if (tmp->options & LOG_OPT_QUOTE)
1419 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001420 last_isspace = 0;
William Lallemand1d705562012-03-12 12:46:41 +01001421 if (tmp->options & LOG_OPT_QUOTE)
1422 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001423 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001424 break;
1425
William Lallemand1d705562012-03-12 12:46:41 +01001426 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001427 /* request header list */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001428 if (fe->nb_req_cap && txn->req.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001429 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1430 if (hdr > 0)
1431 LOGCHAR(' ');
1432 if (tmp->options & LOG_OPT_QUOTE)
1433 LOGCHAR('"');
1434 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001435 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001436 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001437 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001438 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001439 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001440 } else if (!(tmp->options & LOG_OPT_QUOTE))
1441 LOGCHAR('-');
1442 if (tmp->options & LOG_OPT_QUOTE)
1443 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001444 last_isspace = 0;
1445 }
1446 }
1447 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001448
William Lallemand1d705562012-03-12 12:46:41 +01001449
1450 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001451 /* response header */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001452 if (fe->nb_rsp_cap && txn->rsp.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001453 if (tmp->options & LOG_OPT_QUOTE)
1454 LOGCHAR('"');
1455 LOGCHAR('{');
1456 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1457 if (hdr)
1458 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001459 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001460 ret = encode_string(tmplog, dst + maxsize,
1461 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1462 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001463 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001464 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001465 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001466 }
1467 LOGCHAR('}');
1468 last_isspace = 0;
1469 if (tmp->options & LOG_OPT_QUOTE)
1470 LOGCHAR('"');
1471 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001472 break;
1473
William Lallemand1d705562012-03-12 12:46:41 +01001474 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001475 /* response header list */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001476 if (fe->nb_rsp_cap && txn->rsp.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001477 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1478 if (hdr > 0)
1479 LOGCHAR(' ');
1480 if (tmp->options & LOG_OPT_QUOTE)
1481 LOGCHAR('"');
1482 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001483 ret = encode_string(tmplog, dst + maxsize,
1484 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1485 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001486 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001487 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001488 } else if (!(tmp->options & LOG_OPT_QUOTE))
1489 LOGCHAR('-');
1490 if (tmp->options & LOG_OPT_QUOTE)
1491 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001492 last_isspace = 0;
1493 }
1494 }
1495 break;
1496
William Lallemand1d705562012-03-12 12:46:41 +01001497 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001498 /* Request */
1499 if (tmp->options & LOG_OPT_QUOTE)
1500 LOGCHAR('"');
1501 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001502 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001503 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001504 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001505 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001506 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001507 if (tmp->options & LOG_OPT_QUOTE)
1508 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001509 last_isspace = 0;
1510 break;
William Lallemand5f232402012-04-05 18:02:55 +02001511
1512 case LOG_FMT_COUNTER: // %rt
1513 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau9f095212013-08-13 17:51:07 +02001514 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", global.req_count++);
William Lallemand5f232402012-04-05 18:02:55 +02001515 if (iret < 0 || iret > dst + maxsize - tmplog)
1516 goto out;
1517 last_isspace = 0;
1518 tmplog += iret;
1519 } else {
Willy Tarreau9f095212013-08-13 17:51:07 +02001520 ret = ltoa_o(global.req_count++, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001521 if (ret == NULL)
1522 goto out;
1523 tmplog = ret;
1524 last_isspace = 0;
1525 }
1526 break;
1527
1528 case LOG_FMT_HOSTNAME: // %H
1529 src = hostname;
1530 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1531 if (ret == NULL)
1532 goto out;
1533 tmplog = ret;
1534 last_isspace = 0;
1535 break;
1536
1537 case LOG_FMT_PID: // %pid
1538 if (tmp->options & LOG_OPT_HEXA) {
1539 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1540 if (iret < 0 || iret > dst + maxsize - tmplog)
1541 goto out;
1542 last_isspace = 0;
1543 tmplog += iret;
1544 } else {
1545 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1546 if (ret == NULL)
1547 goto out;
1548 tmplog = ret;
1549 last_isspace = 0;
1550 }
1551 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001552
1553 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001554 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001555 src = s->unique_id;
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001556 if (src)
1557 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001558 if (ret == NULL)
1559 goto out;
1560 tmplog = ret;
1561 last_isspace = 0;
1562 break;
1563
William Lallemandbddd4fd2012-02-27 11:23:10 +01001564 }
1565 }
1566
1567out:
William Lallemand1d705562012-03-12 12:46:41 +01001568 /* *tmplog is a unused character */
1569 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001570 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001571
Willy Tarreaubaaee002006-06-26 02:48:02 +02001572}
1573
William Lallemand1d705562012-03-12 12:46:41 +01001574/*
1575 * send a log for the session when we have enough info about it.
1576 * Will not log if the frontend has no log defined.
1577 */
1578void sess_log(struct session *s)
1579{
1580 char *tmplog;
1581 int size, err, level;
1582
1583 /* if we don't want to log normal traffic, return now */
Willy Tarreau570f2212013-06-10 16:42:09 +02001584 err = (s->flags & SN_REDISP) ||
1585 ((s->flags & SN_ERR_MASK) > SN_ERR_LOCAL) ||
1586 (((s->flags & SN_ERR_MASK) == SN_ERR_NONE) &&
1587 (s->req->cons->conn_retries != s->be->conn_retries)) ||
1588 ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001589
William Lallemand1d705562012-03-12 12:46:41 +01001590 if (!err && (s->fe->options2 & PR_O2_NOLOGNORM))
1591 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001592
William Lallemand1d705562012-03-12 12:46:41 +01001593 if (LIST_ISEMPTY(&s->fe->logsrvs))
1594 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001595
Willy Tarreauabcd5142013-06-11 17:18:02 +02001596 if (s->logs.level) { /* loglevel was overridden */
1597 if (s->logs.level == -1) {
1598 s->logs.logwait = 0; /* logs disabled */
1599 return;
1600 }
1601 level = s->logs.level - 1;
1602 }
1603 else {
1604 level = LOG_INFO;
1605 if (err && (s->fe->options2 & PR_O2_LOGERRORS))
1606 level = LOG_ERR;
1607 }
William Lallemand1d705562012-03-12 12:46:41 +01001608
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001609 /* if unique-id was not generated */
1610 if (!s->unique_id && !LIST_ISEMPTY(&s->fe->format_unique_id)) {
1611 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
1612 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
1613 }
1614
William Lallemand1d705562012-03-12 12:46:41 +01001615 tmplog = update_log_hdr();
1616 size = tmplog - logline;
1617 size += build_logline(s, tmplog, sizeof(logline) - size, &s->fe->logformat);
1618 if (size > 0) {
Willy Tarreaudf974472012-12-28 02:44:01 +01001619 __send_log(s->fe, level, logline, size + 1);
William Lallemand1d705562012-03-12 12:46:41 +01001620 s->logs.logwait = 0;
1621 }
1622}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001623
Willy Tarreaubaaee002006-06-26 02:48:02 +02001624/*
1625 * Local variables:
1626 * c-indent-level: 8
1627 * c-basic-offset: 8
1628 * End:
1629 */