blob: 3724dd47e22653a34f946f56ccdf48bf2a47fbcd [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 */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000114 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000115 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
116 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200117 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100118 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200119 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100120 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
121 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100122 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100123 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
124 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
125 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
126 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
127 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200128 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
129 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100130 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
131 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
132 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
133
134 /* The following tags are deprecated and will be removed soon */
135 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
136 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
137 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
138 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
139 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
140 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
141 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
142 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
143 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
144 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
145 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200146 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100147};
148
Willy Tarreau2beef582012-12-20 17:22:52 +0100149char 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
150char 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";
151char 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 +0100152char *log_format = NULL;
153
Dragan Dosen59cee972015-09-19 22:09:02 +0200154/* This is a global syslog header, common to all outgoing messages. It
155 * begins with the syslog tag and the date that are updated by
156 * update_log_hdr().
157 */
158char *logheader = NULL;
159
160/* This is a global syslog message buffer, common to all outgoing
161 * messages. It contains only the data part.
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100162 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200163char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100164
William Lallemand723b73a2012-02-08 16:37:49 +0100165struct logformat_var_args {
166 char *name;
167 int mask;
168};
169
170struct logformat_var_args var_args_list[] = {
171// global
172 { "M", LOG_OPT_MANDATORY },
173 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200174 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100175 { 0, 0 }
176};
177
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200178/* return the name of the directive used in the current proxy for which we're
179 * currently parsing a header, when it is known.
180 */
181static inline const char *fmt_directive(const struct proxy *curproxy)
182{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100183 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200184 case ARGC_ACL:
185 return "acl";
186 case ARGC_STK:
187 return "stick";
188 case ARGC_TRK:
189 return "track-sc";
190 case ARGC_LOG:
191 return "log-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100192 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100193 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100194 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100195 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200196 case ARGC_UIF:
197 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100198 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200199 return "redirect";
200 case ARGC_CAP:
201 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200202 case ARGC_SRV:
203 return "server";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100204 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200205 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100206 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200207}
208
William Lallemand723b73a2012-02-08 16:37:49 +0100209/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100210 * callback used to configure addr source retrieval
211 */
212int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
213{
214 curproxy->options2 |= PR_O2_SRC_ADDR;
215
216 return 0;
217}
218
219
220/*
William Lallemand723b73a2012-02-08 16:37:49 +0100221 * Parse args in a logformat_var
222 */
223int parse_logformat_var_args(char *args, struct logformat_node *node)
224{
225 int i = 0;
226 int end = 0;
227 int flags = 0; // 1 = + 2 = -
228 char *sp = NULL; // start pointer
229
230 if (args == NULL)
231 return 1;
232
233 while (1) {
234 if (*args == '\0')
235 end = 1;
236
237 if (*args == '+') {
238 // add flag
239 sp = args + 1;
240 flags = 1;
241 }
242 if (*args == '-') {
243 // delete flag
244 sp = args + 1;
245 flags = 2;
246 }
247
248 if (*args == '\0' || *args == ',') {
249 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100250 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100251 if (strcmp(sp, var_args_list[i].name) == 0) {
252 if (flags == 1) {
253 node->options |= var_args_list[i].mask;
254 break;
255 } else if (flags == 2) {
256 node->options &= ~var_args_list[i].mask;
257 break;
258 }
259 }
260 }
261 sp = NULL;
262 if (end)
263 break;
264 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100265 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100266 }
267 return 0;
268}
269
270/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100271 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
272 * must pass the args part in the <arg> pointer with its length in <arg_len>,
273 * and varname with its length in <var> and <var_len> respectively. <arg> is
274 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100275 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100276int 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 +0100277{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100278 int j;
279 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100280
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100281 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
282 if (strlen(logformat_keywords[j].name) == var_len &&
283 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
284 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
285 node = calloc(1, sizeof(struct logformat_node));
286 node->type = logformat_keywords[j].type;
287 node->options = *defoptions;
288 if (arg_len) {
289 node->arg = my_strndup(arg, arg_len);
290 parse_logformat_var_args(node->arg, node);
291 }
292 if (node->type == LOG_FMT_GLOBAL) {
293 *defoptions = node->options;
294 free(node->arg);
295 free(node);
296 } else {
297 if (logformat_keywords[j].config_callback &&
298 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100299 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100300 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100301 curproxy->to_log |= logformat_keywords[j].lw;
302 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100303 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100304 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100305 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200306 curproxy->conf.args.file, curproxy->conf.args.line,
307 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100308 return 0;
309 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100310 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200311 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
312 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100313 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100314 }
William Lallemand723b73a2012-02-08 16:37:49 +0100315 }
316 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100317
318 j = var[var_len];
319 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100320 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 +0200321 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100322 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100323 return -1;
324}
325
326/*
327 * push to the logformat linked list
328 *
329 * start: start pointer
330 * end: end text pointer
331 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100332 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100333 *
334 * LOG_TEXT: copy chars from start to end excluding end.
335 *
336*/
William Lallemand1d705562012-03-12 12:46:41 +0100337void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100338{
339 char *str;
340
Willy Tarreaua3571662012-12-20 21:59:12 +0100341 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100342 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100343 str = calloc(end - start + 1, 1);
344 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100345 str[end - start] = '\0';
346 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100347 node->type = LOG_FMT_TEXT; // type string
348 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100349 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100350 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100351 node->type = LOG_FMT_SEPARATOR;
352 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100353 }
354}
355
356/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100357 * Parse the sample fetch expression <text> and add a node to <list_format> upon
358 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200359 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100360 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100361void 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 +0100362{
363 char *cmd[2];
364 struct sample_expr *expr;
365 struct logformat_node *node;
366 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100367 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100368
369 cmd[0] = text;
370 cmd[1] = "";
371 cmd_arg = 0;
372
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100373 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100374 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200375 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
376 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100377 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100378 return;
379 }
380
381 node = calloc(1, sizeof(struct logformat_node));
382 node->type = LOG_FMT_EXPR;
383 node->expr = expr;
384 node->options = options;
385
386 if (arg_len) {
387 node->arg = my_strndup(arg, arg_len);
388 parse_logformat_var_args(node->arg, node);
389 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100390 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100391 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
392
Willy Tarreau434c57c2013-01-08 01:10:24 +0100393 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100394 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
395
Willy Tarreau434c57c2013-01-08 01:10:24 +0100396 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200397 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
398 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
399 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100400
Willy Tarreauc8368452012-12-21 00:09:23 +0100401 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
402 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100403 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100404
William Lallemand65ad6e12014-01-31 15:08:02 +0100405 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
406 * needed with some sample fetches (eg: ssl*). We always set it for
407 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100408 */
409 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100410 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100411 LIST_ADDQ(list_format, &node->list);
412}
413
414/*
William Lallemand723b73a2012-02-08 16:37:49 +0100415 * Parse the log_format string and fill a linked list.
416 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200417 * You can set arguments using { } : %{many arguments}varname.
418 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100419 *
420 * str: the string to parse
421 * curproxy: the proxy affected
422 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100423 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100424 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100425 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100426void 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 +0100427{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100428 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100429 char *arg = NULL; /* start pointer for args */
430 char *var = NULL; /* start pointer for vars */
431 int arg_len = 0;
432 int var_len = 0;
433 int cformat; /* current token format */
434 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100435 struct logformat_node *tmplf, *back;
436
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100437 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200438 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200439
William Lallemand723b73a2012-02-08 16:37:49 +0100440 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100441 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100442 LIST_DEL(&tmplf->list);
443 free(tmplf);
444 }
445
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100446 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100447 pformat = cformat;
448
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100449 if (!*str)
450 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100451
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100452 /* The prinicple of the two-step state machine below is to first detect a change, and
453 * second have all common paths processed at one place. The common paths are the ones
454 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
455 * We use the common LF_INIT state to dispatch to the different final states.
456 */
457 switch (pformat) {
458 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100459 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100460 arg_len = var_len = 0;
461 if (*str == '{') { // optional argument
462 cformat = LF_STARG;
463 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100464 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100465 else if (*str == '[') {
466 cformat = LF_STEXPR;
467 var = str + 1; // store expr in variable name
468 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100469 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100470 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100471 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100472 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100473 else if (*str == '%')
474 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100475 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100476 /* single '%' followed by blank or digit, send them both */
477 cformat = LF_TEXT;
478 pformat = LF_TEXT; /* finally we include the previous char as well */
479 sp = str - 1; /* send both the '%' and the current char */
480 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",
481 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
482
483 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100484 else
485 cformat = LF_INIT; // handle other cases of litterals
486 break;
487
488 case LF_STARG: // text immediately following '%{'
489 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100490 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100491 arg_len = str - arg;
492 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100493 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100494 break;
495
496 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100497 if (*str == '[') {
498 cformat = LF_STEXPR;
499 var = str + 1; // store expr in variable name
500 break;
501 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100502 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100503 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100504 var = str;
505 break;
506 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200507 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
508 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100509 cformat = LF_INIT;
510 break;
511
Willy Tarreauc8368452012-12-21 00:09:23 +0100512 case LF_STEXPR: // text immediately following '%['
513 if (*str == ']') { // end of arg
514 cformat = LF_EDEXPR;
515 var_len = str - var;
516 *str = 0; // needed for parsing the expression
517 }
518 break;
519
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100520 case LF_VAR: // text part of a variable name
521 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100522 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100523 cformat = LF_INIT; // not variable name anymore
524 break;
525
Willy Tarreauc8368452012-12-21 00:09:23 +0100526 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100527 cformat = LF_INIT;
528 }
529
530 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
531 switch (*str) {
532 case '%': cformat = LF_STARTVAR; break;
533 case ' ': cformat = LF_SEPARATOR; break;
534 case 0 : cformat = LF_END; break;
535 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100536 }
537 }
538
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100539 if (cformat != pformat || pformat == LF_SEPARATOR) {
540 switch (pformat) {
541 case LF_VAR:
542 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
543 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100544 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100545 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100546 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100547 case LF_TEXT:
548 case LF_SEPARATOR:
549 add_to_logformat_list(sp, str, pformat, list_format);
550 break;
551 }
552 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100553 }
554 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100555
Willy Tarreauc8368452012-12-21 00:09:23 +0100556 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200557 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
558 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
559 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100560
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100561 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100562}
563
Willy Tarreaubaaee002006-06-26 02:48:02 +0200564/*
565 * Displays the message on stderr with the date and pid. Overrides the quiet
566 * mode during startup.
567 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200568void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569{
570 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200571 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572
573 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
574 va_start(argp, fmt);
575
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200576 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200578 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579 vfprintf(stderr, fmt, argp);
580 fflush(stderr);
581 va_end(argp);
582 }
583}
584
585
586/*
587 * Displays the message on stderr with the date and pid.
588 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200589void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590{
591 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200592 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200593
594 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
595 va_start(argp, fmt);
596
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200597 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200599 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200600 vfprintf(stderr, fmt, argp);
601 fflush(stderr);
602 va_end(argp);
603 }
604}
605
606/*
607 * Displays the message on <out> only if quiet mode is not set.
608 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200609void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610{
611 va_list argp;
612
613 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
614 va_start(argp, fmt);
615 vfprintf(out, fmt, argp);
616 fflush(out);
617 va_end(argp);
618 }
619}
620
621/*
622 * returns log level for <lev> or -1 if not found.
623 */
624int get_log_level(const char *lev)
625{
626 int level;
627
628 level = NB_LOG_LEVELS - 1;
629 while (level >= 0 && strcmp(log_levels[level], lev))
630 level--;
631
632 return level;
633}
634
635
636/*
637 * returns log facility for <fac> or -1 if not found.
638 */
639int get_log_facility(const char *fac)
640{
641 int facility;
642
643 facility = NB_LOG_FACILITIES - 1;
644 while (facility >= 0 && strcmp(log_facilities[facility], fac))
645 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100646
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 return facility;
648}
649
William Lallemanda1cc3812012-02-08 16:38:44 +0100650/*
651 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200652 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100653 *
654 * Return the adress of the \0 character, or NULL on error
655 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100656char *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 +0100657{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100658 if (size < 2)
659 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100660
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100661 if (node->options & LOG_OPT_QUOTE) {
662 *(dst++) = '"';
663 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100664 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100665
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100666 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100667 if (++len > size)
668 len = size;
669 len = strlcpy2(dst, src, len);
670
671 size -= len;
672 dst += len;
673 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100674 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
675 if (size < 2)
676 return NULL;
677 *(dst++) = '-';
678 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100679
680 if (node->options & LOG_OPT_QUOTE) {
681 if (size < 2)
682 return NULL;
683 *(dst++) = '"';
684 }
685
686 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100687 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100688}
689
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100690static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
691{
692 return lf_text_len(dst, src, size, size, node);
693}
694
William Lallemand5f232402012-04-05 18:02:55 +0200695/*
696 * Write a IP adress to the log string
697 * +X option write in hexadecimal notation, most signifant byte on the left
698 */
699char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
700{
701 char *ret = dst;
702 int iret;
703 char pn[INET6_ADDRSTRLEN];
704
705 if (node->options & LOG_OPT_HEXA) {
706 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
707 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
708 if (iret < 0 || iret > size)
709 return NULL;
710 ret += iret;
711 } else {
712 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
713 ret = lf_text(dst, pn, size, node);
714 if (ret == NULL)
715 return NULL;
716 }
717 return ret;
718}
719
720/*
721 * Write a port to the log
722 * +X option write in hexadecimal notation, most signifant byte on the left
723 */
724char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
725{
726 char *ret = dst;
727 int iret;
728
729 if (node->options & LOG_OPT_HEXA) {
730 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
731 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
732 if (iret < 0 || iret > size)
733 return NULL;
734 ret += iret;
735 } else {
736 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
737 if (ret == NULL)
738 return NULL;
739 }
740 return ret;
741}
742
Dragan Dosen59cee972015-09-19 22:09:02 +0200743/* Re-generate the syslog header at the beginning of logheader once a second and
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100744 * return the pointer to the first character after the header.
745 */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100746static char *update_log_hdr(const char *log_tag)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100748 static long tvsec;
749 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100750 int tag_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200752 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200754 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100755 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200756
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200757 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200758 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759
Dragan Dosen59cee972015-09-19 22:09:02 +0200760 hdr_len = snprintf(logheader, global.max_syslog_len,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100761 "<<<<>%s %2d %02d:%02d:%02d %s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200762 monthname[tm.tm_mon],
763 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100764 global.log_send_hostname ? global.log_send_hostname : "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765 /* WARNING: depending upon implementations, snprintf may return
766 * either -1 or the number of bytes that would be needed to store
767 * the total message. In both cases, we must adjust it.
768 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200769 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
770 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771
Dragan Dosen59cee972015-09-19 22:09:02 +0200772 dataptr = logheader + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773 }
774
Willy Tarreau094af4e2015-01-07 15:03:42 +0100775 dataptr[0] = 0; // ensure we get rid of any previous attempt
776
Dragan Dosen59cee972015-09-19 22:09:02 +0200777 tag_len = snprintf(dataptr, logheader + global.max_syslog_len - dataptr, "%s[%d]: ", log_tag, pid);
778 if (tag_len < 0 || tag_len > logheader + global.max_syslog_len - dataptr)
779 tag_len = logheader + global.max_syslog_len - dataptr;
Willy Tarreau094af4e2015-01-07 15:03:42 +0100780
781 return dataptr + tag_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100782}
783
784/*
Dragan Dosen59cee972015-09-19 22:09:02 +0200785 * This function sends the syslog message using a printf format string. It
786 * expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100787 */
788void send_log(struct proxy *p, int level, const char *format, ...)
789{
790 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100791 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100792
Willy Tarreau8c97ab52015-01-15 16:29:53 +0100793 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100794 return;
795
William Lallemand2a4a44f2012-02-06 16:00:33 +0100796 va_start(argp, format);
Dragan Dosen59cee972015-09-19 22:09:02 +0200797 data_len = vsnprintf(logline, global.max_syslog_len, format, argp);
Willy Tarreau18324f52014-06-27 18:10:07 +0200798 if (data_len < 0 || data_len > global.max_syslog_len)
799 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200800 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100801
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100802 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100803}
804
805/*
806 * This function sends a syslog message.
807 * It doesn't care about errors nor does it report them.
Dragan Dosen59cee972015-09-19 22:09:02 +0200808 * It overrides the last byte of the vector with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100809 */
810void __send_log(struct proxy *p, int level, char *message, size_t size)
811{
Dragan Dosen609ac2a2015-09-16 18:25:42 +0200812 static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { };
813 static struct msghdr msghdr = {
814 .msg_iov = iovec,
815 .msg_iovlen = NB_MSG_IOVEC_ELEMENTS
816 };
William Lallemand2a4a44f2012-02-06 16:00:33 +0100817 static int logfdunix = -1; /* syslog to AF_UNIX socket */
818 static int logfdinet = -1; /* syslog to AF_INET socket */
819 static char *dataptr = NULL;
820 int fac_level;
821 struct list *logsrvs = NULL;
822 struct logsrv *tmp = NULL;
823 int nblogger;
Dragan Dosen59cee972015-09-19 22:09:02 +0200824 char *log_tag = global.log_tag;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100825 char *log_ptr;
Dragan Dosen59cee972015-09-19 22:09:02 +0200826 char *hdr_ptr;
827 size_t hdr_size;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100828
829 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830
831 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200832 if (!LIST_ISEMPTY(&global.logsrvs)) {
833 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834 }
835 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200836 if (!LIST_ISEMPTY(&p->logsrvs)) {
837 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 }
Dragan Dosen59cee972015-09-19 22:09:02 +0200839 if (p->log_tag) {
840 log_tag = p->log_tag;
841 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100842 }
843
William Lallemand0f99e342011-10-12 17:50:54 +0200844 if (!logsrvs)
845 return;
846
Dragan Dosen59cee972015-09-19 22:09:02 +0200847 hdr_ptr = update_log_hdr(log_tag);
848 hdr_size = hdr_ptr - logheader;
849
Robert Tsai81ae1952007-12-05 10:47:29 +0100850 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200851 nblogger = 0;
852 list_for_each_entry(tmp, logsrvs, list) {
853 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100854 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100855 &logfdunix : &logfdinet;
856 int sent;
Dragan Dosen59cee972015-09-19 22:09:02 +0200857 int hdr_max = 0;
858 int max = 1;
Willy Tarreau18324f52014-06-27 18:10:07 +0200859 char backup;
Robert Tsai81ae1952007-12-05 10:47:29 +0100860
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200861 nblogger++;
862
Willy Tarreaubaaee002006-06-26 02:48:02 +0200863 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200864 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200865 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100866
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200867 if (unlikely(*plogfd < 0)) {
868 /* socket not successfully initialized yet */
869 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
870
871 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
872 Alert("socket for logger #%d failed: %s (errno=%d)\n",
873 nblogger, strerror(errno), errno);
874 continue;
875 }
876 /* we don't want to receive anything on this socket */
877 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
878 /* does nothing under Linux, maybe needed for others */
879 shutdown(*plogfd, SHUT_RD);
880 }
881
Willy Tarreaubaaee002006-06-26 02:48:02 +0200882 /* For each target, we may have a different facility.
883 * We can also have a different log level for each message.
884 * This induces variations in the message header length.
885 * Since we don't want to recompute it each time, nor copy it every
886 * time, we only change the facility in the pre-computed header,
887 * and we change the pointer to the header accordingly.
888 */
William Lallemand0f99e342011-10-12 17:50:54 +0200889 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
Dragan Dosen59cee972015-09-19 22:09:02 +0200890 hdr_ptr = logheader + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200891 do {
Dragan Dosen59cee972015-09-19 22:09:02 +0200892 *hdr_ptr = '0' + fac_level % 10;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893 fac_level /= 10;
Dragan Dosen59cee972015-09-19 22:09:02 +0200894 hdr_ptr--;
895 } while (fac_level && hdr_ptr > logheader);
896 *hdr_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100897
Dragan Dosen59cee972015-09-19 22:09:02 +0200898 log_ptr = dataptr;
Willy Tarreau18324f52014-06-27 18:10:07 +0200899
Dragan Dosen59cee972015-09-19 22:09:02 +0200900 hdr_max = hdr_size - (hdr_ptr - logheader);
901
902 if (unlikely(hdr_size >= logsrv->maxlen)) {
903 hdr_max = MIN(hdr_max, logsrv->maxlen) - 1;
904 goto send;
905 }
906
907 max = MIN(size, logsrv->maxlen - hdr_max);
908
909 log_ptr += max - 1;
910
911send:
Willy Tarreau18324f52014-06-27 18:10:07 +0200912 /* insert a \n at the end of the message, but save what was
913 * there first because we could have different max lengths
914 * for different log targets.
915 */
Dragan Dosen59cee972015-09-19 22:09:02 +0200916 backup = *log_ptr;
917 *log_ptr = '\n';
Willy Tarreau18324f52014-06-27 18:10:07 +0200918
Dragan Dosen59cee972015-09-19 22:09:02 +0200919 iovec[0].iov_base = hdr_ptr;
920 iovec[0].iov_len = hdr_max;
921 iovec[1].iov_base = dataptr;
922 iovec[1].iov_len = max;
Dragan Dosen609ac2a2015-09-16 18:25:42 +0200923
924 msghdr.msg_name = (struct sockaddr *)&logsrv->addr;
925 msghdr.msg_namelen = get_addr_len(&logsrv->addr);
926
927 sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL);
Willy Tarreau18324f52014-06-27 18:10:07 +0200928
Dragan Dosen59cee972015-09-19 22:09:02 +0200929 *log_ptr = backup;
Willy Tarreau18324f52014-06-27 18:10:07 +0200930
Robert Tsai81ae1952007-12-05 10:47:29 +0100931 if (sent < 0) {
Dragan Dosen609ac2a2015-09-16 18:25:42 +0200932 Alert("sendmsg logger #%d failed: %s (errno=%d)\n",
Robert Tsai81ae1952007-12-05 10:47:29 +0100933 nblogger, strerror(errno), errno);
934 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935 }
936}
937
William Lallemandbddd4fd2012-02-27 11:23:10 +0100938extern fd_set hdr_encode_map[];
939extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100940extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +0100941
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200943const 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 +0100944const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
945 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
946 Set-cookie Updated, unknown, unknown */
947
William Lallemand1d705562012-03-12 12:46:41 +0100948/*
949 * try to write a character if there is enough space, or goto out
950 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100951#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100952 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100953 *(tmplog++) = (x); \
954 } else { \
955 goto out; \
956 } \
957 } while(0)
958
William Lallemand1d705562012-03-12 12:46:41 +0100959
Willy Tarreaudf974472012-12-28 02:44:01 +0100960/* Builds a log line in <dst> based on <list_format>, and stops before reaching
961 * <maxsize> characters. Returns the size of the output string in characters,
962 * not counting the trailing zero which is always added if the resulting size
963 * is not zero.
964 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200965int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200966{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200967 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200968 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100969 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200970 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000971 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100972 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000973 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000974 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000975 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +0200976 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100977 int t_request;
978 int hdr;
979 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000980 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100981 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100982 char *ret;
983 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100984 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200985
William Lallemandbddd4fd2012-02-27 11:23:10 +0100986 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200987
William Lallemandbddd4fd2012-02-27 11:23:10 +0100988 t_request = -1;
989 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
990 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
991
William Lallemand1d705562012-03-12 12:46:41 +0100992 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200993
William Lallemandbddd4fd2012-02-27 11:23:10 +0100994 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100995 if (LIST_ISEMPTY(list_format))
996 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100997
William Lallemand1d705562012-03-12 12:46:41 +0100998 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200999 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +02001000 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +01001001 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001002 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +01001003
Willy Tarreauc8368452012-12-21 00:09:23 +01001004 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +01001005 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +01001006 if (!last_isspace) {
1007 LOGCHAR(' ');
1008 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001009 }
1010 break;
1011
William Lallemand1d705562012-03-12 12:46:41 +01001012 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001013 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001014 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001015 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001016 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001017 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001018 last_isspace = 0;
1019 break;
1020
Willy Tarreauc8368452012-12-21 00:09:23 +01001021 case LOG_FMT_EXPR: // sample expression, may be request or response
1022 key = NULL;
1023 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +02001024 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 +01001025 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +02001026 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 +01001027 if (tmp->options & LOG_OPT_HTTP)
1028 ret = encode_chunk(tmplog, dst + maxsize,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001029 '%', http_encode_map, key ? &key->data.u.str : &empty);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001030 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001031 ret = lf_text_len(tmplog, key ? key->data.u.str.str : NULL, key ? key->data.u.str.len : 0, dst + maxsize - tmplog, tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +01001032 if (ret == 0)
1033 goto out;
1034 tmplog = ret;
1035 last_isspace = 0;
1036 break;
1037
Willy Tarreau2beef582012-12-20 17:22:52 +01001038 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001039 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001040 if (conn)
1041 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1042 else
1043 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001044 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001045 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001046 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001047 last_isspace = 0;
1048 break;
1049
Willy Tarreau2beef582012-12-20 17:22:52 +01001050 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001051 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001052 if (conn) {
1053 if (conn->addr.from.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.from,
1057 dst + maxsize - tmplog, tmp);
1058 }
William Lallemand5f232402012-04-05 18:02:55 +02001059 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001060 else
1061 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1062
William Lallemand5f232402012-04-05 18:02:55 +02001063 if (ret == NULL)
1064 goto out;
1065 tmplog = ret;
1066 last_isspace = 0;
1067 break;
1068
Willy Tarreau2beef582012-12-20 17:22:52 +01001069 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001070 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001071 if (conn) {
1072 conn_get_to_addr(conn);
1073 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1074 }
1075 else
1076 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1077
William Lallemand1d705562012-03-12 12:46:41 +01001078 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001079 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001080 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001081 last_isspace = 0;
1082 break;
1083
Willy Tarreau2beef582012-12-20 17:22:52 +01001084 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001085 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001086 if (conn) {
1087 conn_get_to_addr(conn);
1088 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001089 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001090 else
1091 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001092 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001093 else
1094 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1095
William Lallemand5f232402012-04-05 18:02:55 +02001096 if (ret == NULL)
1097 goto out;
1098 tmplog = ret;
1099 last_isspace = 0;
1100 break;
1101
Willy Tarreau2beef582012-12-20 17:22:52 +01001102 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001103 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001104 if (conn)
1105 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1106 else
1107 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1108
William Lallemand1d705562012-03-12 12:46:41 +01001109 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001110 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001111 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001112 last_isspace = 0;
1113 break;
1114
Willy Tarreau2beef582012-12-20 17:22:52 +01001115 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001116 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001117 if (conn)
1118 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1119 else
1120 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1121
William Lallemand5f232402012-04-05 18:02:55 +02001122 if (ret == NULL)
1123 goto out;
1124 tmplog = ret;
1125 last_isspace = 0;
1126 break;
1127
Willy Tarreau2beef582012-12-20 17:22:52 +01001128 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001129 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001130 if (conn)
1131 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1132 else
1133 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1134
William Lallemand5f232402012-04-05 18:02:55 +02001135 if (ret == NULL)
1136 goto out;
1137 tmplog = ret;
1138 last_isspace = 0;
1139 break;
1140
Willy Tarreau2beef582012-12-20 17:22:52 +01001141 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001142 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001143 if (conn)
1144 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1145 else
1146 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1147
William Lallemand1d705562012-03-12 12:46:41 +01001148 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001149 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001150 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001151 last_isspace = 0;
1152 break;
1153
William Lallemand1d705562012-03-12 12:46:41 +01001154 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001155 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001156 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1157 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001158 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001159 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001160 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001161 last_isspace = 0;
1162 break;
1163
William Lallemand1d705562012-03-12 12:46:41 +01001164 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001165 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001166 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001167 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001168 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001169 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001170 last_isspace = 0;
1171 break;
1172
Yuxans Yao4e25b012012-10-19 10:36:09 +08001173 case LOG_FMT_DATELOCAL: // %Tl
1174 get_localtime(s->logs.accept_date.tv_sec, &tm);
1175 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1176 if (ret == NULL)
1177 goto out;
1178 tmplog = ret;
1179 last_isspace = 0;
1180 break;
1181
William Lallemand5f232402012-04-05 18:02:55 +02001182 case LOG_FMT_TS: // %Ts
1183 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1184 if (tmp->options & LOG_OPT_HEXA) {
1185 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1186 if (iret < 0 || iret > dst + maxsize - tmplog)
1187 goto out;
1188 last_isspace = 0;
1189 tmplog += iret;
1190 } else {
1191 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1192 if (ret == NULL)
1193 goto out;
1194 tmplog = ret;
1195 last_isspace = 0;
1196 }
1197 break;
1198
William Lallemand1d705562012-03-12 12:46:41 +01001199 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001200 if (tmp->options & LOG_OPT_HEXA) {
1201 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1202 if (iret < 0 || iret > dst + maxsize - tmplog)
1203 goto out;
1204 last_isspace = 0;
1205 tmplog += iret;
1206 } else {
1207 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001208 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001209 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1210 tmplog, 4);
1211 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001212 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001213 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001214 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001215 }
1216 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001217
William Lallemand1d705562012-03-12 12:46:41 +01001218 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001219 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001220 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001221 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001222 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001223 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001224 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001225 break;
1226
Willy Tarreau773d65f2012-10-12 14:56:11 +02001227 case LOG_FMT_FRONTEND_XPRT: // %ft
1228 src = fe->id;
1229 if (tmp->options & LOG_OPT_QUOTE)
1230 LOGCHAR('"');
1231 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1232 if (iret == 0)
1233 goto out;
1234 tmplog += iret;
1235#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001236 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001237 LOGCHAR('~');
1238#endif
1239 if (tmp->options & LOG_OPT_QUOTE)
1240 LOGCHAR('"');
1241 last_isspace = 0;
1242 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001243#ifdef USE_OPENSSL
1244 case LOG_FMT_SSL_CIPHER: // %sslc
1245 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001246 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001247 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001248 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001249 src = ssl_sock_get_cipher_name(conn);
1250 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001251 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1252 if (ret == NULL)
1253 goto out;
1254 tmplog = ret;
1255 last_isspace = 0;
1256 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001257
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001258 case LOG_FMT_SSL_VERSION: // %sslv
1259 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001260 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001261 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001262 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001263 src = ssl_sock_get_proto_version(conn);
1264 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001265 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1266 if (ret == NULL)
1267 goto out;
1268 tmplog = ret;
1269 last_isspace = 0;
1270 break;
1271#endif
William Lallemand1d705562012-03-12 12:46:41 +01001272 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001273 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001274 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001275 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001276 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001277 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001278 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001279 break;
1280
William Lallemand1d705562012-03-12 12:46:41 +01001281 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001282 switch (obj_type(s->target)) {
1283 case OBJ_TYPE_SERVER:
1284 src = objt_server(s->target)->id;
1285 break;
1286 case OBJ_TYPE_APPLET:
1287 src = objt_applet(s->target)->name;
1288 break;
1289 default:
1290 src = "<NOSRV>";
1291 break;
1292 }
William Lallemand5f232402012-04-05 18:02:55 +02001293 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
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_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001301 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001302 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001303 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001304 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001305 last_isspace = 0;
1306 break;
1307
William Lallemand1d705562012-03-12 12:46:41 +01001308 case LOG_FMT_TW: // %Tw
1309 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001310 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001311 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001312 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001313 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001314 last_isspace = 0;
1315 break;
1316
William Lallemand1d705562012-03-12 12:46:41 +01001317 case LOG_FMT_TC: // %Tc
1318 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001319 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001320 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001321 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001322 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001323 last_isspace = 0;
1324 break;
1325
William Lallemand1d705562012-03-12 12:46:41 +01001326 case LOG_FMT_TR: // %Tr
1327 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001328 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001329 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001330 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001331 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001332 last_isspace = 0;
1333 break;
1334
William Lallemand1d705562012-03-12 12:46:41 +01001335 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001336 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001337 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001338 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001339 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001340 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_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001346 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001347 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001348 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001349 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001350 last_isspace = 0;
1351 break;
1352
William Lallemand1d705562012-03-12 12:46:41 +01001353 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001354 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001355 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001356 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001357 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001358 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001359 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001360 last_isspace = 0;
1361 break;
1362
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001363 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001364 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1365 if (ret == NULL)
1366 goto out;
1367 tmplog = ret;
1368 last_isspace = 0;
1369 break;
1370
Willy Tarreau2beef582012-12-20 17:22:52 +01001371 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001372 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001373 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001374 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001375 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001376 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001377 last_isspace = 0;
1378 break;
1379
Willy Tarreau2beef582012-12-20 17:22:52 +01001380 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001381 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001382 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001383 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001384 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001385 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001386 last_isspace = 0;
1387 break;
1388
William Lallemand1d705562012-03-12 12:46:41 +01001389 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001390 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1391 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001392 *tmplog = '\0';
1393 last_isspace = 0;
1394 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001395
William Lallemand1d705562012-03-12 12:46:41 +01001396 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001397 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1398 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001399 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1400 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 +01001401 last_isspace = 0;
1402 break;
1403
William Lallemand1d705562012-03-12 12:46:41 +01001404 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001405 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001406 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001407 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001408 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001409 last_isspace = 0;
1410 break;
1411
William Lallemand1d705562012-03-12 12:46:41 +01001412 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001413 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001414 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001415 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001416 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001417 last_isspace = 0;
1418 break;
1419
William Lallemand1d705562012-03-12 12:46:41 +01001420 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001421 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001422 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001423 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001424 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001425 last_isspace = 0;
1426 break;
1427
William Lallemand1d705562012-03-12 12:46:41 +01001428 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001429 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001430 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001431 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001432 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001433 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001434 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001435 last_isspace = 0;
1436 break;
1437
William Lallemand1d705562012-03-12 12:46:41 +01001438 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001439 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001440 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001441 ret = ltoa_o((s->si[1].conn_retries>0) ?
1442 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001443 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001444 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001445 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001446 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001447 last_isspace = 0;
1448 break;
1449
William Lallemand1d705562012-03-12 12:46:41 +01001450 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001451 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001452 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001453 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001454 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001455 last_isspace = 0;
1456 break;
1457
William Lallemand1d705562012-03-12 12:46:41 +01001458 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001459 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001460 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001461 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001462 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001463 last_isspace = 0;
1464 break;
1465
William Lallemand1d705562012-03-12 12:46:41 +01001466 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001467 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001468 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001469 if (tmp->options & LOG_OPT_QUOTE)
1470 LOGCHAR('"');
1471 LOGCHAR('{');
1472 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1473 if (hdr)
1474 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001475 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001476 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001477 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001478 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001479 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001480 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001481 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001482 }
1483 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001484 if (tmp->options & LOG_OPT_QUOTE)
1485 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001486 last_isspace = 0;
1487 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001488 break;
1489
William Lallemand1d705562012-03-12 12:46:41 +01001490 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001491 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001492 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001493 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1494 if (hdr > 0)
1495 LOGCHAR(' ');
1496 if (tmp->options & LOG_OPT_QUOTE)
1497 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001498 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001499 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001500 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001501 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001502 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001503 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001504 } else if (!(tmp->options & LOG_OPT_QUOTE))
1505 LOGCHAR('-');
1506 if (tmp->options & LOG_OPT_QUOTE)
1507 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001508 last_isspace = 0;
1509 }
1510 }
1511 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001512
William Lallemand1d705562012-03-12 12:46:41 +01001513
1514 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001515 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001516 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001517 if (tmp->options & LOG_OPT_QUOTE)
1518 LOGCHAR('"');
1519 LOGCHAR('{');
1520 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1521 if (hdr)
1522 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001523 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001524 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001525 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001526 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001527 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001528 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001529 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001530 }
1531 LOGCHAR('}');
1532 last_isspace = 0;
1533 if (tmp->options & LOG_OPT_QUOTE)
1534 LOGCHAR('"');
1535 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001536 break;
1537
William Lallemand1d705562012-03-12 12:46:41 +01001538 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001539 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001540 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001541 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1542 if (hdr > 0)
1543 LOGCHAR(' ');
1544 if (tmp->options & LOG_OPT_QUOTE)
1545 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001546 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001547 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001548 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001549 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001550 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001551 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001552 } else if (!(tmp->options & LOG_OPT_QUOTE))
1553 LOGCHAR('-');
1554 if (tmp->options & LOG_OPT_QUOTE)
1555 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001556 last_isspace = 0;
1557 }
1558 }
1559 break;
1560
William Lallemand1d705562012-03-12 12:46:41 +01001561 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001562 /* Request */
1563 if (tmp->options & LOG_OPT_QUOTE)
1564 LOGCHAR('"');
1565 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001566 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001567 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001568 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001569 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001570 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001571 if (tmp->options & LOG_OPT_QUOTE)
1572 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001573 last_isspace = 0;
1574 break;
William Lallemand5f232402012-04-05 18:02:55 +02001575
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001576 case LOG_FMT_HTTP_PATH: // %HP
1577 uri = txn->uri ? txn->uri : "<BADREQ>";
1578
Willy Tarreaub7636d12015-06-17 19:58:02 +02001579 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001580 LOGCHAR('"');
1581
1582 end = uri + strlen(uri);
1583 // look for the first whitespace character
1584 while (uri < end && !HTTP_IS_SPHT(*uri))
1585 uri++;
1586
1587 // keep advancing past multiple spaces
1588 while (uri < end && HTTP_IS_SPHT(*uri)) {
1589 uri++; nspaces++;
1590 }
1591
1592 // look for first space or question mark after url
1593 spc = uri;
1594 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
1595 spc++;
1596
1597 if (!txn->uri || nspaces == 0) {
1598 chunk.str = "<BADREQ>";
1599 chunk.len = strlen("<BADREQ>");
1600 } else {
1601 chunk.str = uri;
1602 chunk.len = spc - uri;
1603 }
1604
1605 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1606 if (ret == NULL || *ret != '\0')
1607 goto out;
1608
1609 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001610 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001611 LOGCHAR('"');
1612
1613 last_isspace = 0;
1614 break;
1615
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001616 case LOG_FMT_HTTP_QUERY: // %HQ
1617 if (tmp->options & LOG_OPT_QUOTE)
1618 LOGCHAR('"');
1619
1620 if (!txn->uri) {
1621 chunk.str = "<BADREQ>";
1622 chunk.len = strlen("<BADREQ>");
1623 } else {
1624 uri = txn->uri;
1625 end = uri + strlen(uri);
1626 // look for the first question mark
1627 while (uri < end && *uri != '?')
1628 uri++;
1629
1630 qmark = uri;
1631 // look for first space or question mark after url
1632 while (uri < end && !HTTP_IS_SPHT(*uri))
1633 uri++;
1634
1635 chunk.str = qmark;
1636 chunk.len = uri - qmark;
1637 }
1638
1639 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1640 if (ret == NULL || *ret != '\0')
1641 goto out;
1642
1643 tmplog = ret;
1644 if (tmp->options & LOG_OPT_QUOTE)
1645 LOGCHAR('"');
1646
1647 last_isspace = 0;
1648 break;
1649
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001650 case LOG_FMT_HTTP_URI: // %HU
1651 uri = txn->uri ? txn->uri : "<BADREQ>";
1652
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 first space after url
1667 spc = uri;
1668 while (spc < end && !HTTP_IS_SPHT(*spc))
1669 spc++;
1670
1671 if (!txn->uri || nspaces == 0) {
1672 chunk.str = "<BADREQ>";
1673 chunk.len = strlen("<BADREQ>");
1674 } else {
1675 chunk.str = uri;
1676 chunk.len = spc - uri;
1677 }
1678
1679 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1680 if (ret == NULL || *ret != '\0')
1681 goto out;
1682
1683 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001684 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001685 LOGCHAR('"');
1686
1687 last_isspace = 0;
1688 break;
1689
1690 case LOG_FMT_HTTP_METHOD: // %HM
1691 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001692 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001693 LOGCHAR('"');
1694
1695 end = uri + strlen(uri);
1696 // look for the first whitespace character
1697 spc = uri;
1698 while (spc < end && !HTTP_IS_SPHT(*spc))
1699 spc++;
1700
1701 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
1702 chunk.str = "<BADREQ>";
1703 chunk.len = strlen("<BADREQ>");
1704 } else {
1705 chunk.str = uri;
1706 chunk.len = spc - uri;
1707 }
1708
1709 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1710 if (ret == NULL || *ret != '\0')
1711 goto out;
1712
1713 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001714 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001715 LOGCHAR('"');
1716
1717 last_isspace = 0;
1718 break;
1719
1720 case LOG_FMT_HTTP_VERSION: // %HV
1721 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001722 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001723 LOGCHAR('"');
1724
1725 end = uri + strlen(uri);
1726 // look for the first whitespace character
1727 while (uri < end && !HTTP_IS_SPHT(*uri))
1728 uri++;
1729
1730 // keep advancing past multiple spaces
1731 while (uri < end && HTTP_IS_SPHT(*uri)) {
1732 uri++; nspaces++;
1733 }
1734
1735 // look for the next whitespace character
1736 while (uri < end && !HTTP_IS_SPHT(*uri))
1737 uri++;
1738
1739 // keep advancing past multiple spaces
1740 while (uri < end && HTTP_IS_SPHT(*uri))
1741 uri++;
1742
1743 if (!txn->uri || nspaces == 0) {
1744 chunk.str = "<BADREQ>";
1745 chunk.len = strlen("<BADREQ>");
1746 } else if (uri == end) {
1747 chunk.str = "HTTP/0.9";
1748 chunk.len = strlen("HTTP/0.9");
1749 } else {
1750 chunk.str = uri;
1751 chunk.len = end - uri;
1752 }
1753
1754 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1755 if (ret == NULL || *ret != '\0')
1756 goto out;
1757
1758 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001759 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001760 LOGCHAR('"');
1761
1762 last_isspace = 0;
1763 break;
1764
William Lallemand5f232402012-04-05 18:02:55 +02001765 case LOG_FMT_COUNTER: // %rt
1766 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001767 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001768 if (iret < 0 || iret > dst + maxsize - tmplog)
1769 goto out;
1770 last_isspace = 0;
1771 tmplog += iret;
1772 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001773 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001774 if (ret == NULL)
1775 goto out;
1776 tmplog = ret;
1777 last_isspace = 0;
1778 }
1779 break;
1780
Willy Tarreau7346acb2014-08-28 15:03:15 +02001781 case LOG_FMT_LOGCNT: // %lc
1782 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001783 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001784 if (iret < 0 || iret > dst + maxsize - tmplog)
1785 goto out;
1786 last_isspace = 0;
1787 tmplog += iret;
1788 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001789 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001790 if (ret == NULL)
1791 goto out;
1792 tmplog = ret;
1793 last_isspace = 0;
1794 }
1795 break;
1796
William Lallemand5f232402012-04-05 18:02:55 +02001797 case LOG_FMT_HOSTNAME: // %H
1798 src = hostname;
1799 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1800 if (ret == NULL)
1801 goto out;
1802 tmplog = ret;
1803 last_isspace = 0;
1804 break;
1805
1806 case LOG_FMT_PID: // %pid
1807 if (tmp->options & LOG_OPT_HEXA) {
1808 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1809 if (iret < 0 || iret > dst + maxsize - tmplog)
1810 goto out;
1811 last_isspace = 0;
1812 tmplog += iret;
1813 } else {
1814 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1815 if (ret == NULL)
1816 goto out;
1817 tmplog = ret;
1818 last_isspace = 0;
1819 }
1820 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001821
1822 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001823 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001824 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02001825 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001826 if (ret == NULL)
1827 goto out;
1828 tmplog = ret;
1829 last_isspace = 0;
1830 break;
1831
William Lallemandbddd4fd2012-02-27 11:23:10 +01001832 }
1833 }
1834
1835out:
William Lallemand1d705562012-03-12 12:46:41 +01001836 /* *tmplog is a unused character */
1837 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001838 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001839
Willy Tarreaubaaee002006-06-26 02:48:02 +02001840}
1841
William Lallemand1d705562012-03-12 12:46:41 +01001842/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001843 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01001844 * Will not log if the frontend has no log defined.
1845 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001846void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01001847{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001848 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01001849 int size, err, level;
1850
1851 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001852 err = (s->flags & SF_REDISP) ||
1853 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
1854 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01001855 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02001856 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001857
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001858 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01001859 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001860
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001861 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01001862 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001863
Willy Tarreauabcd5142013-06-11 17:18:02 +02001864 if (s->logs.level) { /* loglevel was overridden */
1865 if (s->logs.level == -1) {
1866 s->logs.logwait = 0; /* logs disabled */
1867 return;
1868 }
1869 level = s->logs.level - 1;
1870 }
1871 else {
1872 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001873 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02001874 level = LOG_ERR;
1875 }
William Lallemand1d705562012-03-12 12:46:41 +01001876
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001877 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001878 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001879 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001880 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001881 }
1882
Dragan Dosen59cee972015-09-19 22:09:02 +02001883 size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01001884 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001885 sess->fe->log_count++;
1886 __send_log(sess->fe, level, logline, size + 1);
William Lallemand1d705562012-03-12 12:46:41 +01001887 s->logs.logwait = 0;
1888 }
1889}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001890
Willy Tarreaubaaee002006-06-26 02:48:02 +02001891/*
1892 * Local variables:
1893 * c-indent-level: 8
1894 * c-basic-offset: 8
1895 * End:
1896 */