blob: ffd8f10dae4e7b0580ade5fda2ff1a7c7fd504dd [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
Willy Tarreaubaaee002006-06-26 02:48:02 +020044const char *log_facilities[NB_LOG_FACILITIES] = {
45 "kern", "user", "mail", "daemon",
46 "auth", "syslog", "lpr", "news",
47 "uucp", "cron", "auth2", "ftp",
48 "ntp", "audit", "alert", "cron2",
49 "local0", "local1", "local2", "local3",
50 "local4", "local5", "local6", "local7"
51};
52
53
54const char *log_levels[NB_LOG_LEVELS] = {
55 "emerg", "alert", "crit", "err",
56 "warning", "notice", "info", "debug"
57};
58
Willy Tarreau570f2212013-06-10 16:42:09 +020059const 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 +020060const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
William Lallemand723b73a2012-02-08 16:37:49 +010062
63/* log_format */
64struct logformat_type {
65 char *name;
66 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010067 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020068 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010069 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +010070 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +010071};
72
William Lallemandb7ff6a32012-03-02 14:35:21 +010073int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
74
William Lallemand723b73a2012-02-08 16:37:49 +010075/* log_format variable names */
76static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020077 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +010078
79 /* please keep these lines sorted ! */
80 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
81 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
82 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
83 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
84 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +020085 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +020086 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +010087 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +080088 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +020089 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +020090 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +010091 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +020092 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +010093 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
94 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +020095 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010096 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +020097 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010098 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
99 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200100 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +0100101 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
102 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
103 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
104 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
105 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
106 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
107 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200108 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
109 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
110 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
111 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000112 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
113 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
114 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
115 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200116 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100117 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200118 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100119 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
120 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100121 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100122 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
123 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
124 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
125 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
126 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200127 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
128 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100129 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
130 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
131 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
132
133 /* The following tags are deprecated and will be removed soon */
134 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
135 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
136 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
137 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
138 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
139 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
140 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
141 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
142 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
143 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
144 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200145 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100146};
147
Willy Tarreau2beef582012-12-20 17:22:52 +0100148char 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
149char 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";
150char 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 +0100151char *log_format = NULL;
152
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100153/* This is a global syslog line, common to all outgoing messages. It begins
154 * with the syslog tag and the date that are updated by update_log_hdr().
155 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200156char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100157
William Lallemand723b73a2012-02-08 16:37:49 +0100158struct logformat_var_args {
159 char *name;
160 int mask;
161};
162
163struct logformat_var_args var_args_list[] = {
164// global
165 { "M", LOG_OPT_MANDATORY },
166 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200167 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100168 { 0, 0 }
169};
170
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200171/* return the name of the directive used in the current proxy for which we're
172 * currently parsing a header, when it is known.
173 */
174static inline const char *fmt_directive(const struct proxy *curproxy)
175{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100176 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200177 case ARGC_ACL:
178 return "acl";
179 case ARGC_STK:
180 return "stick";
181 case ARGC_TRK:
182 return "track-sc";
183 case ARGC_LOG:
184 return "log-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100185 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100186 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100187 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100188 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200189 case ARGC_UIF:
190 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100191 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200192 return "redirect";
193 case ARGC_CAP:
194 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200195 case ARGC_SRV:
196 return "server";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100197 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200198 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100199 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200200}
201
William Lallemand723b73a2012-02-08 16:37:49 +0100202/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100203 * callback used to configure addr source retrieval
204 */
205int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
206{
207 curproxy->options2 |= PR_O2_SRC_ADDR;
208
209 return 0;
210}
211
212
213/*
William Lallemand723b73a2012-02-08 16:37:49 +0100214 * Parse args in a logformat_var
215 */
216int parse_logformat_var_args(char *args, struct logformat_node *node)
217{
218 int i = 0;
219 int end = 0;
220 int flags = 0; // 1 = + 2 = -
221 char *sp = NULL; // start pointer
222
223 if (args == NULL)
224 return 1;
225
226 while (1) {
227 if (*args == '\0')
228 end = 1;
229
230 if (*args == '+') {
231 // add flag
232 sp = args + 1;
233 flags = 1;
234 }
235 if (*args == '-') {
236 // delete flag
237 sp = args + 1;
238 flags = 2;
239 }
240
241 if (*args == '\0' || *args == ',') {
242 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100243 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100244 if (strcmp(sp, var_args_list[i].name) == 0) {
245 if (flags == 1) {
246 node->options |= var_args_list[i].mask;
247 break;
248 } else if (flags == 2) {
249 node->options &= ~var_args_list[i].mask;
250 break;
251 }
252 }
253 }
254 sp = NULL;
255 if (end)
256 break;
257 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100258 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100259 }
260 return 0;
261}
262
263/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100264 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
265 * must pass the args part in the <arg> pointer with its length in <arg_len>,
266 * and varname with its length in <var> and <var_len> respectively. <arg> is
267 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100268 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100269int 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 +0100270{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100271 int j;
272 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100273
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100274 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
275 if (strlen(logformat_keywords[j].name) == var_len &&
276 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
277 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
278 node = calloc(1, sizeof(struct logformat_node));
279 node->type = logformat_keywords[j].type;
280 node->options = *defoptions;
281 if (arg_len) {
282 node->arg = my_strndup(arg, arg_len);
283 parse_logformat_var_args(node->arg, node);
284 }
285 if (node->type == LOG_FMT_GLOBAL) {
286 *defoptions = node->options;
287 free(node->arg);
288 free(node);
289 } else {
290 if (logformat_keywords[j].config_callback &&
291 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100292 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100293 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100294 curproxy->to_log |= logformat_keywords[j].lw;
295 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100296 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100297 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100298 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200299 curproxy->conf.args.file, curproxy->conf.args.line,
300 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100301 return 0;
302 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100303 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200304 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
305 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100306 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100307 }
William Lallemand723b73a2012-02-08 16:37:49 +0100308 }
309 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100310
311 j = var[var_len];
312 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100313 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 +0200314 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100315 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100316 return -1;
317}
318
319/*
320 * push to the logformat linked list
321 *
322 * start: start pointer
323 * end: end text pointer
324 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100325 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100326 *
327 * LOG_TEXT: copy chars from start to end excluding end.
328 *
329*/
William Lallemand1d705562012-03-12 12:46:41 +0100330void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100331{
332 char *str;
333
Willy Tarreaua3571662012-12-20 21:59:12 +0100334 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100335 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100336 str = calloc(end - start + 1, 1);
337 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100338 str[end - start] = '\0';
339 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100340 node->type = LOG_FMT_TEXT; // type string
341 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100342 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100343 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100344 node->type = LOG_FMT_SEPARATOR;
345 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100346 }
347}
348
349/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100350 * Parse the sample fetch expression <text> and add a node to <list_format> upon
351 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200352 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100353 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100354void 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 +0100355{
356 char *cmd[2];
357 struct sample_expr *expr;
358 struct logformat_node *node;
359 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100360 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100361
362 cmd[0] = text;
363 cmd[1] = "";
364 cmd_arg = 0;
365
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100366 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100367 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200368 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
369 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100370 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100371 return;
372 }
373
374 node = calloc(1, sizeof(struct logformat_node));
375 node->type = LOG_FMT_EXPR;
376 node->expr = expr;
377 node->options = options;
378
379 if (arg_len) {
380 node->arg = my_strndup(arg, arg_len);
381 parse_logformat_var_args(node->arg, node);
382 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100383 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100384 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
385
Willy Tarreau434c57c2013-01-08 01:10:24 +0100386 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100387 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
388
Willy Tarreau434c57c2013-01-08 01:10:24 +0100389 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200390 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
391 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
392 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100393
Willy Tarreauc8368452012-12-21 00:09:23 +0100394 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
395 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100396 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100397
William Lallemand65ad6e12014-01-31 15:08:02 +0100398 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
399 * needed with some sample fetches (eg: ssl*). We always set it for
400 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100401 */
402 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100403 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100404 LIST_ADDQ(list_format, &node->list);
405}
406
407/*
William Lallemand723b73a2012-02-08 16:37:49 +0100408 * Parse the log_format string and fill a linked list.
409 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200410 * You can set arguments using { } : %{many arguments}varname.
411 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100412 *
413 * str: the string to parse
414 * curproxy: the proxy affected
415 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100416 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100417 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100418 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100419void 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 +0100420{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100421 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100422 char *arg = NULL; /* start pointer for args */
423 char *var = NULL; /* start pointer for vars */
424 int arg_len = 0;
425 int var_len = 0;
426 int cformat; /* current token format */
427 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100428 struct logformat_node *tmplf, *back;
429
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100430 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200431 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200432
William Lallemand723b73a2012-02-08 16:37:49 +0100433 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100434 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100435 LIST_DEL(&tmplf->list);
436 free(tmplf);
437 }
438
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100439 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100440 pformat = cformat;
441
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100442 if (!*str)
443 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100444
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100445 /* The prinicple of the two-step state machine below is to first detect a change, and
446 * second have all common paths processed at one place. The common paths are the ones
447 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
448 * We use the common LF_INIT state to dispatch to the different final states.
449 */
450 switch (pformat) {
451 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100452 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100453 arg_len = var_len = 0;
454 if (*str == '{') { // optional argument
455 cformat = LF_STARG;
456 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100457 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100458 else if (*str == '[') {
459 cformat = LF_STEXPR;
460 var = str + 1; // store expr in variable name
461 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100462 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100463 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100464 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100465 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100466 else if (*str == '%')
467 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100468 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100469 /* single '%' followed by blank or digit, send them both */
470 cformat = LF_TEXT;
471 pformat = LF_TEXT; /* finally we include the previous char as well */
472 sp = str - 1; /* send both the '%' and the current char */
473 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",
474 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
475
476 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100477 else
478 cformat = LF_INIT; // handle other cases of litterals
479 break;
480
481 case LF_STARG: // text immediately following '%{'
482 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100483 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100484 arg_len = str - arg;
485 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100486 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100487 break;
488
489 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100490 if (*str == '[') {
491 cformat = LF_STEXPR;
492 var = str + 1; // store expr in variable name
493 break;
494 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100495 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100496 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100497 var = str;
498 break;
499 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200500 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
501 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100502 cformat = LF_INIT;
503 break;
504
Willy Tarreauc8368452012-12-21 00:09:23 +0100505 case LF_STEXPR: // text immediately following '%['
506 if (*str == ']') { // end of arg
507 cformat = LF_EDEXPR;
508 var_len = str - var;
509 *str = 0; // needed for parsing the expression
510 }
511 break;
512
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100513 case LF_VAR: // text part of a variable name
514 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100515 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100516 cformat = LF_INIT; // not variable name anymore
517 break;
518
Willy Tarreauc8368452012-12-21 00:09:23 +0100519 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100520 cformat = LF_INIT;
521 }
522
523 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
524 switch (*str) {
525 case '%': cformat = LF_STARTVAR; break;
526 case ' ': cformat = LF_SEPARATOR; break;
527 case 0 : cformat = LF_END; break;
528 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100529 }
530 }
531
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100532 if (cformat != pformat || pformat == LF_SEPARATOR) {
533 switch (pformat) {
534 case LF_VAR:
535 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
536 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100537 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100538 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100539 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100540 case LF_TEXT:
541 case LF_SEPARATOR:
542 add_to_logformat_list(sp, str, pformat, list_format);
543 break;
544 }
545 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100546 }
547 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100548
Willy Tarreauc8368452012-12-21 00:09:23 +0100549 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200550 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
551 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
552 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100553
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100554 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100555}
556
Willy Tarreaubaaee002006-06-26 02:48:02 +0200557/*
558 * Displays the message on stderr with the date and pid. Overrides the quiet
559 * mode during startup.
560 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200561void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562{
563 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200564 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200565
566 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
567 va_start(argp, fmt);
568
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200569 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200571 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572 vfprintf(stderr, fmt, argp);
573 fflush(stderr);
574 va_end(argp);
575 }
576}
577
578
579/*
580 * Displays the message on stderr with the date and pid.
581 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200582void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200583{
584 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200585 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200586
587 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
588 va_start(argp, fmt);
589
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200590 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200591 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200592 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200593 vfprintf(stderr, fmt, argp);
594 fflush(stderr);
595 va_end(argp);
596 }
597}
598
599/*
600 * Displays the message on <out> only if quiet mode is not set.
601 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200602void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200603{
604 va_list argp;
605
606 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
607 va_start(argp, fmt);
608 vfprintf(out, fmt, argp);
609 fflush(out);
610 va_end(argp);
611 }
612}
613
614/*
615 * returns log level for <lev> or -1 if not found.
616 */
617int get_log_level(const char *lev)
618{
619 int level;
620
621 level = NB_LOG_LEVELS - 1;
622 while (level >= 0 && strcmp(log_levels[level], lev))
623 level--;
624
625 return level;
626}
627
628
629/*
630 * returns log facility for <fac> or -1 if not found.
631 */
632int get_log_facility(const char *fac)
633{
634 int facility;
635
636 facility = NB_LOG_FACILITIES - 1;
637 while (facility >= 0 && strcmp(log_facilities[facility], fac))
638 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100639
Willy Tarreaubaaee002006-06-26 02:48:02 +0200640 return facility;
641}
642
William Lallemanda1cc3812012-02-08 16:38:44 +0100643/*
644 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200645 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100646 *
647 * Return the adress of the \0 character, or NULL on error
648 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100649char *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 +0100650{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100651 if (size < 2)
652 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100653
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100654 if (node->options & LOG_OPT_QUOTE) {
655 *(dst++) = '"';
656 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100657 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100658
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100659 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100660 if (++len > size)
661 len = size;
662 len = strlcpy2(dst, src, len);
663
664 size -= len;
665 dst += len;
666 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100667 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
668 if (size < 2)
669 return NULL;
670 *(dst++) = '-';
671 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100672
673 if (node->options & LOG_OPT_QUOTE) {
674 if (size < 2)
675 return NULL;
676 *(dst++) = '"';
677 }
678
679 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100680 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100681}
682
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100683static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
684{
685 return lf_text_len(dst, src, size, size, node);
686}
687
William Lallemand5f232402012-04-05 18:02:55 +0200688/*
689 * Write a IP adress to the log string
690 * +X option write in hexadecimal notation, most signifant byte on the left
691 */
692char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
693{
694 char *ret = dst;
695 int iret;
696 char pn[INET6_ADDRSTRLEN];
697
698 if (node->options & LOG_OPT_HEXA) {
699 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
700 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
701 if (iret < 0 || iret > size)
702 return NULL;
703 ret += iret;
704 } else {
705 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
706 ret = lf_text(dst, pn, size, node);
707 if (ret == NULL)
708 return NULL;
709 }
710 return ret;
711}
712
713/*
714 * Write a port to the log
715 * +X option write in hexadecimal notation, most signifant byte on the left
716 */
717char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
718{
719 char *ret = dst;
720 int iret;
721
722 if (node->options & LOG_OPT_HEXA) {
723 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
724 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
725 if (iret < 0 || iret > size)
726 return NULL;
727 ret += iret;
728 } else {
729 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
730 if (ret == NULL)
731 return NULL;
732 }
733 return ret;
734}
735
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100736/* Re-generate the syslog header at the beginning of logline once a second and
737 * return the pointer to the first character after the header.
738 */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100739static char *update_log_hdr(const char *log_tag)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100741 static long tvsec;
742 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100743 int tag_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200745 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200746 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200747 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100748 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200749
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200750 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200751 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200752
Willy Tarreau18324f52014-06-27 18:10:07 +0200753 hdr_len = snprintf(logline, global.max_syslog_len,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100754 "<<<<>%s %2d %02d:%02d:%02d %s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200755 monthname[tm.tm_mon],
756 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100757 global.log_send_hostname ? global.log_send_hostname : "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758 /* WARNING: depending upon implementations, snprintf may return
759 * either -1 or the number of bytes that would be needed to store
760 * the total message. In both cases, we must adjust it.
761 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200762 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
763 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100765 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766 }
767
Willy Tarreau094af4e2015-01-07 15:03:42 +0100768 dataptr[0] = 0; // ensure we get rid of any previous attempt
769
770 tag_len = snprintf(dataptr, logline + global.max_syslog_len - dataptr, "%s[%d]: ", log_tag, pid);
771 if (tag_len < 0 || tag_len > logline + global.max_syslog_len - dataptr)
772 tag_len = logline + global.max_syslog_len - dataptr;
773
774 return dataptr + tag_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100775}
776
777/*
778 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100779 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100780 */
781void send_log(struct proxy *p, int level, const char *format, ...)
782{
783 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100784 char *dataptr;
785 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100786
Willy Tarreau8c97ab52015-01-15 16:29:53 +0100787 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100788 return;
789
Thierry FOURNIERe83766a2015-03-09 17:10:29 +0100790 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 +0100791 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100792
793 va_start(argp, format);
Willy Tarreau18324f52014-06-27 18:10:07 +0200794 data_len += vsnprintf(dataptr, logline + global.max_syslog_len - dataptr, format, argp);
795 if (data_len < 0 || data_len > global.max_syslog_len)
796 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100798
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100799 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100800}
801
802/*
803 * This function sends a syslog message.
804 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100805 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100806 */
807void __send_log(struct proxy *p, int level, char *message, size_t size)
808{
809 static int logfdunix = -1; /* syslog to AF_UNIX socket */
810 static int logfdinet = -1; /* syslog to AF_INET socket */
811 static char *dataptr = NULL;
812 int fac_level;
813 struct list *logsrvs = NULL;
814 struct logsrv *tmp = NULL;
815 int nblogger;
816 char *log_ptr;
817
818 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819
820 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200821 if (!LIST_ISEMPTY(&global.logsrvs)) {
822 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200823 }
824 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200825 if (!LIST_ISEMPTY(&p->logsrvs)) {
826 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200827 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100828 }
829
William Lallemand0f99e342011-10-12 17:50:54 +0200830 if (!logsrvs)
831 return;
832
Robert Tsai81ae1952007-12-05 10:47:29 +0100833 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200834 nblogger = 0;
835 list_for_each_entry(tmp, logsrvs, list) {
836 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100837 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100838 &logfdunix : &logfdinet;
839 int sent;
Willy Tarreau18324f52014-06-27 18:10:07 +0200840 int max;
841 char backup;
Robert Tsai81ae1952007-12-05 10:47:29 +0100842
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200843 nblogger++;
844
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200846 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100848
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200849 if (unlikely(*plogfd < 0)) {
850 /* socket not successfully initialized yet */
851 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
852
853 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
854 Alert("socket for logger #%d failed: %s (errno=%d)\n",
855 nblogger, strerror(errno), errno);
856 continue;
857 }
858 /* we don't want to receive anything on this socket */
859 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
860 /* does nothing under Linux, maybe needed for others */
861 shutdown(*plogfd, SHUT_RD);
862 }
863
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864 /* For each target, we may have a different facility.
865 * We can also have a different log level for each message.
866 * This induces variations in the message header length.
867 * Since we don't want to recompute it each time, nor copy it every
868 * time, we only change the facility in the pre-computed header,
869 * and we change the pointer to the header accordingly.
870 */
William Lallemand0f99e342011-10-12 17:50:54 +0200871 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100872 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 do {
874 *log_ptr = '0' + fac_level % 10;
875 fac_level /= 10;
876 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100877 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200878 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100879
Willy Tarreau18324f52014-06-27 18:10:07 +0200880 max = size - (log_ptr - dataptr);
881 if (max > logsrv->maxlen)
882 max = logsrv->maxlen;
883
884 /* insert a \n at the end of the message, but save what was
885 * there first because we could have different max lengths
886 * for different log targets.
887 */
888 backup = log_ptr[max - 1];
889 log_ptr[max - 1] = '\n';
890
891 sent = sendto(*plogfd, log_ptr, max,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200892 MSG_DONTWAIT | MSG_NOSIGNAL,
893 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Willy Tarreau18324f52014-06-27 18:10:07 +0200894
895 log_ptr[max - 1] = backup;
896
Robert Tsai81ae1952007-12-05 10:47:29 +0100897 if (sent < 0) {
898 Alert("sendto logger #%d failed: %s (errno=%d)\n",
899 nblogger, strerror(errno), errno);
900 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200901 }
902}
903
William Lallemandbddd4fd2012-02-27 11:23:10 +0100904extern fd_set hdr_encode_map[];
905extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100906extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +0100907
Willy Tarreaubaaee002006-06-26 02:48:02 +0200908
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200909const 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 +0100910const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
911 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
912 Set-cookie Updated, unknown, unknown */
913
William Lallemand1d705562012-03-12 12:46:41 +0100914/*
915 * try to write a character if there is enough space, or goto out
916 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100917#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100918 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100919 *(tmplog++) = (x); \
920 } else { \
921 goto out; \
922 } \
923 } while(0)
924
William Lallemand1d705562012-03-12 12:46:41 +0100925
Willy Tarreaudf974472012-12-28 02:44:01 +0100926/* Builds a log line in <dst> based on <list_format>, and stops before reaching
927 * <maxsize> characters. Returns the size of the output string in characters,
928 * not counting the trailing zero which is always added if the resulting size
929 * is not zero.
930 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200931int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200933 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200934 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100935 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200936 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000937 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100938 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000939 char *spc;
940 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +0200941 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100942 int t_request;
943 int hdr;
944 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000945 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100946 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100947 char *ret;
948 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100949 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950
William Lallemandbddd4fd2012-02-27 11:23:10 +0100951 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952
William Lallemandbddd4fd2012-02-27 11:23:10 +0100953 t_request = -1;
954 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
955 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
956
William Lallemand1d705562012-03-12 12:46:41 +0100957 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200958
William Lallemandbddd4fd2012-02-27 11:23:10 +0100959 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100960 if (LIST_ISEMPTY(list_format))
961 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100962
William Lallemand1d705562012-03-12 12:46:41 +0100963 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200964 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +0200965 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100966 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100967 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +0100968
Willy Tarreauc8368452012-12-21 00:09:23 +0100969 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +0100970 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100971 if (!last_isspace) {
972 LOGCHAR(' ');
973 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100974 }
975 break;
976
William Lallemand1d705562012-03-12 12:46:41 +0100977 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100978 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200979 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100980 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100981 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100982 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100983 last_isspace = 0;
984 break;
985
Willy Tarreauc8368452012-12-21 00:09:23 +0100986 case LOG_FMT_EXPR: // sample expression, may be request or response
987 key = NULL;
988 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +0200989 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 +0100990 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +0200991 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 +0100992 if (tmp->options & LOG_OPT_HTTP)
993 ret = encode_chunk(tmplog, dst + maxsize,
994 '%', http_encode_map, key ? &key->data.str : &empty);
995 else
996 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 +0100997 if (ret == 0)
998 goto out;
999 tmplog = ret;
1000 last_isspace = 0;
1001 break;
1002
Willy Tarreau2beef582012-12-20 17:22:52 +01001003 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001004 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001005 if (conn)
1006 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1007 else
1008 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001009 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001010 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001011 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001012 last_isspace = 0;
1013 break;
1014
Willy Tarreau2beef582012-12-20 17:22:52 +01001015 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001016 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001017 if (conn) {
1018 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001019 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001020 } else {
1021 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1022 dst + maxsize - tmplog, tmp);
1023 }
William Lallemand5f232402012-04-05 18:02:55 +02001024 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001025 else
1026 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1027
William Lallemand5f232402012-04-05 18:02:55 +02001028 if (ret == NULL)
1029 goto out;
1030 tmplog = ret;
1031 last_isspace = 0;
1032 break;
1033
Willy Tarreau2beef582012-12-20 17:22:52 +01001034 case LOG_FMT_FRONTENDIP: // %fi
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 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1039 }
1040 else
1041 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1042
William Lallemand1d705562012-03-12 12:46:41 +01001043 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001044 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001045 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001046 last_isspace = 0;
1047 break;
1048
Willy Tarreau2beef582012-12-20 17:22:52 +01001049 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001050 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001051 if (conn) {
1052 conn_get_to_addr(conn);
1053 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001054 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001055 else
1056 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001057 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001058 else
1059 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1060
William Lallemand5f232402012-04-05 18:02:55 +02001061 if (ret == NULL)
1062 goto out;
1063 tmplog = ret;
1064 last_isspace = 0;
1065 break;
1066
Willy Tarreau2beef582012-12-20 17:22:52 +01001067 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001068 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001069 if (conn)
1070 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1071 else
1072 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1073
William Lallemand1d705562012-03-12 12:46:41 +01001074 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001075 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001076 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001077 last_isspace = 0;
1078 break;
1079
Willy Tarreau2beef582012-12-20 17:22:52 +01001080 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001081 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001082 if (conn)
1083 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1084 else
1085 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1086
William Lallemand5f232402012-04-05 18:02:55 +02001087 if (ret == NULL)
1088 goto out;
1089 tmplog = ret;
1090 last_isspace = 0;
1091 break;
1092
Willy Tarreau2beef582012-12-20 17:22:52 +01001093 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001094 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001095 if (conn)
1096 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1097 else
1098 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1099
William Lallemand5f232402012-04-05 18:02:55 +02001100 if (ret == NULL)
1101 goto out;
1102 tmplog = ret;
1103 last_isspace = 0;
1104 break;
1105
Willy Tarreau2beef582012-12-20 17:22:52 +01001106 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001107 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001108 if (conn)
1109 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1110 else
1111 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1112
William Lallemand1d705562012-03-12 12:46:41 +01001113 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001114 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001115 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001116 last_isspace = 0;
1117 break;
1118
William Lallemand1d705562012-03-12 12:46:41 +01001119 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001120 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001121 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1122 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001123 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001124 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001125 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001126 last_isspace = 0;
1127 break;
1128
William Lallemand1d705562012-03-12 12:46:41 +01001129 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001130 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001131 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001132 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001133 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001134 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001135 last_isspace = 0;
1136 break;
1137
Yuxans Yao4e25b012012-10-19 10:36:09 +08001138 case LOG_FMT_DATELOCAL: // %Tl
1139 get_localtime(s->logs.accept_date.tv_sec, &tm);
1140 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1141 if (ret == NULL)
1142 goto out;
1143 tmplog = ret;
1144 last_isspace = 0;
1145 break;
1146
William Lallemand5f232402012-04-05 18:02:55 +02001147 case LOG_FMT_TS: // %Ts
1148 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1149 if (tmp->options & LOG_OPT_HEXA) {
1150 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1151 if (iret < 0 || iret > dst + maxsize - tmplog)
1152 goto out;
1153 last_isspace = 0;
1154 tmplog += iret;
1155 } else {
1156 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1157 if (ret == NULL)
1158 goto out;
1159 tmplog = ret;
1160 last_isspace = 0;
1161 }
1162 break;
1163
William Lallemand1d705562012-03-12 12:46:41 +01001164 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001165 if (tmp->options & LOG_OPT_HEXA) {
1166 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1167 if (iret < 0 || iret > dst + maxsize - tmplog)
1168 goto out;
1169 last_isspace = 0;
1170 tmplog += iret;
1171 } else {
1172 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001173 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001174 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1175 tmplog, 4);
1176 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001177 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001178 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001179 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001180 }
1181 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001182
William Lallemand1d705562012-03-12 12:46:41 +01001183 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001184 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001185 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001186 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001187 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001188 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001189 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001190 break;
1191
Willy Tarreau773d65f2012-10-12 14:56:11 +02001192 case LOG_FMT_FRONTEND_XPRT: // %ft
1193 src = fe->id;
1194 if (tmp->options & LOG_OPT_QUOTE)
1195 LOGCHAR('"');
1196 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1197 if (iret == 0)
1198 goto out;
1199 tmplog += iret;
1200#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001201 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001202 LOGCHAR('~');
1203#endif
1204 if (tmp->options & LOG_OPT_QUOTE)
1205 LOGCHAR('"');
1206 last_isspace = 0;
1207 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001208#ifdef USE_OPENSSL
1209 case LOG_FMT_SSL_CIPHER: // %sslc
1210 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001211 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001212 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001213 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001214 src = ssl_sock_get_cipher_name(conn);
1215 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001216 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1217 if (ret == NULL)
1218 goto out;
1219 tmplog = ret;
1220 last_isspace = 0;
1221 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001222
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001223 case LOG_FMT_SSL_VERSION: // %sslv
1224 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001225 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001226 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001227 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001228 src = ssl_sock_get_proto_version(conn);
1229 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001230 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1231 if (ret == NULL)
1232 goto out;
1233 tmplog = ret;
1234 last_isspace = 0;
1235 break;
1236#endif
William Lallemand1d705562012-03-12 12:46:41 +01001237 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001238 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001239 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001240 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001241 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001242 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001243 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001244 break;
1245
William Lallemand1d705562012-03-12 12:46:41 +01001246 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001247 switch (obj_type(s->target)) {
1248 case OBJ_TYPE_SERVER:
1249 src = objt_server(s->target)->id;
1250 break;
1251 case OBJ_TYPE_APPLET:
1252 src = objt_applet(s->target)->name;
1253 break;
1254 default:
1255 src = "<NOSRV>";
1256 break;
1257 }
William Lallemand5f232402012-04-05 18:02:55 +02001258 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001259 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001260 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001261 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001262 last_isspace = 0;
1263 break;
1264
William Lallemand1d705562012-03-12 12:46:41 +01001265 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001266 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001267 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001268 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001269 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001270 last_isspace = 0;
1271 break;
1272
William Lallemand1d705562012-03-12 12:46:41 +01001273 case LOG_FMT_TW: // %Tw
1274 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001275 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001276 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001277 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001278 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001279 last_isspace = 0;
1280 break;
1281
William Lallemand1d705562012-03-12 12:46:41 +01001282 case LOG_FMT_TC: // %Tc
1283 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001284 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001285 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001286 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001287 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001288 last_isspace = 0;
1289 break;
1290
William Lallemand1d705562012-03-12 12:46:41 +01001291 case LOG_FMT_TR: // %Tr
1292 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001293 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001294 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001295 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001296 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001297 last_isspace = 0;
1298 break;
1299
William Lallemand1d705562012-03-12 12:46:41 +01001300 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001301 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001302 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001303 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001304 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001305 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001306 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001307 last_isspace = 0;
1308 break;
1309
Willy Tarreau2beef582012-12-20 17:22:52 +01001310 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001311 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001312 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001313 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001314 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001315 last_isspace = 0;
1316 break;
1317
William Lallemand1d705562012-03-12 12:46:41 +01001318 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001319 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001320 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001321 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001322 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001323 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001324 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001325 last_isspace = 0;
1326 break;
1327
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001328 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001329 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1330 if (ret == NULL)
1331 goto out;
1332 tmplog = ret;
1333 last_isspace = 0;
1334 break;
1335
Willy Tarreau2beef582012-12-20 17:22:52 +01001336 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001337 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001338 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001339 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001340 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001341 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001342 last_isspace = 0;
1343 break;
1344
Willy Tarreau2beef582012-12-20 17:22:52 +01001345 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001346 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001347 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001348 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001349 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001350 tmplog = ret;
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_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001355 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1356 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001357 *tmplog = '\0';
1358 last_isspace = 0;
1359 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001360
William Lallemand1d705562012-03-12 12:46:41 +01001361 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001362 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1363 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001364 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1365 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 +01001366 last_isspace = 0;
1367 break;
1368
William Lallemand1d705562012-03-12 12:46:41 +01001369 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001370 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001371 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001372 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001373 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001374 last_isspace = 0;
1375 break;
1376
William Lallemand1d705562012-03-12 12:46:41 +01001377 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001378 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001379 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001380 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001381 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001382 last_isspace = 0;
1383 break;
1384
William Lallemand1d705562012-03-12 12:46:41 +01001385 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001386 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001387 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001388 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001389 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001390 last_isspace = 0;
1391 break;
1392
William Lallemand1d705562012-03-12 12:46:41 +01001393 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001394 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001395 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001396 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001397 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001398 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001399 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001400 last_isspace = 0;
1401 break;
1402
William Lallemand1d705562012-03-12 12:46:41 +01001403 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001404 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001405 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001406 ret = ltoa_o((s->si[1].conn_retries>0) ?
1407 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001408 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001409 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001410 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001411 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001412 last_isspace = 0;
1413 break;
1414
William Lallemand1d705562012-03-12 12:46:41 +01001415 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001416 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001417 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001418 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001419 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001420 last_isspace = 0;
1421 break;
1422
William Lallemand1d705562012-03-12 12:46:41 +01001423 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001424 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001425 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001426 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001427 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001428 last_isspace = 0;
1429 break;
1430
William Lallemand1d705562012-03-12 12:46:41 +01001431 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001432 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001433 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001434 if (tmp->options & LOG_OPT_QUOTE)
1435 LOGCHAR('"');
1436 LOGCHAR('{');
1437 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1438 if (hdr)
1439 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001440 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001441 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001442 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001443 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001444 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001445 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001446 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001447 }
1448 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001449 if (tmp->options & LOG_OPT_QUOTE)
1450 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001451 last_isspace = 0;
1452 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001453 break;
1454
William Lallemand1d705562012-03-12 12:46:41 +01001455 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001456 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001457 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001458 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1459 if (hdr > 0)
1460 LOGCHAR(' ');
1461 if (tmp->options & LOG_OPT_QUOTE)
1462 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001463 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001464 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001465 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001466 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001467 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001468 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001469 } else if (!(tmp->options & LOG_OPT_QUOTE))
1470 LOGCHAR('-');
1471 if (tmp->options & LOG_OPT_QUOTE)
1472 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001473 last_isspace = 0;
1474 }
1475 }
1476 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001477
William Lallemand1d705562012-03-12 12:46:41 +01001478
1479 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001480 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001481 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001482 if (tmp->options & LOG_OPT_QUOTE)
1483 LOGCHAR('"');
1484 LOGCHAR('{');
1485 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1486 if (hdr)
1487 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001488 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001489 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001490 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001491 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001492 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001493 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001494 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001495 }
1496 LOGCHAR('}');
1497 last_isspace = 0;
1498 if (tmp->options & LOG_OPT_QUOTE)
1499 LOGCHAR('"');
1500 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001501 break;
1502
William Lallemand1d705562012-03-12 12:46:41 +01001503 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001504 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001505 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001506 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1507 if (hdr > 0)
1508 LOGCHAR(' ');
1509 if (tmp->options & LOG_OPT_QUOTE)
1510 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001511 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001512 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001513 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001514 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001515 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001516 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001517 } else if (!(tmp->options & LOG_OPT_QUOTE))
1518 LOGCHAR('-');
1519 if (tmp->options & LOG_OPT_QUOTE)
1520 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001521 last_isspace = 0;
1522 }
1523 }
1524 break;
1525
William Lallemand1d705562012-03-12 12:46:41 +01001526 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001527 /* Request */
1528 if (tmp->options & LOG_OPT_QUOTE)
1529 LOGCHAR('"');
1530 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001531 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001532 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001533 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001534 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001535 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001536 if (tmp->options & LOG_OPT_QUOTE)
1537 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001538 last_isspace = 0;
1539 break;
William Lallemand5f232402012-04-05 18:02:55 +02001540
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001541 case LOG_FMT_HTTP_PATH: // %HP
1542 uri = txn->uri ? txn->uri : "<BADREQ>";
1543
Willy Tarreaub7636d12015-06-17 19:58:02 +02001544 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001545 LOGCHAR('"');
1546
1547 end = uri + strlen(uri);
1548 // look for the first whitespace character
1549 while (uri < end && !HTTP_IS_SPHT(*uri))
1550 uri++;
1551
1552 // keep advancing past multiple spaces
1553 while (uri < end && HTTP_IS_SPHT(*uri)) {
1554 uri++; nspaces++;
1555 }
1556
1557 // look for first space or question mark after url
1558 spc = uri;
1559 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
1560 spc++;
1561
1562 if (!txn->uri || nspaces == 0) {
1563 chunk.str = "<BADREQ>";
1564 chunk.len = strlen("<BADREQ>");
1565 } else {
1566 chunk.str = uri;
1567 chunk.len = spc - uri;
1568 }
1569
1570 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1571 if (ret == NULL || *ret != '\0')
1572 goto out;
1573
1574 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001575 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001576 LOGCHAR('"');
1577
1578 last_isspace = 0;
1579 break;
1580
1581 case LOG_FMT_HTTP_URI: // %HU
1582 uri = txn->uri ? txn->uri : "<BADREQ>";
1583
Willy Tarreaub7636d12015-06-17 19:58:02 +02001584 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001585 LOGCHAR('"');
1586
1587 end = uri + strlen(uri);
1588 // look for the first whitespace character
1589 while (uri < end && !HTTP_IS_SPHT(*uri))
1590 uri++;
1591
1592 // keep advancing past multiple spaces
1593 while (uri < end && HTTP_IS_SPHT(*uri)) {
1594 uri++; nspaces++;
1595 }
1596
1597 // look for first space after url
1598 spc = uri;
1599 while (spc < end && !HTTP_IS_SPHT(*spc))
1600 spc++;
1601
1602 if (!txn->uri || nspaces == 0) {
1603 chunk.str = "<BADREQ>";
1604 chunk.len = strlen("<BADREQ>");
1605 } else {
1606 chunk.str = uri;
1607 chunk.len = spc - uri;
1608 }
1609
1610 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1611 if (ret == NULL || *ret != '\0')
1612 goto out;
1613
1614 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001615 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001616 LOGCHAR('"');
1617
1618 last_isspace = 0;
1619 break;
1620
1621 case LOG_FMT_HTTP_METHOD: // %HM
1622 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001623 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001624 LOGCHAR('"');
1625
1626 end = uri + strlen(uri);
1627 // look for the first whitespace character
1628 spc = uri;
1629 while (spc < end && !HTTP_IS_SPHT(*spc))
1630 spc++;
1631
1632 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
1633 chunk.str = "<BADREQ>";
1634 chunk.len = strlen("<BADREQ>");
1635 } else {
1636 chunk.str = uri;
1637 chunk.len = spc - uri;
1638 }
1639
1640 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1641 if (ret == NULL || *ret != '\0')
1642 goto out;
1643
1644 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001645 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001646 LOGCHAR('"');
1647
1648 last_isspace = 0;
1649 break;
1650
1651 case LOG_FMT_HTTP_VERSION: // %HV
1652 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001653 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001654 LOGCHAR('"');
1655
1656 end = uri + strlen(uri);
1657 // look for the first whitespace character
1658 while (uri < end && !HTTP_IS_SPHT(*uri))
1659 uri++;
1660
1661 // keep advancing past multiple spaces
1662 while (uri < end && HTTP_IS_SPHT(*uri)) {
1663 uri++; nspaces++;
1664 }
1665
1666 // look for the next whitespace character
1667 while (uri < end && !HTTP_IS_SPHT(*uri))
1668 uri++;
1669
1670 // keep advancing past multiple spaces
1671 while (uri < end && HTTP_IS_SPHT(*uri))
1672 uri++;
1673
1674 if (!txn->uri || nspaces == 0) {
1675 chunk.str = "<BADREQ>";
1676 chunk.len = strlen("<BADREQ>");
1677 } else if (uri == end) {
1678 chunk.str = "HTTP/0.9";
1679 chunk.len = strlen("HTTP/0.9");
1680 } else {
1681 chunk.str = uri;
1682 chunk.len = end - uri;
1683 }
1684
1685 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1686 if (ret == NULL || *ret != '\0')
1687 goto out;
1688
1689 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001690 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001691 LOGCHAR('"');
1692
1693 last_isspace = 0;
1694 break;
1695
William Lallemand5f232402012-04-05 18:02:55 +02001696 case LOG_FMT_COUNTER: // %rt
1697 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001698 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001699 if (iret < 0 || iret > dst + maxsize - tmplog)
1700 goto out;
1701 last_isspace = 0;
1702 tmplog += iret;
1703 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001704 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001705 if (ret == NULL)
1706 goto out;
1707 tmplog = ret;
1708 last_isspace = 0;
1709 }
1710 break;
1711
Willy Tarreau7346acb2014-08-28 15:03:15 +02001712 case LOG_FMT_LOGCNT: // %lc
1713 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001714 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001715 if (iret < 0 || iret > dst + maxsize - tmplog)
1716 goto out;
1717 last_isspace = 0;
1718 tmplog += iret;
1719 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001720 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001721 if (ret == NULL)
1722 goto out;
1723 tmplog = ret;
1724 last_isspace = 0;
1725 }
1726 break;
1727
William Lallemand5f232402012-04-05 18:02:55 +02001728 case LOG_FMT_HOSTNAME: // %H
1729 src = hostname;
1730 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1731 if (ret == NULL)
1732 goto out;
1733 tmplog = ret;
1734 last_isspace = 0;
1735 break;
1736
1737 case LOG_FMT_PID: // %pid
1738 if (tmp->options & LOG_OPT_HEXA) {
1739 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1740 if (iret < 0 || iret > dst + maxsize - tmplog)
1741 goto out;
1742 last_isspace = 0;
1743 tmplog += iret;
1744 } else {
1745 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1746 if (ret == NULL)
1747 goto out;
1748 tmplog = ret;
1749 last_isspace = 0;
1750 }
1751 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001752
1753 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001754 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001755 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02001756 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001757 if (ret == NULL)
1758 goto out;
1759 tmplog = ret;
1760 last_isspace = 0;
1761 break;
1762
William Lallemandbddd4fd2012-02-27 11:23:10 +01001763 }
1764 }
1765
1766out:
William Lallemand1d705562012-03-12 12:46:41 +01001767 /* *tmplog is a unused character */
1768 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001769 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001770
Willy Tarreaubaaee002006-06-26 02:48:02 +02001771}
1772
William Lallemand1d705562012-03-12 12:46:41 +01001773/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001774 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01001775 * Will not log if the frontend has no log defined.
1776 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001777void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01001778{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001779 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01001780 char *tmplog;
1781 int size, err, level;
1782
1783 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001784 err = (s->flags & SF_REDISP) ||
1785 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
1786 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01001787 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02001788 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001789
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001790 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01001791 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001792
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001793 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01001794 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001795
Willy Tarreauabcd5142013-06-11 17:18:02 +02001796 if (s->logs.level) { /* loglevel was overridden */
1797 if (s->logs.level == -1) {
1798 s->logs.logwait = 0; /* logs disabled */
1799 return;
1800 }
1801 level = s->logs.level - 1;
1802 }
1803 else {
1804 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001805 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02001806 level = LOG_ERR;
1807 }
William Lallemand1d705562012-03-12 12:46:41 +01001808
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001809 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001810 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001811 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001812 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001813 }
1814
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001815 tmplog = update_log_hdr(sess->fe->log_tag ? sess->fe->log_tag : global.log_tag);
William Lallemand1d705562012-03-12 12:46:41 +01001816 size = tmplog - logline;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001817 size += build_logline(s, tmplog, global.max_syslog_len - size, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01001818 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001819 sess->fe->log_count++;
1820 __send_log(sess->fe, level, logline, size + 1);
William Lallemand1d705562012-03-12 12:46:41 +01001821 s->logs.logwait = 0;
1822 }
1823}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001824
Willy Tarreaubaaee002006-06-26 02:48:02 +02001825/*
1826 * Local variables:
1827 * c-indent-level: 8
1828 * c-basic-offset: 8
1829 * End:
1830 */