blob: 3855ae00f1e028802324fa77daf6f4728e242bb0 [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>
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +000035#include <proto/proto_http.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020036#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010037#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020038#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010039#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020040#ifdef USE_OPENSSL
41#include <proto/ssl_sock.h>
42#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Dragan Dosen43885c72015-10-01 13:18:13 +020044struct log_fmt {
45 char *name;
46 struct {
47 struct chunk sep1; /* first pid separator */
48 struct chunk sep2; /* second pid separator */
49 } pid;
50};
51
52static const struct log_fmt log_formats[LOG_FORMATS] = {
53 [LOG_FORMAT_RFC3164] = {
54 .name = "rfc3164",
55 .pid = {
56 .sep1 = { .str = "[", .len = 1 },
57 .sep2 = { .str = "]: ", .len = 3 }
58 }
59 },
60 [LOG_FORMAT_RFC5424] = {
61 .name = "rfc5424",
62 .pid = {
63 .sep1 = { .str = " ", .len = 1 },
64 .sep2 = { .str = " - ", .len = 3 }
65 }
66 }
Dragan Dosen1322d092015-09-22 16:05:32 +020067};
68
Willy Tarreaubaaee002006-06-26 02:48:02 +020069const char *log_facilities[NB_LOG_FACILITIES] = {
70 "kern", "user", "mail", "daemon",
71 "auth", "syslog", "lpr", "news",
72 "uucp", "cron", "auth2", "ftp",
73 "ntp", "audit", "alert", "cron2",
74 "local0", "local1", "local2", "local3",
75 "local4", "local5", "local6", "local7"
76};
77
Willy Tarreaubaaee002006-06-26 02:48:02 +020078const char *log_levels[NB_LOG_LEVELS] = {
79 "emerg", "alert", "crit", "err",
80 "warning", "notice", "info", "debug"
81};
82
Willy Tarreau570f2212013-06-10 16:42:09 +020083const 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 +020084const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020085
William Lallemand723b73a2012-02-08 16:37:49 +010086
87/* log_format */
88struct logformat_type {
89 char *name;
90 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010091 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020092 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010093 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +010094 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +010095};
96
William Lallemandb7ff6a32012-03-02 14:35:21 +010097int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
98
William Lallemand723b73a2012-02-08 16:37:49 +010099/* log_format variable names */
100static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200101 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100102
103 /* please keep these lines sorted ! */
104 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
105 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
106 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
107 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
108 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200109 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200110 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +0100111 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +0800112 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +0200113 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +0200114 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +0100115 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200116 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +0100117 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
118 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200119 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100120 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200121 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100122 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
123 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200124 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +0100125 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
126 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
127 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
128 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
129 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
130 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
131 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200132 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
133 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
134 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
135 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000136 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
137 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000138 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000139 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
140 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200141 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100142 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200143 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100144 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
145 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100146 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100147 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
148 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
149 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
150 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
151 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200152 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
153 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100154 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
155 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
156 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
157
158 /* The following tags are deprecated and will be removed soon */
159 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
160 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
161 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
162 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
163 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
164 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
165 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
166 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
167 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
168 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
169 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200170 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100171};
172
Willy Tarreau2beef582012-12-20 17:22:52 +0100173char 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
174char 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";
175char 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 +0100176char *log_format = NULL;
177
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200178/* Default string used for structured-data part in RFC5424 formatted
179 * syslog messages.
180 */
181char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200182
183/* This is a global syslog header, common to all outgoing messages in
184 * RFC3164 format. It begins with time-based part and is updated by
185 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200186 */
187char *logheader = NULL;
188
Dragan Dosen1322d092015-09-22 16:05:32 +0200189/* This is a global syslog header for messages in RFC5424 format. It is
190 * updated by update_log_hdr_rfc5424().
191 */
192char *logheader_rfc5424 = NULL;
193
Dragan Dosen59cee972015-09-19 22:09:02 +0200194/* This is a global syslog message buffer, common to all outgoing
195 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100196 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200197char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100198
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200199/* A global syslog message buffer, common to all RFC5424 syslog messages.
200 * Currently, it is used for generating the structured-data part.
201 */
202char *logline_rfc5424 = NULL;
203
William Lallemand723b73a2012-02-08 16:37:49 +0100204struct logformat_var_args {
205 char *name;
206 int mask;
207};
208
209struct logformat_var_args var_args_list[] = {
210// global
211 { "M", LOG_OPT_MANDATORY },
212 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200213 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100214 { 0, 0 }
215};
216
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200217/* return the name of the directive used in the current proxy for which we're
218 * currently parsing a header, when it is known.
219 */
220static inline const char *fmt_directive(const struct proxy *curproxy)
221{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100222 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200223 case ARGC_ACL:
224 return "acl";
225 case ARGC_STK:
226 return "stick";
227 case ARGC_TRK:
228 return "track-sc";
229 case ARGC_LOG:
230 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200231 case ARGC_LOGSD:
232 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100233 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100234 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100235 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100236 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200237 case ARGC_UIF:
238 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100239 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200240 return "redirect";
241 case ARGC_CAP:
242 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200243 case ARGC_SRV:
244 return "server";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100245 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200246 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100247 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200248}
249
William Lallemand723b73a2012-02-08 16:37:49 +0100250/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100251 * callback used to configure addr source retrieval
252 */
253int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
254{
255 curproxy->options2 |= PR_O2_SRC_ADDR;
256
257 return 0;
258}
259
260
261/*
William Lallemand723b73a2012-02-08 16:37:49 +0100262 * Parse args in a logformat_var
263 */
264int parse_logformat_var_args(char *args, struct logformat_node *node)
265{
266 int i = 0;
267 int end = 0;
268 int flags = 0; // 1 = + 2 = -
269 char *sp = NULL; // start pointer
270
271 if (args == NULL)
272 return 1;
273
274 while (1) {
275 if (*args == '\0')
276 end = 1;
277
278 if (*args == '+') {
279 // add flag
280 sp = args + 1;
281 flags = 1;
282 }
283 if (*args == '-') {
284 // delete flag
285 sp = args + 1;
286 flags = 2;
287 }
288
289 if (*args == '\0' || *args == ',') {
290 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100291 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100292 if (strcmp(sp, var_args_list[i].name) == 0) {
293 if (flags == 1) {
294 node->options |= var_args_list[i].mask;
295 break;
296 } else if (flags == 2) {
297 node->options &= ~var_args_list[i].mask;
298 break;
299 }
300 }
301 }
302 sp = NULL;
303 if (end)
304 break;
305 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100306 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100307 }
308 return 0;
309}
310
311/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100312 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
313 * must pass the args part in the <arg> pointer with its length in <arg_len>,
314 * and varname with its length in <var> and <var_len> respectively. <arg> is
315 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100316 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100317int 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 +0100318{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100319 int j;
320 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100321
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100322 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
323 if (strlen(logformat_keywords[j].name) == var_len &&
324 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
325 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
326 node = calloc(1, sizeof(struct logformat_node));
327 node->type = logformat_keywords[j].type;
328 node->options = *defoptions;
329 if (arg_len) {
330 node->arg = my_strndup(arg, arg_len);
331 parse_logformat_var_args(node->arg, node);
332 }
333 if (node->type == LOG_FMT_GLOBAL) {
334 *defoptions = node->options;
335 free(node->arg);
336 free(node);
337 } else {
338 if (logformat_keywords[j].config_callback &&
339 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100340 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100341 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100342 curproxy->to_log |= logformat_keywords[j].lw;
343 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100344 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100345 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100346 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200347 curproxy->conf.args.file, curproxy->conf.args.line,
348 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100349 return 0;
350 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100351 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200352 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
353 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100354 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100355 }
William Lallemand723b73a2012-02-08 16:37:49 +0100356 }
357 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100358
359 j = var[var_len];
360 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100361 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 +0200362 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100363 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100364 return -1;
365}
366
367/*
368 * push to the logformat linked list
369 *
370 * start: start pointer
371 * end: end text pointer
372 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100373 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100374 *
375 * LOG_TEXT: copy chars from start to end excluding end.
376 *
377*/
William Lallemand1d705562012-03-12 12:46:41 +0100378void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100379{
380 char *str;
381
Willy Tarreaua3571662012-12-20 21:59:12 +0100382 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100383 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100384 str = calloc(end - start + 1, 1);
385 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100386 str[end - start] = '\0';
387 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100388 node->type = LOG_FMT_TEXT; // type string
389 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100390 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100391 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100392 node->type = LOG_FMT_SEPARATOR;
393 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100394 }
395}
396
397/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100398 * Parse the sample fetch expression <text> and add a node to <list_format> upon
399 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200400 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100401 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100402void add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, const char *file, int line)
Willy Tarreauc8368452012-12-21 00:09:23 +0100403{
404 char *cmd[2];
405 struct sample_expr *expr;
406 struct logformat_node *node;
407 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100408 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100409
410 cmd[0] = text;
411 cmd[1] = "";
412 cmd_arg = 0;
413
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100414 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100415 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200416 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
417 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100418 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100419 return;
420 }
421
422 node = calloc(1, sizeof(struct logformat_node));
423 node->type = LOG_FMT_EXPR;
424 node->expr = expr;
425 node->options = options;
426
427 if (arg_len) {
428 node->arg = my_strndup(arg, arg_len);
429 parse_logformat_var_args(node->arg, node);
430 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100431 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100432 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
433
Willy Tarreau434c57c2013-01-08 01:10:24 +0100434 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100435 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
436
Willy Tarreau434c57c2013-01-08 01:10:24 +0100437 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200438 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
439 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
440 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100441
Willy Tarreauc8368452012-12-21 00:09:23 +0100442 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
443 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100444 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100445
William Lallemand65ad6e12014-01-31 15:08:02 +0100446 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
447 * needed with some sample fetches (eg: ssl*). We always set it for
448 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100449 */
450 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100451 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100452 LIST_ADDQ(list_format, &node->list);
453}
454
455/*
William Lallemand723b73a2012-02-08 16:37:49 +0100456 * Parse the log_format string and fill a linked list.
457 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200458 * You can set arguments using { } : %{many arguments}varname.
459 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100460 *
461 * str: the string to parse
462 * curproxy: the proxy affected
463 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100464 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100465 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100466 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100467void parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, const char *file, int line)
William Lallemand723b73a2012-02-08 16:37:49 +0100468{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100469 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100470 char *arg = NULL; /* start pointer for args */
471 char *var = NULL; /* start pointer for vars */
472 int arg_len = 0;
473 int var_len = 0;
474 int cformat; /* current token format */
475 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100476 struct logformat_node *tmplf, *back;
477
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100478 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200479 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200480
William Lallemand723b73a2012-02-08 16:37:49 +0100481 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100482 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100483 LIST_DEL(&tmplf->list);
484 free(tmplf);
485 }
486
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100487 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100488 pformat = cformat;
489
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100490 if (!*str)
491 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100492
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100493 /* The prinicple of the two-step state machine below is to first detect a change, and
494 * second have all common paths processed at one place. The common paths are the ones
495 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
496 * We use the common LF_INIT state to dispatch to the different final states.
497 */
498 switch (pformat) {
499 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100500 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100501 arg_len = var_len = 0;
502 if (*str == '{') { // optional argument
503 cformat = LF_STARG;
504 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100505 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100506 else if (*str == '[') {
507 cformat = LF_STEXPR;
508 var = str + 1; // store expr in variable name
509 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100510 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100511 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100512 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100513 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100514 else if (*str == '%')
515 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100516 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100517 /* single '%' followed by blank or digit, send them both */
518 cformat = LF_TEXT;
519 pformat = LF_TEXT; /* finally we include the previous char as well */
520 sp = str - 1; /* send both the '%' and the current char */
521 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",
522 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
523
524 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100525 else
526 cformat = LF_INIT; // handle other cases of litterals
527 break;
528
529 case LF_STARG: // text immediately following '%{'
530 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100531 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100532 arg_len = str - arg;
533 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100534 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100535 break;
536
537 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100538 if (*str == '[') {
539 cformat = LF_STEXPR;
540 var = str + 1; // store expr in variable name
541 break;
542 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100543 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100544 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100545 var = str;
546 break;
547 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200548 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
549 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100550 cformat = LF_INIT;
551 break;
552
Willy Tarreauc8368452012-12-21 00:09:23 +0100553 case LF_STEXPR: // text immediately following '%['
554 if (*str == ']') { // end of arg
555 cformat = LF_EDEXPR;
556 var_len = str - var;
557 *str = 0; // needed for parsing the expression
558 }
559 break;
560
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100561 case LF_VAR: // text part of a variable name
562 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100563 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100564 cformat = LF_INIT; // not variable name anymore
565 break;
566
Willy Tarreauc8368452012-12-21 00:09:23 +0100567 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100568 cformat = LF_INIT;
569 }
570
571 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
572 switch (*str) {
573 case '%': cformat = LF_STARTVAR; break;
574 case ' ': cformat = LF_SEPARATOR; break;
575 case 0 : cformat = LF_END; break;
576 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100577 }
578 }
579
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100580 if (cformat != pformat || pformat == LF_SEPARATOR) {
581 switch (pformat) {
582 case LF_VAR:
583 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
584 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100585 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100586 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100587 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100588 case LF_TEXT:
589 case LF_SEPARATOR:
590 add_to_logformat_list(sp, str, pformat, list_format);
591 break;
592 }
593 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100594 }
595 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100596
Willy Tarreauc8368452012-12-21 00:09:23 +0100597 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200598 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
599 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
600 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100601
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100602 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100603}
604
Willy Tarreaubaaee002006-06-26 02:48:02 +0200605/*
606 * Displays the message on stderr with the date and pid. Overrides the quiet
607 * mode during startup.
608 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200609void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610{
611 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200612 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613
614 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
615 va_start(argp, fmt);
616
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200617 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200619 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200620 vfprintf(stderr, fmt, argp);
621 fflush(stderr);
622 va_end(argp);
623 }
624}
625
626
627/*
628 * Displays the message on stderr with the date and pid.
629 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200630void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200631{
632 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200633 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634
635 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
636 va_start(argp, fmt);
637
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200638 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200640 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200641 vfprintf(stderr, fmt, argp);
642 fflush(stderr);
643 va_end(argp);
644 }
645}
646
647/*
648 * Displays the message on <out> only if quiet mode is not set.
649 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200650void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651{
652 va_list argp;
653
654 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
655 va_start(argp, fmt);
656 vfprintf(out, fmt, argp);
657 fflush(out);
658 va_end(argp);
659 }
660}
661
662/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200663 * returns log format for <fmt> or -1 if not found.
664 */
665int get_log_format(const char *fmt)
666{
667 int format;
668
669 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200670 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200671 format--;
672
673 return format;
674}
675
676/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 * returns log level for <lev> or -1 if not found.
678 */
679int get_log_level(const char *lev)
680{
681 int level;
682
683 level = NB_LOG_LEVELS - 1;
684 while (level >= 0 && strcmp(log_levels[level], lev))
685 level--;
686
687 return level;
688}
689
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690/*
691 * returns log facility for <fac> or -1 if not found.
692 */
693int get_log_facility(const char *fac)
694{
695 int facility;
696
697 facility = NB_LOG_FACILITIES - 1;
698 while (facility >= 0 && strcmp(log_facilities[facility], fac))
699 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100700
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701 return facility;
702}
703
William Lallemanda1cc3812012-02-08 16:38:44 +0100704/*
705 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200706 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100707 *
708 * Return the adress of the \0 character, or NULL on error
709 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100710char *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 +0100711{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100712 if (size < 2)
713 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100714
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100715 if (node->options & LOG_OPT_QUOTE) {
716 *(dst++) = '"';
717 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100718 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100719
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100720 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100721 if (++len > size)
722 len = size;
723 len = strlcpy2(dst, src, len);
724
725 size -= len;
726 dst += len;
727 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100728 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
729 if (size < 2)
730 return NULL;
731 *(dst++) = '-';
732 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100733
734 if (node->options & LOG_OPT_QUOTE) {
735 if (size < 2)
736 return NULL;
737 *(dst++) = '"';
738 }
739
740 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100741 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100742}
743
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100744static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
745{
746 return lf_text_len(dst, src, size, size, node);
747}
748
William Lallemand5f232402012-04-05 18:02:55 +0200749/*
750 * Write a IP adress to the log string
751 * +X option write in hexadecimal notation, most signifant byte on the left
752 */
753char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
754{
755 char *ret = dst;
756 int iret;
757 char pn[INET6_ADDRSTRLEN];
758
759 if (node->options & LOG_OPT_HEXA) {
760 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
761 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
762 if (iret < 0 || iret > size)
763 return NULL;
764 ret += iret;
765 } else {
766 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
767 ret = lf_text(dst, pn, size, node);
768 if (ret == NULL)
769 return NULL;
770 }
771 return ret;
772}
773
774/*
775 * Write a port to the log
776 * +X option write in hexadecimal notation, most signifant byte on the left
777 */
778char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
779{
780 char *ret = dst;
781 int iret;
782
783 if (node->options & LOG_OPT_HEXA) {
784 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
785 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
786 if (iret < 0 || iret > size)
787 return NULL;
788 ret += iret;
789 } else {
790 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
791 if (ret == NULL)
792 return NULL;
793 }
794 return ret;
795}
796
Dragan Dosen1322d092015-09-22 16:05:32 +0200797/* Re-generate time-based part of the syslog header in RFC3164 format at
798 * the beginning of logheader once a second and return the pointer to the
799 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100800 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200801static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100803 static long tvsec;
804 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Dragan Dosen43885c72015-10-01 13:18:13 +0200805 static struct chunk host = { NULL, 0, 0 };
806 static int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200808 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200810 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100811 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200812
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200813 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +0200814 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815
Dragan Dosen43885c72015-10-01 13:18:13 +0200816 if (unlikely(global.log_send_hostname != host.str)) {
817 host.str = global.log_send_hostname;
818 host.len = host.str ? strlen(host.str) : 0;
819 sep = host.len ? 1 : 0;
820 }
821
Dragan Dosen59cee972015-09-19 22:09:02 +0200822 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +0200823 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200824 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +0200825 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
826 host.len, host.str, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200827 /* WARNING: depending upon implementations, snprintf may return
828 * either -1 or the number of bytes that would be needed to store
829 * the total message. In both cases, we must adjust it.
830 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200831 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
832 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833
Dragan Dosen59cee972015-09-19 22:09:02 +0200834 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835 }
836
Willy Tarreau094af4e2015-01-07 15:03:42 +0100837 dataptr[0] = 0; // ensure we get rid of any previous attempt
838
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200839 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100840}
841
Dragan Dosen1322d092015-09-22 16:05:32 +0200842/* Re-generate time-based part of the syslog header in RFC5424 format at
843 * the beginning of logheader_rfc5424 once a second and return the pointer
844 * to the first character after it.
845 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200846static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +0200847{
848 static long tvsec;
849 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
850
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200851 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +0200852 /* this string is rebuild only once a second */
853 struct tm tm;
854 int hdr_len;
855
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200856 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +0200857 get_localtime(tvsec, &tm);
858
859 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +0200860 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +0200861 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +0200862 tm.tm_hour, tm.tm_min, tm.tm_sec,
863 localtimezone, localtimezone+3,
Dragan Dosen43885c72015-10-01 13:18:13 +0200864 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +0200865 /* WARNING: depending upon implementations, snprintf may return
866 * either -1 or the number of bytes that would be needed to store
867 * the total message. In both cases, we must adjust it.
868 */
869 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
870 hdr_len = global.max_syslog_len;
871
872 dataptr = logheader_rfc5424 + hdr_len;
873 }
874
875 dataptr[0] = 0; // ensure we get rid of any previous attempt
876
877 return dataptr;
878}
879
William Lallemand2a4a44f2012-02-06 16:00:33 +0100880/*
Dragan Dosen59cee972015-09-19 22:09:02 +0200881 * This function sends the syslog message using a printf format string. It
882 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100883 */
884void send_log(struct proxy *p, int level, const char *format, ...)
885{
886 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100887 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100888
Willy Tarreau8c97ab52015-01-15 16:29:53 +0100889 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100890 return;
891
William Lallemand2a4a44f2012-02-06 16:00:33 +0100892 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +0200893 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +0200894 if (data_len < 0 || data_len > global.max_syslog_len)
895 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200896 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100897
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200898 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100899}
900
901/*
902 * This function sends a syslog message.
903 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200904 * It overrides the last byte of the message vector with an LF character.
905 * The arguments <sd> and <sd_size> are used for the structured-data part
906 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100907 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200908void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100909{
Dragan Dosen609ac2a2015-09-16 18:25:42 +0200910 static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
911 static struct msghdr msghdr = {
912 .msg_iov = iovec,
913 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
914 };
William Lallemand2a4a44f2012-02-06 16:00:33 +0100915 static int logfdunix = -1; /* syslog to AF_UNIX socket */
916 static int logfdinet = -1; /* syslog to AF_INET socket */
917 static char *dataptr = NULL;
918 int fac_level;
919 struct list *logsrvs = NULL;
920 struct logsrv *tmp = NULL;
921 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +0200922 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +0200923 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200924 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +0200925 struct chunk *tag = &global.log_tag;
926 static int curr_pid;
927 static char pidstr[100];
928 static struct chunk pid;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100929
930 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931
932 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200933 if (!LIST_ISEMPTY(&global.logsrvs)) {
934 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935 }
936 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200937 if (!LIST_ISEMPTY(&p->logsrvs)) {
938 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939 }
Dragan Dosen43885c72015-10-01 13:18:13 +0200940 if (p->log_tag.str) {
941 tag = &p->log_tag;
942 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100943 }
944
William Lallemand0f99e342011-10-12 17:50:54 +0200945 if (!logsrvs)
946 return;
947
Dragan Dosen43885c72015-10-01 13:18:13 +0200948 if (unlikely(curr_pid != getpid())) {
949 curr_pid = getpid();
950 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
951 chunk_initstr(&pid, pidstr);
952 }
953
Robert Tsai81ae1952007-12-05 10:47:29 +0100954 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200955 nblogger = 0;
956 list_for_each_entry(tmp, logsrvs, list) {
957 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100958 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100959 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +0200960 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +0100961 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200962 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +0200963 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +0200964 int tag_max = 0;
965 int pid_sep1_max = 0;
966 int pid_max = 0;
967 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200968 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +0200969 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +0100970
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200971 nblogger++;
972
Willy Tarreaubaaee002006-06-26 02:48:02 +0200973 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200974 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200975 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100976
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200977 if (unlikely(*plogfd < 0)) {
978 /* socket not successfully initialized yet */
979 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
980
981 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
982 Alert("socket for logger #%d failed: %s (errno=%d)\n",
983 nblogger, strerror(errno), errno);
984 continue;
985 }
986 /* we don't want to receive anything on this socket */
987 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
988 /* does nothing under Linux, maybe needed for others */
989 shutdown(*plogfd, SHUT_RD);
990 }
991
Dragan Dosen1322d092015-09-22 16:05:32 +0200992 switch (logsrv->format) {
993 case LOG_FORMAT_RFC3164:
994 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200995 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +0200996 break;
997
998 case LOG_FORMAT_RFC5424:
999 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001000 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001001 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001002 break;
1003
1004 default:
1005 continue; /* must never happen */
1006 }
1007
1008 hdr_size = hdr_ptr - hdr;
1009
Willy Tarreaubaaee002006-06-26 02:48:02 +02001010 /* For each target, we may have a different facility.
1011 * We can also have a different log level for each message.
1012 * This induces variations in the message header length.
1013 * Since we don't want to recompute it each time, nor copy it every
1014 * time, we only change the facility in the pre-computed header,
1015 * and we change the pointer to the header accordingly.
1016 */
William Lallemand0f99e342011-10-12 17:50:54 +02001017 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001018 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001020 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001022 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001023 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001024 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001025
Dragan Dosen1322d092015-09-22 16:05:32 +02001026 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001027
Dragan Dosen43885c72015-10-01 13:18:13 +02001028 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001029 if (unlikely(hdr_size >= logsrv->maxlen)) {
1030 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001031 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001032 goto send;
1033 }
1034
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001035 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001036
Dragan Dosen43885c72015-10-01 13:18:13 +02001037 /* tag */
1038 tag_max = tag->len;
1039 if (unlikely(tag_max >= maxlen)) {
1040 tag_max = maxlen - 1;
1041 sd_max = 0;
1042 goto send;
1043 }
1044
1045 maxlen -= tag_max;
1046
1047 /* first pid separator */
1048 pid_sep1_max = log_formats[logsrv->format].pid.sep1.len;
1049 if (unlikely(pid_sep1_max >= maxlen)) {
1050 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001051 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001052 goto send;
1053 }
1054
Dragan Dosen43885c72015-10-01 13:18:13 +02001055 pid_sep1 = log_formats[logsrv->format].pid.sep1.str;
1056 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001057
Dragan Dosen43885c72015-10-01 13:18:13 +02001058 /* pid */
1059 pid_max = pid.len;
1060 if (unlikely(pid_max >= maxlen)) {
1061 pid_max = maxlen - 1;
1062 sd_max = 0;
1063 goto send;
1064 }
1065
1066 maxlen -= pid_max;
1067
1068 /* second pid separator */
1069 pid_sep2_max = log_formats[logsrv->format].pid.sep2.len;
1070 if (unlikely(pid_sep2_max >= maxlen)) {
1071 pid_sep2_max = maxlen - 1;
1072 sd_max = 0;
1073 goto send;
1074 }
1075
1076 pid_sep2 = log_formats[logsrv->format].pid.sep2.str;
1077 maxlen -= pid_sep2_max;
1078
1079 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001080 if (sd_max >= maxlen) {
1081 sd_max = maxlen - 1;
1082 goto send;
1083 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001084
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001085 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001086send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001087 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001088 iovec[0].iov_len = hdr_max;
1089 iovec[1].iov_base = tag->str;
1090 iovec[1].iov_len = tag_max;
1091 iovec[2].iov_base = pid_sep1;
1092 iovec[2].iov_len = pid_sep1_max;
1093 iovec[3].iov_base = pid.str;
1094 iovec[3].iov_len = pid_max;
1095 iovec[4].iov_base = pid_sep2;
1096 iovec[4].iov_len = pid_sep2_max;
1097 iovec[5].iov_base = sd;
1098 iovec[5].iov_len = sd_max;
1099 iovec[6].iov_base = dataptr;
1100 iovec[6].iov_len = max;
1101 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1102 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001103
1104 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1105 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1106
1107 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001108
Robert Tsai81ae1952007-12-05 10:47:29 +01001109 if (sent < 0) {
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001110 Alert("sendmsg logger #%d failed: %s (errno=%d)\n",
Robert Tsai81ae1952007-12-05 10:47:29 +01001111 nblogger, strerror(errno), errno);
1112 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113 }
1114}
1115
William Lallemandbddd4fd2012-02-27 11:23:10 +01001116extern fd_set hdr_encode_map[];
1117extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001118extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001119
Willy Tarreaubaaee002006-06-26 02:48:02 +02001120
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001121const 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 +01001122const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1123 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1124 Set-cookie Updated, unknown, unknown */
1125
William Lallemand1d705562012-03-12 12:46:41 +01001126/*
1127 * try to write a character if there is enough space, or goto out
1128 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001129#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001130 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001131 *(tmplog++) = (x); \
1132 } else { \
1133 goto out; \
1134 } \
1135 } while(0)
1136
William Lallemand1d705562012-03-12 12:46:41 +01001137
Willy Tarreaudf974472012-12-28 02:44:01 +01001138/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1139 * <maxsize> characters. Returns the size of the output string in characters,
1140 * not counting the trailing zero which is always added if the resulting size
1141 * is not zero.
1142 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001143int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001144{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001145 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001146 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +01001147 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001148 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001149 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001150 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001151 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001152 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001153 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001154 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001155 int t_request;
1156 int hdr;
1157 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001158 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001159 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001160 char *ret;
1161 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001162 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001163
William Lallemandbddd4fd2012-02-27 11:23:10 +01001164 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165
William Lallemandbddd4fd2012-02-27 11:23:10 +01001166 t_request = -1;
1167 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1168 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1169
William Lallemand1d705562012-03-12 12:46:41 +01001170 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001171
William Lallemandbddd4fd2012-02-27 11:23:10 +01001172 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001173 if (LIST_ISEMPTY(list_format))
1174 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001175
William Lallemand1d705562012-03-12 12:46:41 +01001176 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001177 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001178 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001179 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001180 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001181
Willy Tarreauc8368452012-12-21 00:09:23 +01001182 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001183 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001184 if (!last_isspace) {
1185 LOGCHAR(' ');
1186 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001187 }
1188 break;
1189
William Lallemand1d705562012-03-12 12:46:41 +01001190 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001191 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001192 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001193 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001194 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001195 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001196 last_isspace = 0;
1197 break;
1198
Willy Tarreauc8368452012-12-21 00:09:23 +01001199 case LOG_FMT_EXPR: // sample expression, may be request or response
1200 key = NULL;
1201 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001202 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Willy Tarreauc8368452012-12-21 00:09:23 +01001203 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001204 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001205 if (tmp->options & LOG_OPT_HTTP)
1206 ret = encode_chunk(tmplog, dst + maxsize,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001207 '%', http_encode_map, key ? &key->data.u.str : &empty);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001208 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001209 ret = lf_text_len(tmplog, key ? key->data.u.str.str : NULL, key ? key->data.u.str.len : 0, dst + maxsize - tmplog, tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01001210 if (ret == 0)
1211 goto out;
1212 tmplog = ret;
1213 last_isspace = 0;
1214 break;
1215
Willy Tarreau2beef582012-12-20 17:22:52 +01001216 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001217 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001218 if (conn)
1219 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1220 else
1221 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001222 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001223 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001224 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001225 last_isspace = 0;
1226 break;
1227
Willy Tarreau2beef582012-12-20 17:22:52 +01001228 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001229 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001230 if (conn) {
1231 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001232 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001233 } else {
1234 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1235 dst + maxsize - tmplog, tmp);
1236 }
William Lallemand5f232402012-04-05 18:02:55 +02001237 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001238 else
1239 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1240
William Lallemand5f232402012-04-05 18:02:55 +02001241 if (ret == NULL)
1242 goto out;
1243 tmplog = ret;
1244 last_isspace = 0;
1245 break;
1246
Willy Tarreau2beef582012-12-20 17:22:52 +01001247 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001248 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001249 if (conn) {
1250 conn_get_to_addr(conn);
1251 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1252 }
1253 else
1254 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1255
William Lallemand1d705562012-03-12 12:46:41 +01001256 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001257 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001258 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001259 last_isspace = 0;
1260 break;
1261
Willy Tarreau2beef582012-12-20 17:22:52 +01001262 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001263 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001264 if (conn) {
1265 conn_get_to_addr(conn);
1266 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001267 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001268 else
1269 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001270 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001271 else
1272 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1273
William Lallemand5f232402012-04-05 18:02:55 +02001274 if (ret == NULL)
1275 goto out;
1276 tmplog = ret;
1277 last_isspace = 0;
1278 break;
1279
Willy Tarreau2beef582012-12-20 17:22:52 +01001280 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001281 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001282 if (conn)
1283 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1284 else
1285 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1286
William Lallemand1d705562012-03-12 12:46:41 +01001287 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001288 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001289 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001290 last_isspace = 0;
1291 break;
1292
Willy Tarreau2beef582012-12-20 17:22:52 +01001293 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001294 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001295 if (conn)
1296 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1297 else
1298 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1299
William Lallemand5f232402012-04-05 18:02:55 +02001300 if (ret == NULL)
1301 goto out;
1302 tmplog = ret;
1303 last_isspace = 0;
1304 break;
1305
Willy Tarreau2beef582012-12-20 17:22:52 +01001306 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001307 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001308 if (conn)
1309 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1310 else
1311 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1312
William Lallemand5f232402012-04-05 18:02:55 +02001313 if (ret == NULL)
1314 goto out;
1315 tmplog = ret;
1316 last_isspace = 0;
1317 break;
1318
Willy Tarreau2beef582012-12-20 17:22:52 +01001319 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001320 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001321 if (conn)
1322 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1323 else
1324 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1325
William Lallemand1d705562012-03-12 12:46:41 +01001326 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001327 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001328 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001329 last_isspace = 0;
1330 break;
1331
William Lallemand1d705562012-03-12 12:46:41 +01001332 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001333 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001334 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1335 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001336 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001337 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001338 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001339 last_isspace = 0;
1340 break;
1341
William Lallemand1d705562012-03-12 12:46:41 +01001342 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001343 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001344 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001345 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001346 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001347 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001348 last_isspace = 0;
1349 break;
1350
Yuxans Yao4e25b012012-10-19 10:36:09 +08001351 case LOG_FMT_DATELOCAL: // %Tl
1352 get_localtime(s->logs.accept_date.tv_sec, &tm);
1353 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1354 if (ret == NULL)
1355 goto out;
1356 tmplog = ret;
1357 last_isspace = 0;
1358 break;
1359
William Lallemand5f232402012-04-05 18:02:55 +02001360 case LOG_FMT_TS: // %Ts
1361 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1362 if (tmp->options & LOG_OPT_HEXA) {
1363 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1364 if (iret < 0 || iret > dst + maxsize - tmplog)
1365 goto out;
1366 last_isspace = 0;
1367 tmplog += iret;
1368 } else {
1369 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1370 if (ret == NULL)
1371 goto out;
1372 tmplog = ret;
1373 last_isspace = 0;
1374 }
1375 break;
1376
William Lallemand1d705562012-03-12 12:46:41 +01001377 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001378 if (tmp->options & LOG_OPT_HEXA) {
1379 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1380 if (iret < 0 || iret > dst + maxsize - tmplog)
1381 goto out;
1382 last_isspace = 0;
1383 tmplog += iret;
1384 } else {
1385 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001386 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001387 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1388 tmplog, 4);
1389 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001390 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001391 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001392 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001393 }
1394 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001395
William Lallemand1d705562012-03-12 12:46:41 +01001396 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001397 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001398 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001399 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001400 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001401 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001402 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001403 break;
1404
Willy Tarreau773d65f2012-10-12 14:56:11 +02001405 case LOG_FMT_FRONTEND_XPRT: // %ft
1406 src = fe->id;
1407 if (tmp->options & LOG_OPT_QUOTE)
1408 LOGCHAR('"');
1409 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1410 if (iret == 0)
1411 goto out;
1412 tmplog += iret;
1413#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001414 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001415 LOGCHAR('~');
1416#endif
1417 if (tmp->options & LOG_OPT_QUOTE)
1418 LOGCHAR('"');
1419 last_isspace = 0;
1420 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001421#ifdef USE_OPENSSL
1422 case LOG_FMT_SSL_CIPHER: // %sslc
1423 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001424 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001425 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001426 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001427 src = ssl_sock_get_cipher_name(conn);
1428 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001429 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1430 if (ret == NULL)
1431 goto out;
1432 tmplog = ret;
1433 last_isspace = 0;
1434 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001435
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001436 case LOG_FMT_SSL_VERSION: // %sslv
1437 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001438 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001439 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001440 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001441 src = ssl_sock_get_proto_version(conn);
1442 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001443 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1444 if (ret == NULL)
1445 goto out;
1446 tmplog = ret;
1447 last_isspace = 0;
1448 break;
1449#endif
William Lallemand1d705562012-03-12 12:46:41 +01001450 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001451 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001452 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001453 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001454 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001455 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001456 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001457 break;
1458
William Lallemand1d705562012-03-12 12:46:41 +01001459 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001460 switch (obj_type(s->target)) {
1461 case OBJ_TYPE_SERVER:
1462 src = objt_server(s->target)->id;
1463 break;
1464 case OBJ_TYPE_APPLET:
1465 src = objt_applet(s->target)->name;
1466 break;
1467 default:
1468 src = "<NOSRV>";
1469 break;
1470 }
William Lallemand5f232402012-04-05 18:02:55 +02001471 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001472 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001473 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001474 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001475 last_isspace = 0;
1476 break;
1477
William Lallemand1d705562012-03-12 12:46:41 +01001478 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001479 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001480 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001481 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001482 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001483 last_isspace = 0;
1484 break;
1485
William Lallemand1d705562012-03-12 12:46:41 +01001486 case LOG_FMT_TW: // %Tw
1487 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001488 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001489 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001490 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001491 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001492 last_isspace = 0;
1493 break;
1494
William Lallemand1d705562012-03-12 12:46:41 +01001495 case LOG_FMT_TC: // %Tc
1496 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001497 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001498 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001499 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001500 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001501 last_isspace = 0;
1502 break;
1503
William Lallemand1d705562012-03-12 12:46:41 +01001504 case LOG_FMT_TR: // %Tr
1505 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001506 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001507 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001508 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001509 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001510 last_isspace = 0;
1511 break;
1512
William Lallemand1d705562012-03-12 12:46:41 +01001513 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001514 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001515 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001516 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001517 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001518 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001519 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001520 last_isspace = 0;
1521 break;
1522
Willy Tarreau2beef582012-12-20 17:22:52 +01001523 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001524 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001525 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001526 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001527 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001528 last_isspace = 0;
1529 break;
1530
William Lallemand1d705562012-03-12 12:46:41 +01001531 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001532 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001533 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001534 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001535 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001536 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001537 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001538 last_isspace = 0;
1539 break;
1540
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001541 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001542 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1543 if (ret == NULL)
1544 goto out;
1545 tmplog = ret;
1546 last_isspace = 0;
1547 break;
1548
Willy Tarreau2beef582012-12-20 17:22:52 +01001549 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001550 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001551 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001552 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001553 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001554 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001555 last_isspace = 0;
1556 break;
1557
Willy Tarreau2beef582012-12-20 17:22:52 +01001558 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001559 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001560 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001561 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001562 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001563 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001564 last_isspace = 0;
1565 break;
1566
William Lallemand1d705562012-03-12 12:46:41 +01001567 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001568 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1569 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001570 *tmplog = '\0';
1571 last_isspace = 0;
1572 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001573
William Lallemand1d705562012-03-12 12:46:41 +01001574 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001575 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1576 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001577 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1578 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 +01001579 last_isspace = 0;
1580 break;
1581
William Lallemand1d705562012-03-12 12:46:41 +01001582 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001583 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001584 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001585 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001586 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001587 last_isspace = 0;
1588 break;
1589
William Lallemand1d705562012-03-12 12:46:41 +01001590 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001591 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001592 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001593 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001594 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001595 last_isspace = 0;
1596 break;
1597
William Lallemand1d705562012-03-12 12:46:41 +01001598 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001599 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001600 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001601 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001602 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001603 last_isspace = 0;
1604 break;
1605
William Lallemand1d705562012-03-12 12:46:41 +01001606 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001607 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001608 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001609 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001610 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001611 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001612 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001613 last_isspace = 0;
1614 break;
1615
William Lallemand1d705562012-03-12 12:46:41 +01001616 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001617 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001618 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001619 ret = ltoa_o((s->si[1].conn_retries>0) ?
1620 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001621 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001622 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001623 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001624 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001625 last_isspace = 0;
1626 break;
1627
William Lallemand1d705562012-03-12 12:46:41 +01001628 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001629 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001630 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001631 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001632 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001633 last_isspace = 0;
1634 break;
1635
William Lallemand1d705562012-03-12 12:46:41 +01001636 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001637 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001638 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001639 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001640 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001641 last_isspace = 0;
1642 break;
1643
William Lallemand1d705562012-03-12 12:46:41 +01001644 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001645 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001646 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001647 if (tmp->options & LOG_OPT_QUOTE)
1648 LOGCHAR('"');
1649 LOGCHAR('{');
1650 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1651 if (hdr)
1652 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001653 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001654 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001655 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001656 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001657 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001658 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001659 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001660 }
1661 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001662 if (tmp->options & LOG_OPT_QUOTE)
1663 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001664 last_isspace = 0;
1665 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001666 break;
1667
William Lallemand1d705562012-03-12 12:46:41 +01001668 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001669 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001670 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001671 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1672 if (hdr > 0)
1673 LOGCHAR(' ');
1674 if (tmp->options & LOG_OPT_QUOTE)
1675 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001676 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001677 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001678 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001679 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001680 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001681 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001682 } else if (!(tmp->options & LOG_OPT_QUOTE))
1683 LOGCHAR('-');
1684 if (tmp->options & LOG_OPT_QUOTE)
1685 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001686 last_isspace = 0;
1687 }
1688 }
1689 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001690
William Lallemand1d705562012-03-12 12:46:41 +01001691
1692 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001693 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001694 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001695 if (tmp->options & LOG_OPT_QUOTE)
1696 LOGCHAR('"');
1697 LOGCHAR('{');
1698 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1699 if (hdr)
1700 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001701 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001702 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001703 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001704 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001705 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001706 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001707 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001708 }
1709 LOGCHAR('}');
1710 last_isspace = 0;
1711 if (tmp->options & LOG_OPT_QUOTE)
1712 LOGCHAR('"');
1713 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001714 break;
1715
William Lallemand1d705562012-03-12 12:46:41 +01001716 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001717 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001718 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001719 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1720 if (hdr > 0)
1721 LOGCHAR(' ');
1722 if (tmp->options & LOG_OPT_QUOTE)
1723 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001724 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001725 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001726 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001727 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001728 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001729 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001730 } else if (!(tmp->options & LOG_OPT_QUOTE))
1731 LOGCHAR('-');
1732 if (tmp->options & LOG_OPT_QUOTE)
1733 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001734 last_isspace = 0;
1735 }
1736 }
1737 break;
1738
William Lallemand1d705562012-03-12 12:46:41 +01001739 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001740 /* Request */
1741 if (tmp->options & LOG_OPT_QUOTE)
1742 LOGCHAR('"');
1743 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001744 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001745 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001746 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001747 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001748 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001749 if (tmp->options & LOG_OPT_QUOTE)
1750 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001751 last_isspace = 0;
1752 break;
William Lallemand5f232402012-04-05 18:02:55 +02001753
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001754 case LOG_FMT_HTTP_PATH: // %HP
1755 uri = txn->uri ? txn->uri : "<BADREQ>";
1756
Willy Tarreaub7636d12015-06-17 19:58:02 +02001757 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001758 LOGCHAR('"');
1759
1760 end = uri + strlen(uri);
1761 // look for the first whitespace character
1762 while (uri < end && !HTTP_IS_SPHT(*uri))
1763 uri++;
1764
1765 // keep advancing past multiple spaces
1766 while (uri < end && HTTP_IS_SPHT(*uri)) {
1767 uri++; nspaces++;
1768 }
1769
1770 // look for first space or question mark after url
1771 spc = uri;
1772 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
1773 spc++;
1774
1775 if (!txn->uri || nspaces == 0) {
1776 chunk.str = "<BADREQ>";
1777 chunk.len = strlen("<BADREQ>");
1778 } else {
1779 chunk.str = uri;
1780 chunk.len = spc - uri;
1781 }
1782
1783 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1784 if (ret == NULL || *ret != '\0')
1785 goto out;
1786
1787 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001788 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001789 LOGCHAR('"');
1790
1791 last_isspace = 0;
1792 break;
1793
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001794 case LOG_FMT_HTTP_QUERY: // %HQ
1795 if (tmp->options & LOG_OPT_QUOTE)
1796 LOGCHAR('"');
1797
1798 if (!txn->uri) {
1799 chunk.str = "<BADREQ>";
1800 chunk.len = strlen("<BADREQ>");
1801 } else {
1802 uri = txn->uri;
1803 end = uri + strlen(uri);
1804 // look for the first question mark
1805 while (uri < end && *uri != '?')
1806 uri++;
1807
1808 qmark = uri;
1809 // look for first space or question mark after url
1810 while (uri < end && !HTTP_IS_SPHT(*uri))
1811 uri++;
1812
1813 chunk.str = qmark;
1814 chunk.len = uri - qmark;
1815 }
1816
1817 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1818 if (ret == NULL || *ret != '\0')
1819 goto out;
1820
1821 tmplog = ret;
1822 if (tmp->options & LOG_OPT_QUOTE)
1823 LOGCHAR('"');
1824
1825 last_isspace = 0;
1826 break;
1827
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001828 case LOG_FMT_HTTP_URI: // %HU
1829 uri = txn->uri ? txn->uri : "<BADREQ>";
1830
Willy Tarreaub7636d12015-06-17 19:58:02 +02001831 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001832 LOGCHAR('"');
1833
1834 end = uri + strlen(uri);
1835 // look for the first whitespace character
1836 while (uri < end && !HTTP_IS_SPHT(*uri))
1837 uri++;
1838
1839 // keep advancing past multiple spaces
1840 while (uri < end && HTTP_IS_SPHT(*uri)) {
1841 uri++; nspaces++;
1842 }
1843
1844 // look for first space after url
1845 spc = uri;
1846 while (spc < end && !HTTP_IS_SPHT(*spc))
1847 spc++;
1848
1849 if (!txn->uri || nspaces == 0) {
1850 chunk.str = "<BADREQ>";
1851 chunk.len = strlen("<BADREQ>");
1852 } else {
1853 chunk.str = uri;
1854 chunk.len = spc - uri;
1855 }
1856
1857 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1858 if (ret == NULL || *ret != '\0')
1859 goto out;
1860
1861 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001862 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001863 LOGCHAR('"');
1864
1865 last_isspace = 0;
1866 break;
1867
1868 case LOG_FMT_HTTP_METHOD: // %HM
1869 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001870 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001871 LOGCHAR('"');
1872
1873 end = uri + strlen(uri);
1874 // look for the first whitespace character
1875 spc = uri;
1876 while (spc < end && !HTTP_IS_SPHT(*spc))
1877 spc++;
1878
1879 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
1880 chunk.str = "<BADREQ>";
1881 chunk.len = strlen("<BADREQ>");
1882 } else {
1883 chunk.str = uri;
1884 chunk.len = spc - uri;
1885 }
1886
1887 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1888 if (ret == NULL || *ret != '\0')
1889 goto out;
1890
1891 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001892 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001893 LOGCHAR('"');
1894
1895 last_isspace = 0;
1896 break;
1897
1898 case LOG_FMT_HTTP_VERSION: // %HV
1899 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001900 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001901 LOGCHAR('"');
1902
1903 end = uri + strlen(uri);
1904 // look for the first whitespace character
1905 while (uri < end && !HTTP_IS_SPHT(*uri))
1906 uri++;
1907
1908 // keep advancing past multiple spaces
1909 while (uri < end && HTTP_IS_SPHT(*uri)) {
1910 uri++; nspaces++;
1911 }
1912
1913 // look for the next whitespace character
1914 while (uri < end && !HTTP_IS_SPHT(*uri))
1915 uri++;
1916
1917 // keep advancing past multiple spaces
1918 while (uri < end && HTTP_IS_SPHT(*uri))
1919 uri++;
1920
1921 if (!txn->uri || nspaces == 0) {
1922 chunk.str = "<BADREQ>";
1923 chunk.len = strlen("<BADREQ>");
1924 } else if (uri == end) {
1925 chunk.str = "HTTP/0.9";
1926 chunk.len = strlen("HTTP/0.9");
1927 } else {
1928 chunk.str = uri;
1929 chunk.len = end - uri;
1930 }
1931
1932 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1933 if (ret == NULL || *ret != '\0')
1934 goto out;
1935
1936 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001937 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001938 LOGCHAR('"');
1939
1940 last_isspace = 0;
1941 break;
1942
William Lallemand5f232402012-04-05 18:02:55 +02001943 case LOG_FMT_COUNTER: // %rt
1944 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001945 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001946 if (iret < 0 || iret > dst + maxsize - tmplog)
1947 goto out;
1948 last_isspace = 0;
1949 tmplog += iret;
1950 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001951 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001952 if (ret == NULL)
1953 goto out;
1954 tmplog = ret;
1955 last_isspace = 0;
1956 }
1957 break;
1958
Willy Tarreau7346acb2014-08-28 15:03:15 +02001959 case LOG_FMT_LOGCNT: // %lc
1960 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001961 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001962 if (iret < 0 || iret > dst + maxsize - tmplog)
1963 goto out;
1964 last_isspace = 0;
1965 tmplog += iret;
1966 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001967 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001968 if (ret == NULL)
1969 goto out;
1970 tmplog = ret;
1971 last_isspace = 0;
1972 }
1973 break;
1974
William Lallemand5f232402012-04-05 18:02:55 +02001975 case LOG_FMT_HOSTNAME: // %H
1976 src = hostname;
1977 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1978 if (ret == NULL)
1979 goto out;
1980 tmplog = ret;
1981 last_isspace = 0;
1982 break;
1983
1984 case LOG_FMT_PID: // %pid
1985 if (tmp->options & LOG_OPT_HEXA) {
1986 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1987 if (iret < 0 || iret > dst + maxsize - tmplog)
1988 goto out;
1989 last_isspace = 0;
1990 tmplog += iret;
1991 } else {
1992 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1993 if (ret == NULL)
1994 goto out;
1995 tmplog = ret;
1996 last_isspace = 0;
1997 }
1998 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001999
2000 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002001 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01002002 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002003 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002004 if (ret == NULL)
2005 goto out;
2006 tmplog = ret;
2007 last_isspace = 0;
2008 break;
2009
William Lallemandbddd4fd2012-02-27 11:23:10 +01002010 }
2011 }
2012
2013out:
William Lallemand1d705562012-03-12 12:46:41 +01002014 /* *tmplog is a unused character */
2015 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002016 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002017
Willy Tarreaubaaee002006-06-26 02:48:02 +02002018}
2019
William Lallemand1d705562012-03-12 12:46:41 +01002020/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002021 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002022 * Will not log if the frontend has no log defined.
2023 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002024void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002025{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002026 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002027 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002028 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002029
2030 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002031 err = (s->flags & SF_REDISP) ||
2032 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2033 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002034 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002035 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002036
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002037 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002038 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002039
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002040 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002041 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002042
Willy Tarreauabcd5142013-06-11 17:18:02 +02002043 if (s->logs.level) { /* loglevel was overridden */
2044 if (s->logs.level == -1) {
2045 s->logs.logwait = 0; /* logs disabled */
2046 return;
2047 }
2048 level = s->logs.level - 1;
2049 }
2050 else {
2051 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002052 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002053 level = LOG_ERR;
2054 }
William Lallemand1d705562012-03-12 12:46:41 +01002055
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002056 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002057 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002058 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002059 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002060 }
2061
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002062 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2063 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2064 &sess->fe->logformat_sd);
2065 }
2066
Dragan Dosen59cee972015-09-19 22:09:02 +02002067 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002068 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002069 sess->fe->log_count++;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002070 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002071 s->logs.logwait = 0;
2072 }
2073}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002074
Willy Tarreaubaaee002006-06-26 02:48:02 +02002075/*
2076 * Local variables:
2077 * c-indent-level: 8
2078 * c-basic-offset: 8
2079 * End:
2080 */