blob: 5fc68cef95a32d8a5cb6cdb768b185f48a4cf9a8 [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>
Willy Tarreau077edcb2016-08-10 18:30:56 +020025#include <sys/uio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020028#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020030#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010033#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020034
William Lallemand5f232402012-04-05 18:02:55 +020035#include <proto/frontend.h>
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +000036#include <proto/proto_http.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020037#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010038#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020039#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010040#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020041#ifdef USE_OPENSSL
42#include <proto/ssl_sock.h>
43#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Dragan Dosen43885c72015-10-01 13:18:13 +020045struct log_fmt {
46 char *name;
47 struct {
48 struct chunk sep1; /* first pid separator */
49 struct chunk sep2; /* second pid separator */
50 } pid;
51};
52
53static const struct log_fmt log_formats[LOG_FORMATS] = {
54 [LOG_FORMAT_RFC3164] = {
55 .name = "rfc3164",
56 .pid = {
57 .sep1 = { .str = "[", .len = 1 },
58 .sep2 = { .str = "]: ", .len = 3 }
59 }
60 },
61 [LOG_FORMAT_RFC5424] = {
62 .name = "rfc5424",
63 .pid = {
64 .sep1 = { .str = " ", .len = 1 },
65 .sep2 = { .str = " - ", .len = 3 }
66 }
67 }
Dragan Dosen1322d092015-09-22 16:05:32 +020068};
69
Dragan Dosen835b9212016-02-12 13:23:03 +010070#define FD_SETS_ARE_BITFIELDS
71#ifdef FD_SETS_ARE_BITFIELDS
72/*
73 * This map is used with all the FD_* macros to check whether a particular bit
74 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
75 * which should be escaped. When FD_ISSET() returns non-zero, it means that the
76 * byte should be escaped. Be careful to always pass bytes from 0 to 255
77 * exclusively to the macros.
78 */
79fd_set rfc5424_escape_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
80
81#else
82#error "Check if your OS uses bitfields for fd_sets"
83#endif
84
Willy Tarreaubaaee002006-06-26 02:48:02 +020085const char *log_facilities[NB_LOG_FACILITIES] = {
86 "kern", "user", "mail", "daemon",
87 "auth", "syslog", "lpr", "news",
88 "uucp", "cron", "auth2", "ftp",
89 "ntp", "audit", "alert", "cron2",
90 "local0", "local1", "local2", "local3",
91 "local4", "local5", "local6", "local7"
92};
93
Willy Tarreaubaaee002006-06-26 02:48:02 +020094const char *log_levels[NB_LOG_LEVELS] = {
95 "emerg", "alert", "crit", "err",
96 "warning", "notice", "info", "debug"
97};
98
Willy Tarreau570f2212013-06-10 16:42:09 +020099const 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 +0200100const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101
William Lallemand723b73a2012-02-08 16:37:49 +0100102
103/* log_format */
104struct logformat_type {
105 char *name;
106 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100107 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +0200108 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +0100109 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +0100110 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +0100111};
112
William Lallemandb7ff6a32012-03-02 14:35:21 +0100113int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
114
William Lallemand723b73a2012-02-08 16:37:49 +0100115/* log_format variable names */
116static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +0200117 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +0100118
119 /* please keep these lines sorted ! */
120 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
121 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
122 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
123 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
124 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +0200125 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200126 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200127 { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100128 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200129 { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */
130 { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */
131 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
132 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */
133 { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
134 { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */
Willy Tarreau27b639d2016-05-17 17:55:27 +0200135 { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100136 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +0200137 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +0100138 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
139 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +0200140 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100141 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200142 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100143 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
144 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200145 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +0100146 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
147 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
148 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
149 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
150 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
151 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
152 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200153 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
154 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
155 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
156 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000157 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
158 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000159 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000160 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
161 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200162 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100163 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200164 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100165 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
166 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100167 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100168 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
169 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
170 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
171 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
172 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200173 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
174 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100175 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200176 { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */
177 { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */
178 { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */
Willy Tarreau2beef582012-12-20 17:22:52 +0100179 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
180 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
181
182 /* The following tags are deprecated and will be removed soon */
183 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
184 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
185 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
186 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
187 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
188 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
189 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
190 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
191 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
192 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
193 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200194 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100195};
196
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200197char default_http_log_format[] = "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
198char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%trg] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl";
Willy Tarreau2beef582012-12-20 17:22:52 +0100199char 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 +0100200char *log_format = NULL;
201
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200202/* Default string used for structured-data part in RFC5424 formatted
203 * syslog messages.
204 */
205char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200206
207/* This is a global syslog header, common to all outgoing messages in
208 * RFC3164 format. It begins with time-based part and is updated by
209 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200210 */
211char *logheader = NULL;
212
Dragan Dosen1322d092015-09-22 16:05:32 +0200213/* This is a global syslog header for messages in RFC5424 format. It is
214 * updated by update_log_hdr_rfc5424().
215 */
216char *logheader_rfc5424 = NULL;
217
Dragan Dosen59cee972015-09-19 22:09:02 +0200218/* This is a global syslog message buffer, common to all outgoing
219 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100220 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200221char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100222
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200223/* A global syslog message buffer, common to all RFC5424 syslog messages.
224 * Currently, it is used for generating the structured-data part.
225 */
226char *logline_rfc5424 = NULL;
227
William Lallemand723b73a2012-02-08 16:37:49 +0100228struct logformat_var_args {
229 char *name;
230 int mask;
231};
232
233struct logformat_var_args var_args_list[] = {
234// global
235 { "M", LOG_OPT_MANDATORY },
236 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200237 { "X", LOG_OPT_HEXA },
Dragan Dosen835b9212016-02-12 13:23:03 +0100238 { "E", LOG_OPT_ESC },
William Lallemand723b73a2012-02-08 16:37:49 +0100239 { 0, 0 }
240};
241
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200242/* return the name of the directive used in the current proxy for which we're
243 * currently parsing a header, when it is known.
244 */
245static inline const char *fmt_directive(const struct proxy *curproxy)
246{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100247 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200248 case ARGC_ACL:
249 return "acl";
250 case ARGC_STK:
251 return "stick";
252 case ARGC_TRK:
253 return "track-sc";
254 case ARGC_LOG:
255 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200256 case ARGC_LOGSD:
257 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100258 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100259 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100260 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100261 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200262 case ARGC_UIF:
263 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100264 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200265 return "redirect";
266 case ARGC_CAP:
267 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200268 case ARGC_SRV:
269 return "server";
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200270 case ARGC_SPOE:
271 return "spoe-message";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100272 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200273 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100274 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200275}
276
William Lallemand723b73a2012-02-08 16:37:49 +0100277/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100278 * callback used to configure addr source retrieval
279 */
280int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
281{
282 curproxy->options2 |= PR_O2_SRC_ADDR;
283
284 return 0;
285}
286
287
288/*
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100289 * Parse args in a logformat_var. Returns 0 in error
290 * case, otherwise, it returns 1.
William Lallemand723b73a2012-02-08 16:37:49 +0100291 */
292int parse_logformat_var_args(char *args, struct logformat_node *node)
293{
294 int i = 0;
295 int end = 0;
296 int flags = 0; // 1 = + 2 = -
297 char *sp = NULL; // start pointer
298
299 if (args == NULL)
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100300 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100301
302 while (1) {
303 if (*args == '\0')
304 end = 1;
305
306 if (*args == '+') {
307 // add flag
308 sp = args + 1;
309 flags = 1;
310 }
311 if (*args == '-') {
312 // delete flag
313 sp = args + 1;
314 flags = 2;
315 }
316
317 if (*args == '\0' || *args == ',') {
318 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100319 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100320 if (strcmp(sp, var_args_list[i].name) == 0) {
321 if (flags == 1) {
322 node->options |= var_args_list[i].mask;
323 break;
324 } else if (flags == 2) {
325 node->options &= ~var_args_list[i].mask;
326 break;
327 }
328 }
329 }
330 sp = NULL;
331 if (end)
332 break;
333 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100334 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100335 }
Thierry FOURNIER / OZON.IObca46f02016-11-22 23:13:04 +0100336 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100337}
338
339/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100340 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
341 * must pass the args part in the <arg> pointer with its length in <arg_len>,
342 * and varname with its length in <var> and <var_len> respectively. <arg> is
343 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100344 * Returns false in error case and err is filled, otherwise returns true.
William Lallemand723b73a2012-02-08 16:37:49 +0100345 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100346int 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 +0100347{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100348 int j;
349 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100350
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100351 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
352 if (strlen(logformat_keywords[j].name) == var_len &&
353 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
354 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200355 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100356 if (!node) {
357 Alert("Out of memory error.\n");
358 return 0;
359 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100360 node->type = logformat_keywords[j].type;
361 node->options = *defoptions;
362 if (arg_len) {
363 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100364 if (!parse_logformat_var_args(node->arg, node))
365 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100366 }
367 if (node->type == LOG_FMT_GLOBAL) {
368 *defoptions = node->options;
369 free(node->arg);
370 free(node);
371 } else {
372 if (logformat_keywords[j].config_callback &&
373 logformat_keywords[j].config_callback(node, curproxy) != 0) {
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100374 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100375 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100376 curproxy->to_log |= logformat_keywords[j].lw;
377 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100378 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100379 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100380 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200381 curproxy->conf.args.file, curproxy->conf.args.line,
382 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100383 return 1;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100384 } else {
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100385 Alert("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
386 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
387 logformat_keywords[j].name);
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100388 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100389 }
William Lallemand723b73a2012-02-08 16:37:49 +0100390 }
391 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100392
393 j = var[var_len];
394 var[var_len] = 0;
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100395 Alert("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",
396 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100397 var[var_len] = j;
Thierry FOURNIER / OZON.IOeca4d952016-11-22 22:06:04 +0100398 return 0;
William Lallemand723b73a2012-02-08 16:37:49 +0100399}
400
401/*
402 * push to the logformat linked list
403 *
404 * start: start pointer
405 * end: end text pointer
406 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100407 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100408 *
409 * LOG_TEXT: copy chars from start to end excluding end.
410 *
411*/
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100412int add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100413{
414 char *str;
415
Willy Tarreaua3571662012-12-20 21:59:12 +0100416 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200417 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100418 if (!node) {
419 Alert("Out of memory error.\n");
420 return 0;
421 }
Vincent Bernat02779b62016-04-03 13:48:43 +0200422 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100423 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100424 str[end - start] = '\0';
425 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100426 node->type = LOG_FMT_TEXT; // type string
427 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100428 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200429 struct logformat_node *node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100430 if (!node) {
431 Alert("Out of memory error.\n");
432 return 0;
433 }
William Lallemand1d705562012-03-12 12:46:41 +0100434 node->type = LOG_FMT_SEPARATOR;
435 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100436 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100437 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100438}
439
440/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100441 * Parse the sample fetch expression <text> and add a node to <list_format> upon
442 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200443 * should work. The curpx->conf.args.ctx must be set by the caller.
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100444 *
445 * In error case, the function returns 0, otherwise it returns 1.
Willy Tarreauc8368452012-12-21 00:09:23 +0100446 */
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100447int 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 +0100448{
449 char *cmd[2];
450 struct sample_expr *expr;
451 struct logformat_node *node;
452 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100453 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100454
455 cmd[0] = text;
456 cmd[1] = "";
457 cmd_arg = 0;
458
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100459 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100460 if (!expr) {
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100461 Alert("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
462 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
463 text, errmsg);
464 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100465 }
466
Vincent Bernat02779b62016-04-03 13:48:43 +0200467 node = calloc(1, sizeof(*node));
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100468 if (!node) {
469 Alert("Out of memory error.\n");
470 return 0;
471 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100472 node->type = LOG_FMT_EXPR;
473 node->expr = expr;
474 node->options = options;
475
476 if (arg_len) {
477 node->arg = my_strndup(arg, arg_len);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100478 if (!parse_logformat_var_args(node->arg, node))
479 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100480 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100481 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100482 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
483
Willy Tarreau434c57c2013-01-08 01:10:24 +0100484 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100485 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
486
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100487 if (!(expr->fetch->val & cap)) {
488 Alert("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
489 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
490 text, sample_src_names(expr->fetch->use));
491 return 0;
492 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100493
Willy Tarreauc8368452012-12-21 00:09:23 +0100494 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
495 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100496 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100497
William Lallemand65ad6e12014-01-31 15:08:02 +0100498 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
499 * needed with some sample fetches (eg: ssl*). We always set it for
500 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100501 */
502 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100503 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100504 LIST_ADDQ(list_format, &node->list);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100505 return 1;
Willy Tarreauc8368452012-12-21 00:09:23 +0100506}
507
508/*
William Lallemand723b73a2012-02-08 16:37:49 +0100509 * Parse the log_format string and fill a linked list.
510 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200511 * You can set arguments using { } : %{many arguments}varname.
512 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100513 *
514 * str: the string to parse
515 * curproxy: the proxy affected
516 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100517 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100518 * cap: all SMP_VAL_* flags supported by the consumer
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100519 *
520 * The function returns 1 in success case, otherwise, it returns 0.
William Lallemand723b73a2012-02-08 16:37:49 +0100521 */
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100522int parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap)
William Lallemand723b73a2012-02-08 16:37:49 +0100523{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100524 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100525 char *arg = NULL; /* start pointer for args */
526 char *var = NULL; /* start pointer for vars */
527 int arg_len = 0;
528 int var_len = 0;
529 int cformat; /* current token format */
530 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100531 struct logformat_node *tmplf, *back;
532
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100533 sp = str = backfmt = strdup(fmt);
Thierry FOURNIER / OZON.IO9cbfef22016-11-22 23:24:10 +0100534 if (!str) {
535 Alert("Out of memory error.\n");
536 return 0;
537 }
William Lallemand1dc00ef2012-08-09 16:41:35 +0200538 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200539
William Lallemand723b73a2012-02-08 16:37:49 +0100540 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100541 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100542 LIST_DEL(&tmplf->list);
543 free(tmplf);
544 }
545
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100546 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100547 pformat = cformat;
548
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100549 if (!*str)
550 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100551
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100552 /* The prinicple of the two-step state machine below is to first detect a change, and
553 * second have all common paths processed at one place. The common paths are the ones
554 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
555 * We use the common LF_INIT state to dispatch to the different final states.
556 */
557 switch (pformat) {
558 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100559 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100560 arg_len = var_len = 0;
561 if (*str == '{') { // optional argument
562 cformat = LF_STARG;
563 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100564 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100565 else if (*str == '[') {
566 cformat = LF_STEXPR;
567 var = str + 1; // store expr in variable name
568 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100569 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100570 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100571 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100572 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100573 else if (*str == '%')
574 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100575 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100576 /* single '%' followed by blank or digit, send them both */
577 cformat = LF_TEXT;
578 pformat = LF_TEXT; /* finally we include the previous char as well */
579 sp = str - 1; /* send both the '%' and the current char */
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100580 Alert("parsing [%s:%d] : Unexpected variable name near '%c' at position %d in %s line : '%s'. Maybe you want to write a simgle '%%', use the syntax '%%%%'.\n",
581 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
582 return 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100583
584 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100585 else
586 cformat = LF_INIT; // handle other cases of litterals
587 break;
588
589 case LF_STARG: // text immediately following '%{'
590 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100591 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100592 arg_len = str - arg;
593 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100594 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100595 break;
596
597 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100598 if (*str == '[') {
599 cformat = LF_STEXPR;
600 var = str + 1; // store expr in variable name
601 break;
602 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100603 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100604 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100605 var = str;
606 break;
607 }
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100608 Alert("parsing [%s:%d] : Parse argument modifier without variable name, in '%s' near '%%{%s}'\n",
609 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
610 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100611
Willy Tarreauc8368452012-12-21 00:09:23 +0100612 case LF_STEXPR: // text immediately following '%['
613 if (*str == ']') { // end of arg
614 cformat = LF_EDEXPR;
615 var_len = str - var;
616 *str = 0; // needed for parsing the expression
617 }
618 break;
619
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100620 case LF_VAR: // text part of a variable name
621 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100622 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100623 cformat = LF_INIT; // not variable name anymore
624 break;
625
Willy Tarreauc8368452012-12-21 00:09:23 +0100626 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100627 cformat = LF_INIT;
628 }
629
630 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
631 switch (*str) {
632 case '%': cformat = LF_STARTVAR; break;
633 case ' ': cformat = LF_SEPARATOR; break;
634 case 0 : cformat = LF_END; break;
635 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100636 }
637 }
638
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100639 if (cformat != pformat || pformat == LF_SEPARATOR) {
640 switch (pformat) {
641 case LF_VAR:
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100642 if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options))
643 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100644 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100645 case LF_STEXPR:
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100646 if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap,
647 curproxy->conf.args.file, curproxy->conf.args.line))
648 return 0;
Willy Tarreauc8368452012-12-21 00:09:23 +0100649 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100650 case LF_TEXT:
651 case LF_SEPARATOR:
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100652 if (!add_to_logformat_list(sp, str, pformat, list_format))
653 return 0;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100654 break;
655 }
656 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100657 }
658 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100659
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100660 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) {
661 Alert("parsing [%s:%d] : Truncated '%s' line after '%s'\n",
662 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
663 var ? var : arg ? arg : "%");
664 return 0;
665 }
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100666 free(backfmt);
Thierry FOURNIER / OZON.IOa2c38d72016-11-22 23:11:21 +0100667
668 return 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100669}
670
Willy Tarreaubaaee002006-06-26 02:48:02 +0200671/*
672 * Displays the message on stderr with the date and pid. Overrides the quiet
673 * mode during startup.
674 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200675void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676{
677 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200678 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679
680 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
681 va_start(argp, fmt);
682
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200683 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200685 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686 vfprintf(stderr, fmt, argp);
687 fflush(stderr);
688 va_end(argp);
689 }
690}
691
692
693/*
694 * Displays the message on stderr with the date and pid.
695 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200696void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697{
698 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200699 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700
701 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
702 va_start(argp, fmt);
703
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200704 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200705 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200706 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200707 vfprintf(stderr, fmt, argp);
708 fflush(stderr);
709 va_end(argp);
710 }
711}
712
713/*
714 * Displays the message on <out> only if quiet mode is not set.
715 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200716void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717{
718 va_list argp;
719
720 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
721 va_start(argp, fmt);
722 vfprintf(out, fmt, argp);
723 fflush(out);
724 va_end(argp);
725 }
726}
727
728/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200729 * returns log format for <fmt> or -1 if not found.
730 */
731int get_log_format(const char *fmt)
732{
733 int format;
734
735 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200736 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200737 format--;
738
739 return format;
740}
741
742/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 * returns log level for <lev> or -1 if not found.
744 */
745int get_log_level(const char *lev)
746{
747 int level;
748
749 level = NB_LOG_LEVELS - 1;
750 while (level >= 0 && strcmp(log_levels[level], lev))
751 level--;
752
753 return level;
754}
755
Willy Tarreaubaaee002006-06-26 02:48:02 +0200756/*
757 * returns log facility for <fac> or -1 if not found.
758 */
759int get_log_facility(const char *fac)
760{
761 int facility;
762
763 facility = NB_LOG_FACILITIES - 1;
764 while (facility >= 0 && strcmp(log_facilities[facility], fac))
765 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100766
Willy Tarreaubaaee002006-06-26 02:48:02 +0200767 return facility;
768}
769
William Lallemanda1cc3812012-02-08 16:38:44 +0100770/*
Dragan Dosen835b9212016-02-12 13:23:03 +0100771 * Encode the string.
772 *
773 * When using the +E log format option, it will try to escape '"\]'
774 * characters with '\' as prefix. The same prefix should not be used as
775 * <escape>.
776 */
777static char *lf_encode_string(char *start, char *stop,
778 const char escape, const fd_set *map,
779 const char *string,
780 struct logformat_node *node)
781{
782 if (node->options & LOG_OPT_ESC) {
783 if (start < stop) {
784 stop--; /* reserve one byte for the final '\0' */
785 while (start < stop && *string != '\0') {
786 if (!FD_ISSET((unsigned char)(*string), map)) {
787 if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map))
788 *start++ = *string;
789 else {
790 if (start + 2 >= stop)
791 break;
792 *start++ = '\\';
793 *start++ = *string;
794 }
795 }
796 else {
797 if (start + 3 >= stop)
798 break;
799 *start++ = escape;
800 *start++ = hextab[(*string >> 4) & 15];
801 *start++ = hextab[*string & 15];
802 }
803 string++;
804 }
805 *start = '\0';
806 }
807 }
808 else {
809 return encode_string(start, stop, escape, map, string);
810 }
811
812 return start;
813}
814
815/*
816 * Encode the chunk.
817 *
818 * When using the +E log format option, it will try to escape '"\]'
819 * characters with '\' as prefix. The same prefix should not be used as
820 * <escape>.
821 */
822static char *lf_encode_chunk(char *start, char *stop,
823 const char escape, const fd_set *map,
824 const struct chunk *chunk,
825 struct logformat_node *node)
826{
827 char *str, *end;
828
829 if (node->options & LOG_OPT_ESC) {
830 if (start < stop) {
831 str = chunk->str;
832 end = chunk->str + chunk->len;
833
834 stop--; /* reserve one byte for the final '\0' */
835 while (start < stop && str < end) {
836 if (!FD_ISSET((unsigned char)(*str), map)) {
837 if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map))
838 *start++ = *str;
839 else {
840 if (start + 2 >= stop)
841 break;
842 *start++ = '\\';
843 *start++ = *str;
844 }
845 }
846 else {
847 if (start + 3 >= stop)
848 break;
849 *start++ = escape;
850 *start++ = hextab[(*str >> 4) & 15];
851 *start++ = hextab[*str & 15];
852 }
853 str++;
854 }
855 *start = '\0';
856 }
857 }
858 else {
859 return encode_chunk(start, stop, escape, map, chunk);
860 }
861
862 return start;
863}
864
865/*
William Lallemanda1cc3812012-02-08 16:38:44 +0100866 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +0100867 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +0100868 *
869 * Return the adress of the \0 character, or NULL on error
870 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100871char *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 +0100872{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100873 if (size < 2)
874 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100875
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100876 if (node->options & LOG_OPT_QUOTE) {
877 *(dst++) = '"';
878 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100879 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100880
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100881 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +0200882 if (++len > size)
883 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +0100884 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +0100885 char *ret;
886
Dragan Dosendb1b6f92016-07-25 11:35:02 +0200887 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +0100888 if (ret == NULL || *ret != '\0')
889 return NULL;
890 len = ret - dst;
891 }
892 else {
Dragan Dosen835b9212016-02-12 13:23:03 +0100893 len = strlcpy2(dst, src, len);
894 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100895
896 size -= len;
897 dst += len;
898 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100899 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
900 if (size < 2)
901 return NULL;
902 *(dst++) = '-';
903 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100904
905 if (node->options & LOG_OPT_QUOTE) {
906 if (size < 2)
907 return NULL;
908 *(dst++) = '"';
909 }
910
911 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100912 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100913}
914
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100915static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
916{
917 return lf_text_len(dst, src, size, size, node);
918}
919
William Lallemand5f232402012-04-05 18:02:55 +0200920/*
921 * Write a IP adress to the log string
922 * +X option write in hexadecimal notation, most signifant byte on the left
923 */
924char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
925{
926 char *ret = dst;
927 int iret;
928 char pn[INET6_ADDRSTRLEN];
929
930 if (node->options & LOG_OPT_HEXA) {
931 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
932 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
933 if (iret < 0 || iret > size)
934 return NULL;
935 ret += iret;
936 } else {
937 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
938 ret = lf_text(dst, pn, size, node);
939 if (ret == NULL)
940 return NULL;
941 }
942 return ret;
943}
944
945/*
946 * Write a port to the log
947 * +X option write in hexadecimal notation, most signifant byte on the left
948 */
949char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
950{
951 char *ret = dst;
952 int iret;
953
954 if (node->options & LOG_OPT_HEXA) {
955 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
956 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
957 if (iret < 0 || iret > size)
958 return NULL;
959 ret += iret;
960 } else {
961 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
962 if (ret == NULL)
963 return NULL;
964 }
965 return ret;
966}
967
Dragan Dosen1322d092015-09-22 16:05:32 +0200968/* Re-generate time-based part of the syslog header in RFC3164 format at
969 * the beginning of logheader once a second and return the pointer to the
970 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100971 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200972static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200973{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100974 static long tvsec;
975 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Dragan Dosen43885c72015-10-01 13:18:13 +0200976 static struct chunk host = { NULL, 0, 0 };
977 static int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200978
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200979 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200980 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200981 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100982 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200983
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200984 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +0200985 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200986
Dragan Dosen43885c72015-10-01 13:18:13 +0200987 if (unlikely(global.log_send_hostname != host.str)) {
988 host.str = global.log_send_hostname;
989 host.len = host.str ? strlen(host.str) : 0;
990 sep = host.len ? 1 : 0;
991 }
992
Dragan Dosen59cee972015-09-19 22:09:02 +0200993 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +0200994 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200995 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +0200996 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
997 host.len, host.str, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200998 /* WARNING: depending upon implementations, snprintf may return
999 * either -1 or the number of bytes that would be needed to store
1000 * the total message. In both cases, we must adjust it.
1001 */
Willy Tarreau18324f52014-06-27 18:10:07 +02001002 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1003 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001004
Dragan Dosen59cee972015-09-19 22:09:02 +02001005 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001006 }
1007
Willy Tarreau094af4e2015-01-07 15:03:42 +01001008 dataptr[0] = 0; // ensure we get rid of any previous attempt
1009
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001010 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001011}
1012
Dragan Dosen1322d092015-09-22 16:05:32 +02001013/* Re-generate time-based part of the syslog header in RFC5424 format at
1014 * the beginning of logheader_rfc5424 once a second and return the pointer
1015 * to the first character after it.
1016 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001017static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +02001018{
1019 static long tvsec;
1020 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001021 const char *gmt_offset;
Dragan Dosen1322d092015-09-22 16:05:32 +02001022
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001023 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +02001024 /* this string is rebuild only once a second */
1025 struct tm tm;
1026 int hdr_len;
1027
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001028 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +02001029 get_localtime(tvsec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001030 gmt_offset = get_gmt_offset(time, &tm);
Dragan Dosen1322d092015-09-22 16:05:32 +02001031
1032 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +02001033 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +02001034 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +02001035 tm.tm_hour, tm.tm_min, tm.tm_sec,
Benoit GARNIERb413c2a2016-03-27 11:08:03 +02001036 gmt_offset, gmt_offset+3,
Dragan Dosen43885c72015-10-01 13:18:13 +02001037 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +02001038 /* WARNING: depending upon implementations, snprintf may return
1039 * either -1 or the number of bytes that would be needed to store
1040 * the total message. In both cases, we must adjust it.
1041 */
1042 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1043 hdr_len = global.max_syslog_len;
1044
1045 dataptr = logheader_rfc5424 + hdr_len;
1046 }
1047
1048 dataptr[0] = 0; // ensure we get rid of any previous attempt
1049
1050 return dataptr;
1051}
1052
William Lallemand2a4a44f2012-02-06 16:00:33 +01001053/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001054 * This function sends the syslog message using a printf format string. It
1055 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001056 */
1057void send_log(struct proxy *p, int level, const char *format, ...)
1058{
1059 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001060 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001061
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001062 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001063 return;
1064
William Lallemand2a4a44f2012-02-06 16:00:33 +01001065 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001066 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001067 if (data_len < 0 || data_len > global.max_syslog_len)
1068 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001070
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001071 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001072}
1073
1074/*
1075 * This function sends a syslog message.
1076 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001077 * It overrides the last byte of the message vector with an LF character.
1078 * The arguments <sd> and <sd_size> are used for the structured-data part
1079 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001080 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001081void __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 +01001082{
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001083 static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1084 static struct msghdr msghdr = {
1085 .msg_iov = iovec,
1086 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1087 };
William Lallemand2a4a44f2012-02-06 16:00:33 +01001088 static int logfdunix = -1; /* syslog to AF_UNIX socket */
1089 static int logfdinet = -1; /* syslog to AF_INET socket */
1090 static char *dataptr = NULL;
1091 int fac_level;
1092 struct list *logsrvs = NULL;
1093 struct logsrv *tmp = NULL;
1094 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +02001095 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001096 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001097 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001098 struct chunk *tag = &global.log_tag;
1099 static int curr_pid;
1100 static char pidstr[100];
1101 static struct chunk pid;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001102
1103 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104
1105 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +02001106 if (!LIST_ISEMPTY(&global.logsrvs)) {
1107 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001108 }
1109 } else {
William Lallemand0f99e342011-10-12 17:50:54 +02001110 if (!LIST_ISEMPTY(&p->logsrvs)) {
1111 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001112 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001113 if (p->log_tag.str) {
1114 tag = &p->log_tag;
1115 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001116 }
1117
William Lallemand0f99e342011-10-12 17:50:54 +02001118 if (!logsrvs)
1119 return;
1120
Dragan Dosen43885c72015-10-01 13:18:13 +02001121 if (unlikely(curr_pid != getpid())) {
1122 curr_pid = getpid();
1123 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1124 chunk_initstr(&pid, pidstr);
1125 }
1126
Robert Tsai81ae1952007-12-05 10:47:29 +01001127 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +02001128 nblogger = 0;
1129 list_for_each_entry(tmp, logsrvs, list) {
1130 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +01001131 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +01001132 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +02001133 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +01001134 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001135 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +02001136 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001137 int tag_max = 0;
1138 int pid_sep1_max = 0;
1139 int pid_max = 0;
1140 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001141 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001142 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +01001143
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001144 nblogger++;
1145
Willy Tarreaubaaee002006-06-26 02:48:02 +02001146 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +02001147 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001148 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001149
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001150 if (unlikely(*plogfd < 0)) {
1151 /* socket not successfully initialized yet */
1152 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
1153
1154 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
1155 Alert("socket for logger #%d failed: %s (errno=%d)\n",
1156 nblogger, strerror(errno), errno);
1157 continue;
1158 }
1159 /* we don't want to receive anything on this socket */
1160 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1161 /* does nothing under Linux, maybe needed for others */
1162 shutdown(*plogfd, SHUT_RD);
1163 }
1164
Dragan Dosen1322d092015-09-22 16:05:32 +02001165 switch (logsrv->format) {
1166 case LOG_FORMAT_RFC3164:
1167 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001168 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +02001169 break;
1170
1171 case LOG_FORMAT_RFC5424:
1172 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001173 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001174 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001175 break;
1176
1177 default:
1178 continue; /* must never happen */
1179 }
1180
1181 hdr_size = hdr_ptr - hdr;
1182
Willy Tarreaubaaee002006-06-26 02:48:02 +02001183 /* For each target, we may have a different facility.
1184 * We can also have a different log level for each message.
1185 * This induces variations in the message header length.
1186 * Since we don't want to recompute it each time, nor copy it every
1187 * time, we only change the facility in the pre-computed header,
1188 * and we change the pointer to the header accordingly.
1189 */
William Lallemand0f99e342011-10-12 17:50:54 +02001190 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001191 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001192 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001193 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001194 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001195 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001196 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001197 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001198
Dragan Dosen1322d092015-09-22 16:05:32 +02001199 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001200
Dragan Dosen43885c72015-10-01 13:18:13 +02001201 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001202 if (unlikely(hdr_size >= logsrv->maxlen)) {
1203 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001204 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001205 goto send;
1206 }
1207
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001208 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001209
Dragan Dosen43885c72015-10-01 13:18:13 +02001210 /* tag */
1211 tag_max = tag->len;
1212 if (unlikely(tag_max >= maxlen)) {
1213 tag_max = maxlen - 1;
1214 sd_max = 0;
1215 goto send;
1216 }
1217
1218 maxlen -= tag_max;
1219
1220 /* first pid separator */
1221 pid_sep1_max = log_formats[logsrv->format].pid.sep1.len;
1222 if (unlikely(pid_sep1_max >= maxlen)) {
1223 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001224 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001225 goto send;
1226 }
1227
Dragan Dosen43885c72015-10-01 13:18:13 +02001228 pid_sep1 = log_formats[logsrv->format].pid.sep1.str;
1229 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001230
Dragan Dosen43885c72015-10-01 13:18:13 +02001231 /* pid */
1232 pid_max = pid.len;
1233 if (unlikely(pid_max >= maxlen)) {
1234 pid_max = maxlen - 1;
1235 sd_max = 0;
1236 goto send;
1237 }
1238
1239 maxlen -= pid_max;
1240
1241 /* second pid separator */
1242 pid_sep2_max = log_formats[logsrv->format].pid.sep2.len;
1243 if (unlikely(pid_sep2_max >= maxlen)) {
1244 pid_sep2_max = maxlen - 1;
1245 sd_max = 0;
1246 goto send;
1247 }
1248
1249 pid_sep2 = log_formats[logsrv->format].pid.sep2.str;
1250 maxlen -= pid_sep2_max;
1251
1252 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001253 if (sd_max >= maxlen) {
1254 sd_max = maxlen - 1;
1255 goto send;
1256 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001257
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001258 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001259send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001260 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001261 iovec[0].iov_len = hdr_max;
1262 iovec[1].iov_base = tag->str;
1263 iovec[1].iov_len = tag_max;
1264 iovec[2].iov_base = pid_sep1;
1265 iovec[2].iov_len = pid_sep1_max;
1266 iovec[3].iov_base = pid.str;
1267 iovec[3].iov_len = pid_max;
1268 iovec[4].iov_base = pid_sep2;
1269 iovec[4].iov_len = pid_sep2_max;
1270 iovec[5].iov_base = sd;
1271 iovec[5].iov_len = sd_max;
1272 iovec[6].iov_base = dataptr;
1273 iovec[6].iov_len = max;
1274 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1275 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001276
1277 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1278 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1279
1280 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001281
Robert Tsai81ae1952007-12-05 10:47:29 +01001282 if (sent < 0) {
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001283 Alert("sendmsg logger #%d failed: %s (errno=%d)\n",
Robert Tsai81ae1952007-12-05 10:47:29 +01001284 nblogger, strerror(errno), errno);
1285 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001286 }
1287}
1288
William Lallemandbddd4fd2012-02-27 11:23:10 +01001289extern fd_set hdr_encode_map[];
1290extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001291extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001292
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001294const 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 +01001295const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1296 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1297 Set-cookie Updated, unknown, unknown */
1298
William Lallemand1d705562012-03-12 12:46:41 +01001299/*
1300 * try to write a character if there is enough space, or goto out
1301 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001302#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001303 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001304 *(tmplog++) = (x); \
1305 } else { \
1306 goto out; \
1307 } \
1308 } while(0)
1309
Dragan Dosen835b9212016-02-12 13:23:03 +01001310
1311/* Initializes some log data.
1312 */
1313void init_log()
1314{
1315 char *tmp;
1316
1317 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1318 * inside PARAM-VALUE should be escaped with '\' as prefix.
1319 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1320 * details.
1321 */
1322 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1323
1324 tmp = "\"\\]";
1325 while (*tmp) {
1326 FD_SET(*tmp, rfc5424_escape_map);
1327 tmp++;
1328 }
1329}
William Lallemand1d705562012-03-12 12:46:41 +01001330
Willy Tarreaudf974472012-12-28 02:44:01 +01001331/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1332 * <maxsize> characters. Returns the size of the output string in characters,
1333 * not counting the trailing zero which is always added if the resulting size
1334 * is not zero.
1335 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001336int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001337{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001338 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001339 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +01001340 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001341 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001342 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001343 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001344 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001345 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001346 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001347 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001348 int t_request;
1349 int hdr;
1350 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001351 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001352 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001353 char *ret;
1354 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001355 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001356 struct timeval tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001357
William Lallemandbddd4fd2012-02-27 11:23:10 +01001358 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001359
William Lallemandbddd4fd2012-02-27 11:23:10 +01001360 t_request = -1;
1361 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1362 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1363
William Lallemand1d705562012-03-12 12:46:41 +01001364 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001365
William Lallemandbddd4fd2012-02-27 11:23:10 +01001366 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001367 if (LIST_ISEMPTY(list_format))
1368 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001369
William Lallemand1d705562012-03-12 12:46:41 +01001370 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001371 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001372 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001373 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001374 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001375
Willy Tarreauc8368452012-12-21 00:09:23 +01001376 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001377 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001378 if (!last_isspace) {
1379 LOGCHAR(' ');
1380 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001381 }
1382 break;
1383
William Lallemand1d705562012-03-12 12:46:41 +01001384 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001385 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001386 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001387 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001388 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001389 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001390 last_isspace = 0;
1391 break;
1392
Willy Tarreauc8368452012-12-21 00:09:23 +01001393 case LOG_FMT_EXPR: // sample expression, may be request or response
1394 key = NULL;
1395 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001396 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 +01001397 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001398 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 +01001399 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01001400 ret = lf_encode_chunk(tmplog, dst + maxsize,
1401 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001402 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001403 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 +01001404 if (ret == 0)
1405 goto out;
1406 tmplog = ret;
1407 last_isspace = 0;
1408 break;
1409
Willy Tarreau2beef582012-12-20 17:22:52 +01001410 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001411 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001412 if (conn)
1413 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1414 else
1415 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001416 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001417 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001418 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001419 last_isspace = 0;
1420 break;
1421
Willy Tarreau2beef582012-12-20 17:22:52 +01001422 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001423 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001424 if (conn) {
1425 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001426 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001427 } else {
1428 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1429 dst + maxsize - tmplog, tmp);
1430 }
William Lallemand5f232402012-04-05 18:02:55 +02001431 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001432 else
1433 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1434
William Lallemand5f232402012-04-05 18:02:55 +02001435 if (ret == NULL)
1436 goto out;
1437 tmplog = ret;
1438 last_isspace = 0;
1439 break;
1440
Willy Tarreau2beef582012-12-20 17:22:52 +01001441 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001442 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001443 if (conn) {
1444 conn_get_to_addr(conn);
1445 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1446 }
1447 else
1448 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1449
William Lallemand1d705562012-03-12 12:46:41 +01001450 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001451 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001452 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001453 last_isspace = 0;
1454 break;
1455
Willy Tarreau2beef582012-12-20 17:22:52 +01001456 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001457 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001458 if (conn) {
1459 conn_get_to_addr(conn);
1460 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001461 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001462 else
1463 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001464 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001465 else
1466 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1467
William Lallemand5f232402012-04-05 18:02:55 +02001468 if (ret == NULL)
1469 goto out;
1470 tmplog = ret;
1471 last_isspace = 0;
1472 break;
1473
Willy Tarreau2beef582012-12-20 17:22:52 +01001474 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001475 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001476 if (conn)
1477 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1478 else
1479 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1480
William Lallemand1d705562012-03-12 12:46:41 +01001481 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001482 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001483 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001484 last_isspace = 0;
1485 break;
1486
Willy Tarreau2beef582012-12-20 17:22:52 +01001487 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001488 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001489 if (conn)
1490 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1491 else
1492 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1493
William Lallemand5f232402012-04-05 18:02:55 +02001494 if (ret == NULL)
1495 goto out;
1496 tmplog = ret;
1497 last_isspace = 0;
1498 break;
1499
Willy Tarreau2beef582012-12-20 17:22:52 +01001500 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001501 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001502 if (conn)
1503 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1504 else
1505 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1506
William Lallemand5f232402012-04-05 18:02:55 +02001507 if (ret == NULL)
1508 goto out;
1509 tmplog = ret;
1510 last_isspace = 0;
1511 break;
1512
Willy Tarreau2beef582012-12-20 17:22:52 +01001513 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001514 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001515 if (conn)
1516 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1517 else
1518 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1519
William Lallemand1d705562012-03-12 12:46:41 +01001520 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001521 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001522 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001523 last_isspace = 0;
1524 break;
1525
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001526 case LOG_FMT_DATE: // %t = accept date
William Lallemandbddd4fd2012-02-27 11:23:10 +01001527 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001528 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1529 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001530 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001531 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001532 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001533 last_isspace = 0;
1534 break;
1535
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001536 case LOG_FMT_tr: // %tr = start of request date
1537 /* Note that the timers are valid if we get here */
1538 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1539 get_localtime(tv.tv_sec, &tm);
1540 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
1541 if (ret == NULL)
1542 goto out;
1543 tmplog = ret;
1544 last_isspace = 0;
1545 break;
1546
1547 case LOG_FMT_DATEGMT: // %T = accept date, GMT
William Lallemandbddd4fd2012-02-27 11:23:10 +01001548 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001549 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001550 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001551 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001552 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001553 last_isspace = 0;
1554 break;
1555
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001556 case LOG_FMT_trg: // %trg = start of request date, GMT
1557 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1558 get_gmtime(tv.tv_sec, &tm);
1559 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
1560 if (ret == NULL)
1561 goto out;
1562 tmplog = ret;
1563 last_isspace = 0;
1564 break;
1565
1566 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Yuxans Yao4e25b012012-10-19 10:36:09 +08001567 get_localtime(s->logs.accept_date.tv_sec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001568 ret = localdate2str_log(tmplog, s->logs.accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08001569 if (ret == NULL)
1570 goto out;
1571 tmplog = ret;
1572 last_isspace = 0;
1573 break;
1574
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001575 case LOG_FMT_trl: // %trl = start of request date, local
1576 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1577 get_localtime(tv.tv_sec, &tm);
1578 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
1579 if (ret == NULL)
1580 goto out;
1581 tmplog = ret;
1582 last_isspace = 0;
1583 break;
1584
William Lallemand5f232402012-04-05 18:02:55 +02001585 case LOG_FMT_TS: // %Ts
1586 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1587 if (tmp->options & LOG_OPT_HEXA) {
1588 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1589 if (iret < 0 || iret > dst + maxsize - tmplog)
1590 goto out;
1591 last_isspace = 0;
1592 tmplog += iret;
1593 } else {
1594 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1595 if (ret == NULL)
1596 goto out;
1597 tmplog = ret;
1598 last_isspace = 0;
1599 }
1600 break;
1601
William Lallemand1d705562012-03-12 12:46:41 +01001602 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001603 if (tmp->options & LOG_OPT_HEXA) {
1604 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1605 if (iret < 0 || iret > dst + maxsize - tmplog)
1606 goto out;
1607 last_isspace = 0;
1608 tmplog += iret;
1609 } else {
1610 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001611 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001612 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1613 tmplog, 4);
1614 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001615 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001616 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001617 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001618 }
1619 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001620
William Lallemand1d705562012-03-12 12:46:41 +01001621 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001622 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001623 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001624 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001625 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001626 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001627 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001628 break;
1629
Willy Tarreau773d65f2012-10-12 14:56:11 +02001630 case LOG_FMT_FRONTEND_XPRT: // %ft
1631 src = fe->id;
1632 if (tmp->options & LOG_OPT_QUOTE)
1633 LOGCHAR('"');
1634 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1635 if (iret == 0)
1636 goto out;
1637 tmplog += iret;
1638#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001639 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001640 LOGCHAR('~');
1641#endif
1642 if (tmp->options & LOG_OPT_QUOTE)
1643 LOGCHAR('"');
1644 last_isspace = 0;
1645 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001646#ifdef USE_OPENSSL
1647 case LOG_FMT_SSL_CIPHER: // %sslc
1648 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001649 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001650 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001651 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001652 src = ssl_sock_get_cipher_name(conn);
1653 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001654 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1655 if (ret == NULL)
1656 goto out;
1657 tmplog = ret;
1658 last_isspace = 0;
1659 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001660
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001661 case LOG_FMT_SSL_VERSION: // %sslv
1662 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001663 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001664 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001665 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001666 src = ssl_sock_get_proto_version(conn);
1667 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001668 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1669 if (ret == NULL)
1670 goto out;
1671 tmplog = ret;
1672 last_isspace = 0;
1673 break;
1674#endif
William Lallemand1d705562012-03-12 12:46:41 +01001675 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001676 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001677 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001678 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001679 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001680 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001681 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001682 break;
1683
William Lallemand1d705562012-03-12 12:46:41 +01001684 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001685 switch (obj_type(s->target)) {
1686 case OBJ_TYPE_SERVER:
1687 src = objt_server(s->target)->id;
1688 break;
1689 case OBJ_TYPE_APPLET:
1690 src = objt_applet(s->target)->name;
1691 break;
1692 default:
1693 src = "<NOSRV>";
1694 break;
1695 }
William Lallemand5f232402012-04-05 18:02:55 +02001696 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001697 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001698 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001699 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001700 last_isspace = 0;
1701 break;
1702
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001703 case LOG_FMT_Th: // %Th = handshake time
1704 ret = ltoa_o(s->logs.t_handshake, tmplog, dst + maxsize - tmplog);
1705 if (ret == NULL)
1706 goto out;
1707 tmplog = ret;
1708 last_isspace = 0;
1709 break;
1710
1711 case LOG_FMT_Ti: // %Ti = HTTP idle time
1712 ret = ltoa_o(s->logs.t_idle, tmplog, dst + maxsize - tmplog);
1713 if (ret == NULL)
1714 goto out;
1715 tmplog = ret;
1716 last_isspace = 0;
1717 break;
1718
1719 case LOG_FMT_TR: // %TR = HTTP request time
1720 ret = ltoa_o((t_request >= 0) ? t_request - s->logs.t_idle - s->logs.t_handshake : -1,
1721 tmplog, dst + maxsize - tmplog);
1722 if (ret == NULL)
1723 goto out;
1724 tmplog = ret;
1725 last_isspace = 0;
1726 break;
1727
1728 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02001729 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001730 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001731 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001732 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001733 last_isspace = 0;
1734 break;
1735
William Lallemand1d705562012-03-12 12:46:41 +01001736 case LOG_FMT_TW: // %Tw
1737 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001738 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001739 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001740 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001741 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001742 last_isspace = 0;
1743 break;
1744
William Lallemand1d705562012-03-12 12:46:41 +01001745 case LOG_FMT_TC: // %Tc
1746 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001747 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001748 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001749 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001750 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001751 last_isspace = 0;
1752 break;
1753
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001754 case LOG_FMT_Tr: // %Tr
William Lallemand1d705562012-03-12 12:46:41 +01001755 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001756 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001757 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001758 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001759 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001760 last_isspace = 0;
1761 break;
1762
Willy Tarreau27b639d2016-05-17 17:55:27 +02001763 case LOG_FMT_TD: // %Td
1764 if (s->be->mode == PR_MODE_HTTP)
1765 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_close - s->logs.t_data : -1,
1766 tmplog, dst + maxsize - tmplog);
1767 else
1768 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_close - s->logs.t_connect : -1,
1769 tmplog, dst + maxsize - tmplog);
1770 if (ret == NULL)
1771 goto out;
1772 tmplog = ret;
1773 last_isspace = 0;
1774 break;
1775
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001776 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
1777 if (!(fe->to_log & LW_BYTES))
1778 LOGCHAR('+');
1779 ret = ltoa_o(s->logs.t_close - (s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0),
1780 tmplog, dst + maxsize - tmplog);
1781 if (ret == NULL)
1782 goto out;
1783 tmplog = ret;
1784 last_isspace = 0;
1785 break;
1786
1787 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001788 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001789 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001790 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001791 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001792 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001793 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001794 last_isspace = 0;
1795 break;
1796
Willy Tarreau2beef582012-12-20 17:22:52 +01001797 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02001798 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001799 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001800 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001801 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001802 last_isspace = 0;
1803 break;
1804
William Lallemand1d705562012-03-12 12:46:41 +01001805 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001806 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001807 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001808 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001809 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001810 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001811 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001812 last_isspace = 0;
1813 break;
1814
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001815 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001816 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1817 if (ret == NULL)
1818 goto out;
1819 tmplog = ret;
1820 last_isspace = 0;
1821 break;
1822
Willy Tarreau2beef582012-12-20 17:22:52 +01001823 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02001824 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02001825 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001826 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001827 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001828 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001829 last_isspace = 0;
1830 break;
1831
Willy Tarreau2beef582012-12-20 17:22:52 +01001832 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02001833 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02001834 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001835 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001836 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001837 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001838 last_isspace = 0;
1839 break;
1840
William Lallemand1d705562012-03-12 12:46:41 +01001841 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001842 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1843 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001844 *tmplog = '\0';
1845 last_isspace = 0;
1846 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001847
William Lallemand1d705562012-03-12 12:46:41 +01001848 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001849 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1850 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02001851 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1852 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001853 last_isspace = 0;
1854 break;
1855
William Lallemand1d705562012-03-12 12:46:41 +01001856 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001857 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001858 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001859 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001860 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001861 last_isspace = 0;
1862 break;
1863
William Lallemand1d705562012-03-12 12:46:41 +01001864 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001865 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001866 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001867 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001868 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001869 last_isspace = 0;
1870 break;
1871
William Lallemand1d705562012-03-12 12:46:41 +01001872 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001873 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001874 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001875 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001876 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001877 last_isspace = 0;
1878 break;
1879
William Lallemand1d705562012-03-12 12:46:41 +01001880 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001881 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001882 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001883 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001884 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001885 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001886 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001887 last_isspace = 0;
1888 break;
1889
William Lallemand1d705562012-03-12 12:46:41 +01001890 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001891 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001892 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001893 ret = ltoa_o((s->si[1].conn_retries>0) ?
1894 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001895 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001896 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001897 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001898 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001899 last_isspace = 0;
1900 break;
1901
William Lallemand1d705562012-03-12 12:46:41 +01001902 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001903 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001904 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001905 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001906 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001907 last_isspace = 0;
1908 break;
1909
William Lallemand1d705562012-03-12 12:46:41 +01001910 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001911 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001912 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001913 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001914 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001915 last_isspace = 0;
1916 break;
1917
William Lallemand1d705562012-03-12 12:46:41 +01001918 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001919 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001920 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001921 if (tmp->options & LOG_OPT_QUOTE)
1922 LOGCHAR('"');
1923 LOGCHAR('{');
1924 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1925 if (hdr)
1926 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001927 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001928 ret = lf_encode_string(tmplog, dst + maxsize,
1929 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001930 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001931 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001932 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001933 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001934 }
1935 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001936 if (tmp->options & LOG_OPT_QUOTE)
1937 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001938 last_isspace = 0;
1939 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001940 break;
1941
William Lallemand1d705562012-03-12 12:46:41 +01001942 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001943 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001944 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001945 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1946 if (hdr > 0)
1947 LOGCHAR(' ');
1948 if (tmp->options & LOG_OPT_QUOTE)
1949 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001950 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001951 ret = lf_encode_string(tmplog, dst + maxsize,
1952 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001953 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001954 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001955 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001956 } else if (!(tmp->options & LOG_OPT_QUOTE))
1957 LOGCHAR('-');
1958 if (tmp->options & LOG_OPT_QUOTE)
1959 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001960 last_isspace = 0;
1961 }
1962 }
1963 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001964
William Lallemand1d705562012-03-12 12:46:41 +01001965
1966 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001967 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001968 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001969 if (tmp->options & LOG_OPT_QUOTE)
1970 LOGCHAR('"');
1971 LOGCHAR('{');
1972 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1973 if (hdr)
1974 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001975 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001976 ret = lf_encode_string(tmplog, dst + maxsize,
1977 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001978 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001979 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001980 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001981 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001982 }
1983 LOGCHAR('}');
1984 last_isspace = 0;
1985 if (tmp->options & LOG_OPT_QUOTE)
1986 LOGCHAR('"');
1987 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001988 break;
1989
William Lallemand1d705562012-03-12 12:46:41 +01001990 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001991 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001992 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001993 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1994 if (hdr > 0)
1995 LOGCHAR(' ');
1996 if (tmp->options & LOG_OPT_QUOTE)
1997 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001998 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001999 ret = lf_encode_string(tmplog, dst + maxsize,
2000 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002001 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002002 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002003 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002004 } else if (!(tmp->options & LOG_OPT_QUOTE))
2005 LOGCHAR('-');
2006 if (tmp->options & LOG_OPT_QUOTE)
2007 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002008 last_isspace = 0;
2009 }
2010 }
2011 break;
2012
William Lallemand1d705562012-03-12 12:46:41 +01002013 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01002014 /* Request */
2015 if (tmp->options & LOG_OPT_QUOTE)
2016 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02002017 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01002018 ret = lf_encode_string(tmplog, dst + maxsize,
2019 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01002020 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02002021 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01002022 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002023 if (tmp->options & LOG_OPT_QUOTE)
2024 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01002025 last_isspace = 0;
2026 break;
William Lallemand5f232402012-04-05 18:02:55 +02002027
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002028 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02002029 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002030
Willy Tarreaub7636d12015-06-17 19:58:02 +02002031 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002032 LOGCHAR('"');
2033
2034 end = uri + strlen(uri);
2035 // look for the first whitespace character
2036 while (uri < end && !HTTP_IS_SPHT(*uri))
2037 uri++;
2038
2039 // keep advancing past multiple spaces
2040 while (uri < end && HTTP_IS_SPHT(*uri)) {
2041 uri++; nspaces++;
2042 }
2043
2044 // look for first space or question mark after url
2045 spc = uri;
2046 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2047 spc++;
2048
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002049 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002050 chunk.str = "<BADREQ>";
2051 chunk.len = strlen("<BADREQ>");
2052 } else {
2053 chunk.str = uri;
2054 chunk.len = spc - uri;
2055 }
2056
Dragan Dosen835b9212016-02-12 13:23:03 +01002057 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002058 if (ret == NULL || *ret != '\0')
2059 goto out;
2060
2061 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002062 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002063 LOGCHAR('"');
2064
2065 last_isspace = 0;
2066 break;
2067
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002068 case LOG_FMT_HTTP_QUERY: // %HQ
2069 if (tmp->options & LOG_OPT_QUOTE)
2070 LOGCHAR('"');
2071
Willy Tarreau57bc8912016-04-25 17:09:40 +02002072 if (!txn || !txn->uri) {
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002073 chunk.str = "<BADREQ>";
2074 chunk.len = strlen("<BADREQ>");
2075 } else {
2076 uri = txn->uri;
2077 end = uri + strlen(uri);
2078 // look for the first question mark
2079 while (uri < end && *uri != '?')
2080 uri++;
2081
2082 qmark = uri;
2083 // look for first space or question mark after url
2084 while (uri < end && !HTTP_IS_SPHT(*uri))
2085 uri++;
2086
2087 chunk.str = qmark;
2088 chunk.len = uri - qmark;
2089 }
2090
Dragan Dosen835b9212016-02-12 13:23:03 +01002091 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002092 if (ret == NULL || *ret != '\0')
2093 goto out;
2094
2095 tmplog = ret;
2096 if (tmp->options & LOG_OPT_QUOTE)
2097 LOGCHAR('"');
2098
2099 last_isspace = 0;
2100 break;
2101
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002102 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002103 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002104
Willy Tarreaub7636d12015-06-17 19:58:02 +02002105 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002106 LOGCHAR('"');
2107
2108 end = uri + strlen(uri);
2109 // look for the first whitespace character
2110 while (uri < end && !HTTP_IS_SPHT(*uri))
2111 uri++;
2112
2113 // keep advancing past multiple spaces
2114 while (uri < end && HTTP_IS_SPHT(*uri)) {
2115 uri++; nspaces++;
2116 }
2117
2118 // look for first space after url
2119 spc = uri;
2120 while (spc < end && !HTTP_IS_SPHT(*spc))
2121 spc++;
2122
Willy Tarreau57bc8912016-04-25 17:09:40 +02002123 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002124 chunk.str = "<BADREQ>";
2125 chunk.len = strlen("<BADREQ>");
2126 } else {
2127 chunk.str = uri;
2128 chunk.len = spc - uri;
2129 }
2130
Dragan Dosen835b9212016-02-12 13:23:03 +01002131 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002132 if (ret == NULL || *ret != '\0')
2133 goto out;
2134
2135 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002136 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002137 LOGCHAR('"');
2138
2139 last_isspace = 0;
2140 break;
2141
2142 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002143 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002144 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002145 LOGCHAR('"');
2146
2147 end = uri + strlen(uri);
2148 // look for the first whitespace character
2149 spc = uri;
2150 while (spc < end && !HTTP_IS_SPHT(*spc))
2151 spc++;
2152
2153 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
2154 chunk.str = "<BADREQ>";
2155 chunk.len = strlen("<BADREQ>");
2156 } else {
2157 chunk.str = uri;
2158 chunk.len = spc - uri;
2159 }
2160
Dragan Dosen835b9212016-02-12 13:23:03 +01002161 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002162 if (ret == NULL || *ret != '\0')
2163 goto out;
2164
2165 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002166 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002167 LOGCHAR('"');
2168
2169 last_isspace = 0;
2170 break;
2171
2172 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002173 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002174 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002175 LOGCHAR('"');
2176
2177 end = uri + strlen(uri);
2178 // look for the first whitespace character
2179 while (uri < end && !HTTP_IS_SPHT(*uri))
2180 uri++;
2181
2182 // keep advancing past multiple spaces
2183 while (uri < end && HTTP_IS_SPHT(*uri)) {
2184 uri++; nspaces++;
2185 }
2186
2187 // look for the next whitespace character
2188 while (uri < end && !HTTP_IS_SPHT(*uri))
2189 uri++;
2190
2191 // keep advancing past multiple spaces
2192 while (uri < end && HTTP_IS_SPHT(*uri))
2193 uri++;
2194
Willy Tarreau57bc8912016-04-25 17:09:40 +02002195 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002196 chunk.str = "<BADREQ>";
2197 chunk.len = strlen("<BADREQ>");
2198 } else if (uri == end) {
2199 chunk.str = "HTTP/0.9";
2200 chunk.len = strlen("HTTP/0.9");
2201 } else {
2202 chunk.str = uri;
2203 chunk.len = end - uri;
2204 }
2205
Dragan Dosen835b9212016-02-12 13:23:03 +01002206 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002207 if (ret == NULL || *ret != '\0')
2208 goto out;
2209
2210 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002211 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002212 LOGCHAR('"');
2213
2214 last_isspace = 0;
2215 break;
2216
William Lallemand5f232402012-04-05 18:02:55 +02002217 case LOG_FMT_COUNTER: // %rt
2218 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002219 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002220 if (iret < 0 || iret > dst + maxsize - tmplog)
2221 goto out;
2222 last_isspace = 0;
2223 tmplog += iret;
2224 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002225 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002226 if (ret == NULL)
2227 goto out;
2228 tmplog = ret;
2229 last_isspace = 0;
2230 }
2231 break;
2232
Willy Tarreau7346acb2014-08-28 15:03:15 +02002233 case LOG_FMT_LOGCNT: // %lc
2234 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002235 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002236 if (iret < 0 || iret > dst + maxsize - tmplog)
2237 goto out;
2238 last_isspace = 0;
2239 tmplog += iret;
2240 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002241 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002242 if (ret == NULL)
2243 goto out;
2244 tmplog = ret;
2245 last_isspace = 0;
2246 }
2247 break;
2248
William Lallemand5f232402012-04-05 18:02:55 +02002249 case LOG_FMT_HOSTNAME: // %H
2250 src = hostname;
2251 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2252 if (ret == NULL)
2253 goto out;
2254 tmplog = ret;
2255 last_isspace = 0;
2256 break;
2257
2258 case LOG_FMT_PID: // %pid
2259 if (tmp->options & LOG_OPT_HEXA) {
2260 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2261 if (iret < 0 || iret > dst + maxsize - tmplog)
2262 goto out;
2263 last_isspace = 0;
2264 tmplog += iret;
2265 } else {
2266 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2267 if (ret == NULL)
2268 goto out;
2269 tmplog = ret;
2270 last_isspace = 0;
2271 }
2272 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002273
2274 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002275 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01002276 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002277 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002278 if (ret == NULL)
2279 goto out;
2280 tmplog = ret;
2281 last_isspace = 0;
2282 break;
2283
William Lallemandbddd4fd2012-02-27 11:23:10 +01002284 }
2285 }
2286
2287out:
William Lallemand1d705562012-03-12 12:46:41 +01002288 /* *tmplog is a unused character */
2289 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002290 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002291
Willy Tarreaubaaee002006-06-26 02:48:02 +02002292}
2293
William Lallemand1d705562012-03-12 12:46:41 +01002294/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002295 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002296 * Will not log if the frontend has no log defined.
2297 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002298void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002299{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002300 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002301 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002302 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002303
2304 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002305 err = (s->flags & SF_REDISP) ||
2306 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2307 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002308 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002309 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002310
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002311 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002312 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002313
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002314 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002315 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002316
Willy Tarreauabcd5142013-06-11 17:18:02 +02002317 if (s->logs.level) { /* loglevel was overridden */
2318 if (s->logs.level == -1) {
2319 s->logs.logwait = 0; /* logs disabled */
2320 return;
2321 }
2322 level = s->logs.level - 1;
2323 }
2324 else {
2325 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002326 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002327 level = LOG_ERR;
2328 }
William Lallemand1d705562012-03-12 12:46:41 +01002329
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002330 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002331 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002332 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002333 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002334 }
2335
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002336 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2337 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2338 &sess->fe->logformat_sd);
2339 }
2340
Dragan Dosen59cee972015-09-19 22:09:02 +02002341 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002342 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002343 sess->fe->log_count++;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002344 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002345 s->logs.logwait = 0;
2346 }
2347}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002348
Willy Tarreaubaaee002006-06-26 02:48:02 +02002349/*
2350 * Local variables:
2351 * c-indent-level: 8
2352 * c-basic-offset: 8
2353 * End:
2354 */