blob: f0db0a82913e39a6571f22e49f59ae52a291bca5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
25
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020027#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020029#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010032#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020033
William Lallemand5f232402012-04-05 18:02:55 +020034#include <proto/frontend.h>
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +000035#include <proto/proto_http.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020036#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010037#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020038#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010039#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020040#ifdef USE_OPENSSL
41#include <proto/ssl_sock.h>
42#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreaubaaee002006-06-26 02:48:02 +020044const char *log_facilities[NB_LOG_FACILITIES] = {
45 "kern", "user", "mail", "daemon",
46 "auth", "syslog", "lpr", "news",
47 "uucp", "cron", "auth2", "ftp",
48 "ntp", "audit", "alert", "cron2",
49 "local0", "local1", "local2", "local3",
50 "local4", "local5", "local6", "local7"
51};
52
53
54const char *log_levels[NB_LOG_LEVELS] = {
55 "emerg", "alert", "crit", "err",
56 "warning", "notice", "info", "debug"
57};
58
Willy Tarreau570f2212013-06-10 16:42:09 +020059const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +020060const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
William Lallemand723b73a2012-02-08 16:37:49 +010062
63/* log_format */
64struct logformat_type {
65 char *name;
66 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010067 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020068 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010069 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +010070 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +010071};
72
William Lallemandb7ff6a32012-03-02 14:35:21 +010073int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
74
William Lallemand723b73a2012-02-08 16:37:49 +010075/* log_format variable names */
76static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020077 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +010078
79 /* please keep these lines sorted ! */
80 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
81 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
82 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
83 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
84 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +020085 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +020086 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +010087 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +080088 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +020089 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +020090 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +010091 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +020092 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +010093 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
94 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +020095 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010096 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +020097 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010098 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
99 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200100 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +0100101 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
102 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
103 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
104 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
105 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
106 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
107 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200108 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
109 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
110 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
111 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000112 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
113 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
114 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
115 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200116 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100117 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200118 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100119 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
120 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100121 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100122 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
123 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
124 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
125 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
126 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200127 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
128 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100129 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
130 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
131 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
132
133 /* The following tags are deprecated and will be removed soon */
134 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
135 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
136 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
137 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
138 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
139 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
140 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
141 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
142 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
143 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
144 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200145 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100146};
147
Willy Tarreau2beef582012-12-20 17:22:52 +0100148char default_http_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
149char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%T] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %Tq %Tw %Tc %Tr %Tt %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl";
150char default_tcp_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq";
William Lallemand723b73a2012-02-08 16:37:49 +0100151char *log_format = NULL;
152
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100153/* This is a global syslog line, common to all outgoing messages. It begins
154 * with the syslog tag and the date that are updated by update_log_hdr().
155 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200156char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100157
William Lallemand723b73a2012-02-08 16:37:49 +0100158struct logformat_var_args {
159 char *name;
160 int mask;
161};
162
163struct logformat_var_args var_args_list[] = {
164// global
165 { "M", LOG_OPT_MANDATORY },
166 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200167 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100168 { 0, 0 }
169};
170
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200171/* return the name of the directive used in the current proxy for which we're
172 * currently parsing a header, when it is known.
173 */
174static inline const char *fmt_directive(const struct proxy *curproxy)
175{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100176 switch (curproxy->conf.args.ctx) {
177 case ARGC_UIF:
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200178 return "unique-id-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100179 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100180 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100181 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100182 return "http-response";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100183 case ARGC_STK:
184 return "stick";
185 case ARGC_TRK:
186 return "track-sc"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100187 case ARGC_RDR:
188 return "redirect"; break;
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100189 case ARGC_ACL:
190 return "acl"; break;
191 default:
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200192 return "log-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100193 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200194}
195
William Lallemand723b73a2012-02-08 16:37:49 +0100196/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100197 * callback used to configure addr source retrieval
198 */
199int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
200{
201 curproxy->options2 |= PR_O2_SRC_ADDR;
202
203 return 0;
204}
205
206
207/*
William Lallemand723b73a2012-02-08 16:37:49 +0100208 * Parse args in a logformat_var
209 */
210int parse_logformat_var_args(char *args, struct logformat_node *node)
211{
212 int i = 0;
213 int end = 0;
214 int flags = 0; // 1 = + 2 = -
215 char *sp = NULL; // start pointer
216
217 if (args == NULL)
218 return 1;
219
220 while (1) {
221 if (*args == '\0')
222 end = 1;
223
224 if (*args == '+') {
225 // add flag
226 sp = args + 1;
227 flags = 1;
228 }
229 if (*args == '-') {
230 // delete flag
231 sp = args + 1;
232 flags = 2;
233 }
234
235 if (*args == '\0' || *args == ',') {
236 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100237 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100238 if (strcmp(sp, var_args_list[i].name) == 0) {
239 if (flags == 1) {
240 node->options |= var_args_list[i].mask;
241 break;
242 } else if (flags == 2) {
243 node->options &= ~var_args_list[i].mask;
244 break;
245 }
246 }
247 }
248 sp = NULL;
249 if (end)
250 break;
251 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100252 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100253 }
254 return 0;
255}
256
257/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100258 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
259 * must pass the args part in the <arg> pointer with its length in <arg_len>,
260 * and varname with its length in <var> and <var_len> respectively. <arg> is
261 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100262 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100263int 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 +0100264{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100265 int j;
266 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100267
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100268 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
269 if (strlen(logformat_keywords[j].name) == var_len &&
270 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
271 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
272 node = calloc(1, sizeof(struct logformat_node));
273 node->type = logformat_keywords[j].type;
274 node->options = *defoptions;
275 if (arg_len) {
276 node->arg = my_strndup(arg, arg_len);
277 parse_logformat_var_args(node->arg, node);
278 }
279 if (node->type == LOG_FMT_GLOBAL) {
280 *defoptions = node->options;
281 free(node->arg);
282 free(node);
283 } else {
284 if (logformat_keywords[j].config_callback &&
285 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100286 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100287 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100288 curproxy->to_log |= logformat_keywords[j].lw;
289 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100290 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100291 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100292 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200293 curproxy->conf.args.file, curproxy->conf.args.line,
294 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100295 return 0;
296 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100297 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200298 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
299 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100300 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100301 }
William Lallemand723b73a2012-02-08 16:37:49 +0100302 }
303 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100304
305 j = var[var_len];
306 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100307 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 +0200308 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100309 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100310 return -1;
311}
312
313/*
314 * push to the logformat linked list
315 *
316 * start: start pointer
317 * end: end text pointer
318 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100319 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100320 *
321 * LOG_TEXT: copy chars from start to end excluding end.
322 *
323*/
William Lallemand1d705562012-03-12 12:46:41 +0100324void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100325{
326 char *str;
327
Willy Tarreaua3571662012-12-20 21:59:12 +0100328 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100329 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100330 str = calloc(end - start + 1, 1);
331 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100332 str[end - start] = '\0';
333 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100334 node->type = LOG_FMT_TEXT; // type string
335 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100336 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100337 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100338 node->type = LOG_FMT_SEPARATOR;
339 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100340 }
341}
342
343/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100344 * Parse the sample fetch expression <text> and add a node to <list_format> upon
345 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200346 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100347 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100348void 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 +0100349{
350 char *cmd[2];
351 struct sample_expr *expr;
352 struct logformat_node *node;
353 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100354 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100355
356 cmd[0] = text;
357 cmd[1] = "";
358 cmd_arg = 0;
359
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100360 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100361 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200362 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
363 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100364 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100365 return;
366 }
367
368 node = calloc(1, sizeof(struct logformat_node));
369 node->type = LOG_FMT_EXPR;
370 node->expr = expr;
371 node->options = options;
372
373 if (arg_len) {
374 node->arg = my_strndup(arg, arg_len);
375 parse_logformat_var_args(node->arg, node);
376 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100377 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100378 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
379
Willy Tarreau434c57c2013-01-08 01:10:24 +0100380 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100381 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
382
Willy Tarreau434c57c2013-01-08 01:10:24 +0100383 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200384 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
385 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
386 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100387
Willy Tarreauc8368452012-12-21 00:09:23 +0100388 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
389 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100390 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100391
William Lallemand65ad6e12014-01-31 15:08:02 +0100392 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
393 * needed with some sample fetches (eg: ssl*). We always set it for
394 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100395 */
396 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100397 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100398 LIST_ADDQ(list_format, &node->list);
399}
400
401/*
William Lallemand723b73a2012-02-08 16:37:49 +0100402 * Parse the log_format string and fill a linked list.
403 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200404 * You can set arguments using { } : %{many arguments}varname.
405 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100406 *
407 * str: the string to parse
408 * curproxy: the proxy affected
409 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100410 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100411 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100412 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100413void 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 +0100414{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100415 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100416 char *arg = NULL; /* start pointer for args */
417 char *var = NULL; /* start pointer for vars */
418 int arg_len = 0;
419 int var_len = 0;
420 int cformat; /* current token format */
421 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100422 struct logformat_node *tmplf, *back;
423
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100424 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200425 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200426
William Lallemand723b73a2012-02-08 16:37:49 +0100427 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100428 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100429 LIST_DEL(&tmplf->list);
430 free(tmplf);
431 }
432
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100433 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100434 pformat = cformat;
435
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100436 if (!*str)
437 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100438
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100439 /* The prinicple of the two-step state machine below is to first detect a change, and
440 * second have all common paths processed at one place. The common paths are the ones
441 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
442 * We use the common LF_INIT state to dispatch to the different final states.
443 */
444 switch (pformat) {
445 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100446 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100447 arg_len = var_len = 0;
448 if (*str == '{') { // optional argument
449 cformat = LF_STARG;
450 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100451 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100452 else if (*str == '[') {
453 cformat = LF_STEXPR;
454 var = str + 1; // store expr in variable name
455 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100456 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100457 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100458 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100459 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100460 else if (*str == '%')
461 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100462 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100463 /* single '%' followed by blank or digit, send them both */
464 cformat = LF_TEXT;
465 pformat = LF_TEXT; /* finally we include the previous char as well */
466 sp = str - 1; /* send both the '%' and the current char */
467 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",
468 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
469
470 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100471 else
472 cformat = LF_INIT; // handle other cases of litterals
473 break;
474
475 case LF_STARG: // text immediately following '%{'
476 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100477 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100478 arg_len = str - arg;
479 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100480 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100481 break;
482
483 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100484 if (*str == '[') {
485 cformat = LF_STEXPR;
486 var = str + 1; // store expr in variable name
487 break;
488 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100489 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100490 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100491 var = str;
492 break;
493 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200494 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
495 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100496 cformat = LF_INIT;
497 break;
498
Willy Tarreauc8368452012-12-21 00:09:23 +0100499 case LF_STEXPR: // text immediately following '%['
500 if (*str == ']') { // end of arg
501 cformat = LF_EDEXPR;
502 var_len = str - var;
503 *str = 0; // needed for parsing the expression
504 }
505 break;
506
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100507 case LF_VAR: // text part of a variable name
508 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100509 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100510 cformat = LF_INIT; // not variable name anymore
511 break;
512
Willy Tarreauc8368452012-12-21 00:09:23 +0100513 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100514 cformat = LF_INIT;
515 }
516
517 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
518 switch (*str) {
519 case '%': cformat = LF_STARTVAR; break;
520 case ' ': cformat = LF_SEPARATOR; break;
521 case 0 : cformat = LF_END; break;
522 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100523 }
524 }
525
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100526 if (cformat != pformat || pformat == LF_SEPARATOR) {
527 switch (pformat) {
528 case LF_VAR:
529 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
530 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100531 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100532 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100533 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100534 case LF_TEXT:
535 case LF_SEPARATOR:
536 add_to_logformat_list(sp, str, pformat, list_format);
537 break;
538 }
539 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100540 }
541 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100542
Willy Tarreauc8368452012-12-21 00:09:23 +0100543 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200544 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
545 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
546 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100547
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100548 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100549}
550
Willy Tarreaubaaee002006-06-26 02:48:02 +0200551/*
552 * Displays the message on stderr with the date and pid. Overrides the quiet
553 * mode during startup.
554 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200555void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200556{
557 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200558 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559
560 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
561 va_start(argp, fmt);
562
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200563 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200564 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200565 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566 vfprintf(stderr, fmt, argp);
567 fflush(stderr);
568 va_end(argp);
569 }
570}
571
572
573/*
574 * Displays the message on stderr with the date and pid.
575 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200576void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577{
578 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200579 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200580
581 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
582 va_start(argp, fmt);
583
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200584 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200585 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200586 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200587 vfprintf(stderr, fmt, argp);
588 fflush(stderr);
589 va_end(argp);
590 }
591}
592
593/*
594 * Displays the message on <out> only if quiet mode is not set.
595 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200596void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200597{
598 va_list argp;
599
600 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
601 va_start(argp, fmt);
602 vfprintf(out, fmt, argp);
603 fflush(out);
604 va_end(argp);
605 }
606}
607
608/*
609 * returns log level for <lev> or -1 if not found.
610 */
611int get_log_level(const char *lev)
612{
613 int level;
614
615 level = NB_LOG_LEVELS - 1;
616 while (level >= 0 && strcmp(log_levels[level], lev))
617 level--;
618
619 return level;
620}
621
622
623/*
624 * returns log facility for <fac> or -1 if not found.
625 */
626int get_log_facility(const char *fac)
627{
628 int facility;
629
630 facility = NB_LOG_FACILITIES - 1;
631 while (facility >= 0 && strcmp(log_facilities[facility], fac))
632 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100633
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 return facility;
635}
636
William Lallemanda1cc3812012-02-08 16:38:44 +0100637/*
638 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200639 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100640 *
641 * Return the adress of the \0 character, or NULL on error
642 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100643char *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 +0100644{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100645 if (size < 2)
646 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100647
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100648 if (node->options & LOG_OPT_QUOTE) {
649 *(dst++) = '"';
650 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100651 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100652
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100653 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100654 if (++len > size)
655 len = size;
656 len = strlcpy2(dst, src, len);
657
658 size -= len;
659 dst += len;
660 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100661 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
662 if (size < 2)
663 return NULL;
664 *(dst++) = '-';
665 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100666
667 if (node->options & LOG_OPT_QUOTE) {
668 if (size < 2)
669 return NULL;
670 *(dst++) = '"';
671 }
672
673 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100674 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100675}
676
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100677static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
678{
679 return lf_text_len(dst, src, size, size, node);
680}
681
William Lallemand5f232402012-04-05 18:02:55 +0200682/*
683 * Write a IP adress to the log string
684 * +X option write in hexadecimal notation, most signifant byte on the left
685 */
686char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
687{
688 char *ret = dst;
689 int iret;
690 char pn[INET6_ADDRSTRLEN];
691
692 if (node->options & LOG_OPT_HEXA) {
693 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
694 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
695 if (iret < 0 || iret > size)
696 return NULL;
697 ret += iret;
698 } else {
699 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
700 ret = lf_text(dst, pn, size, node);
701 if (ret == NULL)
702 return NULL;
703 }
704 return ret;
705}
706
707/*
708 * Write a port to the log
709 * +X option write in hexadecimal notation, most signifant byte on the left
710 */
711char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
712{
713 char *ret = dst;
714 int iret;
715
716 if (node->options & LOG_OPT_HEXA) {
717 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
718 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
719 if (iret < 0 || iret > size)
720 return NULL;
721 ret += iret;
722 } else {
723 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
724 if (ret == NULL)
725 return NULL;
726 }
727 return ret;
728}
729
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100730/* Re-generate the syslog header at the beginning of logline once a second and
731 * return the pointer to the first character after the header.
732 */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100733static char *update_log_hdr(const char *log_tag)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200734{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100735 static long tvsec;
736 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100737 int tag_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200739 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200741 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100742 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200743
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200744 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200745 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200746
Willy Tarreau18324f52014-06-27 18:10:07 +0200747 hdr_len = snprintf(logline, global.max_syslog_len,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100748 "<<<<>%s %2d %02d:%02d:%02d %s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200749 monthname[tm.tm_mon],
750 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100751 global.log_send_hostname ? global.log_send_hostname : "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200752 /* WARNING: depending upon implementations, snprintf may return
753 * either -1 or the number of bytes that would be needed to store
754 * the total message. In both cases, we must adjust it.
755 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200756 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
757 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200758
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100759 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760 }
761
Willy Tarreau094af4e2015-01-07 15:03:42 +0100762 dataptr[0] = 0; // ensure we get rid of any previous attempt
763
764 tag_len = snprintf(dataptr, logline + global.max_syslog_len - dataptr, "%s[%d]: ", log_tag, pid);
765 if (tag_len < 0 || tag_len > logline + global.max_syslog_len - dataptr)
766 tag_len = logline + global.max_syslog_len - dataptr;
767
768 return dataptr + tag_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100769}
770
771/*
772 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100773 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100774 */
775void send_log(struct proxy *p, int level, const char *format, ...)
776{
777 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100778 char *dataptr;
779 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100780
Willy Tarreau8c97ab52015-01-15 16:29:53 +0100781 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100782 return;
783
Thierry FOURNIERe83766a2015-03-09 17:10:29 +0100784 dataptr = update_log_hdr(p && p->log_tag ? p->log_tag : global.log_tag); /* update log header and skip it */
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100785 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100786
787 va_start(argp, format);
Willy Tarreau18324f52014-06-27 18:10:07 +0200788 data_len += vsnprintf(dataptr, logline + global.max_syslog_len - dataptr, format, argp);
789 if (data_len < 0 || data_len > global.max_syslog_len)
790 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100792
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100793 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100794}
795
796/*
797 * This function sends a syslog message.
798 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100799 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100800 */
801void __send_log(struct proxy *p, int level, char *message, size_t size)
802{
803 static int logfdunix = -1; /* syslog to AF_UNIX socket */
804 static int logfdinet = -1; /* syslog to AF_INET socket */
805 static char *dataptr = NULL;
806 int fac_level;
807 struct list *logsrvs = NULL;
808 struct logsrv *tmp = NULL;
809 int nblogger;
810 char *log_ptr;
811
812 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813
814 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200815 if (!LIST_ISEMPTY(&global.logsrvs)) {
816 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 }
818 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200819 if (!LIST_ISEMPTY(&p->logsrvs)) {
820 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200821 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100822 }
823
William Lallemand0f99e342011-10-12 17:50:54 +0200824 if (!logsrvs)
825 return;
826
Robert Tsai81ae1952007-12-05 10:47:29 +0100827 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200828 nblogger = 0;
829 list_for_each_entry(tmp, logsrvs, list) {
830 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100831 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100832 &logfdunix : &logfdinet;
833 int sent;
Willy Tarreau18324f52014-06-27 18:10:07 +0200834 int max;
835 char backup;
Robert Tsai81ae1952007-12-05 10:47:29 +0100836
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200837 nblogger++;
838
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200840 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200841 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100842
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200843 if (unlikely(*plogfd < 0)) {
844 /* socket not successfully initialized yet */
845 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
846
847 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
848 Alert("socket for logger #%d failed: %s (errno=%d)\n",
849 nblogger, strerror(errno), errno);
850 continue;
851 }
852 /* we don't want to receive anything on this socket */
853 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
854 /* does nothing under Linux, maybe needed for others */
855 shutdown(*plogfd, SHUT_RD);
856 }
857
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858 /* For each target, we may have a different facility.
859 * We can also have a different log level for each message.
860 * This induces variations in the message header length.
861 * Since we don't want to recompute it each time, nor copy it every
862 * time, we only change the facility in the pre-computed header,
863 * and we change the pointer to the header accordingly.
864 */
William Lallemand0f99e342011-10-12 17:50:54 +0200865 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100866 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867 do {
868 *log_ptr = '0' + fac_level % 10;
869 fac_level /= 10;
870 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100871 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200872 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100873
Willy Tarreau18324f52014-06-27 18:10:07 +0200874 max = size - (log_ptr - dataptr);
875 if (max > logsrv->maxlen)
876 max = logsrv->maxlen;
877
878 /* insert a \n at the end of the message, but save what was
879 * there first because we could have different max lengths
880 * for different log targets.
881 */
882 backup = log_ptr[max - 1];
883 log_ptr[max - 1] = '\n';
884
885 sent = sendto(*plogfd, log_ptr, max,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200886 MSG_DONTWAIT | MSG_NOSIGNAL,
887 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Willy Tarreau18324f52014-06-27 18:10:07 +0200888
889 log_ptr[max - 1] = backup;
890
Robert Tsai81ae1952007-12-05 10:47:29 +0100891 if (sent < 0) {
892 Alert("sendto logger #%d failed: %s (errno=%d)\n",
893 nblogger, strerror(errno), errno);
894 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895 }
896}
897
William Lallemandbddd4fd2012-02-27 11:23:10 +0100898extern fd_set hdr_encode_map[];
899extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100900extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +0100901
Willy Tarreaubaaee002006-06-26 02:48:02 +0200902
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200903const 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 +0100904const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
905 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
906 Set-cookie Updated, unknown, unknown */
907
William Lallemand1d705562012-03-12 12:46:41 +0100908/*
909 * try to write a character if there is enough space, or goto out
910 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100911#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100912 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100913 *(tmplog++) = (x); \
914 } else { \
915 goto out; \
916 } \
917 } while(0)
918
William Lallemand1d705562012-03-12 12:46:41 +0100919
Willy Tarreaudf974472012-12-28 02:44:01 +0100920/* Builds a log line in <dst> based on <list_format>, and stops before reaching
921 * <maxsize> characters. Returns the size of the output string in characters,
922 * not counting the trailing zero which is always added if the resulting size
923 * is not zero.
924 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200925int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200927 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200928 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100929 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200930 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000931 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100932 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000933 char *spc;
934 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +0200935 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100936 int t_request;
937 int hdr;
938 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000939 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100940 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100941 char *ret;
942 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100943 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200944
William Lallemandbddd4fd2012-02-27 11:23:10 +0100945 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946
William Lallemandbddd4fd2012-02-27 11:23:10 +0100947 t_request = -1;
948 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
949 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
950
William Lallemand1d705562012-03-12 12:46:41 +0100951 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200952
William Lallemandbddd4fd2012-02-27 11:23:10 +0100953 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100954 if (LIST_ISEMPTY(list_format))
955 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100956
William Lallemand1d705562012-03-12 12:46:41 +0100957 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200958 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +0200959 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100960 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100961 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +0100962
Willy Tarreauc8368452012-12-21 00:09:23 +0100963 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +0100964 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100965 if (!last_isspace) {
966 LOGCHAR(' ');
967 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100968 }
969 break;
970
William Lallemand1d705562012-03-12 12:46:41 +0100971 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100972 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200973 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100974 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100975 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100976 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100977 last_isspace = 0;
978 break;
979
Willy Tarreauc8368452012-12-21 00:09:23 +0100980 case LOG_FMT_EXPR: // sample expression, may be request or response
981 key = NULL;
982 if (tmp->options & LOG_OPT_REQ_CAP)
Willy Tarreau192252e2015-04-04 01:47:55 +0200983 key = sample_fetch_string(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr);
Willy Tarreauc8368452012-12-21 00:09:23 +0100984 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Willy Tarreau192252e2015-04-04 01:47:55 +0200985 key = sample_fetch_string(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100986 if (tmp->options & LOG_OPT_HTTP)
987 ret = encode_chunk(tmplog, dst + maxsize,
988 '%', http_encode_map, key ? &key->data.str : &empty);
989 else
990 ret = lf_text_len(tmplog, key ? key->data.str.str : NULL, key ? key->data.str.len : 0, dst + maxsize - tmplog, tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +0100991 if (ret == 0)
992 goto out;
993 tmplog = ret;
994 last_isspace = 0;
995 break;
996
Willy Tarreau2beef582012-12-20 17:22:52 +0100997 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200998 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200999 if (conn)
1000 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1001 else
1002 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001003 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001004 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001005 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001006 last_isspace = 0;
1007 break;
1008
Willy Tarreau2beef582012-12-20 17:22:52 +01001009 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001010 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001011 if (conn) {
1012 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001013 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001014 } else {
1015 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1016 dst + maxsize - tmplog, tmp);
1017 }
William Lallemand5f232402012-04-05 18:02:55 +02001018 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001019 else
1020 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1021
William Lallemand5f232402012-04-05 18:02:55 +02001022 if (ret == NULL)
1023 goto out;
1024 tmplog = ret;
1025 last_isspace = 0;
1026 break;
1027
Willy Tarreau2beef582012-12-20 17:22:52 +01001028 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001029 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001030 if (conn) {
1031 conn_get_to_addr(conn);
1032 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1033 }
1034 else
1035 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1036
William Lallemand1d705562012-03-12 12:46:41 +01001037 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001038 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001039 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001040 last_isspace = 0;
1041 break;
1042
Willy Tarreau2beef582012-12-20 17:22:52 +01001043 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001044 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001045 if (conn) {
1046 conn_get_to_addr(conn);
1047 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001048 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001049 else
1050 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001051 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001052 else
1053 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1054
William Lallemand5f232402012-04-05 18:02:55 +02001055 if (ret == NULL)
1056 goto out;
1057 tmplog = ret;
1058 last_isspace = 0;
1059 break;
1060
Willy Tarreau2beef582012-12-20 17:22:52 +01001061 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001062 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001063 if (conn)
1064 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1065 else
1066 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1067
William Lallemand1d705562012-03-12 12:46:41 +01001068 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001069 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001070 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001071 last_isspace = 0;
1072 break;
1073
Willy Tarreau2beef582012-12-20 17:22:52 +01001074 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001075 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001076 if (conn)
1077 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1078 else
1079 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1080
William Lallemand5f232402012-04-05 18:02:55 +02001081 if (ret == NULL)
1082 goto out;
1083 tmplog = ret;
1084 last_isspace = 0;
1085 break;
1086
Willy Tarreau2beef582012-12-20 17:22:52 +01001087 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001088 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001089 if (conn)
1090 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1091 else
1092 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1093
William Lallemand5f232402012-04-05 18:02:55 +02001094 if (ret == NULL)
1095 goto out;
1096 tmplog = ret;
1097 last_isspace = 0;
1098 break;
1099
Willy Tarreau2beef582012-12-20 17:22:52 +01001100 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001101 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001102 if (conn)
1103 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1104 else
1105 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1106
William Lallemand1d705562012-03-12 12:46:41 +01001107 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001108 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001109 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001110 last_isspace = 0;
1111 break;
1112
William Lallemand1d705562012-03-12 12:46:41 +01001113 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001114 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001115 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1116 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001117 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001118 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001119 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001120 last_isspace = 0;
1121 break;
1122
William Lallemand1d705562012-03-12 12:46:41 +01001123 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001124 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001125 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001126 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001127 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001128 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001129 last_isspace = 0;
1130 break;
1131
Yuxans Yao4e25b012012-10-19 10:36:09 +08001132 case LOG_FMT_DATELOCAL: // %Tl
1133 get_localtime(s->logs.accept_date.tv_sec, &tm);
1134 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1135 if (ret == NULL)
1136 goto out;
1137 tmplog = ret;
1138 last_isspace = 0;
1139 break;
1140
William Lallemand5f232402012-04-05 18:02:55 +02001141 case LOG_FMT_TS: // %Ts
1142 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1143 if (tmp->options & LOG_OPT_HEXA) {
1144 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1145 if (iret < 0 || iret > dst + maxsize - tmplog)
1146 goto out;
1147 last_isspace = 0;
1148 tmplog += iret;
1149 } else {
1150 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1151 if (ret == NULL)
1152 goto out;
1153 tmplog = ret;
1154 last_isspace = 0;
1155 }
1156 break;
1157
William Lallemand1d705562012-03-12 12:46:41 +01001158 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001159 if (tmp->options & LOG_OPT_HEXA) {
1160 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1161 if (iret < 0 || iret > dst + maxsize - tmplog)
1162 goto out;
1163 last_isspace = 0;
1164 tmplog += iret;
1165 } else {
1166 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001167 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001168 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1169 tmplog, 4);
1170 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001171 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001172 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001173 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001174 }
1175 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001176
William Lallemand1d705562012-03-12 12:46:41 +01001177 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001178 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001179 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001180 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001181 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001182 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001183 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001184 break;
1185
Willy Tarreau773d65f2012-10-12 14:56:11 +02001186 case LOG_FMT_FRONTEND_XPRT: // %ft
1187 src = fe->id;
1188 if (tmp->options & LOG_OPT_QUOTE)
1189 LOGCHAR('"');
1190 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1191 if (iret == 0)
1192 goto out;
1193 tmplog += iret;
1194#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001195 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001196 LOGCHAR('~');
1197#endif
1198 if (tmp->options & LOG_OPT_QUOTE)
1199 LOGCHAR('"');
1200 last_isspace = 0;
1201 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001202#ifdef USE_OPENSSL
1203 case LOG_FMT_SSL_CIPHER: // %sslc
1204 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001205 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001206 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001207 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001208 src = ssl_sock_get_cipher_name(conn);
1209 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001210 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1211 if (ret == NULL)
1212 goto out;
1213 tmplog = ret;
1214 last_isspace = 0;
1215 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001216
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001217 case LOG_FMT_SSL_VERSION: // %sslv
1218 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001219 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001220 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001221 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001222 src = ssl_sock_get_proto_version(conn);
1223 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001224 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1225 if (ret == NULL)
1226 goto out;
1227 tmplog = ret;
1228 last_isspace = 0;
1229 break;
1230#endif
William Lallemand1d705562012-03-12 12:46:41 +01001231 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001232 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001233 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001234 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001235 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001236 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001237 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001238 break;
1239
William Lallemand1d705562012-03-12 12:46:41 +01001240 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001241 switch (obj_type(s->target)) {
1242 case OBJ_TYPE_SERVER:
1243 src = objt_server(s->target)->id;
1244 break;
1245 case OBJ_TYPE_APPLET:
1246 src = objt_applet(s->target)->name;
1247 break;
1248 default:
1249 src = "<NOSRV>";
1250 break;
1251 }
William Lallemand5f232402012-04-05 18:02:55 +02001252 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001253 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001254 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001255 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001256 last_isspace = 0;
1257 break;
1258
William Lallemand1d705562012-03-12 12:46:41 +01001259 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001260 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001261 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001262 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001263 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001264 last_isspace = 0;
1265 break;
1266
William Lallemand1d705562012-03-12 12:46:41 +01001267 case LOG_FMT_TW: // %Tw
1268 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001269 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001270 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001271 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001272 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001273 last_isspace = 0;
1274 break;
1275
William Lallemand1d705562012-03-12 12:46:41 +01001276 case LOG_FMT_TC: // %Tc
1277 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001278 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001279 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001280 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001281 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001282 last_isspace = 0;
1283 break;
1284
William Lallemand1d705562012-03-12 12:46:41 +01001285 case LOG_FMT_TR: // %Tr
1286 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001287 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001288 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001289 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001290 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001291 last_isspace = 0;
1292 break;
1293
William Lallemand1d705562012-03-12 12:46:41 +01001294 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001295 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001296 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001297 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001298 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001299 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001300 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001301 last_isspace = 0;
1302 break;
1303
Willy Tarreau2beef582012-12-20 17:22:52 +01001304 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001305 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001306 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001307 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001308 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001309 last_isspace = 0;
1310 break;
1311
William Lallemand1d705562012-03-12 12:46:41 +01001312 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001313 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001314 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001315 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001316 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001317 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001318 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001319 last_isspace = 0;
1320 break;
1321
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001322 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001323 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1324 if (ret == NULL)
1325 goto out;
1326 tmplog = ret;
1327 last_isspace = 0;
1328 break;
1329
Willy Tarreau2beef582012-12-20 17:22:52 +01001330 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001331 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001332 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001333 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001334 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001335 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001336 last_isspace = 0;
1337 break;
1338
Willy Tarreau2beef582012-12-20 17:22:52 +01001339 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001340 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001341 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001342 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001343 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001344 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001345 last_isspace = 0;
1346 break;
1347
William Lallemand1d705562012-03-12 12:46:41 +01001348 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001349 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1350 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001351 *tmplog = '\0';
1352 last_isspace = 0;
1353 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001354
William Lallemand1d705562012-03-12 12:46:41 +01001355 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001356 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1357 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001358 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1359 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 +01001360 last_isspace = 0;
1361 break;
1362
William Lallemand1d705562012-03-12 12:46:41 +01001363 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001364 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001365 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001366 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001367 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001368 last_isspace = 0;
1369 break;
1370
William Lallemand1d705562012-03-12 12:46:41 +01001371 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001372 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001373 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001374 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001375 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001376 last_isspace = 0;
1377 break;
1378
William Lallemand1d705562012-03-12 12:46:41 +01001379 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001380 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001381 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001382 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001383 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001384 last_isspace = 0;
1385 break;
1386
William Lallemand1d705562012-03-12 12:46:41 +01001387 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001388 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001389 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001390 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001391 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001392 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001393 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001394 last_isspace = 0;
1395 break;
1396
William Lallemand1d705562012-03-12 12:46:41 +01001397 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001398 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001399 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001400 ret = ltoa_o((s->si[1].conn_retries>0) ?
1401 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001402 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001403 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001404 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001405 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001406 last_isspace = 0;
1407 break;
1408
William Lallemand1d705562012-03-12 12:46:41 +01001409 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001410 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001411 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001412 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001413 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001414 last_isspace = 0;
1415 break;
1416
William Lallemand1d705562012-03-12 12:46:41 +01001417 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001418 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001419 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001420 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001421 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001422 last_isspace = 0;
1423 break;
1424
William Lallemand1d705562012-03-12 12:46:41 +01001425 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001426 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001427 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001428 if (tmp->options & LOG_OPT_QUOTE)
1429 LOGCHAR('"');
1430 LOGCHAR('{');
1431 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1432 if (hdr)
1433 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001434 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001435 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001436 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001437 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001438 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001439 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001440 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001441 }
1442 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001443 if (tmp->options & LOG_OPT_QUOTE)
1444 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001445 last_isspace = 0;
1446 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001447 break;
1448
William Lallemand1d705562012-03-12 12:46:41 +01001449 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001450 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001451 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001452 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1453 if (hdr > 0)
1454 LOGCHAR(' ');
1455 if (tmp->options & LOG_OPT_QUOTE)
1456 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001457 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001458 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001459 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001460 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001461 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001462 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001463 } else if (!(tmp->options & LOG_OPT_QUOTE))
1464 LOGCHAR('-');
1465 if (tmp->options & LOG_OPT_QUOTE)
1466 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001467 last_isspace = 0;
1468 }
1469 }
1470 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001471
William Lallemand1d705562012-03-12 12:46:41 +01001472
1473 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001474 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001475 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001476 if (tmp->options & LOG_OPT_QUOTE)
1477 LOGCHAR('"');
1478 LOGCHAR('{');
1479 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1480 if (hdr)
1481 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001482 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001483 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001484 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001485 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001486 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001487 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001488 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001489 }
1490 LOGCHAR('}');
1491 last_isspace = 0;
1492 if (tmp->options & LOG_OPT_QUOTE)
1493 LOGCHAR('"');
1494 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001495 break;
1496
William Lallemand1d705562012-03-12 12:46:41 +01001497 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001498 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001499 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001500 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1501 if (hdr > 0)
1502 LOGCHAR(' ');
1503 if (tmp->options & LOG_OPT_QUOTE)
1504 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001505 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001506 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001507 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001508 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001509 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001510 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001511 } else if (!(tmp->options & LOG_OPT_QUOTE))
1512 LOGCHAR('-');
1513 if (tmp->options & LOG_OPT_QUOTE)
1514 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001515 last_isspace = 0;
1516 }
1517 }
1518 break;
1519
William Lallemand1d705562012-03-12 12:46:41 +01001520 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001521 /* Request */
1522 if (tmp->options & LOG_OPT_QUOTE)
1523 LOGCHAR('"');
1524 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001525 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001526 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001527 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001528 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001529 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001530 if (tmp->options & LOG_OPT_QUOTE)
1531 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001532 last_isspace = 0;
1533 break;
William Lallemand5f232402012-04-05 18:02:55 +02001534
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001535 case LOG_FMT_HTTP_PATH: // %HP
1536 uri = txn->uri ? txn->uri : "<BADREQ>";
1537
Willy Tarreaub7636d12015-06-17 19:58:02 +02001538 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001539 LOGCHAR('"');
1540
1541 end = uri + strlen(uri);
1542 // look for the first whitespace character
1543 while (uri < end && !HTTP_IS_SPHT(*uri))
1544 uri++;
1545
1546 // keep advancing past multiple spaces
1547 while (uri < end && HTTP_IS_SPHT(*uri)) {
1548 uri++; nspaces++;
1549 }
1550
1551 // look for first space or question mark after url
1552 spc = uri;
1553 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
1554 spc++;
1555
1556 if (!txn->uri || nspaces == 0) {
1557 chunk.str = "<BADREQ>";
1558 chunk.len = strlen("<BADREQ>");
1559 } else {
1560 chunk.str = uri;
1561 chunk.len = spc - uri;
1562 }
1563
1564 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1565 if (ret == NULL || *ret != '\0')
1566 goto out;
1567
1568 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001569 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001570 LOGCHAR('"');
1571
1572 last_isspace = 0;
1573 break;
1574
1575 case LOG_FMT_HTTP_URI: // %HU
1576 uri = txn->uri ? txn->uri : "<BADREQ>";
1577
Willy Tarreaub7636d12015-06-17 19:58:02 +02001578 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001579 LOGCHAR('"');
1580
1581 end = uri + strlen(uri);
1582 // look for the first whitespace character
1583 while (uri < end && !HTTP_IS_SPHT(*uri))
1584 uri++;
1585
1586 // keep advancing past multiple spaces
1587 while (uri < end && HTTP_IS_SPHT(*uri)) {
1588 uri++; nspaces++;
1589 }
1590
1591 // look for first space after url
1592 spc = uri;
1593 while (spc < end && !HTTP_IS_SPHT(*spc))
1594 spc++;
1595
1596 if (!txn->uri || nspaces == 0) {
1597 chunk.str = "<BADREQ>";
1598 chunk.len = strlen("<BADREQ>");
1599 } else {
1600 chunk.str = uri;
1601 chunk.len = spc - uri;
1602 }
1603
1604 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1605 if (ret == NULL || *ret != '\0')
1606 goto out;
1607
1608 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001609 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001610 LOGCHAR('"');
1611
1612 last_isspace = 0;
1613 break;
1614
1615 case LOG_FMT_HTTP_METHOD: // %HM
1616 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001617 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001618 LOGCHAR('"');
1619
1620 end = uri + strlen(uri);
1621 // look for the first whitespace character
1622 spc = uri;
1623 while (spc < end && !HTTP_IS_SPHT(*spc))
1624 spc++;
1625
1626 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
1627 chunk.str = "<BADREQ>";
1628 chunk.len = strlen("<BADREQ>");
1629 } else {
1630 chunk.str = uri;
1631 chunk.len = spc - uri;
1632 }
1633
1634 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1635 if (ret == NULL || *ret != '\0')
1636 goto out;
1637
1638 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001639 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001640 LOGCHAR('"');
1641
1642 last_isspace = 0;
1643 break;
1644
1645 case LOG_FMT_HTTP_VERSION: // %HV
1646 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001647 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001648 LOGCHAR('"');
1649
1650 end = uri + strlen(uri);
1651 // look for the first whitespace character
1652 while (uri < end && !HTTP_IS_SPHT(*uri))
1653 uri++;
1654
1655 // keep advancing past multiple spaces
1656 while (uri < end && HTTP_IS_SPHT(*uri)) {
1657 uri++; nspaces++;
1658 }
1659
1660 // look for the next whitespace character
1661 while (uri < end && !HTTP_IS_SPHT(*uri))
1662 uri++;
1663
1664 // keep advancing past multiple spaces
1665 while (uri < end && HTTP_IS_SPHT(*uri))
1666 uri++;
1667
1668 if (!txn->uri || nspaces == 0) {
1669 chunk.str = "<BADREQ>";
1670 chunk.len = strlen("<BADREQ>");
1671 } else if (uri == end) {
1672 chunk.str = "HTTP/0.9";
1673 chunk.len = strlen("HTTP/0.9");
1674 } else {
1675 chunk.str = uri;
1676 chunk.len = end - 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
William Lallemand5f232402012-04-05 18:02:55 +02001690 case LOG_FMT_COUNTER: // %rt
1691 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001692 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001693 if (iret < 0 || iret > dst + maxsize - tmplog)
1694 goto out;
1695 last_isspace = 0;
1696 tmplog += iret;
1697 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001698 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001699 if (ret == NULL)
1700 goto out;
1701 tmplog = ret;
1702 last_isspace = 0;
1703 }
1704 break;
1705
Willy Tarreau7346acb2014-08-28 15:03:15 +02001706 case LOG_FMT_LOGCNT: // %lc
1707 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001708 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001709 if (iret < 0 || iret > dst + maxsize - tmplog)
1710 goto out;
1711 last_isspace = 0;
1712 tmplog += iret;
1713 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001714 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001715 if (ret == NULL)
1716 goto out;
1717 tmplog = ret;
1718 last_isspace = 0;
1719 }
1720 break;
1721
William Lallemand5f232402012-04-05 18:02:55 +02001722 case LOG_FMT_HOSTNAME: // %H
1723 src = hostname;
1724 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1725 if (ret == NULL)
1726 goto out;
1727 tmplog = ret;
1728 last_isspace = 0;
1729 break;
1730
1731 case LOG_FMT_PID: // %pid
1732 if (tmp->options & LOG_OPT_HEXA) {
1733 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1734 if (iret < 0 || iret > dst + maxsize - tmplog)
1735 goto out;
1736 last_isspace = 0;
1737 tmplog += iret;
1738 } else {
1739 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1740 if (ret == NULL)
1741 goto out;
1742 tmplog = ret;
1743 last_isspace = 0;
1744 }
1745 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001746
1747 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001748 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001749 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02001750 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001751 if (ret == NULL)
1752 goto out;
1753 tmplog = ret;
1754 last_isspace = 0;
1755 break;
1756
William Lallemandbddd4fd2012-02-27 11:23:10 +01001757 }
1758 }
1759
1760out:
William Lallemand1d705562012-03-12 12:46:41 +01001761 /* *tmplog is a unused character */
1762 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001763 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001764
Willy Tarreaubaaee002006-06-26 02:48:02 +02001765}
1766
William Lallemand1d705562012-03-12 12:46:41 +01001767/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001768 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01001769 * Will not log if the frontend has no log defined.
1770 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001771void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01001772{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001773 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01001774 char *tmplog;
1775 int size, err, level;
1776
1777 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001778 err = (s->flags & SF_REDISP) ||
1779 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
1780 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01001781 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02001782 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001783
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001784 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01001785 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001786
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001787 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01001788 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001789
Willy Tarreauabcd5142013-06-11 17:18:02 +02001790 if (s->logs.level) { /* loglevel was overridden */
1791 if (s->logs.level == -1) {
1792 s->logs.logwait = 0; /* logs disabled */
1793 return;
1794 }
1795 level = s->logs.level - 1;
1796 }
1797 else {
1798 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001799 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02001800 level = LOG_ERR;
1801 }
William Lallemand1d705562012-03-12 12:46:41 +01001802
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001803 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001804 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001805 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001806 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001807 }
1808
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001809 tmplog = update_log_hdr(sess->fe->log_tag ? sess->fe->log_tag : global.log_tag);
William Lallemand1d705562012-03-12 12:46:41 +01001810 size = tmplog - logline;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001811 size += build_logline(s, tmplog, global.max_syslog_len - size, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01001812 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001813 sess->fe->log_count++;
1814 __send_log(sess->fe, level, logline, size + 1);
William Lallemand1d705562012-03-12 12:46:41 +01001815 s->logs.logwait = 0;
1816 }
1817}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001818
Willy Tarreaubaaee002006-06-26 02:48:02 +02001819/*
1820 * Local variables:
1821 * c-indent-level: 8
1822 * c-basic-offset: 8
1823 * End:
1824 */