blob: 18296967a10d26d1b992ef8188e0de5548b0b8d1 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
25
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020027#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020029#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010032#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020033
William Lallemand5f232402012-04-05 18:02:55 +020034#include <proto/frontend.h>
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +000035#include <proto/proto_http.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020036#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010037#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020038#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010039#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020040#ifdef USE_OPENSSL
41#include <proto/ssl_sock.h>
42#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Dragan Dosen1322d092015-09-22 16:05:32 +020044const char *log_formats[LOG_FORMATS] = {
45 [LOG_FORMAT_RFC3164] = "rfc3164",
46 [LOG_FORMAT_RFC5424] = "rfc5424"
47};
48
Willy Tarreaubaaee002006-06-26 02:48:02 +020049const char *log_facilities[NB_LOG_FACILITIES] = {
50 "kern", "user", "mail", "daemon",
51 "auth", "syslog", "lpr", "news",
52 "uucp", "cron", "auth2", "ftp",
53 "ntp", "audit", "alert", "cron2",
54 "local0", "local1", "local2", "local3",
55 "local4", "local5", "local6", "local7"
56};
57
Willy Tarreaubaaee002006-06-26 02:48:02 +020058const char *log_levels[NB_LOG_LEVELS] = {
59 "emerg", "alert", "crit", "err",
60 "warning", "notice", "info", "debug"
61};
62
Willy Tarreau570f2212013-06-10 16:42:09 +020063const 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 +020064const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
William Lallemand723b73a2012-02-08 16:37:49 +010066
67/* log_format */
68struct logformat_type {
69 char *name;
70 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010071 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020072 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010073 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +010074 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +010075};
76
William Lallemandb7ff6a32012-03-02 14:35:21 +010077int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
78
William Lallemand723b73a2012-02-08 16:37:49 +010079/* log_format variable names */
80static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020081 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +010082
83 /* please keep these lines sorted ! */
84 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
85 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
86 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
87 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
88 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +020089 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +020090 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +010091 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +080092 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +020093 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +020094 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +010095 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +020096 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +010097 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
98 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +020099 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100100 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +0200101 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +0100102 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
103 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200104 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +0100105 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
106 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
107 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
108 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
109 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
110 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
111 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200112 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
113 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
114 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
115 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000116 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
117 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000118 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000119 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
120 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200121 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100122 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200123 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100124 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
125 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100126 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100127 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
128 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
129 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
130 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
131 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200132 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
133 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100134 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
135 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
136 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
137
138 /* The following tags are deprecated and will be removed soon */
139 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
140 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
141 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
142 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
143 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
144 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
145 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
146 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
147 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
148 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
149 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200150 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100151};
152
Willy Tarreau2beef582012-12-20 17:22:52 +0100153char default_http_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
154char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%T] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %Tq %Tw %Tc %Tr %Tt %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl";
155char 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 +0100156char *log_format = NULL;
157
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200158/* Common printf format strings for hostname, log_tag and pid used in all
159 * outgoing syslog messages.
160 */
Dragan Dosen1322d092015-09-22 16:05:32 +0200161char default_host_tag_pid_log_format[] = "%s%s[%d]: ";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200162char rfc5424_host_tag_pid_log_format[] = "%s%s %d - ";
163
164/* Default string used for structured-data part in RFC5424 formatted
165 * syslog messages.
166 */
167char default_rfc5424_sd_log_format[] = "- ";
Dragan Dosen1322d092015-09-22 16:05:32 +0200168
169/* This is a global syslog header, common to all outgoing messages in
170 * RFC3164 format. It begins with time-based part and is updated by
171 * update_log_hdr().
Dragan Dosen59cee972015-09-19 22:09:02 +0200172 */
173char *logheader = NULL;
174
Dragan Dosen1322d092015-09-22 16:05:32 +0200175/* This is a global syslog header for messages in RFC5424 format. It is
176 * updated by update_log_hdr_rfc5424().
177 */
178char *logheader_rfc5424 = NULL;
179
Dragan Dosen59cee972015-09-19 22:09:02 +0200180/* This is a global syslog message buffer, common to all outgoing
181 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100182 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200183char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100184
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200185/* A global syslog message buffer, common to all RFC5424 syslog messages.
186 * Currently, it is used for generating the structured-data part.
187 */
188char *logline_rfc5424 = NULL;
189
William Lallemand723b73a2012-02-08 16:37:49 +0100190struct logformat_var_args {
191 char *name;
192 int mask;
193};
194
195struct logformat_var_args var_args_list[] = {
196// global
197 { "M", LOG_OPT_MANDATORY },
198 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200199 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100200 { 0, 0 }
201};
202
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200203/* return the name of the directive used in the current proxy for which we're
204 * currently parsing a header, when it is known.
205 */
206static inline const char *fmt_directive(const struct proxy *curproxy)
207{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100208 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200209 case ARGC_ACL:
210 return "acl";
211 case ARGC_STK:
212 return "stick";
213 case ARGC_TRK:
214 return "track-sc";
215 case ARGC_LOG:
216 return "log-format";
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200217 case ARGC_LOGSD:
218 return "log-format-sd";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100219 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100220 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100221 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100222 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200223 case ARGC_UIF:
224 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100225 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200226 return "redirect";
227 case ARGC_CAP:
228 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200229 case ARGC_SRV:
230 return "server";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100231 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200232 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100233 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200234}
235
William Lallemand723b73a2012-02-08 16:37:49 +0100236/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100237 * callback used to configure addr source retrieval
238 */
239int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
240{
241 curproxy->options2 |= PR_O2_SRC_ADDR;
242
243 return 0;
244}
245
246
247/*
William Lallemand723b73a2012-02-08 16:37:49 +0100248 * Parse args in a logformat_var
249 */
250int parse_logformat_var_args(char *args, struct logformat_node *node)
251{
252 int i = 0;
253 int end = 0;
254 int flags = 0; // 1 = + 2 = -
255 char *sp = NULL; // start pointer
256
257 if (args == NULL)
258 return 1;
259
260 while (1) {
261 if (*args == '\0')
262 end = 1;
263
264 if (*args == '+') {
265 // add flag
266 sp = args + 1;
267 flags = 1;
268 }
269 if (*args == '-') {
270 // delete flag
271 sp = args + 1;
272 flags = 2;
273 }
274
275 if (*args == '\0' || *args == ',') {
276 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100277 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100278 if (strcmp(sp, var_args_list[i].name) == 0) {
279 if (flags == 1) {
280 node->options |= var_args_list[i].mask;
281 break;
282 } else if (flags == 2) {
283 node->options &= ~var_args_list[i].mask;
284 break;
285 }
286 }
287 }
288 sp = NULL;
289 if (end)
290 break;
291 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100292 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100293 }
294 return 0;
295}
296
297/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100298 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
299 * must pass the args part in the <arg> pointer with its length in <arg_len>,
300 * and varname with its length in <var> and <var_len> respectively. <arg> is
301 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100302 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100303int 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 +0100304{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100305 int j;
306 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100307
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100308 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
309 if (strlen(logformat_keywords[j].name) == var_len &&
310 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
311 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
312 node = calloc(1, sizeof(struct logformat_node));
313 node->type = logformat_keywords[j].type;
314 node->options = *defoptions;
315 if (arg_len) {
316 node->arg = my_strndup(arg, arg_len);
317 parse_logformat_var_args(node->arg, node);
318 }
319 if (node->type == LOG_FMT_GLOBAL) {
320 *defoptions = node->options;
321 free(node->arg);
322 free(node);
323 } else {
324 if (logformat_keywords[j].config_callback &&
325 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100326 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100327 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100328 curproxy->to_log |= logformat_keywords[j].lw;
329 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100330 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100331 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100332 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200333 curproxy->conf.args.file, curproxy->conf.args.line,
334 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100335 return 0;
336 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100337 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200338 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
339 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100340 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100341 }
William Lallemand723b73a2012-02-08 16:37:49 +0100342 }
343 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100344
345 j = var[var_len];
346 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100347 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 +0200348 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100349 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100350 return -1;
351}
352
353/*
354 * push to the logformat linked list
355 *
356 * start: start pointer
357 * end: end text pointer
358 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100359 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100360 *
361 * LOG_TEXT: copy chars from start to end excluding end.
362 *
363*/
William Lallemand1d705562012-03-12 12:46:41 +0100364void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100365{
366 char *str;
367
Willy Tarreaua3571662012-12-20 21:59:12 +0100368 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100369 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100370 str = calloc(end - start + 1, 1);
371 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100372 str[end - start] = '\0';
373 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100374 node->type = LOG_FMT_TEXT; // type string
375 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100376 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100377 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100378 node->type = LOG_FMT_SEPARATOR;
379 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100380 }
381}
382
383/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100384 * Parse the sample fetch expression <text> and add a node to <list_format> upon
385 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200386 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100387 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100388void 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 +0100389{
390 char *cmd[2];
391 struct sample_expr *expr;
392 struct logformat_node *node;
393 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100394 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100395
396 cmd[0] = text;
397 cmd[1] = "";
398 cmd_arg = 0;
399
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100400 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100401 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200402 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
403 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100404 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100405 return;
406 }
407
408 node = calloc(1, sizeof(struct logformat_node));
409 node->type = LOG_FMT_EXPR;
410 node->expr = expr;
411 node->options = options;
412
413 if (arg_len) {
414 node->arg = my_strndup(arg, arg_len);
415 parse_logformat_var_args(node->arg, node);
416 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100417 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100418 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
419
Willy Tarreau434c57c2013-01-08 01:10:24 +0100420 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100421 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
422
Willy Tarreau434c57c2013-01-08 01:10:24 +0100423 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200424 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
425 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
426 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100427
Willy Tarreauc8368452012-12-21 00:09:23 +0100428 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
429 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100430 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100431
William Lallemand65ad6e12014-01-31 15:08:02 +0100432 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
433 * needed with some sample fetches (eg: ssl*). We always set it for
434 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100435 */
436 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100437 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100438 LIST_ADDQ(list_format, &node->list);
439}
440
441/*
William Lallemand723b73a2012-02-08 16:37:49 +0100442 * Parse the log_format string and fill a linked list.
443 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200444 * You can set arguments using { } : %{many arguments}varname.
445 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100446 *
447 * str: the string to parse
448 * curproxy: the proxy affected
449 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100450 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100451 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100452 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100453void 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 +0100454{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100455 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100456 char *arg = NULL; /* start pointer for args */
457 char *var = NULL; /* start pointer for vars */
458 int arg_len = 0;
459 int var_len = 0;
460 int cformat; /* current token format */
461 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100462 struct logformat_node *tmplf, *back;
463
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100464 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200465 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200466
William Lallemand723b73a2012-02-08 16:37:49 +0100467 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100468 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100469 LIST_DEL(&tmplf->list);
470 free(tmplf);
471 }
472
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100473 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100474 pformat = cformat;
475
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100476 if (!*str)
477 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100478
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100479 /* The prinicple of the two-step state machine below is to first detect a change, and
480 * second have all common paths processed at one place. The common paths are the ones
481 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
482 * We use the common LF_INIT state to dispatch to the different final states.
483 */
484 switch (pformat) {
485 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100486 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100487 arg_len = var_len = 0;
488 if (*str == '{') { // optional argument
489 cformat = LF_STARG;
490 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100491 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100492 else if (*str == '[') {
493 cformat = LF_STEXPR;
494 var = str + 1; // store expr in variable name
495 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100496 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100497 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100498 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100499 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100500 else if (*str == '%')
501 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100502 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100503 /* single '%' followed by blank or digit, send them both */
504 cformat = LF_TEXT;
505 pformat = LF_TEXT; /* finally we include the previous char as well */
506 sp = str - 1; /* send both the '%' and the current char */
507 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",
508 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
509
510 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100511 else
512 cformat = LF_INIT; // handle other cases of litterals
513 break;
514
515 case LF_STARG: // text immediately following '%{'
516 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100517 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100518 arg_len = str - arg;
519 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100520 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100521 break;
522
523 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100524 if (*str == '[') {
525 cformat = LF_STEXPR;
526 var = str + 1; // store expr in variable name
527 break;
528 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100529 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100530 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100531 var = str;
532 break;
533 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200534 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
535 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100536 cformat = LF_INIT;
537 break;
538
Willy Tarreauc8368452012-12-21 00:09:23 +0100539 case LF_STEXPR: // text immediately following '%['
540 if (*str == ']') { // end of arg
541 cformat = LF_EDEXPR;
542 var_len = str - var;
543 *str = 0; // needed for parsing the expression
544 }
545 break;
546
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100547 case LF_VAR: // text part of a variable name
548 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100549 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100550 cformat = LF_INIT; // not variable name anymore
551 break;
552
Willy Tarreauc8368452012-12-21 00:09:23 +0100553 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100554 cformat = LF_INIT;
555 }
556
557 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
558 switch (*str) {
559 case '%': cformat = LF_STARTVAR; break;
560 case ' ': cformat = LF_SEPARATOR; break;
561 case 0 : cformat = LF_END; break;
562 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100563 }
564 }
565
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100566 if (cformat != pformat || pformat == LF_SEPARATOR) {
567 switch (pformat) {
568 case LF_VAR:
569 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
570 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100571 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100572 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100573 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100574 case LF_TEXT:
575 case LF_SEPARATOR:
576 add_to_logformat_list(sp, str, pformat, list_format);
577 break;
578 }
579 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100580 }
581 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100582
Willy Tarreauc8368452012-12-21 00:09:23 +0100583 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200584 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
585 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
586 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100587
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100588 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100589}
590
Willy Tarreaubaaee002006-06-26 02:48:02 +0200591/*
592 * Displays the message on stderr with the date and pid. Overrides the quiet
593 * mode during startup.
594 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200595void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200596{
597 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200598 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200599
600 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
601 va_start(argp, fmt);
602
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200603 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200604 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200605 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200606 vfprintf(stderr, fmt, argp);
607 fflush(stderr);
608 va_end(argp);
609 }
610}
611
612
613/*
614 * Displays the message on stderr with the date and pid.
615 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200616void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200617{
618 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200619 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200620
621 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
622 va_start(argp, fmt);
623
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200624 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200625 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200626 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200627 vfprintf(stderr, fmt, argp);
628 fflush(stderr);
629 va_end(argp);
630 }
631}
632
633/*
634 * Displays the message on <out> only if quiet mode is not set.
635 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200636void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637{
638 va_list argp;
639
640 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
641 va_start(argp, fmt);
642 vfprintf(out, fmt, argp);
643 fflush(out);
644 va_end(argp);
645 }
646}
647
648/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200649 * returns log format for <fmt> or -1 if not found.
650 */
651int get_log_format(const char *fmt)
652{
653 int format;
654
655 format = LOG_FORMATS - 1;
656 while (format >= 0 && strcmp(log_formats[format], fmt))
657 format--;
658
659 return format;
660}
661
662/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663 * returns log level for <lev> or -1 if not found.
664 */
665int get_log_level(const char *lev)
666{
667 int level;
668
669 level = NB_LOG_LEVELS - 1;
670 while (level >= 0 && strcmp(log_levels[level], lev))
671 level--;
672
673 return level;
674}
675
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676/*
677 * returns log facility for <fac> or -1 if not found.
678 */
679int get_log_facility(const char *fac)
680{
681 int facility;
682
683 facility = NB_LOG_FACILITIES - 1;
684 while (facility >= 0 && strcmp(log_facilities[facility], fac))
685 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100686
Willy Tarreaubaaee002006-06-26 02:48:02 +0200687 return facility;
688}
689
William Lallemanda1cc3812012-02-08 16:38:44 +0100690/*
691 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200692 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100693 *
694 * Return the adress of the \0 character, or NULL on error
695 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100696char *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 +0100697{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100698 if (size < 2)
699 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100700
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100701 if (node->options & LOG_OPT_QUOTE) {
702 *(dst++) = '"';
703 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100704 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100705
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100706 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100707 if (++len > size)
708 len = size;
709 len = strlcpy2(dst, src, len);
710
711 size -= len;
712 dst += len;
713 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100714 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
715 if (size < 2)
716 return NULL;
717 *(dst++) = '-';
718 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100719
720 if (node->options & LOG_OPT_QUOTE) {
721 if (size < 2)
722 return NULL;
723 *(dst++) = '"';
724 }
725
726 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100727 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100728}
729
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100730static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
731{
732 return lf_text_len(dst, src, size, size, node);
733}
734
William Lallemand5f232402012-04-05 18:02:55 +0200735/*
736 * Write a IP adress to the log string
737 * +X option write in hexadecimal notation, most signifant byte on the left
738 */
739char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
740{
741 char *ret = dst;
742 int iret;
743 char pn[INET6_ADDRSTRLEN];
744
745 if (node->options & LOG_OPT_HEXA) {
746 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
747 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
748 if (iret < 0 || iret > size)
749 return NULL;
750 ret += iret;
751 } else {
752 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
753 ret = lf_text(dst, pn, size, node);
754 if (ret == NULL)
755 return NULL;
756 }
757 return ret;
758}
759
760/*
761 * Write a port to the log
762 * +X option write in hexadecimal notation, most signifant byte on the left
763 */
764char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
765{
766 char *ret = dst;
767 int iret;
768
769 if (node->options & LOG_OPT_HEXA) {
770 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
771 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
772 if (iret < 0 || iret > size)
773 return NULL;
774 ret += iret;
775 } else {
776 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
777 if (ret == NULL)
778 return NULL;
779 }
780 return ret;
781}
782
Dragan Dosen1322d092015-09-22 16:05:32 +0200783char *lf_host_tag_pid(char *dst, const char *format, const char *hostname, const char *log_tag, int pid, size_t size)
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200784{
785 char *ret = dst;
786 int iret;
787
Dragan Dosen1322d092015-09-22 16:05:32 +0200788 iret = snprintf(dst, size, format, hostname, log_tag, pid);
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200789 if (iret < 0 || iret > size)
790 return NULL;
791 ret += iret;
792
793 return ret;
794}
795
Dragan Dosen1322d092015-09-22 16:05:32 +0200796/* Re-generate time-based part of the syslog header in RFC3164 format at
797 * the beginning of logheader once a second and return the pointer to the
798 * first character after it.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100799 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200800static char *update_log_hdr(const time_t time)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100802 static long tvsec;
803 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200804
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200805 if (unlikely(time != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200806 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200807 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100808 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200809
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200810 tvsec = time;
Willy Tarreaufe944602007-10-25 10:34:16 +0200811 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812
Dragan Dosen59cee972015-09-19 22:09:02 +0200813 hdr_len = snprintf(logheader, global.max_syslog_len,
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200814 "<<<<>%s %2d %02d:%02d:%02d ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200815 monthname[tm.tm_mon],
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200816 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 /* WARNING: depending upon implementations, snprintf may return
818 * either -1 or the number of bytes that would be needed to store
819 * the total message. In both cases, we must adjust it.
820 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200821 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
822 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200823
Dragan Dosen59cee972015-09-19 22:09:02 +0200824 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200825 }
826
Willy Tarreau094af4e2015-01-07 15:03:42 +0100827 dataptr[0] = 0; // ensure we get rid of any previous attempt
828
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200829 return dataptr;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100830}
831
Dragan Dosen1322d092015-09-22 16:05:32 +0200832/* Re-generate time-based part of the syslog header in RFC5424 format at
833 * the beginning of logheader_rfc5424 once a second and return the pointer
834 * to the first character after it.
835 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200836static char *update_log_hdr_rfc5424(const time_t time)
Dragan Dosen1322d092015-09-22 16:05:32 +0200837{
838 static long tvsec;
839 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
840
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200841 if (unlikely(time != tvsec || dataptr == NULL)) {
Dragan Dosen1322d092015-09-22 16:05:32 +0200842 /* this string is rebuild only once a second */
843 struct tm tm;
844 int hdr_len;
845
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200846 tvsec = time;
Dragan Dosen1322d092015-09-22 16:05:32 +0200847 get_localtime(tvsec, &tm);
848
849 hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len,
850 "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%s ",
851 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
852 tm.tm_hour, tm.tm_min, tm.tm_sec,
853 localtimezone);
854 /* WARNING: depending upon implementations, snprintf may return
855 * either -1 or the number of bytes that would be needed to store
856 * the total message. In both cases, we must adjust it.
857 */
858 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
859 hdr_len = global.max_syslog_len;
860
861 dataptr = logheader_rfc5424 + hdr_len;
862 }
863
864 dataptr[0] = 0; // ensure we get rid of any previous attempt
865
866 return dataptr;
867}
868
William Lallemand2a4a44f2012-02-06 16:00:33 +0100869/*
Dragan Dosen59cee972015-09-19 22:09:02 +0200870 * This function sends the syslog message using a printf format string. It
871 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100872 */
873void send_log(struct proxy *p, int level, const char *format, ...)
874{
875 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100876 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100877
Willy Tarreau8c97ab52015-01-15 16:29:53 +0100878 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100879 return;
880
William Lallemand2a4a44f2012-02-06 16:00:33 +0100881 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +0200882 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +0200883 if (data_len < 0 || data_len > global.max_syslog_len)
884 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200885 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100886
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200887 __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100888}
889
890/*
891 * This function sends a syslog message.
892 * It doesn't care about errors nor does it report them.
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200893 * It overrides the last byte of the message vector with an LF character.
894 * The arguments <sd> and <sd_size> are used for the structured-data part
895 * in RFC5424 formatted syslog messages.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100896 */
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200897void __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 +0100898{
Dragan Dosen609ac2a2015-09-16 18:25:42 +0200899 static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
900 static struct msghdr msghdr = {
901 .msg_iov = iovec,
902 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
903 };
William Lallemand2a4a44f2012-02-06 16:00:33 +0100904 static int logfdunix = -1; /* syslog to AF_UNIX socket */
905 static int logfdinet = -1; /* syslog to AF_INET socket */
906 static char *dataptr = NULL;
907 int fac_level;
908 struct list *logsrvs = NULL;
909 struct logsrv *tmp = NULL;
910 int nblogger;
911 char *log_ptr;
Dragan Dosen1322d092015-09-22 16:05:32 +0200912 char *hdr, *hdr_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +0200913 size_t hdr_size;
Dragan Dosen1322d092015-09-22 16:05:32 +0200914 struct chunk *htp;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200915 time_t time = date.tv_sec;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100916
917 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918
919 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200920 if (!LIST_ISEMPTY(&global.logsrvs)) {
921 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922 }
923 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200924 if (!LIST_ISEMPTY(&p->logsrvs)) {
925 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100927 }
928
William Lallemand0f99e342011-10-12 17:50:54 +0200929 if (!logsrvs)
930 return;
931
Robert Tsai81ae1952007-12-05 10:47:29 +0100932 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200933 nblogger = 0;
934 list_for_each_entry(tmp, logsrvs, list) {
935 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100936 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100937 &logfdunix : &logfdinet;
938 int sent;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200939 int maxlen;
Dragan Dosen59cee972015-09-19 22:09:02 +0200940 int hdr_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +0200941 int htp_max = 0;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200942 int sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +0200943 int max = 1;
Willy Tarreau18324f52014-06-27 18:10:07 +0200944 char backup;
Robert Tsai81ae1952007-12-05 10:47:29 +0100945
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200946 nblogger++;
947
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200949 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100951
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200952 if (unlikely(*plogfd < 0)) {
953 /* socket not successfully initialized yet */
954 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
955
956 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
957 Alert("socket for logger #%d failed: %s (errno=%d)\n",
958 nblogger, strerror(errno), errno);
959 continue;
960 }
961 /* we don't want to receive anything on this socket */
962 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
963 /* does nothing under Linux, maybe needed for others */
964 shutdown(*plogfd, SHUT_RD);
965 }
966
Dragan Dosen1322d092015-09-22 16:05:32 +0200967 switch (logsrv->format) {
968 case LOG_FORMAT_RFC3164:
969 hdr = logheader;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200970 hdr_ptr = update_log_hdr(time);
Dragan Dosen1322d092015-09-22 16:05:32 +0200971 htp = &p->log_htp;
972 break;
973
974 case LOG_FORMAT_RFC5424:
975 hdr = logheader_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200976 hdr_ptr = update_log_hdr_rfc5424(time);
Dragan Dosen1322d092015-09-22 16:05:32 +0200977 htp = &p->log_htp_rfc5424;
Dragan Dosen0b85ece2015-09-25 19:17:44 +0200978 sd_max = sd_size; /* the SD part allowed only in RFC5424 */
Dragan Dosen1322d092015-09-22 16:05:32 +0200979 break;
980
981 default:
982 continue; /* must never happen */
983 }
984
985 hdr_size = hdr_ptr - hdr;
986
Willy Tarreaubaaee002006-06-26 02:48:02 +0200987 /* For each target, we may have a different facility.
988 * We can also have a different log level for each message.
989 * This induces variations in the message header length.
990 * Since we don't want to recompute it each time, nor copy it every
991 * time, we only change the facility in the pre-computed header,
992 * and we change the pointer to the header accordingly.
993 */
William Lallemand0f99e342011-10-12 17:50:54 +0200994 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen1322d092015-09-22 16:05:32 +0200995 hdr_ptr = hdr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996 do {
Dragan Dosen59cee972015-09-19 22:09:02 +0200997 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200998 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +0200999 hdr_ptr--;
Dragan Dosen1322d092015-09-22 16:05:32 +02001000 } while (fac_level && hdr_ptr > hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001001 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +01001002
Dragan Dosen59cee972015-09-19 22:09:02 +02001003 log_ptr = dataptr;
Willy Tarreau18324f52014-06-27 18:10:07 +02001004
Dragan Dosen1322d092015-09-22 16:05:32 +02001005 hdr_max = hdr_size - (hdr_ptr - hdr);
Dragan Dosen59cee972015-09-19 22:09:02 +02001006
1007 if (unlikely(hdr_size >= logsrv->maxlen)) {
1008 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001009 sd_max = 0;
Dragan Dosen59cee972015-09-19 22:09:02 +02001010 goto send;
1011 }
1012
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001013 maxlen = logsrv->maxlen - hdr_max;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001014
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001015 if (unlikely(htp->len >= maxlen)) {
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001016 htp_max = maxlen - 1;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001017 sd_max = 0;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001018 goto send;
1019 }
1020
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001021 htp_max = htp->len;
1022 maxlen -= htp_max;
Dragan Dosen59cee972015-09-19 22:09:02 +02001023
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001024 if (sd_max >= maxlen) {
1025 sd_max = maxlen - 1;
1026 goto send;
1027 }
Dragan Dosen59cee972015-09-19 22:09:02 +02001028
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001029 max = MIN(size, maxlen - sd_max);
1030 log_ptr += max - 1;
Dragan Dosen59cee972015-09-19 22:09:02 +02001031send:
Willy Tarreau18324f52014-06-27 18:10:07 +02001032 /* insert a \n at the end of the message, but save what was
1033 * there first because we could have different max lengths
1034 * for different log targets.
1035 */
Dragan Dosen59cee972015-09-19 22:09:02 +02001036 backup = *log_ptr;
1037 *log_ptr = '\n';
Willy Tarreau18324f52014-06-27 18:10:07 +02001038
Dragan Dosen59cee972015-09-19 22:09:02 +02001039 iovec[0].iov_base = hdr_ptr;
1040 iovec[0].iov_len = hdr_max;
Dragan Dosen1322d092015-09-22 16:05:32 +02001041 iovec[1].iov_base = htp->str;
Dragan Dosen68d2e3a2015-09-19 22:35:44 +02001042 iovec[1].iov_len = htp_max;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001043 iovec[2].iov_base = sd;
1044 iovec[2].iov_len = sd_max;
1045 iovec[3].iov_base = dataptr;
1046 iovec[3].iov_len = max;
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001047
1048 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
1049 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
1050
1051 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +02001052
Dragan Dosen59cee972015-09-19 22:09:02 +02001053 *log_ptr = backup;
Willy Tarreau18324f52014-06-27 18:10:07 +02001054
Robert Tsai81ae1952007-12-05 10:47:29 +01001055 if (sent < 0) {
Dragan Dosen609ac2a2015-09-16 18:25:42 +02001056 Alert("sendmsg logger #%d failed: %s (errno=%d)\n",
Robert Tsai81ae1952007-12-05 10:47:29 +01001057 nblogger, strerror(errno), errno);
1058 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001059 }
1060}
1061
William Lallemandbddd4fd2012-02-27 11:23:10 +01001062extern fd_set hdr_encode_map[];
1063extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001064extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +01001065
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066
Willy Tarreauc89ccb62012-04-05 21:18:22 +02001067const 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 +01001068const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
1069 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
1070 Set-cookie Updated, unknown, unknown */
1071
William Lallemand1d705562012-03-12 12:46:41 +01001072/*
1073 * try to write a character if there is enough space, or goto out
1074 */
William Lallemandbddd4fd2012-02-27 11:23:10 +01001075#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +01001076 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +01001077 *(tmplog++) = (x); \
1078 } else { \
1079 goto out; \
1080 } \
1081 } while(0)
1082
William Lallemand1d705562012-03-12 12:46:41 +01001083
Willy Tarreaudf974472012-12-28 02:44:01 +01001084/* Builds a log line in <dst> based on <list_format>, and stops before reaching
1085 * <maxsize> characters. Returns the size of the output string in characters,
1086 * not counting the trailing zero which is always added if the resulting size
1087 * is not zero.
1088 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001089int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001090{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001091 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001092 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +01001093 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001094 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001095 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001096 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001097 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001098 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001099 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +02001100 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001101 int t_request;
1102 int hdr;
1103 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001104 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +01001105 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +01001106 char *ret;
1107 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001108 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001109
William Lallemandbddd4fd2012-02-27 11:23:10 +01001110 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001111
William Lallemandbddd4fd2012-02-27 11:23:10 +01001112 t_request = -1;
1113 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1114 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1115
William Lallemand1d705562012-03-12 12:46:41 +01001116 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001117
William Lallemandbddd4fd2012-02-27 11:23:10 +01001118 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +01001119 if (LIST_ISEMPTY(list_format))
1120 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001121
William Lallemand1d705562012-03-12 12:46:41 +01001122 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001123 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001124 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001125 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001126 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001127
Willy Tarreauc8368452012-12-21 00:09:23 +01001128 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001129 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001130 if (!last_isspace) {
1131 LOGCHAR(' ');
1132 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001133 }
1134 break;
1135
William Lallemand1d705562012-03-12 12:46:41 +01001136 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001137 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001138 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001139 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001140 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001141 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001142 last_isspace = 0;
1143 break;
1144
Willy Tarreauc8368452012-12-21 00:09:23 +01001145 case LOG_FMT_EXPR: // sample expression, may be request or response
1146 key = NULL;
1147 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001148 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 +01001149 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001150 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 +01001151 if (tmp->options & LOG_OPT_HTTP)
1152 ret = encode_chunk(tmplog, dst + maxsize,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001153 '%', http_encode_map, key ? &key->data.u.str : &empty);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001154 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001155 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 +01001156 if (ret == 0)
1157 goto out;
1158 tmplog = ret;
1159 last_isspace = 0;
1160 break;
1161
Willy Tarreau2beef582012-12-20 17:22:52 +01001162 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001163 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001164 if (conn)
1165 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1166 else
1167 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001168 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001169 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001170 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001171 last_isspace = 0;
1172 break;
1173
Willy Tarreau2beef582012-12-20 17:22:52 +01001174 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001175 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001176 if (conn) {
1177 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001178 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001179 } else {
1180 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1181 dst + maxsize - tmplog, tmp);
1182 }
William Lallemand5f232402012-04-05 18:02:55 +02001183 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001184 else
1185 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1186
William Lallemand5f232402012-04-05 18:02:55 +02001187 if (ret == NULL)
1188 goto out;
1189 tmplog = ret;
1190 last_isspace = 0;
1191 break;
1192
Willy Tarreau2beef582012-12-20 17:22:52 +01001193 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001194 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001195 if (conn) {
1196 conn_get_to_addr(conn);
1197 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1198 }
1199 else
1200 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1201
William Lallemand1d705562012-03-12 12:46:41 +01001202 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001203 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001204 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001205 last_isspace = 0;
1206 break;
1207
Willy Tarreau2beef582012-12-20 17:22:52 +01001208 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001209 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001210 if (conn) {
1211 conn_get_to_addr(conn);
1212 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001213 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001214 else
1215 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001216 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001217 else
1218 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1219
William Lallemand5f232402012-04-05 18:02:55 +02001220 if (ret == NULL)
1221 goto out;
1222 tmplog = ret;
1223 last_isspace = 0;
1224 break;
1225
Willy Tarreau2beef582012-12-20 17:22:52 +01001226 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001227 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001228 if (conn)
1229 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1230 else
1231 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1232
William Lallemand1d705562012-03-12 12:46:41 +01001233 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001234 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001235 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001236 last_isspace = 0;
1237 break;
1238
Willy Tarreau2beef582012-12-20 17:22:52 +01001239 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001240 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001241 if (conn)
1242 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1243 else
1244 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1245
William Lallemand5f232402012-04-05 18:02:55 +02001246 if (ret == NULL)
1247 goto out;
1248 tmplog = ret;
1249 last_isspace = 0;
1250 break;
1251
Willy Tarreau2beef582012-12-20 17:22:52 +01001252 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001253 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001254 if (conn)
1255 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1256 else
1257 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1258
William Lallemand5f232402012-04-05 18:02:55 +02001259 if (ret == NULL)
1260 goto out;
1261 tmplog = ret;
1262 last_isspace = 0;
1263 break;
1264
Willy Tarreau2beef582012-12-20 17:22:52 +01001265 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001266 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001267 if (conn)
1268 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1269 else
1270 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1271
William Lallemand1d705562012-03-12 12:46:41 +01001272 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001273 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001274 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001275 last_isspace = 0;
1276 break;
1277
William Lallemand1d705562012-03-12 12:46:41 +01001278 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001279 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001280 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1281 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001282 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001283 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001284 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001285 last_isspace = 0;
1286 break;
1287
William Lallemand1d705562012-03-12 12:46:41 +01001288 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001289 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001290 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001291 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001292 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001293 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001294 last_isspace = 0;
1295 break;
1296
Yuxans Yao4e25b012012-10-19 10:36:09 +08001297 case LOG_FMT_DATELOCAL: // %Tl
1298 get_localtime(s->logs.accept_date.tv_sec, &tm);
1299 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1300 if (ret == NULL)
1301 goto out;
1302 tmplog = ret;
1303 last_isspace = 0;
1304 break;
1305
William Lallemand5f232402012-04-05 18:02:55 +02001306 case LOG_FMT_TS: // %Ts
1307 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1308 if (tmp->options & LOG_OPT_HEXA) {
1309 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1310 if (iret < 0 || iret > dst + maxsize - tmplog)
1311 goto out;
1312 last_isspace = 0;
1313 tmplog += iret;
1314 } else {
1315 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1316 if (ret == NULL)
1317 goto out;
1318 tmplog = ret;
1319 last_isspace = 0;
1320 }
1321 break;
1322
William Lallemand1d705562012-03-12 12:46:41 +01001323 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001324 if (tmp->options & LOG_OPT_HEXA) {
1325 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1326 if (iret < 0 || iret > dst + maxsize - tmplog)
1327 goto out;
1328 last_isspace = 0;
1329 tmplog += iret;
1330 } else {
1331 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001332 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001333 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1334 tmplog, 4);
1335 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001336 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001337 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001338 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001339 }
1340 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001341
William Lallemand1d705562012-03-12 12:46:41 +01001342 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001343 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001344 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001345 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001346 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001347 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001348 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001349 break;
1350
Willy Tarreau773d65f2012-10-12 14:56:11 +02001351 case LOG_FMT_FRONTEND_XPRT: // %ft
1352 src = fe->id;
1353 if (tmp->options & LOG_OPT_QUOTE)
1354 LOGCHAR('"');
1355 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1356 if (iret == 0)
1357 goto out;
1358 tmplog += iret;
1359#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001360 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001361 LOGCHAR('~');
1362#endif
1363 if (tmp->options & LOG_OPT_QUOTE)
1364 LOGCHAR('"');
1365 last_isspace = 0;
1366 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001367#ifdef USE_OPENSSL
1368 case LOG_FMT_SSL_CIPHER: // %sslc
1369 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001370 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001371 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001372 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001373 src = ssl_sock_get_cipher_name(conn);
1374 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001375 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1376 if (ret == NULL)
1377 goto out;
1378 tmplog = ret;
1379 last_isspace = 0;
1380 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001381
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001382 case LOG_FMT_SSL_VERSION: // %sslv
1383 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001384 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001385 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001386 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001387 src = ssl_sock_get_proto_version(conn);
1388 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001389 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1390 if (ret == NULL)
1391 goto out;
1392 tmplog = ret;
1393 last_isspace = 0;
1394 break;
1395#endif
William Lallemand1d705562012-03-12 12:46:41 +01001396 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001397 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001398 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001399 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001400 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001401 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001402 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001403 break;
1404
William Lallemand1d705562012-03-12 12:46:41 +01001405 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001406 switch (obj_type(s->target)) {
1407 case OBJ_TYPE_SERVER:
1408 src = objt_server(s->target)->id;
1409 break;
1410 case OBJ_TYPE_APPLET:
1411 src = objt_applet(s->target)->name;
1412 break;
1413 default:
1414 src = "<NOSRV>";
1415 break;
1416 }
William Lallemand5f232402012-04-05 18:02:55 +02001417 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001418 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001419 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001420 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001421 last_isspace = 0;
1422 break;
1423
William Lallemand1d705562012-03-12 12:46:41 +01001424 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001425 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001426 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001427 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001428 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001429 last_isspace = 0;
1430 break;
1431
William Lallemand1d705562012-03-12 12:46:41 +01001432 case LOG_FMT_TW: // %Tw
1433 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001434 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001435 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001436 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001437 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001438 last_isspace = 0;
1439 break;
1440
William Lallemand1d705562012-03-12 12:46:41 +01001441 case LOG_FMT_TC: // %Tc
1442 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001443 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001444 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001445 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001446 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001447 last_isspace = 0;
1448 break;
1449
William Lallemand1d705562012-03-12 12:46:41 +01001450 case LOG_FMT_TR: // %Tr
1451 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001452 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001453 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001454 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001455 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001456 last_isspace = 0;
1457 break;
1458
William Lallemand1d705562012-03-12 12:46:41 +01001459 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001460 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001461 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001462 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001463 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001464 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001465 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001466 last_isspace = 0;
1467 break;
1468
Willy Tarreau2beef582012-12-20 17:22:52 +01001469 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001470 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001471 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001472 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001473 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001474 last_isspace = 0;
1475 break;
1476
William Lallemand1d705562012-03-12 12:46:41 +01001477 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001478 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001479 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001480 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001481 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001482 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001483 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001484 last_isspace = 0;
1485 break;
1486
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001487 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001488 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1489 if (ret == NULL)
1490 goto out;
1491 tmplog = ret;
1492 last_isspace = 0;
1493 break;
1494
Willy Tarreau2beef582012-12-20 17:22:52 +01001495 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001496 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001497 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001498 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001499 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001500 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001501 last_isspace = 0;
1502 break;
1503
Willy Tarreau2beef582012-12-20 17:22:52 +01001504 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001505 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001506 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001507 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001508 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001509 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001510 last_isspace = 0;
1511 break;
1512
William Lallemand1d705562012-03-12 12:46:41 +01001513 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001514 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1515 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001516 *tmplog = '\0';
1517 last_isspace = 0;
1518 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001519
William Lallemand1d705562012-03-12 12:46:41 +01001520 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001521 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1522 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001523 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1524 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001525 last_isspace = 0;
1526 break;
1527
William Lallemand1d705562012-03-12 12:46:41 +01001528 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001529 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001530 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001531 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001532 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001533 last_isspace = 0;
1534 break;
1535
William Lallemand1d705562012-03-12 12:46:41 +01001536 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001537 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001538 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001539 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001540 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001541 last_isspace = 0;
1542 break;
1543
William Lallemand1d705562012-03-12 12:46:41 +01001544 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001545 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001546 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001547 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001548 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001549 last_isspace = 0;
1550 break;
1551
William Lallemand1d705562012-03-12 12:46:41 +01001552 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001553 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001554 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001555 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001556 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001557 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001558 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001559 last_isspace = 0;
1560 break;
1561
William Lallemand1d705562012-03-12 12:46:41 +01001562 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001563 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001564 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001565 ret = ltoa_o((s->si[1].conn_retries>0) ?
1566 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001567 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001568 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001569 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001570 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001571 last_isspace = 0;
1572 break;
1573
William Lallemand1d705562012-03-12 12:46:41 +01001574 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001575 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001576 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001577 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001578 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001579 last_isspace = 0;
1580 break;
1581
William Lallemand1d705562012-03-12 12:46:41 +01001582 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001583 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001584 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001585 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001586 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001587 last_isspace = 0;
1588 break;
1589
William Lallemand1d705562012-03-12 12:46:41 +01001590 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001591 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001592 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001593 if (tmp->options & LOG_OPT_QUOTE)
1594 LOGCHAR('"');
1595 LOGCHAR('{');
1596 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1597 if (hdr)
1598 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001599 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001600 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001601 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001602 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001603 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001604 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001605 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001606 }
1607 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001608 if (tmp->options & LOG_OPT_QUOTE)
1609 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001610 last_isspace = 0;
1611 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001612 break;
1613
William Lallemand1d705562012-03-12 12:46:41 +01001614 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001615 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001616 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001617 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1618 if (hdr > 0)
1619 LOGCHAR(' ');
1620 if (tmp->options & LOG_OPT_QUOTE)
1621 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001622 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001623 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001624 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001625 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001626 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001627 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001628 } else if (!(tmp->options & LOG_OPT_QUOTE))
1629 LOGCHAR('-');
1630 if (tmp->options & LOG_OPT_QUOTE)
1631 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001632 last_isspace = 0;
1633 }
1634 }
1635 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001636
William Lallemand1d705562012-03-12 12:46:41 +01001637
1638 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001639 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001640 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001641 if (tmp->options & LOG_OPT_QUOTE)
1642 LOGCHAR('"');
1643 LOGCHAR('{');
1644 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1645 if (hdr)
1646 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001647 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001648 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001649 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001650 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001651 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001652 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001653 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001654 }
1655 LOGCHAR('}');
1656 last_isspace = 0;
1657 if (tmp->options & LOG_OPT_QUOTE)
1658 LOGCHAR('"');
1659 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001660 break;
1661
William Lallemand1d705562012-03-12 12:46:41 +01001662 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001663 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001664 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001665 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1666 if (hdr > 0)
1667 LOGCHAR(' ');
1668 if (tmp->options & LOG_OPT_QUOTE)
1669 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001670 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001671 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001672 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001673 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001674 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001675 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001676 } else if (!(tmp->options & LOG_OPT_QUOTE))
1677 LOGCHAR('-');
1678 if (tmp->options & LOG_OPT_QUOTE)
1679 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001680 last_isspace = 0;
1681 }
1682 }
1683 break;
1684
William Lallemand1d705562012-03-12 12:46:41 +01001685 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001686 /* Request */
1687 if (tmp->options & LOG_OPT_QUOTE)
1688 LOGCHAR('"');
1689 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001690 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001691 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001692 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001693 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001694 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001695 if (tmp->options & LOG_OPT_QUOTE)
1696 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001697 last_isspace = 0;
1698 break;
William Lallemand5f232402012-04-05 18:02:55 +02001699
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001700 case LOG_FMT_HTTP_PATH: // %HP
1701 uri = txn->uri ? txn->uri : "<BADREQ>";
1702
Willy Tarreaub7636d12015-06-17 19:58:02 +02001703 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001704 LOGCHAR('"');
1705
1706 end = uri + strlen(uri);
1707 // look for the first whitespace character
1708 while (uri < end && !HTTP_IS_SPHT(*uri))
1709 uri++;
1710
1711 // keep advancing past multiple spaces
1712 while (uri < end && HTTP_IS_SPHT(*uri)) {
1713 uri++; nspaces++;
1714 }
1715
1716 // look for first space or question mark after url
1717 spc = uri;
1718 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
1719 spc++;
1720
1721 if (!txn->uri || nspaces == 0) {
1722 chunk.str = "<BADREQ>";
1723 chunk.len = strlen("<BADREQ>");
1724 } else {
1725 chunk.str = uri;
1726 chunk.len = spc - uri;
1727 }
1728
1729 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1730 if (ret == NULL || *ret != '\0')
1731 goto out;
1732
1733 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001734 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001735 LOGCHAR('"');
1736
1737 last_isspace = 0;
1738 break;
1739
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001740 case LOG_FMT_HTTP_QUERY: // %HQ
1741 if (tmp->options & LOG_OPT_QUOTE)
1742 LOGCHAR('"');
1743
1744 if (!txn->uri) {
1745 chunk.str = "<BADREQ>";
1746 chunk.len = strlen("<BADREQ>");
1747 } else {
1748 uri = txn->uri;
1749 end = uri + strlen(uri);
1750 // look for the first question mark
1751 while (uri < end && *uri != '?')
1752 uri++;
1753
1754 qmark = uri;
1755 // look for first space or question mark after url
1756 while (uri < end && !HTTP_IS_SPHT(*uri))
1757 uri++;
1758
1759 chunk.str = qmark;
1760 chunk.len = uri - qmark;
1761 }
1762
1763 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1764 if (ret == NULL || *ret != '\0')
1765 goto out;
1766
1767 tmplog = ret;
1768 if (tmp->options & LOG_OPT_QUOTE)
1769 LOGCHAR('"');
1770
1771 last_isspace = 0;
1772 break;
1773
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001774 case LOG_FMT_HTTP_URI: // %HU
1775 uri = txn->uri ? txn->uri : "<BADREQ>";
1776
Willy Tarreaub7636d12015-06-17 19:58:02 +02001777 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001778 LOGCHAR('"');
1779
1780 end = uri + strlen(uri);
1781 // look for the first whitespace character
1782 while (uri < end && !HTTP_IS_SPHT(*uri))
1783 uri++;
1784
1785 // keep advancing past multiple spaces
1786 while (uri < end && HTTP_IS_SPHT(*uri)) {
1787 uri++; nspaces++;
1788 }
1789
1790 // look for first space after url
1791 spc = uri;
1792 while (spc < end && !HTTP_IS_SPHT(*spc))
1793 spc++;
1794
1795 if (!txn->uri || nspaces == 0) {
1796 chunk.str = "<BADREQ>";
1797 chunk.len = strlen("<BADREQ>");
1798 } else {
1799 chunk.str = uri;
1800 chunk.len = spc - uri;
1801 }
1802
1803 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1804 if (ret == NULL || *ret != '\0')
1805 goto out;
1806
1807 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001808 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001809 LOGCHAR('"');
1810
1811 last_isspace = 0;
1812 break;
1813
1814 case LOG_FMT_HTTP_METHOD: // %HM
1815 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001816 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001817 LOGCHAR('"');
1818
1819 end = uri + strlen(uri);
1820 // look for the first whitespace character
1821 spc = uri;
1822 while (spc < end && !HTTP_IS_SPHT(*spc))
1823 spc++;
1824
1825 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
1826 chunk.str = "<BADREQ>";
1827 chunk.len = strlen("<BADREQ>");
1828 } else {
1829 chunk.str = uri;
1830 chunk.len = spc - uri;
1831 }
1832
1833 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1834 if (ret == NULL || *ret != '\0')
1835 goto out;
1836
1837 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001838 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001839 LOGCHAR('"');
1840
1841 last_isspace = 0;
1842 break;
1843
1844 case LOG_FMT_HTTP_VERSION: // %HV
1845 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001846 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001847 LOGCHAR('"');
1848
1849 end = uri + strlen(uri);
1850 // look for the first whitespace character
1851 while (uri < end && !HTTP_IS_SPHT(*uri))
1852 uri++;
1853
1854 // keep advancing past multiple spaces
1855 while (uri < end && HTTP_IS_SPHT(*uri)) {
1856 uri++; nspaces++;
1857 }
1858
1859 // look for the next whitespace character
1860 while (uri < end && !HTTP_IS_SPHT(*uri))
1861 uri++;
1862
1863 // keep advancing past multiple spaces
1864 while (uri < end && HTTP_IS_SPHT(*uri))
1865 uri++;
1866
1867 if (!txn->uri || nspaces == 0) {
1868 chunk.str = "<BADREQ>";
1869 chunk.len = strlen("<BADREQ>");
1870 } else if (uri == end) {
1871 chunk.str = "HTTP/0.9";
1872 chunk.len = strlen("HTTP/0.9");
1873 } else {
1874 chunk.str = uri;
1875 chunk.len = end - uri;
1876 }
1877
1878 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1879 if (ret == NULL || *ret != '\0')
1880 goto out;
1881
1882 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001883 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001884 LOGCHAR('"');
1885
1886 last_isspace = 0;
1887 break;
1888
William Lallemand5f232402012-04-05 18:02:55 +02001889 case LOG_FMT_COUNTER: // %rt
1890 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001891 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001892 if (iret < 0 || iret > dst + maxsize - tmplog)
1893 goto out;
1894 last_isspace = 0;
1895 tmplog += iret;
1896 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001897 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001898 if (ret == NULL)
1899 goto out;
1900 tmplog = ret;
1901 last_isspace = 0;
1902 }
1903 break;
1904
Willy Tarreau7346acb2014-08-28 15:03:15 +02001905 case LOG_FMT_LOGCNT: // %lc
1906 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001907 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001908 if (iret < 0 || iret > dst + maxsize - tmplog)
1909 goto out;
1910 last_isspace = 0;
1911 tmplog += iret;
1912 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001913 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001914 if (ret == NULL)
1915 goto out;
1916 tmplog = ret;
1917 last_isspace = 0;
1918 }
1919 break;
1920
William Lallemand5f232402012-04-05 18:02:55 +02001921 case LOG_FMT_HOSTNAME: // %H
1922 src = hostname;
1923 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1924 if (ret == NULL)
1925 goto out;
1926 tmplog = ret;
1927 last_isspace = 0;
1928 break;
1929
1930 case LOG_FMT_PID: // %pid
1931 if (tmp->options & LOG_OPT_HEXA) {
1932 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1933 if (iret < 0 || iret > dst + maxsize - tmplog)
1934 goto out;
1935 last_isspace = 0;
1936 tmplog += iret;
1937 } else {
1938 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1939 if (ret == NULL)
1940 goto out;
1941 tmplog = ret;
1942 last_isspace = 0;
1943 }
1944 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001945
1946 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001947 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001948 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02001949 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001950 if (ret == NULL)
1951 goto out;
1952 tmplog = ret;
1953 last_isspace = 0;
1954 break;
1955
William Lallemandbddd4fd2012-02-27 11:23:10 +01001956 }
1957 }
1958
1959out:
William Lallemand1d705562012-03-12 12:46:41 +01001960 /* *tmplog is a unused character */
1961 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001962 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001963
Willy Tarreaubaaee002006-06-26 02:48:02 +02001964}
1965
William Lallemand1d705562012-03-12 12:46:41 +01001966/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001967 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01001968 * Will not log if the frontend has no log defined.
1969 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001970void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01001971{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001972 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01001973 int size, err, level;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001974 int sd_size = 0;
William Lallemand1d705562012-03-12 12:46:41 +01001975
1976 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001977 err = (s->flags & SF_REDISP) ||
1978 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
1979 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01001980 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02001981 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001982
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001983 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01001984 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001985
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001986 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01001987 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001988
Willy Tarreauabcd5142013-06-11 17:18:02 +02001989 if (s->logs.level) { /* loglevel was overridden */
1990 if (s->logs.level == -1) {
1991 s->logs.logwait = 0; /* logs disabled */
1992 return;
1993 }
1994 level = s->logs.level - 1;
1995 }
1996 else {
1997 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001998 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02001999 level = LOG_ERR;
2000 }
William Lallemand1d705562012-03-12 12:46:41 +01002001
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002002 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002003 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002004 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002005 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002006 }
2007
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002008 if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) {
2009 sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len,
2010 &sess->fe->logformat_sd);
2011 }
2012
Dragan Dosen59cee972015-09-19 22:09:02 +02002013 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01002014 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002015 sess->fe->log_count++;
Dragan Dosen0b85ece2015-09-25 19:17:44 +02002016 __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size);
William Lallemand1d705562012-03-12 12:46:41 +01002017 s->logs.logwait = 0;
2018 }
2019}
William Lallemandbddd4fd2012-02-27 11:23:10 +01002020
Willy Tarreaubaaee002006-06-26 02:48:02 +02002021/*
2022 * Local variables:
2023 * c-indent-level: 8
2024 * c-basic-offset: 8
2025 * End:
2026 */