blob: 866b11077af79c1374e81db9620eb903fb03710a [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
25
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020027#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020029#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010032#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020033
William Lallemand5f232402012-04-05 18:02:55 +020034#include <proto/frontend.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020035#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010036#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020037#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010038#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020039#ifdef USE_OPENSSL
40#include <proto/ssl_sock.h>
41#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020042
Willy Tarreaubaaee002006-06-26 02:48:02 +020043const char *log_facilities[NB_LOG_FACILITIES] = {
44 "kern", "user", "mail", "daemon",
45 "auth", "syslog", "lpr", "news",
46 "uucp", "cron", "auth2", "ftp",
47 "ntp", "audit", "alert", "cron2",
48 "local0", "local1", "local2", "local3",
49 "local4", "local5", "local6", "local7"
50};
51
52
53const char *log_levels[NB_LOG_LEVELS] = {
54 "emerg", "alert", "crit", "err",
55 "warning", "notice", "info", "debug"
56};
57
Willy Tarreau570f2212013-06-10 16:42:09 +020058const 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 +020059const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
William Lallemand723b73a2012-02-08 16:37:49 +010061
62/* log_format */
63struct logformat_type {
64 char *name;
65 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010066 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020067 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010068 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +010069 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +010070};
71
William Lallemandb7ff6a32012-03-02 14:35:21 +010072int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
73
William Lallemand723b73a2012-02-08 16:37:49 +010074/* log_format variable names */
75static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020076 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +010077
78 /* please keep these lines sorted ! */
79 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
80 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
81 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
82 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
83 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +020084 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +020085 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +010086 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +080087 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +020088 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +020089 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +010090 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +020091 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +010092 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
93 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +020094 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010095 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +020096 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010097 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
98 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +020099 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +0100100 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
101 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
102 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
103 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
104 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
105 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
106 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200107 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
108 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
109 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
110 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200111 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100112 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200113 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100114 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
115 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100116 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100117 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
118 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
119 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
120 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
121 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200122 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
123 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100124 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
125 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
126 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
127
128 /* The following tags are deprecated and will be removed soon */
129 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
130 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
131 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
132 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
133 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
134 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
135 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
136 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
137 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
138 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
139 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200140 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100141};
142
Willy Tarreau2beef582012-12-20 17:22:52 +0100143char 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
144char 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";
145char 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 +0100146char *log_format = NULL;
147
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100148/* This is a global syslog line, common to all outgoing messages. It begins
149 * with the syslog tag and the date that are updated by update_log_hdr().
150 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200151char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100152
William Lallemand723b73a2012-02-08 16:37:49 +0100153struct logformat_var_args {
154 char *name;
155 int mask;
156};
157
158struct logformat_var_args var_args_list[] = {
159// global
160 { "M", LOG_OPT_MANDATORY },
161 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200162 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100163 { 0, 0 }
164};
165
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200166/* return the name of the directive used in the current proxy for which we're
167 * currently parsing a header, when it is known.
168 */
169static inline const char *fmt_directive(const struct proxy *curproxy)
170{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100171 switch (curproxy->conf.args.ctx) {
172 case ARGC_UIF:
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200173 return "unique-id-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100174 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100175 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100176 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100177 return "http-response";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100178 case ARGC_STK:
179 return "stick";
180 case ARGC_TRK:
181 return "track-sc"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100182 case ARGC_RDR:
183 return "redirect"; break;
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100184 case ARGC_ACL:
185 return "acl"; break;
186 default:
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200187 return "log-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100188 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200189}
190
William Lallemand723b73a2012-02-08 16:37:49 +0100191/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100192 * callback used to configure addr source retrieval
193 */
194int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
195{
196 curproxy->options2 |= PR_O2_SRC_ADDR;
197
198 return 0;
199}
200
201
202/*
William Lallemand723b73a2012-02-08 16:37:49 +0100203 * Parse args in a logformat_var
204 */
205int parse_logformat_var_args(char *args, struct logformat_node *node)
206{
207 int i = 0;
208 int end = 0;
209 int flags = 0; // 1 = + 2 = -
210 char *sp = NULL; // start pointer
211
212 if (args == NULL)
213 return 1;
214
215 while (1) {
216 if (*args == '\0')
217 end = 1;
218
219 if (*args == '+') {
220 // add flag
221 sp = args + 1;
222 flags = 1;
223 }
224 if (*args == '-') {
225 // delete flag
226 sp = args + 1;
227 flags = 2;
228 }
229
230 if (*args == '\0' || *args == ',') {
231 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100232 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100233 if (strcmp(sp, var_args_list[i].name) == 0) {
234 if (flags == 1) {
235 node->options |= var_args_list[i].mask;
236 break;
237 } else if (flags == 2) {
238 node->options &= ~var_args_list[i].mask;
239 break;
240 }
241 }
242 }
243 sp = NULL;
244 if (end)
245 break;
246 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100247 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100248 }
249 return 0;
250}
251
252/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100253 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
254 * must pass the args part in the <arg> pointer with its length in <arg_len>,
255 * and varname with its length in <var> and <var_len> respectively. <arg> is
256 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100257 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100258int 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 +0100259{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100260 int j;
261 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100262
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100263 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
264 if (strlen(logformat_keywords[j].name) == var_len &&
265 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
266 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
267 node = calloc(1, sizeof(struct logformat_node));
268 node->type = logformat_keywords[j].type;
269 node->options = *defoptions;
270 if (arg_len) {
271 node->arg = my_strndup(arg, arg_len);
272 parse_logformat_var_args(node->arg, node);
273 }
274 if (node->type == LOG_FMT_GLOBAL) {
275 *defoptions = node->options;
276 free(node->arg);
277 free(node);
278 } else {
279 if (logformat_keywords[j].config_callback &&
280 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100281 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100282 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100283 curproxy->to_log |= logformat_keywords[j].lw;
284 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100285 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100286 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100287 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200288 curproxy->conf.args.file, curproxy->conf.args.line,
289 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100290 return 0;
291 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100292 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200293 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
294 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100295 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100296 }
William Lallemand723b73a2012-02-08 16:37:49 +0100297 }
298 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100299
300 j = var[var_len];
301 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100302 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 +0200303 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100304 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100305 return -1;
306}
307
308/*
309 * push to the logformat linked list
310 *
311 * start: start pointer
312 * end: end text pointer
313 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100314 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100315 *
316 * LOG_TEXT: copy chars from start to end excluding end.
317 *
318*/
William Lallemand1d705562012-03-12 12:46:41 +0100319void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100320{
321 char *str;
322
Willy Tarreaua3571662012-12-20 21:59:12 +0100323 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100324 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100325 str = calloc(end - start + 1, 1);
326 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100327 str[end - start] = '\0';
328 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100329 node->type = LOG_FMT_TEXT; // type string
330 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100331 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100332 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100333 node->type = LOG_FMT_SEPARATOR;
334 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100335 }
336}
337
338/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100339 * Parse the sample fetch expression <text> and add a node to <list_format> upon
340 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200341 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100342 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100343void 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 +0100344{
345 char *cmd[2];
346 struct sample_expr *expr;
347 struct logformat_node *node;
348 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100349 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100350
351 cmd[0] = text;
352 cmd[1] = "";
353 cmd_arg = 0;
354
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100355 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100356 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200357 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
358 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100359 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100360 return;
361 }
362
363 node = calloc(1, sizeof(struct logformat_node));
364 node->type = LOG_FMT_EXPR;
365 node->expr = expr;
366 node->options = options;
367
368 if (arg_len) {
369 node->arg = my_strndup(arg, arg_len);
370 parse_logformat_var_args(node->arg, node);
371 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100372 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100373 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
374
Willy Tarreau434c57c2013-01-08 01:10:24 +0100375 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100376 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
377
Willy Tarreau434c57c2013-01-08 01:10:24 +0100378 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200379 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
380 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
381 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100382
Willy Tarreauc8368452012-12-21 00:09:23 +0100383 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
384 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100385 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100386
William Lallemand65ad6e12014-01-31 15:08:02 +0100387 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
388 * needed with some sample fetches (eg: ssl*). We always set it for
389 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100390 */
391 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100392 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100393 LIST_ADDQ(list_format, &node->list);
394}
395
396/*
William Lallemand723b73a2012-02-08 16:37:49 +0100397 * Parse the log_format string and fill a linked list.
398 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200399 * You can set arguments using { } : %{many arguments}varname.
400 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100401 *
402 * str: the string to parse
403 * curproxy: the proxy affected
404 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100405 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100406 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100407 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100408void 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 +0100409{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100410 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100411 char *arg = NULL; /* start pointer for args */
412 char *var = NULL; /* start pointer for vars */
413 int arg_len = 0;
414 int var_len = 0;
415 int cformat; /* current token format */
416 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100417 struct logformat_node *tmplf, *back;
418
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100419 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200420 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200421
William Lallemand723b73a2012-02-08 16:37:49 +0100422 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100423 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100424 LIST_DEL(&tmplf->list);
425 free(tmplf);
426 }
427
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100428 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100429 pformat = cformat;
430
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100431 if (!*str)
432 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100433
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100434 /* The prinicple of the two-step state machine below is to first detect a change, and
435 * second have all common paths processed at one place. The common paths are the ones
436 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
437 * We use the common LF_INIT state to dispatch to the different final states.
438 */
439 switch (pformat) {
440 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100441 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100442 arg_len = var_len = 0;
443 if (*str == '{') { // optional argument
444 cformat = LF_STARG;
445 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100446 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100447 else if (*str == '[') {
448 cformat = LF_STEXPR;
449 var = str + 1; // store expr in variable name
450 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100451 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100452 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100453 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100454 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100455 else if (*str == '%')
456 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100457 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100458 /* single '%' followed by blank or digit, send them both */
459 cformat = LF_TEXT;
460 pformat = LF_TEXT; /* finally we include the previous char as well */
461 sp = str - 1; /* send both the '%' and the current char */
462 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",
463 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
464
465 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100466 else
467 cformat = LF_INIT; // handle other cases of litterals
468 break;
469
470 case LF_STARG: // text immediately following '%{'
471 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100472 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100473 arg_len = str - arg;
474 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100475 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100476 break;
477
478 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100479 if (*str == '[') {
480 cformat = LF_STEXPR;
481 var = str + 1; // store expr in variable name
482 break;
483 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100484 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100485 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100486 var = str;
487 break;
488 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200489 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
490 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100491 cformat = LF_INIT;
492 break;
493
Willy Tarreauc8368452012-12-21 00:09:23 +0100494 case LF_STEXPR: // text immediately following '%['
495 if (*str == ']') { // end of arg
496 cformat = LF_EDEXPR;
497 var_len = str - var;
498 *str = 0; // needed for parsing the expression
499 }
500 break;
501
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100502 case LF_VAR: // text part of a variable name
503 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100504 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100505 cformat = LF_INIT; // not variable name anymore
506 break;
507
Willy Tarreauc8368452012-12-21 00:09:23 +0100508 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100509 cformat = LF_INIT;
510 }
511
512 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
513 switch (*str) {
514 case '%': cformat = LF_STARTVAR; break;
515 case ' ': cformat = LF_SEPARATOR; break;
516 case 0 : cformat = LF_END; break;
517 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100518 }
519 }
520
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100521 if (cformat != pformat || pformat == LF_SEPARATOR) {
522 switch (pformat) {
523 case LF_VAR:
524 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
525 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100526 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100527 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100528 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100529 case LF_TEXT:
530 case LF_SEPARATOR:
531 add_to_logformat_list(sp, str, pformat, list_format);
532 break;
533 }
534 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100535 }
536 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100537
Willy Tarreauc8368452012-12-21 00:09:23 +0100538 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200539 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
540 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
541 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100542
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100543 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100544}
545
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546/*
547 * Displays the message on stderr with the date and pid. Overrides the quiet
548 * mode during startup.
549 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200550void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200551{
552 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200553 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554
555 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
556 va_start(argp, fmt);
557
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200558 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200560 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200561 vfprintf(stderr, fmt, argp);
562 fflush(stderr);
563 va_end(argp);
564 }
565}
566
567
568/*
569 * Displays the message on stderr with the date and pid.
570 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200571void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572{
573 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200574 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575
576 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
577 va_start(argp, fmt);
578
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200579 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200580 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200581 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200582 vfprintf(stderr, fmt, argp);
583 fflush(stderr);
584 va_end(argp);
585 }
586}
587
588/*
589 * Displays the message on <out> only if quiet mode is not set.
590 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200591void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200592{
593 va_list argp;
594
595 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
596 va_start(argp, fmt);
597 vfprintf(out, fmt, argp);
598 fflush(out);
599 va_end(argp);
600 }
601}
602
603/*
604 * returns log level for <lev> or -1 if not found.
605 */
606int get_log_level(const char *lev)
607{
608 int level;
609
610 level = NB_LOG_LEVELS - 1;
611 while (level >= 0 && strcmp(log_levels[level], lev))
612 level--;
613
614 return level;
615}
616
617
618/*
619 * returns log facility for <fac> or -1 if not found.
620 */
621int get_log_facility(const char *fac)
622{
623 int facility;
624
625 facility = NB_LOG_FACILITIES - 1;
626 while (facility >= 0 && strcmp(log_facilities[facility], fac))
627 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100628
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629 return facility;
630}
631
William Lallemanda1cc3812012-02-08 16:38:44 +0100632/*
633 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200634 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100635 *
636 * Return the adress of the \0 character, or NULL on error
637 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100638char *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 +0100639{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100640 if (size < 2)
641 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100642
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100643 if (node->options & LOG_OPT_QUOTE) {
644 *(dst++) = '"';
645 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100646 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100647
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100648 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100649 if (++len > size)
650 len = size;
651 len = strlcpy2(dst, src, len);
652
653 size -= len;
654 dst += len;
655 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100656 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
657 if (size < 2)
658 return NULL;
659 *(dst++) = '-';
660 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100661
662 if (node->options & LOG_OPT_QUOTE) {
663 if (size < 2)
664 return NULL;
665 *(dst++) = '"';
666 }
667
668 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100669 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100670}
671
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100672static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
673{
674 return lf_text_len(dst, src, size, size, node);
675}
676
William Lallemand5f232402012-04-05 18:02:55 +0200677/*
678 * Write a IP adress to the log string
679 * +X option write in hexadecimal notation, most signifant byte on the left
680 */
681char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
682{
683 char *ret = dst;
684 int iret;
685 char pn[INET6_ADDRSTRLEN];
686
687 if (node->options & LOG_OPT_HEXA) {
688 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
689 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
690 if (iret < 0 || iret > size)
691 return NULL;
692 ret += iret;
693 } else {
694 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
695 ret = lf_text(dst, pn, size, node);
696 if (ret == NULL)
697 return NULL;
698 }
699 return ret;
700}
701
702/*
703 * Write a port to the log
704 * +X option write in hexadecimal notation, most signifant byte on the left
705 */
706char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
707{
708 char *ret = dst;
709 int iret;
710
711 if (node->options & LOG_OPT_HEXA) {
712 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
713 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
714 if (iret < 0 || iret > size)
715 return NULL;
716 ret += iret;
717 } else {
718 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
719 if (ret == NULL)
720 return NULL;
721 }
722 return ret;
723}
724
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100725/* Re-generate the syslog header at the beginning of logline once a second and
726 * return the pointer to the first character after the header.
727 */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100728static char *update_log_hdr(const char *log_tag)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100730 static long tvsec;
731 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100732 int tag_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200734 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200736 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100737 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200738
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200739 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200740 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741
Willy Tarreau18324f52014-06-27 18:10:07 +0200742 hdr_len = snprintf(logline, global.max_syslog_len,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100743 "<<<<>%s %2d %02d:%02d:%02d %s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200744 monthname[tm.tm_mon],
745 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100746 global.log_send_hostname ? global.log_send_hostname : "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747 /* WARNING: depending upon implementations, snprintf may return
748 * either -1 or the number of bytes that would be needed to store
749 * the total message. In both cases, we must adjust it.
750 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200751 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
752 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100754 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755 }
756
Willy Tarreau094af4e2015-01-07 15:03:42 +0100757 dataptr[0] = 0; // ensure we get rid of any previous attempt
758
759 tag_len = snprintf(dataptr, logline + global.max_syslog_len - dataptr, "%s[%d]: ", log_tag, pid);
760 if (tag_len < 0 || tag_len > logline + global.max_syslog_len - dataptr)
761 tag_len = logline + global.max_syslog_len - dataptr;
762
763 return dataptr + tag_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100764}
765
766/*
767 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100768 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100769 */
770void send_log(struct proxy *p, int level, const char *format, ...)
771{
772 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100773 char *dataptr;
774 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100775
Willy Tarreau8c97ab52015-01-15 16:29:53 +0100776 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100777 return;
778
Thierry FOURNIERe83766a2015-03-09 17:10:29 +0100779 dataptr = update_log_hdr(p && p->log_tag ? p->log_tag : global.log_tag); /* update log header and skip it */
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100780 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100781
782 va_start(argp, format);
Willy Tarreau18324f52014-06-27 18:10:07 +0200783 data_len += vsnprintf(dataptr, logline + global.max_syslog_len - dataptr, format, argp);
784 if (data_len < 0 || data_len > global.max_syslog_len)
785 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200786 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100787
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100788 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100789}
790
791/*
792 * This function sends a syslog message.
793 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100794 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100795 */
796void __send_log(struct proxy *p, int level, char *message, size_t size)
797{
798 static int logfdunix = -1; /* syslog to AF_UNIX socket */
799 static int logfdinet = -1; /* syslog to AF_INET socket */
800 static char *dataptr = NULL;
801 int fac_level;
802 struct list *logsrvs = NULL;
803 struct logsrv *tmp = NULL;
804 int nblogger;
805 char *log_ptr;
806
807 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200808
809 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200810 if (!LIST_ISEMPTY(&global.logsrvs)) {
811 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812 }
813 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200814 if (!LIST_ISEMPTY(&p->logsrvs)) {
815 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200816 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100817 }
818
William Lallemand0f99e342011-10-12 17:50:54 +0200819 if (!logsrvs)
820 return;
821
Robert Tsai81ae1952007-12-05 10:47:29 +0100822 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200823 nblogger = 0;
824 list_for_each_entry(tmp, logsrvs, list) {
825 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100826 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100827 &logfdunix : &logfdinet;
828 int sent;
Willy Tarreau18324f52014-06-27 18:10:07 +0200829 int max;
830 char backup;
Robert Tsai81ae1952007-12-05 10:47:29 +0100831
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200832 nblogger++;
833
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200835 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200836 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100837
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200838 if (unlikely(*plogfd < 0)) {
839 /* socket not successfully initialized yet */
840 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
841
842 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
843 Alert("socket for logger #%d failed: %s (errno=%d)\n",
844 nblogger, strerror(errno), errno);
845 continue;
846 }
847 /* we don't want to receive anything on this socket */
848 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
849 /* does nothing under Linux, maybe needed for others */
850 shutdown(*plogfd, SHUT_RD);
851 }
852
Willy Tarreaubaaee002006-06-26 02:48:02 +0200853 /* For each target, we may have a different facility.
854 * We can also have a different log level for each message.
855 * This induces variations in the message header length.
856 * Since we don't want to recompute it each time, nor copy it every
857 * time, we only change the facility in the pre-computed header,
858 * and we change the pointer to the header accordingly.
859 */
William Lallemand0f99e342011-10-12 17:50:54 +0200860 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100861 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200862 do {
863 *log_ptr = '0' + fac_level % 10;
864 fac_level /= 10;
865 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100866 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100868
Willy Tarreau18324f52014-06-27 18:10:07 +0200869 max = size - (log_ptr - dataptr);
870 if (max > logsrv->maxlen)
871 max = logsrv->maxlen;
872
873 /* insert a \n at the end of the message, but save what was
874 * there first because we could have different max lengths
875 * for different log targets.
876 */
877 backup = log_ptr[max - 1];
878 log_ptr[max - 1] = '\n';
879
880 sent = sendto(*plogfd, log_ptr, max,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200881 MSG_DONTWAIT | MSG_NOSIGNAL,
882 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Willy Tarreau18324f52014-06-27 18:10:07 +0200883
884 log_ptr[max - 1] = backup;
885
Robert Tsai81ae1952007-12-05 10:47:29 +0100886 if (sent < 0) {
887 Alert("sendto logger #%d failed: %s (errno=%d)\n",
888 nblogger, strerror(errno), errno);
889 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200890 }
891}
892
William Lallemandbddd4fd2012-02-27 11:23:10 +0100893extern fd_set hdr_encode_map[];
894extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100895extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +0100896
Willy Tarreaubaaee002006-06-26 02:48:02 +0200897
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200898const 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 +0100899const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
900 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
901 Set-cookie Updated, unknown, unknown */
902
William Lallemand1d705562012-03-12 12:46:41 +0100903/*
904 * try to write a character if there is enough space, or goto out
905 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100906#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100907 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100908 *(tmplog++) = (x); \
909 } else { \
910 goto out; \
911 } \
912 } while(0)
913
William Lallemand1d705562012-03-12 12:46:41 +0100914
Willy Tarreaudf974472012-12-28 02:44:01 +0100915/* Builds a log line in <dst> based on <list_format>, and stops before reaching
916 * <maxsize> characters. Returns the size of the output string in characters,
917 * not counting the trailing zero which is always added if the resulting size
918 * is not zero.
919 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200920int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200921{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200922 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200923 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100924 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200925 struct http_txn *txn = s->txn;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100926 char *uri;
Willy Tarreaufe944602007-10-25 10:34:16 +0200927 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100928 int t_request;
929 int hdr;
930 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100931 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100932 char *ret;
933 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100934 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935
William Lallemandbddd4fd2012-02-27 11:23:10 +0100936 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937
William Lallemandbddd4fd2012-02-27 11:23:10 +0100938 t_request = -1;
939 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
940 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
941
William Lallemand1d705562012-03-12 12:46:41 +0100942 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200943
William Lallemandbddd4fd2012-02-27 11:23:10 +0100944 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100945 if (LIST_ISEMPTY(list_format))
946 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100947
William Lallemand1d705562012-03-12 12:46:41 +0100948 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200949 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +0200950 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100951 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100952 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +0100953
Willy Tarreauc8368452012-12-21 00:09:23 +0100954 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +0100955 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100956 if (!last_isspace) {
957 LOGCHAR(' ');
958 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100959 }
960 break;
961
William Lallemand1d705562012-03-12 12:46:41 +0100962 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100963 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200964 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100965 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100966 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100967 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100968 last_isspace = 0;
969 break;
970
Willy Tarreauc8368452012-12-21 00:09:23 +0100971 case LOG_FMT_EXPR: // sample expression, may be request or response
972 key = NULL;
973 if (tmp->options & LOG_OPT_REQ_CAP)
Willy Tarreau192252e2015-04-04 01:47:55 +0200974 key = sample_fetch_string(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr);
Willy Tarreauc8368452012-12-21 00:09:23 +0100975 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Willy Tarreau192252e2015-04-04 01:47:55 +0200976 key = sample_fetch_string(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100977 if (tmp->options & LOG_OPT_HTTP)
978 ret = encode_chunk(tmplog, dst + maxsize,
979 '%', http_encode_map, key ? &key->data.str : &empty);
980 else
981 ret = lf_text_len(tmplog, key ? key->data.str.str : NULL, key ? key->data.str.len : 0, dst + maxsize - tmplog, tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +0100982 if (ret == 0)
983 goto out;
984 tmplog = ret;
985 last_isspace = 0;
986 break;
987
Willy Tarreau2beef582012-12-20 17:22:52 +0100988 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200989 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200990 if (conn)
991 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
992 else
993 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100994 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100995 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100996 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100997 last_isspace = 0;
998 break;
999
Willy Tarreau2beef582012-12-20 17:22:52 +01001000 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001001 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001002 if (conn) {
1003 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001004 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001005 } else {
1006 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1007 dst + maxsize - tmplog, tmp);
1008 }
William Lallemand5f232402012-04-05 18:02:55 +02001009 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001010 else
1011 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1012
William Lallemand5f232402012-04-05 18:02:55 +02001013 if (ret == NULL)
1014 goto out;
1015 tmplog = ret;
1016 last_isspace = 0;
1017 break;
1018
Willy Tarreau2beef582012-12-20 17:22:52 +01001019 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001020 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001021 if (conn) {
1022 conn_get_to_addr(conn);
1023 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1024 }
1025 else
1026 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1027
William Lallemand1d705562012-03-12 12:46:41 +01001028 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001029 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001030 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001031 last_isspace = 0;
1032 break;
1033
Willy Tarreau2beef582012-12-20 17:22:52 +01001034 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001035 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001036 if (conn) {
1037 conn_get_to_addr(conn);
1038 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001039 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001040 else
1041 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001042 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001043 else
1044 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1045
William Lallemand5f232402012-04-05 18:02:55 +02001046 if (ret == NULL)
1047 goto out;
1048 tmplog = ret;
1049 last_isspace = 0;
1050 break;
1051
Willy Tarreau2beef582012-12-20 17:22:52 +01001052 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001053 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001054 if (conn)
1055 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1056 else
1057 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1058
William Lallemand1d705562012-03-12 12:46:41 +01001059 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001060 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001061 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001062 last_isspace = 0;
1063 break;
1064
Willy Tarreau2beef582012-12-20 17:22:52 +01001065 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001066 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001067 if (conn)
1068 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1069 else
1070 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1071
William Lallemand5f232402012-04-05 18:02:55 +02001072 if (ret == NULL)
1073 goto out;
1074 tmplog = ret;
1075 last_isspace = 0;
1076 break;
1077
Willy Tarreau2beef582012-12-20 17:22:52 +01001078 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001079 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001080 if (conn)
1081 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1082 else
1083 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1084
William Lallemand5f232402012-04-05 18:02:55 +02001085 if (ret == NULL)
1086 goto out;
1087 tmplog = ret;
1088 last_isspace = 0;
1089 break;
1090
Willy Tarreau2beef582012-12-20 17:22:52 +01001091 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001092 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001093 if (conn)
1094 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1095 else
1096 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1097
William Lallemand1d705562012-03-12 12:46:41 +01001098 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001099 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001100 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001101 last_isspace = 0;
1102 break;
1103
William Lallemand1d705562012-03-12 12:46:41 +01001104 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001105 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001106 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1107 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001108 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001109 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001110 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001111 last_isspace = 0;
1112 break;
1113
William Lallemand1d705562012-03-12 12:46:41 +01001114 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001115 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001116 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001117 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001118 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001119 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001120 last_isspace = 0;
1121 break;
1122
Yuxans Yao4e25b012012-10-19 10:36:09 +08001123 case LOG_FMT_DATELOCAL: // %Tl
1124 get_localtime(s->logs.accept_date.tv_sec, &tm);
1125 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1126 if (ret == NULL)
1127 goto out;
1128 tmplog = ret;
1129 last_isspace = 0;
1130 break;
1131
William Lallemand5f232402012-04-05 18:02:55 +02001132 case LOG_FMT_TS: // %Ts
1133 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1134 if (tmp->options & LOG_OPT_HEXA) {
1135 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1136 if (iret < 0 || iret > dst + maxsize - tmplog)
1137 goto out;
1138 last_isspace = 0;
1139 tmplog += iret;
1140 } else {
1141 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1142 if (ret == NULL)
1143 goto out;
1144 tmplog = ret;
1145 last_isspace = 0;
1146 }
1147 break;
1148
William Lallemand1d705562012-03-12 12:46:41 +01001149 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001150 if (tmp->options & LOG_OPT_HEXA) {
1151 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1152 if (iret < 0 || iret > dst + maxsize - tmplog)
1153 goto out;
1154 last_isspace = 0;
1155 tmplog += iret;
1156 } else {
1157 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001158 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001159 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1160 tmplog, 4);
1161 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001162 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001163 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001164 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001165 }
1166 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001167
William Lallemand1d705562012-03-12 12:46:41 +01001168 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001169 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001170 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001171 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001172 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001173 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001174 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001175 break;
1176
Willy Tarreau773d65f2012-10-12 14:56:11 +02001177 case LOG_FMT_FRONTEND_XPRT: // %ft
1178 src = fe->id;
1179 if (tmp->options & LOG_OPT_QUOTE)
1180 LOGCHAR('"');
1181 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1182 if (iret == 0)
1183 goto out;
1184 tmplog += iret;
1185#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001186 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001187 LOGCHAR('~');
1188#endif
1189 if (tmp->options & LOG_OPT_QUOTE)
1190 LOGCHAR('"');
1191 last_isspace = 0;
1192 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001193#ifdef USE_OPENSSL
1194 case LOG_FMT_SSL_CIPHER: // %sslc
1195 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001196 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001197 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001198 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001199 src = ssl_sock_get_cipher_name(conn);
1200 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001201 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1202 if (ret == NULL)
1203 goto out;
1204 tmplog = ret;
1205 last_isspace = 0;
1206 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001207
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001208 case LOG_FMT_SSL_VERSION: // %sslv
1209 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001210 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001211 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001212 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001213 src = ssl_sock_get_proto_version(conn);
1214 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001215 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1216 if (ret == NULL)
1217 goto out;
1218 tmplog = ret;
1219 last_isspace = 0;
1220 break;
1221#endif
William Lallemand1d705562012-03-12 12:46:41 +01001222 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001223 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001224 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001225 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001226 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001227 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001228 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001229 break;
1230
William Lallemand1d705562012-03-12 12:46:41 +01001231 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001232 switch (obj_type(s->target)) {
1233 case OBJ_TYPE_SERVER:
1234 src = objt_server(s->target)->id;
1235 break;
1236 case OBJ_TYPE_APPLET:
1237 src = objt_applet(s->target)->name;
1238 break;
1239 default:
1240 src = "<NOSRV>";
1241 break;
1242 }
William Lallemand5f232402012-04-05 18:02:55 +02001243 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001244 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001245 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001246 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001247 last_isspace = 0;
1248 break;
1249
William Lallemand1d705562012-03-12 12:46:41 +01001250 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001251 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001252 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001253 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001254 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001255 last_isspace = 0;
1256 break;
1257
William Lallemand1d705562012-03-12 12:46:41 +01001258 case LOG_FMT_TW: // %Tw
1259 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001260 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001261 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001262 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001263 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001264 last_isspace = 0;
1265 break;
1266
William Lallemand1d705562012-03-12 12:46:41 +01001267 case LOG_FMT_TC: // %Tc
1268 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001269 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001270 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001271 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001272 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001273 last_isspace = 0;
1274 break;
1275
William Lallemand1d705562012-03-12 12:46:41 +01001276 case LOG_FMT_TR: // %Tr
1277 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001278 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001279 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001280 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001281 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001282 last_isspace = 0;
1283 break;
1284
William Lallemand1d705562012-03-12 12:46:41 +01001285 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001286 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001287 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001288 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001289 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001290 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001291 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001292 last_isspace = 0;
1293 break;
1294
Willy Tarreau2beef582012-12-20 17:22:52 +01001295 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001296 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001297 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001298 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001299 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001300 last_isspace = 0;
1301 break;
1302
William Lallemand1d705562012-03-12 12:46:41 +01001303 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001304 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001305 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001306 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001307 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001308 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001309 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001310 last_isspace = 0;
1311 break;
1312
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001313 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001314 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1315 if (ret == NULL)
1316 goto out;
1317 tmplog = ret;
1318 last_isspace = 0;
1319 break;
1320
Willy Tarreau2beef582012-12-20 17:22:52 +01001321 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001322 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001323 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001324 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001325 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001326 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001327 last_isspace = 0;
1328 break;
1329
Willy Tarreau2beef582012-12-20 17:22:52 +01001330 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001331 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001332 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001333 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001334 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001335 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001336 last_isspace = 0;
1337 break;
1338
William Lallemand1d705562012-03-12 12:46:41 +01001339 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001340 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1341 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001342 *tmplog = '\0';
1343 last_isspace = 0;
1344 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001345
William Lallemand1d705562012-03-12 12:46:41 +01001346 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001347 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1348 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001349 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1350 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 +01001351 last_isspace = 0;
1352 break;
1353
William Lallemand1d705562012-03-12 12:46:41 +01001354 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001355 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001356 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001357 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001358 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001359 last_isspace = 0;
1360 break;
1361
William Lallemand1d705562012-03-12 12:46:41 +01001362 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001363 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001364 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001365 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001366 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001367 last_isspace = 0;
1368 break;
1369
William Lallemand1d705562012-03-12 12:46:41 +01001370 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001371 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001372 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001373 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001374 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001375 last_isspace = 0;
1376 break;
1377
William Lallemand1d705562012-03-12 12:46:41 +01001378 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001379 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001380 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001381 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001382 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001383 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001384 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001385 last_isspace = 0;
1386 break;
1387
William Lallemand1d705562012-03-12 12:46:41 +01001388 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001389 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001390 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001391 ret = ltoa_o((s->si[1].conn_retries>0) ?
1392 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001393 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001394 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001395 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001396 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001397 last_isspace = 0;
1398 break;
1399
William Lallemand1d705562012-03-12 12:46:41 +01001400 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001401 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001402 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001403 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001404 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001405 last_isspace = 0;
1406 break;
1407
William Lallemand1d705562012-03-12 12:46:41 +01001408 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001409 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001410 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001411 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001412 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001413 last_isspace = 0;
1414 break;
1415
William Lallemand1d705562012-03-12 12:46:41 +01001416 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001417 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001418 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001419 if (tmp->options & LOG_OPT_QUOTE)
1420 LOGCHAR('"');
1421 LOGCHAR('{');
1422 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1423 if (hdr)
1424 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001425 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001426 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001427 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001428 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001429 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001430 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001431 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001432 }
1433 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001434 if (tmp->options & LOG_OPT_QUOTE)
1435 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001436 last_isspace = 0;
1437 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001438 break;
1439
William Lallemand1d705562012-03-12 12:46:41 +01001440 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001441 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001442 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001443 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1444 if (hdr > 0)
1445 LOGCHAR(' ');
1446 if (tmp->options & LOG_OPT_QUOTE)
1447 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001448 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001449 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001450 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001451 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001452 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001453 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001454 } else if (!(tmp->options & LOG_OPT_QUOTE))
1455 LOGCHAR('-');
1456 if (tmp->options & LOG_OPT_QUOTE)
1457 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001458 last_isspace = 0;
1459 }
1460 }
1461 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001462
William Lallemand1d705562012-03-12 12:46:41 +01001463
1464 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001465 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001466 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001467 if (tmp->options & LOG_OPT_QUOTE)
1468 LOGCHAR('"');
1469 LOGCHAR('{');
1470 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1471 if (hdr)
1472 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001473 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001474 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001475 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001476 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001477 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001478 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001479 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001480 }
1481 LOGCHAR('}');
1482 last_isspace = 0;
1483 if (tmp->options & LOG_OPT_QUOTE)
1484 LOGCHAR('"');
1485 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001486 break;
1487
William Lallemand1d705562012-03-12 12:46:41 +01001488 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001489 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001490 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001491 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1492 if (hdr > 0)
1493 LOGCHAR(' ');
1494 if (tmp->options & LOG_OPT_QUOTE)
1495 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001496 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001497 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001498 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001499 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001500 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001501 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001502 } else if (!(tmp->options & LOG_OPT_QUOTE))
1503 LOGCHAR('-');
1504 if (tmp->options & LOG_OPT_QUOTE)
1505 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001506 last_isspace = 0;
1507 }
1508 }
1509 break;
1510
William Lallemand1d705562012-03-12 12:46:41 +01001511 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001512 /* Request */
1513 if (tmp->options & LOG_OPT_QUOTE)
1514 LOGCHAR('"');
1515 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001516 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001517 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001518 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001519 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001520 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001521 if (tmp->options & LOG_OPT_QUOTE)
1522 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001523 last_isspace = 0;
1524 break;
William Lallemand5f232402012-04-05 18:02:55 +02001525
1526 case LOG_FMT_COUNTER: // %rt
1527 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001528 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001529 if (iret < 0 || iret > dst + maxsize - tmplog)
1530 goto out;
1531 last_isspace = 0;
1532 tmplog += iret;
1533 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001534 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001535 if (ret == NULL)
1536 goto out;
1537 tmplog = ret;
1538 last_isspace = 0;
1539 }
1540 break;
1541
Willy Tarreau7346acb2014-08-28 15:03:15 +02001542 case LOG_FMT_LOGCNT: // %lc
1543 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001544 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001545 if (iret < 0 || iret > dst + maxsize - tmplog)
1546 goto out;
1547 last_isspace = 0;
1548 tmplog += iret;
1549 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001550 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001551 if (ret == NULL)
1552 goto out;
1553 tmplog = ret;
1554 last_isspace = 0;
1555 }
1556 break;
1557
William Lallemand5f232402012-04-05 18:02:55 +02001558 case LOG_FMT_HOSTNAME: // %H
1559 src = hostname;
1560 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1561 if (ret == NULL)
1562 goto out;
1563 tmplog = ret;
1564 last_isspace = 0;
1565 break;
1566
1567 case LOG_FMT_PID: // %pid
1568 if (tmp->options & LOG_OPT_HEXA) {
1569 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1570 if (iret < 0 || iret > dst + maxsize - tmplog)
1571 goto out;
1572 last_isspace = 0;
1573 tmplog += iret;
1574 } else {
1575 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1576 if (ret == NULL)
1577 goto out;
1578 tmplog = ret;
1579 last_isspace = 0;
1580 }
1581 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001582
1583 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001584 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001585 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02001586 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001587 if (ret == NULL)
1588 goto out;
1589 tmplog = ret;
1590 last_isspace = 0;
1591 break;
1592
William Lallemandbddd4fd2012-02-27 11:23:10 +01001593 }
1594 }
1595
1596out:
William Lallemand1d705562012-03-12 12:46:41 +01001597 /* *tmplog is a unused character */
1598 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001599 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001600
Willy Tarreaubaaee002006-06-26 02:48:02 +02001601}
1602
William Lallemand1d705562012-03-12 12:46:41 +01001603/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001604 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01001605 * Will not log if the frontend has no log defined.
1606 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001607void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01001608{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001609 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01001610 char *tmplog;
1611 int size, err, level;
1612
1613 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001614 err = (s->flags & SF_REDISP) ||
1615 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
1616 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01001617 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02001618 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001619
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001620 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01001621 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001622
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001623 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01001624 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001625
Willy Tarreauabcd5142013-06-11 17:18:02 +02001626 if (s->logs.level) { /* loglevel was overridden */
1627 if (s->logs.level == -1) {
1628 s->logs.logwait = 0; /* logs disabled */
1629 return;
1630 }
1631 level = s->logs.level - 1;
1632 }
1633 else {
1634 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001635 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02001636 level = LOG_ERR;
1637 }
William Lallemand1d705562012-03-12 12:46:41 +01001638
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001639 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001640 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001641 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001642 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001643 }
1644
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001645 tmplog = update_log_hdr(sess->fe->log_tag ? sess->fe->log_tag : global.log_tag);
William Lallemand1d705562012-03-12 12:46:41 +01001646 size = tmplog - logline;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001647 size += build_logline(s, tmplog, global.max_syslog_len - size, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01001648 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001649 sess->fe->log_count++;
1650 __send_log(sess->fe, level, logline, size + 1);
William Lallemand1d705562012-03-12 12:46:41 +01001651 s->logs.logwait = 0;
1652 }
1653}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001654
Willy Tarreaubaaee002006-06-26 02:48:02 +02001655/*
1656 * Local variables:
1657 * c-indent-level: 8
1658 * c-basic-offset: 8
1659 * End:
1660 */