blob: 64c7922c933d9cd773f36bc5512b5b2a8a5d6541 [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";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100270 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200271 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100272 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200273}
274
William Lallemand723b73a2012-02-08 16:37:49 +0100275/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100276 * callback used to configure addr source retrieval
277 */
278int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
279{
280 curproxy->options2 |= PR_O2_SRC_ADDR;
281
282 return 0;
283}
284
285
286/*
William Lallemand723b73a2012-02-08 16:37:49 +0100287 * Parse args in a logformat_var
288 */
289int parse_logformat_var_args(char *args, struct logformat_node *node)
290{
291 int i = 0;
292 int end = 0;
293 int flags = 0; // 1 = + 2 = -
294 char *sp = NULL; // start pointer
295
296 if (args == NULL)
297 return 1;
298
299 while (1) {
300 if (*args == '\0')
301 end = 1;
302
303 if (*args == '+') {
304 // add flag
305 sp = args + 1;
306 flags = 1;
307 }
308 if (*args == '-') {
309 // delete flag
310 sp = args + 1;
311 flags = 2;
312 }
313
314 if (*args == '\0' || *args == ',') {
315 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100316 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100317 if (strcmp(sp, var_args_list[i].name) == 0) {
318 if (flags == 1) {
319 node->options |= var_args_list[i].mask;
320 break;
321 } else if (flags == 2) {
322 node->options &= ~var_args_list[i].mask;
323 break;
324 }
325 }
326 }
327 sp = NULL;
328 if (end)
329 break;
330 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100331 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100332 }
333 return 0;
334}
335
336/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100337 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
338 * must pass the args part in the <arg> pointer with its length in <arg_len>,
339 * and varname with its length in <var> and <var_len> respectively. <arg> is
340 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100341 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100342int 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 +0100343{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100344 int j;
345 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100346
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100347 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
348 if (strlen(logformat_keywords[j].name) == var_len &&
349 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
350 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200351 node = calloc(1, sizeof(*node));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100352 node->type = logformat_keywords[j].type;
353 node->options = *defoptions;
354 if (arg_len) {
355 node->arg = my_strndup(arg, arg_len);
356 parse_logformat_var_args(node->arg, node);
357 }
358 if (node->type == LOG_FMT_GLOBAL) {
359 *defoptions = node->options;
360 free(node->arg);
361 free(node);
362 } else {
363 if (logformat_keywords[j].config_callback &&
364 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100365 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100366 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100367 curproxy->to_log |= logformat_keywords[j].lw;
368 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100369 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100370 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100371 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200372 curproxy->conf.args.file, curproxy->conf.args.line,
373 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100374 return 0;
375 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100376 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200377 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
378 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100379 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100380 }
William Lallemand723b73a2012-02-08 16:37:49 +0100381 }
382 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100383
384 j = var[var_len];
385 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100386 Warning("parsing [%s:%d] : no such format variable '%s' in '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%' in log-format expressions.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200387 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100388 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100389 return -1;
390}
391
392/*
393 * push to the logformat linked list
394 *
395 * start: start pointer
396 * end: end text pointer
397 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100398 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100399 *
400 * LOG_TEXT: copy chars from start to end excluding end.
401 *
402*/
William Lallemand1d705562012-03-12 12:46:41 +0100403void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100404{
405 char *str;
406
Willy Tarreaua3571662012-12-20 21:59:12 +0100407 if (type == LF_TEXT) { /* type text */
Vincent Bernat02779b62016-04-03 13:48:43 +0200408 struct logformat_node *node = calloc(1, sizeof(*node));
409 str = calloc(1, end - start + 1);
William Lallemand723b73a2012-02-08 16:37:49 +0100410 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100411 str[end - start] = '\0';
412 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100413 node->type = LOG_FMT_TEXT; // type string
414 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100415 } else if (type == LF_SEPARATOR) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200416 struct logformat_node *node = calloc(1, sizeof(*node));
William Lallemand1d705562012-03-12 12:46:41 +0100417 node->type = LOG_FMT_SEPARATOR;
418 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100419 }
420}
421
422/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100423 * Parse the sample fetch expression <text> and add a node to <list_format> upon
424 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200425 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100426 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100427void 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 +0100428{
429 char *cmd[2];
430 struct sample_expr *expr;
431 struct logformat_node *node;
432 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100433 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100434
435 cmd[0] = text;
436 cmd[1] = "";
437 cmd_arg = 0;
438
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100439 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100440 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200441 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
442 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100443 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100444 return;
445 }
446
Vincent Bernat02779b62016-04-03 13:48:43 +0200447 node = calloc(1, sizeof(*node));
Willy Tarreauc8368452012-12-21 00:09:23 +0100448 node->type = LOG_FMT_EXPR;
449 node->expr = expr;
450 node->options = options;
451
452 if (arg_len) {
453 node->arg = my_strndup(arg, arg_len);
454 parse_logformat_var_args(node->arg, node);
455 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100456 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100457 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
458
Willy Tarreau434c57c2013-01-08 01:10:24 +0100459 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100460 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
461
Willy Tarreau434c57c2013-01-08 01:10:24 +0100462 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200463 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
464 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
465 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100466
Willy Tarreauc8368452012-12-21 00:09:23 +0100467 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
468 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100469 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100470
William Lallemand65ad6e12014-01-31 15:08:02 +0100471 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
472 * needed with some sample fetches (eg: ssl*). We always set it for
473 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100474 */
475 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100476 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100477 LIST_ADDQ(list_format, &node->list);
478}
479
480/*
William Lallemand723b73a2012-02-08 16:37:49 +0100481 * Parse the log_format string and fill a linked list.
482 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200483 * You can set arguments using { } : %{many arguments}varname.
484 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100485 *
486 * str: the string to parse
487 * curproxy: the proxy affected
488 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100489 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100490 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100491 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100492void parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, const char *file, int line)
William Lallemand723b73a2012-02-08 16:37:49 +0100493{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100494 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100495 char *arg = NULL; /* start pointer for args */
496 char *var = NULL; /* start pointer for vars */
497 int arg_len = 0;
498 int var_len = 0;
499 int cformat; /* current token format */
500 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100501 struct logformat_node *tmplf, *back;
502
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100503 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200504 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200505
William Lallemand723b73a2012-02-08 16:37:49 +0100506 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100507 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100508 LIST_DEL(&tmplf->list);
509 free(tmplf);
510 }
511
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100512 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100513 pformat = cformat;
514
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100515 if (!*str)
516 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100517
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100518 /* The prinicple of the two-step state machine below is to first detect a change, and
519 * second have all common paths processed at one place. The common paths are the ones
520 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
521 * We use the common LF_INIT state to dispatch to the different final states.
522 */
523 switch (pformat) {
524 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100525 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100526 arg_len = var_len = 0;
527 if (*str == '{') { // optional argument
528 cformat = LF_STARG;
529 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100530 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100531 else if (*str == '[') {
532 cformat = LF_STEXPR;
533 var = str + 1; // store expr in variable name
534 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100535 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100536 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100537 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100538 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100539 else if (*str == '%')
540 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100541 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100542 /* single '%' followed by blank or digit, send them both */
543 cformat = LF_TEXT;
544 pformat = LF_TEXT; /* finally we include the previous char as well */
545 sp = str - 1; /* send both the '%' and the current char */
546 Warning("parsing [%s:%d] : Fixed missing '%%' before '%c' at position %d in %s line : '%s'. Please use '%%%%' when you need the '%%' character in a log-format expression.\n",
547 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
548
549 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100550 else
551 cformat = LF_INIT; // handle other cases of litterals
552 break;
553
554 case LF_STARG: // text immediately following '%{'
555 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100556 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100557 arg_len = str - arg;
558 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100559 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100560 break;
561
562 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100563 if (*str == '[') {
564 cformat = LF_STEXPR;
565 var = str + 1; // store expr in variable name
566 break;
567 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100568 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100569 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100570 var = str;
571 break;
572 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200573 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
574 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100575 cformat = LF_INIT;
576 break;
577
Willy Tarreauc8368452012-12-21 00:09:23 +0100578 case LF_STEXPR: // text immediately following '%['
579 if (*str == ']') { // end of arg
580 cformat = LF_EDEXPR;
581 var_len = str - var;
582 *str = 0; // needed for parsing the expression
583 }
584 break;
585
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100586 case LF_VAR: // text part of a variable name
587 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100588 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100589 cformat = LF_INIT; // not variable name anymore
590 break;
591
Willy Tarreauc8368452012-12-21 00:09:23 +0100592 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100593 cformat = LF_INIT;
594 }
595
596 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
597 switch (*str) {
598 case '%': cformat = LF_STARTVAR; break;
599 case ' ': cformat = LF_SEPARATOR; break;
600 case 0 : cformat = LF_END; break;
601 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100602 }
603 }
604
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100605 if (cformat != pformat || pformat == LF_SEPARATOR) {
606 switch (pformat) {
607 case LF_VAR:
608 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
609 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100610 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100611 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100612 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100613 case LF_TEXT:
614 case LF_SEPARATOR:
615 add_to_logformat_list(sp, str, pformat, list_format);
616 break;
617 }
618 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100619 }
620 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100621
Willy Tarreauc8368452012-12-21 00:09:23 +0100622 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200623 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
624 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
625 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100626
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100627 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100628}
629
Willy Tarreaubaaee002006-06-26 02:48:02 +0200630/*
631 * Displays the message on stderr with the date and pid. Overrides the quiet
632 * mode during startup.
633 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200634void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635{
636 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200637 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200638
639 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
640 va_start(argp, fmt);
641
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200642 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200643 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200644 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 vfprintf(stderr, fmt, argp);
646 fflush(stderr);
647 va_end(argp);
648 }
649}
650
651
652/*
653 * Displays the message on stderr with the date and pid.
654 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200655void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656{
657 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200658 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659
660 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
661 va_start(argp, fmt);
662
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200663 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200664 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200665 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200666 vfprintf(stderr, fmt, argp);
667 fflush(stderr);
668 va_end(argp);
669 }
670}
671
672/*
673 * Displays the message on <out> only if quiet mode is not set.
674 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200675void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676{
677 va_list argp;
678
679 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
680 va_start(argp, fmt);
681 vfprintf(out, fmt, argp);
682 fflush(out);
683 va_end(argp);
684 }
685}
686
687/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200688 * returns log format for <fmt> or -1 if not found.
689 */
690int get_log_format(const char *fmt)
691{
692 int format;
693
694 format = LOG_FORMATS - 1;
Dragan Dosen43885c72015-10-01 13:18:13 +0200695 while (format >= 0 && strcmp(log_formats[format].name, fmt))
Dragan Dosen1322d092015-09-22 16:05:32 +0200696 format--;
697
698 return format;
699}
700
701/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200702 * returns log level for <lev> or -1 if not found.
703 */
704int get_log_level(const char *lev)
705{
706 int level;
707
708 level = NB_LOG_LEVELS - 1;
709 while (level >= 0 && strcmp(log_levels[level], lev))
710 level--;
711
712 return level;
713}
714
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715/*
716 * returns log facility for <fac> or -1 if not found.
717 */
718int get_log_facility(const char *fac)
719{
720 int facility;
721
722 facility = NB_LOG_FACILITIES - 1;
723 while (facility >= 0 && strcmp(log_facilities[facility], fac))
724 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100725
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 return facility;
727}
728
William Lallemanda1cc3812012-02-08 16:38:44 +0100729/*
Dragan Dosen835b9212016-02-12 13:23:03 +0100730 * Encode the string.
731 *
732 * When using the +E log format option, it will try to escape '"\]'
733 * characters with '\' as prefix. The same prefix should not be used as
734 * <escape>.
735 */
736static char *lf_encode_string(char *start, char *stop,
737 const char escape, const fd_set *map,
738 const char *string,
739 struct logformat_node *node)
740{
741 if (node->options & LOG_OPT_ESC) {
742 if (start < stop) {
743 stop--; /* reserve one byte for the final '\0' */
744 while (start < stop && *string != '\0') {
745 if (!FD_ISSET((unsigned char)(*string), map)) {
746 if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map))
747 *start++ = *string;
748 else {
749 if (start + 2 >= stop)
750 break;
751 *start++ = '\\';
752 *start++ = *string;
753 }
754 }
755 else {
756 if (start + 3 >= stop)
757 break;
758 *start++ = escape;
759 *start++ = hextab[(*string >> 4) & 15];
760 *start++ = hextab[*string & 15];
761 }
762 string++;
763 }
764 *start = '\0';
765 }
766 }
767 else {
768 return encode_string(start, stop, escape, map, string);
769 }
770
771 return start;
772}
773
774/*
775 * Encode the chunk.
776 *
777 * When using the +E log format option, it will try to escape '"\]'
778 * characters with '\' as prefix. The same prefix should not be used as
779 * <escape>.
780 */
781static char *lf_encode_chunk(char *start, char *stop,
782 const char escape, const fd_set *map,
783 const struct chunk *chunk,
784 struct logformat_node *node)
785{
786 char *str, *end;
787
788 if (node->options & LOG_OPT_ESC) {
789 if (start < stop) {
790 str = chunk->str;
791 end = chunk->str + chunk->len;
792
793 stop--; /* reserve one byte for the final '\0' */
794 while (start < stop && str < end) {
795 if (!FD_ISSET((unsigned char)(*str), map)) {
796 if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map))
797 *start++ = *str;
798 else {
799 if (start + 2 >= stop)
800 break;
801 *start++ = '\\';
802 *start++ = *str;
803 }
804 }
805 else {
806 if (start + 3 >= stop)
807 break;
808 *start++ = escape;
809 *start++ = hextab[(*str >> 4) & 15];
810 *start++ = hextab[*str & 15];
811 }
812 str++;
813 }
814 *start = '\0';
815 }
816 }
817 else {
818 return encode_chunk(start, stop, escape, map, chunk);
819 }
820
821 return start;
822}
823
824/*
William Lallemanda1cc3812012-02-08 16:38:44 +0100825 * Write a string in the log string
Dragan Dosen835b9212016-02-12 13:23:03 +0100826 * Take cares of quote and escape options
William Lallemanda1cc3812012-02-08 16:38:44 +0100827 *
828 * Return the adress of the \0 character, or NULL on error
829 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100830char *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 +0100831{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100832 if (size < 2)
833 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100834
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100835 if (node->options & LOG_OPT_QUOTE) {
836 *(dst++) = '"';
837 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100838 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100839
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100840 if (src && len) {
Dragan Dosendb1b6f92016-07-25 11:35:02 +0200841 if (++len > size)
842 len = size;
Dragan Dosen835b9212016-02-12 13:23:03 +0100843 if (node->options & LOG_OPT_ESC) {
Dragan Dosen835b9212016-02-12 13:23:03 +0100844 char *ret;
845
Dragan Dosendb1b6f92016-07-25 11:35:02 +0200846 ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src);
Dragan Dosen835b9212016-02-12 13:23:03 +0100847 if (ret == NULL || *ret != '\0')
848 return NULL;
849 len = ret - dst;
850 }
851 else {
Dragan Dosen835b9212016-02-12 13:23:03 +0100852 len = strlcpy2(dst, src, len);
853 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100854
855 size -= len;
856 dst += len;
857 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100858 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
859 if (size < 2)
860 return NULL;
861 *(dst++) = '-';
862 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100863
864 if (node->options & LOG_OPT_QUOTE) {
865 if (size < 2)
866 return NULL;
867 *(dst++) = '"';
868 }
869
870 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100871 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100872}
873
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100874static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
875{
876 return lf_text_len(dst, src, size, size, node);
877}
878
William Lallemand5f232402012-04-05 18:02:55 +0200879/*
880 * Write a IP adress to the log string
881 * +X option write in hexadecimal notation, most signifant byte on the left
882 */
883char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
884{
885 char *ret = dst;
886 int iret;
887 char pn[INET6_ADDRSTRLEN];
888
889 if (node->options & LOG_OPT_HEXA) {
890 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
891 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
892 if (iret < 0 || iret > size)
893 return NULL;
894 ret += iret;
895 } else {
896 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
897 ret = lf_text(dst, pn, size, node);
898 if (ret == NULL)
899 return NULL;
900 }
901 return ret;
902}
903
904/*
905 * Write a port to the log
906 * +X option write in hexadecimal notation, most signifant byte on the left
907 */
908char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
909{
910 char *ret = dst;
911 int iret;
912
913 if (node->options & LOG_OPT_HEXA) {
914 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
915 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
916 if (iret < 0 || iret > size)
917 return NULL;
918 ret += iret;
919 } else {
920 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
921 if (ret == NULL)
922 return NULL;
923 }
924 return ret;
925}
926
Dragan Dosen1322d092015-09-22 16:05:32 +0200927/* Re-generate time-based part of the syslog header in RFC3164 format at
928 * the beginning of logheader once a second and return the pointer to the
929 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100930 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200931static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100933 static long tvsec;
934 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Dragan Dosen43885c72015-10-01 13:18:13 +0200935 static struct chunk host = { NULL, 0, 0 };
936 static int sep = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200938 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200940 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100941 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200942
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200943 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +0200944 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945
Dragan Dosen43885c72015-10-01 13:18:13 +0200946 if (unlikely(global.log_send_hostname != host.str)) {
947 host.str = global.log_send_hostname;
948 host.len = host.str ? strlen(host.str) : 0;
949 sep = host.len ? 1 : 0;
950 }
951
Dragan Dosen59cee972015-09-19 22:09:02 +0200952 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen43885c72015-10-01 13:18:13 +0200953 "<<<<>%s %2d %02d:%02d:%02d %.*s%*s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200954 monthname[tm.tm_mon],
Dragan Dosen43885c72015-10-01 13:18:13 +0200955 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
956 host.len, host.str, sep, "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200957 /* WARNING: depending upon implementations, snprintf may return
958 * either -1 or the number of bytes that would be needed to store
959 * the total message. In both cases, we must adjust it.
960 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200961 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
962 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200963
Dragan Dosen59cee972015-09-19 22:09:02 +0200964 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200965 }
966
Willy Tarreau094af4e2015-01-07 15:03:42 +0100967 dataptr[0] = 0; // ensure we get rid of any previous attempt
968
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200969 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100970}
971
Dragan Dosen1322d092015-09-22 16:05:32 +0200972/* Re-generate time-based part of the syslog header in RFC5424 format at
973 * the beginning of logheader_rfc5424 once a second and return the pointer
974 * to the first character after it.
975 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200976static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +0200977{
978 static long tvsec;
979 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Benoit GARNIERb413c2a2016-03-27 11:08:03 +0200980 const char *gmt_offset;
Dragan Dosen1322d092015-09-22 16:05:32 +0200981
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200982 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +0200983 /* this string is rebuild only once a second */
984 struct tm tm;
985 int hdr_len;
986
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200987 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +0200988 get_localtime(tvsec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +0200989 gmt_offset = get_gmt_offset(time, &tm);
Dragan Dosen1322d092015-09-22 16:05:32 +0200990
991 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
Dragan Dosen17def462015-10-09 21:31:43 +0200992 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ",
Dragan Dosen1322d092015-09-22 16:05:32 +0200993 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
Dragan Dosen17def462015-10-09 21:31:43 +0200994 tm.tm_hour, tm.tm_min, tm.tm_sec,
Benoit GARNIERb413c2a2016-03-27 11:08:03 +0200995 gmt_offset, gmt_offset+3,
Dragan Dosen43885c72015-10-01 13:18:13 +0200996 global.log_send_hostname ? global.log_send_hostname : hostname);
Dragan Dosen1322d092015-09-22 16:05:32 +0200997 /* WARNING: depending upon implementations, snprintf may return
998 * either -1 or the number of bytes that would be needed to store
999 * the total message. In both cases, we must adjust it.
1000 */
1001 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
1002 hdr_len = global.max_syslog_len;
1003
1004 dataptr = logheader_rfc5424 + hdr_len;
1005 }
1006
1007 dataptr[0] = 0; // ensure we get rid of any previous attempt
1008
1009 return dataptr;
1010}
1011
William Lallemand2a4a44f2012-02-06 16:00:33 +01001012/*
Dragan Dosen59cee972015-09-19 22:09:02 +02001013 * This function sends the syslog message using a printf format string. It
1014 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001015 */
1016void send_log(struct proxy *p, int level, const char *format, ...)
1017{
1018 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001019 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001020
Willy Tarreau8c97ab52015-01-15 16:29:53 +01001021 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +01001022 return;
1023
William Lallemand2a4a44f2012-02-06 16:00:33 +01001024 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +02001025 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +02001026 if (data_len < 0 || data_len > global.max_syslog_len)
1027 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001028 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001029
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001030 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +01001031}
1032
1033/*
1034 * This function sends a syslog message.
1035 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001036 * It overrides the last byte of the message vector with an LF character.
1037 * The arguments <sd> and <sd_size> are used for the structured-data part
1038 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +01001039 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001040void __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 +01001041{
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001042 static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
1043 static struct msghdr msghdr = {
1044 .msg_iov = iovec,
1045 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
1046 };
William Lallemand2a4a44f2012-02-06 16:00:33 +01001047 static int logfdunix = -1; /* syslog to AF_UNIX socket */
1048 static int logfdinet = -1; /* syslog to AF_INET socket */
1049 static char *dataptr = NULL;
1050 int fac_level;
1051 struct list *logsrvs = NULL;
1052 struct logsrv *tmp = NULL;
1053 int nblogger;
Dragan Dosen1322d092015-09-22 16:05:32 +02001054 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +02001055 size_t hdr_size;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001056 time_t time = date.tv_sec;
Dragan Dosen43885c72015-10-01 13:18:13 +02001057 struct chunk *tag = &global.log_tag;
1058 static int curr_pid;
1059 static char pidstr[100];
1060 static struct chunk pid;
William Lallemand2a4a44f2012-02-06 16:00:33 +01001061
1062 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001063
1064 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +02001065 if (!LIST_ISEMPTY(&global.logsrvs)) {
1066 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001067 }
1068 } else {
William Lallemand0f99e342011-10-12 17:50:54 +02001069 if (!LIST_ISEMPTY(&p->logsrvs)) {
1070 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001071 }
Dragan Dosen43885c72015-10-01 13:18:13 +02001072 if (p->log_tag.str) {
1073 tag = &p->log_tag;
1074 }
Robert Tsai81ae1952007-12-05 10:47:29 +01001075 }
1076
William Lallemand0f99e342011-10-12 17:50:54 +02001077 if (!logsrvs)
1078 return;
1079
Dragan Dosen43885c72015-10-01 13:18:13 +02001080 if (unlikely(curr_pid != getpid())) {
1081 curr_pid = getpid();
1082 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
1083 chunk_initstr(&pid, pidstr);
1084 }
1085
Robert Tsai81ae1952007-12-05 10:47:29 +01001086 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +02001087 nblogger = 0;
1088 list_for_each_entry(tmp, logsrvs, list) {
1089 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +01001090 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +01001091 &logfdunix : &logfdinet;
Dragan Dosen43885c72015-10-01 13:18:13 +02001092 char *pid_sep1 = NULL, *pid_sep2 = NULL;
Robert Tsai81ae1952007-12-05 10:47:29 +01001093 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001094 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +02001095 int hdr_max = 0;
Dragan Dosen43885c72015-10-01 13:18:13 +02001096 int tag_max = 0;
1097 int pid_sep1_max = 0;
1098 int pid_max = 0;
1099 int pid_sep2_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001100 int sd_max = 0;
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001101 int max = 0;
Robert Tsai81ae1952007-12-05 10:47:29 +01001102
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001103 nblogger++;
1104
Willy Tarreaubaaee002006-06-26 02:48:02 +02001105 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +02001106 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001107 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001108
Willy Tarreauc7c7be22014-06-23 18:07:15 +02001109 if (unlikely(*plogfd < 0)) {
1110 /* socket not successfully initialized yet */
1111 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
1112
1113 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
1114 Alert("socket for logger #%d failed: %s (errno=%d)\n",
1115 nblogger, strerror(errno), errno);
1116 continue;
1117 }
1118 /* we don't want to receive anything on this socket */
1119 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
1120 /* does nothing under Linux, maybe needed for others */
1121 shutdown(*plogfd, SHUT_RD);
1122 }
1123
Dragan Dosen1322d092015-09-22 16:05:32 +02001124 switch (logsrv->format) {
1125 case LOG_FORMAT_RFC3164:
1126 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001127 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +02001128 break;
1129
1130 case LOG_FORMAT_RFC5424:
1131 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001132 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001133 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +02001134 break;
1135
1136 default:
1137 continue; /* must never happen */
1138 }
1139
1140 hdr_size = hdr_ptr - hdr;
1141
Willy Tarreaubaaee002006-06-26 02:48:02 +02001142 /* For each target, we may have a different facility.
1143 * We can also have a different log level for each message.
1144 * This induces variations in the message header length.
1145 * Since we don't want to recompute it each time, nor copy it every
1146 * time, we only change the facility in the pre-computed header,
1147 * and we change the pointer to the header accordingly.
1148 */
William Lallemand0f99e342011-10-12 17:50:54 +02001149 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +02001150 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001151 do {
Dragan Dosen59cee972015-09-19 22:09:02 +02001152 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +02001154 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001155 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001156 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001157
Dragan Dosen1322d092015-09-22 16:05:32 +02001158 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001159
Dragan Dosen43885c72015-10-01 13:18:13 +02001160 /* time-based header */
Dragan Dosen59cee972015-09-19 22:09:02 +02001161 if (unlikely(hdr_size >= logsrv->maxlen)) {
1162 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001163 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001164 goto send;
1165 }
1166
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001167 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001168
Dragan Dosen43885c72015-10-01 13:18:13 +02001169 /* tag */
1170 tag_max = tag->len;
1171 if (unlikely(tag_max >= maxlen)) {
1172 tag_max = maxlen - 1;
1173 sd_max = 0;
1174 goto send;
1175 }
1176
1177 maxlen -= tag_max;
1178
1179 /* first pid separator */
1180 pid_sep1_max = log_formats[logsrv->format].pid.sep1.len;
1181 if (unlikely(pid_sep1_max >= maxlen)) {
1182 pid_sep1_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001183 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001184 goto send;
1185 }
1186
Dragan Dosen43885c72015-10-01 13:18:13 +02001187 pid_sep1 = log_formats[logsrv->format].pid.sep1.str;
1188 maxlen -= pid_sep1_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001189
Dragan Dosen43885c72015-10-01 13:18:13 +02001190 /* pid */
1191 pid_max = pid.len;
1192 if (unlikely(pid_max >= maxlen)) {
1193 pid_max = maxlen - 1;
1194 sd_max = 0;
1195 goto send;
1196 }
1197
1198 maxlen -= pid_max;
1199
1200 /* second pid separator */
1201 pid_sep2_max = log_formats[logsrv->format].pid.sep2.len;
1202 if (unlikely(pid_sep2_max >= maxlen)) {
1203 pid_sep2_max = maxlen - 1;
1204 sd_max = 0;
1205 goto send;
1206 }
1207
1208 pid_sep2 = log_formats[logsrv->format].pid.sep2.str;
1209 maxlen -= pid_sep2_max;
1210
1211 /* structured-data */
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001212 if (sd_max >= maxlen) {
1213 sd_max = maxlen - 1;
1214 goto send;
1215 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001216
Dragan Dosen5b78d9b2015-09-28 16:01:03 +02001217 max = MIN(size, maxlen - sd_max) - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001218send:
Dragan Dosen59cee972015-09-19 22:09:02 +02001219 iovec[0].iov_base = hdr_ptr;
Dragan Dosen43885c72015-10-01 13:18:13 +02001220 iovec[0].iov_len = hdr_max;
1221 iovec[1].iov_base = tag->str;
1222 iovec[1].iov_len = tag_max;
1223 iovec[2].iov_base = pid_sep1;
1224 iovec[2].iov_len = pid_sep1_max;
1225 iovec[3].iov_base = pid.str;
1226 iovec[3].iov_len = pid_max;
1227 iovec[4].iov_base = pid_sep2;
1228 iovec[4].iov_len = pid_sep2_max;
1229 iovec[5].iov_base = sd;
1230 iovec[5].iov_len = sd_max;
1231 iovec[6].iov_base = dataptr;
1232 iovec[6].iov_len = max;
1233 iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */
1234 iovec[7].iov_len = 1;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001235
1236 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1237 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1238
1239 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001240
Robert Tsai81ae1952007-12-05 10:47:29 +01001241 if (sent < 0) {
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001242 Alert("sendmsg logger #%d failed: %s (errno=%d)\n",
Robert Tsai81ae1952007-12-05 10:47:29 +01001243 nblogger, strerror(errno), errno);
1244 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001245 }
1246}
1247
William Lallemandbddd4fd2012-02-27 11:23:10 +01001248extern fd_set hdr_encode_map[];
1249extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001250extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001251
Willy Tarreaubaaee002006-06-26 02:48:02 +02001252
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001253const 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 +01001254const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1255 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1256 Set-cookie Updated, unknown, unknown */
1257
William Lallemand1d705562012-03-12 12:46:41 +01001258/*
1259 * try to write a character if there is enough space, or goto out
1260 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001261#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001262 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001263 *(tmplog++) = (x); \
1264 } else { \
1265 goto out; \
1266 } \
1267 } while(0)
1268
Dragan Dosen835b9212016-02-12 13:23:03 +01001269
1270/* Initializes some log data.
1271 */
1272void init_log()
1273{
1274 char *tmp;
1275
1276 /* Initialize the escape map for the RFC5424 structured-data : '"\]'
1277 * inside PARAM-VALUE should be escaped with '\' as prefix.
1278 * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more
1279 * details.
1280 */
1281 memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map));
1282
1283 tmp = "\"\\]";
1284 while (*tmp) {
1285 FD_SET(*tmp, rfc5424_escape_map);
1286 tmp++;
1287 }
1288}
William Lallemand1d705562012-03-12 12:46:41 +01001289
Willy Tarreaudf974472012-12-28 02:44:01 +01001290/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1291 * <maxsize> characters. Returns the size of the output string in characters,
1292 * not counting the trailing zero which is always added if the resulting size
1293 * is not zero.
1294 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001295int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001296{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001297 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001298 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +01001299 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001300 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001301 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001302 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001303 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001304 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001305 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001306 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001307 int t_request;
1308 int hdr;
1309 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001310 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001311 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001312 char *ret;
1313 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001314 struct logformat_node *tmp;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001315 struct timeval tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001316
William Lallemandbddd4fd2012-02-27 11:23:10 +01001317 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001318
William Lallemandbddd4fd2012-02-27 11:23:10 +01001319 t_request = -1;
1320 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1321 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1322
William Lallemand1d705562012-03-12 12:46:41 +01001323 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001324
William Lallemandbddd4fd2012-02-27 11:23:10 +01001325 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001326 if (LIST_ISEMPTY(list_format))
1327 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001328
William Lallemand1d705562012-03-12 12:46:41 +01001329 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001330 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001331 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001332 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001333 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001334
Willy Tarreauc8368452012-12-21 00:09:23 +01001335 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001336 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001337 if (!last_isspace) {
1338 LOGCHAR(' ');
1339 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001340 }
1341 break;
1342
William Lallemand1d705562012-03-12 12:46:41 +01001343 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001344 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001345 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001346 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001347 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001348 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001349 last_isspace = 0;
1350 break;
1351
Willy Tarreauc8368452012-12-21 00:09:23 +01001352 case LOG_FMT_EXPR: // sample expression, may be request or response
1353 key = NULL;
1354 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001355 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 +01001356 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001357 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 +01001358 if (tmp->options & LOG_OPT_HTTP)
Dragan Dosen835b9212016-02-12 13:23:03 +01001359 ret = lf_encode_chunk(tmplog, dst + maxsize,
1360 '%', http_encode_map, key ? &key->data.u.str : &empty, tmp);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001361 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001362 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 +01001363 if (ret == 0)
1364 goto out;
1365 tmplog = ret;
1366 last_isspace = 0;
1367 break;
1368
Willy Tarreau2beef582012-12-20 17:22:52 +01001369 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001370 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001371 if (conn)
1372 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1373 else
1374 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001375 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001376 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001377 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001378 last_isspace = 0;
1379 break;
1380
Willy Tarreau2beef582012-12-20 17:22:52 +01001381 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001382 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001383 if (conn) {
1384 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001385 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001386 } else {
1387 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1388 dst + maxsize - tmplog, tmp);
1389 }
William Lallemand5f232402012-04-05 18:02:55 +02001390 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001391 else
1392 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1393
William Lallemand5f232402012-04-05 18:02:55 +02001394 if (ret == NULL)
1395 goto out;
1396 tmplog = ret;
1397 last_isspace = 0;
1398 break;
1399
Willy Tarreau2beef582012-12-20 17:22:52 +01001400 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001401 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001402 if (conn) {
1403 conn_get_to_addr(conn);
1404 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1405 }
1406 else
1407 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1408
William Lallemand1d705562012-03-12 12:46:41 +01001409 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001410 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001411 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001412 last_isspace = 0;
1413 break;
1414
Willy Tarreau2beef582012-12-20 17:22:52 +01001415 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001416 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001417 if (conn) {
1418 conn_get_to_addr(conn);
1419 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001420 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001421 else
1422 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001423 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001424 else
1425 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1426
William Lallemand5f232402012-04-05 18:02:55 +02001427 if (ret == NULL)
1428 goto out;
1429 tmplog = ret;
1430 last_isspace = 0;
1431 break;
1432
Willy Tarreau2beef582012-12-20 17:22:52 +01001433 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001434 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001435 if (conn)
1436 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1437 else
1438 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1439
William Lallemand1d705562012-03-12 12:46:41 +01001440 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001441 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001442 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001443 last_isspace = 0;
1444 break;
1445
Willy Tarreau2beef582012-12-20 17:22:52 +01001446 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001447 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001448 if (conn)
1449 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1450 else
1451 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1452
William Lallemand5f232402012-04-05 18:02:55 +02001453 if (ret == NULL)
1454 goto out;
1455 tmplog = ret;
1456 last_isspace = 0;
1457 break;
1458
Willy Tarreau2beef582012-12-20 17:22:52 +01001459 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001460 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001461 if (conn)
1462 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1463 else
1464 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1465
William Lallemand5f232402012-04-05 18:02:55 +02001466 if (ret == NULL)
1467 goto out;
1468 tmplog = ret;
1469 last_isspace = 0;
1470 break;
1471
Willy Tarreau2beef582012-12-20 17:22:52 +01001472 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001473 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001474 if (conn)
1475 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1476 else
1477 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1478
William Lallemand1d705562012-03-12 12:46:41 +01001479 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001480 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001481 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001482 last_isspace = 0;
1483 break;
1484
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001485 case LOG_FMT_DATE: // %t = accept date
William Lallemandbddd4fd2012-02-27 11:23:10 +01001486 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001487 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1488 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001489 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001490 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001491 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001492 last_isspace = 0;
1493 break;
1494
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001495 case LOG_FMT_tr: // %tr = start of request date
1496 /* Note that the timers are valid if we get here */
1497 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1498 get_localtime(tv.tv_sec, &tm);
1499 ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog);
1500 if (ret == NULL)
1501 goto out;
1502 tmplog = ret;
1503 last_isspace = 0;
1504 break;
1505
1506 case LOG_FMT_DATEGMT: // %T = accept date, GMT
William Lallemandbddd4fd2012-02-27 11:23:10 +01001507 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001508 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001509 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001510 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001511 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001512 last_isspace = 0;
1513 break;
1514
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001515 case LOG_FMT_trg: // %trg = start of request date, GMT
1516 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1517 get_gmtime(tv.tv_sec, &tm);
1518 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
1519 if (ret == NULL)
1520 goto out;
1521 tmplog = ret;
1522 last_isspace = 0;
1523 break;
1524
1525 case LOG_FMT_DATELOCAL: // %Tl = accept date, local
Yuxans Yao4e25b012012-10-19 10:36:09 +08001526 get_localtime(s->logs.accept_date.tv_sec, &tm);
Benoit GARNIERe2e5bde2016-03-27 03:04:16 +02001527 ret = localdate2str_log(tmplog, s->logs.accept_date.tv_sec, &tm, dst + maxsize - tmplog);
Yuxans Yao4e25b012012-10-19 10:36:09 +08001528 if (ret == NULL)
1529 goto out;
1530 tmplog = ret;
1531 last_isspace = 0;
1532 break;
1533
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001534 case LOG_FMT_trl: // %trl = start of request date, local
1535 tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0);
1536 get_localtime(tv.tv_sec, &tm);
1537 ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog);
1538 if (ret == NULL)
1539 goto out;
1540 tmplog = ret;
1541 last_isspace = 0;
1542 break;
1543
William Lallemand5f232402012-04-05 18:02:55 +02001544 case LOG_FMT_TS: // %Ts
1545 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1546 if (tmp->options & LOG_OPT_HEXA) {
1547 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1548 if (iret < 0 || iret > dst + maxsize - tmplog)
1549 goto out;
1550 last_isspace = 0;
1551 tmplog += iret;
1552 } else {
1553 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1554 if (ret == NULL)
1555 goto out;
1556 tmplog = ret;
1557 last_isspace = 0;
1558 }
1559 break;
1560
William Lallemand1d705562012-03-12 12:46:41 +01001561 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001562 if (tmp->options & LOG_OPT_HEXA) {
1563 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1564 if (iret < 0 || iret > dst + maxsize - tmplog)
1565 goto out;
1566 last_isspace = 0;
1567 tmplog += iret;
1568 } else {
1569 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001570 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001571 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1572 tmplog, 4);
1573 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001574 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001575 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001576 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001577 }
1578 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001579
William Lallemand1d705562012-03-12 12:46:41 +01001580 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001581 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001582 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001583 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001584 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001585 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001586 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001587 break;
1588
Willy Tarreau773d65f2012-10-12 14:56:11 +02001589 case LOG_FMT_FRONTEND_XPRT: // %ft
1590 src = fe->id;
1591 if (tmp->options & LOG_OPT_QUOTE)
1592 LOGCHAR('"');
1593 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1594 if (iret == 0)
1595 goto out;
1596 tmplog += iret;
1597#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001598 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001599 LOGCHAR('~');
1600#endif
1601 if (tmp->options & LOG_OPT_QUOTE)
1602 LOGCHAR('"');
1603 last_isspace = 0;
1604 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001605#ifdef USE_OPENSSL
1606 case LOG_FMT_SSL_CIPHER: // %sslc
1607 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001608 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001609 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001610 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001611 src = ssl_sock_get_cipher_name(conn);
1612 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001613 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1614 if (ret == NULL)
1615 goto out;
1616 tmplog = ret;
1617 last_isspace = 0;
1618 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001619
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001620 case LOG_FMT_SSL_VERSION: // %sslv
1621 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001622 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001623 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001624 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001625 src = ssl_sock_get_proto_version(conn);
1626 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001627 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1628 if (ret == NULL)
1629 goto out;
1630 tmplog = ret;
1631 last_isspace = 0;
1632 break;
1633#endif
William Lallemand1d705562012-03-12 12:46:41 +01001634 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001635 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001636 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001637 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001638 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001639 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001640 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001641 break;
1642
William Lallemand1d705562012-03-12 12:46:41 +01001643 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001644 switch (obj_type(s->target)) {
1645 case OBJ_TYPE_SERVER:
1646 src = objt_server(s->target)->id;
1647 break;
1648 case OBJ_TYPE_APPLET:
1649 src = objt_applet(s->target)->name;
1650 break;
1651 default:
1652 src = "<NOSRV>";
1653 break;
1654 }
William Lallemand5f232402012-04-05 18:02:55 +02001655 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001656 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001657 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001658 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001659 last_isspace = 0;
1660 break;
1661
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001662 case LOG_FMT_Th: // %Th = handshake time
1663 ret = ltoa_o(s->logs.t_handshake, tmplog, dst + maxsize - tmplog);
1664 if (ret == NULL)
1665 goto out;
1666 tmplog = ret;
1667 last_isspace = 0;
1668 break;
1669
1670 case LOG_FMT_Ti: // %Ti = HTTP idle time
1671 ret = ltoa_o(s->logs.t_idle, tmplog, dst + maxsize - tmplog);
1672 if (ret == NULL)
1673 goto out;
1674 tmplog = ret;
1675 last_isspace = 0;
1676 break;
1677
1678 case LOG_FMT_TR: // %TR = HTTP request time
1679 ret = ltoa_o((t_request >= 0) ? t_request - s->logs.t_idle - s->logs.t_handshake : -1,
1680 tmplog, dst + maxsize - tmplog);
1681 if (ret == NULL)
1682 goto out;
1683 tmplog = ret;
1684 last_isspace = 0;
1685 break;
1686
1687 case LOG_FMT_TQ: // %Tq = Th + Ti + TR
William Lallemand5f232402012-04-05 18:02:55 +02001688 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001689 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001690 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001691 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001692 last_isspace = 0;
1693 break;
1694
William Lallemand1d705562012-03-12 12:46:41 +01001695 case LOG_FMT_TW: // %Tw
1696 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001697 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001698 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001699 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001700 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001701 last_isspace = 0;
1702 break;
1703
William Lallemand1d705562012-03-12 12:46:41 +01001704 case LOG_FMT_TC: // %Tc
1705 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001706 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001707 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001708 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001709 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001710 last_isspace = 0;
1711 break;
1712
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001713 case LOG_FMT_Tr: // %Tr
William Lallemand1d705562012-03-12 12:46:41 +01001714 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001715 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001716 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001717 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001718 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001719 last_isspace = 0;
1720 break;
1721
Willy Tarreau27b639d2016-05-17 17:55:27 +02001722 case LOG_FMT_TD: // %Td
1723 if (s->be->mode == PR_MODE_HTTP)
1724 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_close - s->logs.t_data : -1,
1725 tmplog, dst + maxsize - tmplog);
1726 else
1727 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_close - s->logs.t_connect : -1,
1728 tmplog, dst + maxsize - tmplog);
1729 if (ret == NULL)
1730 goto out;
1731 tmplog = ret;
1732 last_isspace = 0;
1733 break;
1734
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001735 case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti
1736 if (!(fe->to_log & LW_BYTES))
1737 LOGCHAR('+');
1738 ret = ltoa_o(s->logs.t_close - (s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0),
1739 tmplog, dst + maxsize - tmplog);
1740 if (ret == NULL)
1741 goto out;
1742 tmplog = ret;
1743 last_isspace = 0;
1744 break;
1745
1746 case LOG_FMT_TT: // %Tt = total time
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001747 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001748 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001749 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001750 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001751 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001752 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001753 last_isspace = 0;
1754 break;
1755
Willy Tarreau2beef582012-12-20 17:22:52 +01001756 case LOG_FMT_STATUS: // %ST
Willy Tarreau57bc8912016-04-25 17:09:40 +02001757 ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001758 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001759 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001760 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001761 last_isspace = 0;
1762 break;
1763
William Lallemand1d705562012-03-12 12:46:41 +01001764 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001765 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001766 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001767 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001768 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001769 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001770 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001771 last_isspace = 0;
1772 break;
1773
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001774 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001775 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1776 if (ret == NULL)
1777 goto out;
1778 tmplog = ret;
1779 last_isspace = 0;
1780 break;
1781
Willy Tarreau2beef582012-12-20 17:22:52 +01001782 case LOG_FMT_CCLIENT: // %CC
Willy Tarreau57bc8912016-04-25 17:09:40 +02001783 src = txn ? txn->cli_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02001784 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001785 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001786 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001787 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001788 last_isspace = 0;
1789 break;
1790
Willy Tarreau2beef582012-12-20 17:22:52 +01001791 case LOG_FMT_CSERVER: // %CS
Willy Tarreau57bc8912016-04-25 17:09:40 +02001792 src = txn ? txn->srv_cookie : NULL;
William Lallemand5f232402012-04-05 18:02:55 +02001793 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001794 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001795 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001796 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001797 last_isspace = 0;
1798 break;
1799
William Lallemand1d705562012-03-12 12:46:41 +01001800 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001801 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1802 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001803 *tmplog = '\0';
1804 last_isspace = 0;
1805 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001806
William Lallemand1d705562012-03-12 12:46:41 +01001807 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001808 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1809 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau57bc8912016-04-25 17:09:40 +02001810 LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1811 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 +01001812 last_isspace = 0;
1813 break;
1814
William Lallemand1d705562012-03-12 12:46:41 +01001815 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001816 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001817 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001818 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001819 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001820 last_isspace = 0;
1821 break;
1822
William Lallemand1d705562012-03-12 12:46:41 +01001823 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001824 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001825 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001826 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001827 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001828 last_isspace = 0;
1829 break;
1830
William Lallemand1d705562012-03-12 12:46:41 +01001831 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001832 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001833 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001834 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001835 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001836 last_isspace = 0;
1837 break;
1838
William Lallemand1d705562012-03-12 12:46:41 +01001839 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001840 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001841 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001842 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001843 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001844 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001845 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001846 last_isspace = 0;
1847 break;
1848
William Lallemand1d705562012-03-12 12:46:41 +01001849 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001850 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001851 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001852 ret = ltoa_o((s->si[1].conn_retries>0) ?
1853 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001854 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001855 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001856 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001857 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001858 last_isspace = 0;
1859 break;
1860
William Lallemand1d705562012-03-12 12:46:41 +01001861 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001862 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001863 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001864 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001865 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001866 last_isspace = 0;
1867 break;
1868
William Lallemand1d705562012-03-12 12:46:41 +01001869 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001870 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001871 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001872 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001873 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001874 last_isspace = 0;
1875 break;
1876
William Lallemand1d705562012-03-12 12:46:41 +01001877 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001878 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001879 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001880 if (tmp->options & LOG_OPT_QUOTE)
1881 LOGCHAR('"');
1882 LOGCHAR('{');
1883 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1884 if (hdr)
1885 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001886 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001887 ret = lf_encode_string(tmplog, dst + maxsize,
1888 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001889 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001890 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001891 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001892 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001893 }
1894 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001895 if (tmp->options & LOG_OPT_QUOTE)
1896 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001897 last_isspace = 0;
1898 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001899 break;
1900
William Lallemand1d705562012-03-12 12:46:41 +01001901 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001902 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001903 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001904 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1905 if (hdr > 0)
1906 LOGCHAR(' ');
1907 if (tmp->options & LOG_OPT_QUOTE)
1908 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001909 if (s->req_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001910 ret = lf_encode_string(tmplog, dst + maxsize,
1911 '#', hdr_encode_map, s->req_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001912 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001913 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001914 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001915 } else if (!(tmp->options & LOG_OPT_QUOTE))
1916 LOGCHAR('-');
1917 if (tmp->options & LOG_OPT_QUOTE)
1918 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001919 last_isspace = 0;
1920 }
1921 }
1922 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001923
William Lallemand1d705562012-03-12 12:46:41 +01001924
1925 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001926 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001927 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001928 if (tmp->options & LOG_OPT_QUOTE)
1929 LOGCHAR('"');
1930 LOGCHAR('{');
1931 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1932 if (hdr)
1933 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001934 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001935 ret = lf_encode_string(tmplog, dst + maxsize,
1936 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001937 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001938 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001939 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001940 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001941 }
1942 LOGCHAR('}');
1943 last_isspace = 0;
1944 if (tmp->options & LOG_OPT_QUOTE)
1945 LOGCHAR('"');
1946 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001947 break;
1948
William Lallemand1d705562012-03-12 12:46:41 +01001949 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001950 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001951 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001952 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1953 if (hdr > 0)
1954 LOGCHAR(' ');
1955 if (tmp->options & LOG_OPT_QUOTE)
1956 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001957 if (s->res_cap[hdr] != NULL) {
Dragan Dosen835b9212016-02-12 13:23:03 +01001958 ret = lf_encode_string(tmplog, dst + maxsize,
1959 '#', hdr_encode_map, s->res_cap[hdr], tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001960 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001961 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001962 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001963 } else if (!(tmp->options & LOG_OPT_QUOTE))
1964 LOGCHAR('-');
1965 if (tmp->options & LOG_OPT_QUOTE)
1966 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001967 last_isspace = 0;
1968 }
1969 }
1970 break;
1971
William Lallemand1d705562012-03-12 12:46:41 +01001972 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001973 /* Request */
1974 if (tmp->options & LOG_OPT_QUOTE)
1975 LOGCHAR('"');
Willy Tarreau57bc8912016-04-25 17:09:40 +02001976 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Dragan Dosen835b9212016-02-12 13:23:03 +01001977 ret = lf_encode_string(tmplog, dst + maxsize,
1978 '#', url_encode_map, uri, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001979 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001980 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001981 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001982 if (tmp->options & LOG_OPT_QUOTE)
1983 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001984 last_isspace = 0;
1985 break;
William Lallemand5f232402012-04-05 18:02:55 +02001986
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001987 case LOG_FMT_HTTP_PATH: // %HP
Willy Tarreau57bc8912016-04-25 17:09:40 +02001988 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001989
Willy Tarreaub7636d12015-06-17 19:58:02 +02001990 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001991 LOGCHAR('"');
1992
1993 end = uri + strlen(uri);
1994 // look for the first whitespace character
1995 while (uri < end && !HTTP_IS_SPHT(*uri))
1996 uri++;
1997
1998 // keep advancing past multiple spaces
1999 while (uri < end && HTTP_IS_SPHT(*uri)) {
2000 uri++; nspaces++;
2001 }
2002
2003 // look for first space or question mark after url
2004 spc = uri;
2005 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
2006 spc++;
2007
Nenad Merdanovic54e439f2016-04-26 01:39:02 +02002008 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002009 chunk.str = "<BADREQ>";
2010 chunk.len = strlen("<BADREQ>");
2011 } else {
2012 chunk.str = uri;
2013 chunk.len = spc - uri;
2014 }
2015
Dragan Dosen835b9212016-02-12 13:23:03 +01002016 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002017 if (ret == NULL || *ret != '\0')
2018 goto out;
2019
2020 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002021 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002022 LOGCHAR('"');
2023
2024 last_isspace = 0;
2025 break;
2026
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002027 case LOG_FMT_HTTP_QUERY: // %HQ
2028 if (tmp->options & LOG_OPT_QUOTE)
2029 LOGCHAR('"');
2030
Willy Tarreau57bc8912016-04-25 17:09:40 +02002031 if (!txn || !txn->uri) {
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002032 chunk.str = "<BADREQ>";
2033 chunk.len = strlen("<BADREQ>");
2034 } else {
2035 uri = txn->uri;
2036 end = uri + strlen(uri);
2037 // look for the first question mark
2038 while (uri < end && *uri != '?')
2039 uri++;
2040
2041 qmark = uri;
2042 // look for first space or question mark after url
2043 while (uri < end && !HTTP_IS_SPHT(*uri))
2044 uri++;
2045
2046 chunk.str = qmark;
2047 chunk.len = uri - qmark;
2048 }
2049
Dragan Dosen835b9212016-02-12 13:23:03 +01002050 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworthe63ac872015-07-31 16:14:16 +00002051 if (ret == NULL || *ret != '\0')
2052 goto out;
2053
2054 tmplog = ret;
2055 if (tmp->options & LOG_OPT_QUOTE)
2056 LOGCHAR('"');
2057
2058 last_isspace = 0;
2059 break;
2060
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002061 case LOG_FMT_HTTP_URI: // %HU
Willy Tarreau57bc8912016-04-25 17:09:40 +02002062 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002063
Willy Tarreaub7636d12015-06-17 19:58:02 +02002064 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002065 LOGCHAR('"');
2066
2067 end = uri + strlen(uri);
2068 // look for the first whitespace character
2069 while (uri < end && !HTTP_IS_SPHT(*uri))
2070 uri++;
2071
2072 // keep advancing past multiple spaces
2073 while (uri < end && HTTP_IS_SPHT(*uri)) {
2074 uri++; nspaces++;
2075 }
2076
2077 // look for first space after url
2078 spc = uri;
2079 while (spc < end && !HTTP_IS_SPHT(*spc))
2080 spc++;
2081
Willy Tarreau57bc8912016-04-25 17:09:40 +02002082 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002083 chunk.str = "<BADREQ>";
2084 chunk.len = strlen("<BADREQ>");
2085 } else {
2086 chunk.str = uri;
2087 chunk.len = spc - uri;
2088 }
2089
Dragan Dosen835b9212016-02-12 13:23:03 +01002090 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002091 if (ret == NULL || *ret != '\0')
2092 goto out;
2093
2094 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002095 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002096 LOGCHAR('"');
2097
2098 last_isspace = 0;
2099 break;
2100
2101 case LOG_FMT_HTTP_METHOD: // %HM
Willy Tarreau57bc8912016-04-25 17:09:40 +02002102 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002103 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002104 LOGCHAR('"');
2105
2106 end = uri + strlen(uri);
2107 // look for the first whitespace character
2108 spc = uri;
2109 while (spc < end && !HTTP_IS_SPHT(*spc))
2110 spc++;
2111
2112 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
2113 chunk.str = "<BADREQ>";
2114 chunk.len = strlen("<BADREQ>");
2115 } else {
2116 chunk.str = uri;
2117 chunk.len = spc - uri;
2118 }
2119
Dragan Dosen835b9212016-02-12 13:23:03 +01002120 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002121 if (ret == NULL || *ret != '\0')
2122 goto out;
2123
2124 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002125 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002126 LOGCHAR('"');
2127
2128 last_isspace = 0;
2129 break;
2130
2131 case LOG_FMT_HTTP_VERSION: // %HV
Willy Tarreau57bc8912016-04-25 17:09:40 +02002132 uri = txn && txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02002133 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002134 LOGCHAR('"');
2135
2136 end = uri + strlen(uri);
2137 // look for the first whitespace character
2138 while (uri < end && !HTTP_IS_SPHT(*uri))
2139 uri++;
2140
2141 // keep advancing past multiple spaces
2142 while (uri < end && HTTP_IS_SPHT(*uri)) {
2143 uri++; nspaces++;
2144 }
2145
2146 // look for the next whitespace character
2147 while (uri < end && !HTTP_IS_SPHT(*uri))
2148 uri++;
2149
2150 // keep advancing past multiple spaces
2151 while (uri < end && HTTP_IS_SPHT(*uri))
2152 uri++;
2153
Willy Tarreau57bc8912016-04-25 17:09:40 +02002154 if (!txn || !txn->uri || nspaces == 0) {
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002155 chunk.str = "<BADREQ>";
2156 chunk.len = strlen("<BADREQ>");
2157 } else if (uri == end) {
2158 chunk.str = "HTTP/0.9";
2159 chunk.len = strlen("HTTP/0.9");
2160 } else {
2161 chunk.str = uri;
2162 chunk.len = end - uri;
2163 }
2164
Dragan Dosen835b9212016-02-12 13:23:03 +01002165 ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp);
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002166 if (ret == NULL || *ret != '\0')
2167 goto out;
2168
2169 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02002170 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00002171 LOGCHAR('"');
2172
2173 last_isspace = 0;
2174 break;
2175
William Lallemand5f232402012-04-05 18:02:55 +02002176 case LOG_FMT_COUNTER: // %rt
2177 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002178 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02002179 if (iret < 0 || iret > dst + maxsize - tmplog)
2180 goto out;
2181 last_isspace = 0;
2182 tmplog += iret;
2183 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01002184 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02002185 if (ret == NULL)
2186 goto out;
2187 tmplog = ret;
2188 last_isspace = 0;
2189 }
2190 break;
2191
Willy Tarreau7346acb2014-08-28 15:03:15 +02002192 case LOG_FMT_LOGCNT: // %lc
2193 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002194 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002195 if (iret < 0 || iret > dst + maxsize - tmplog)
2196 goto out;
2197 last_isspace = 0;
2198 tmplog += iret;
2199 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002200 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02002201 if (ret == NULL)
2202 goto out;
2203 tmplog = ret;
2204 last_isspace = 0;
2205 }
2206 break;
2207
William Lallemand5f232402012-04-05 18:02:55 +02002208 case LOG_FMT_HOSTNAME: // %H
2209 src = hostname;
2210 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
2211 if (ret == NULL)
2212 goto out;
2213 tmplog = ret;
2214 last_isspace = 0;
2215 break;
2216
2217 case LOG_FMT_PID: // %pid
2218 if (tmp->options & LOG_OPT_HEXA) {
2219 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
2220 if (iret < 0 || iret > dst + maxsize - tmplog)
2221 goto out;
2222 last_isspace = 0;
2223 tmplog += iret;
2224 } else {
2225 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
2226 if (ret == NULL)
2227 goto out;
2228 tmplog = ret;
2229 last_isspace = 0;
2230 }
2231 break;
William Lallemanda73203e2012-03-12 12:48:57 +01002232
2233 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002234 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01002235 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02002236 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01002237 if (ret == NULL)
2238 goto out;
2239 tmplog = ret;
2240 last_isspace = 0;
2241 break;
2242
William Lallemandbddd4fd2012-02-27 11:23:10 +01002243 }
2244 }
2245
2246out:
William Lallemand1d705562012-03-12 12:46:41 +01002247 /* *tmplog is a unused character */
2248 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01002249 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002250
Willy Tarreaubaaee002006-06-26 02:48:02 +02002251}
2252
William Lallemand1d705562012-03-12 12:46:41 +01002253/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002254 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01002255 * Will not log if the frontend has no log defined.
2256 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002257void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01002258{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002259 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01002260 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002261 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01002262
2263 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002264 err = (s->flags & SF_REDISP) ||
2265 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
2266 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01002267 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02002268 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002269
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002270 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01002271 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002272
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002273 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01002274 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01002275
Willy Tarreauabcd5142013-06-11 17:18:02 +02002276 if (s->logs.level) { /* loglevel was overridden */
2277 if (s->logs.level == -1) {
2278 s->logs.logwait = 0; /* logs disabled */
2279 return;
2280 }
2281 level = s->logs.level - 1;
2282 }
2283 else {
2284 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002285 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02002286 level = LOG_ERR;
2287 }
William Lallemand1d705562012-03-12 12:46:41 +01002288
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002289 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002290 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002291 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002292 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002293 }
2294
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002295 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2296 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2297 &sess->fe->logformat_sd);
2298 }
2299
Dragan Dosen59cee972015-09-19 22:09:02 +02002300 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002301 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002302 sess->fe->log_count++;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002303 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002304 s->logs.logwait = 0;
2305 }
2306}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002307
Willy Tarreaubaaee002006-06-26 02:48:02 +02002308/*
2309 * Local variables:
2310 * c-indent-level: 8
2311 * c-basic-offset: 8
2312 * End:
2313 */