blob: 20a98b2ccf2fe741e2f09b5bca38fac150d18899 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
25
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020027#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020029#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010032#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020033
William Lallemand5f232402012-04-05 18:02:55 +020034#include <proto/frontend.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020035#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010036#include <proto/sample.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010037#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020038#ifdef USE_OPENSSL
39#include <proto/ssl_sock.h>
40#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreaubaaee002006-06-26 02:48:02 +020042const char *log_facilities[NB_LOG_FACILITIES] = {
43 "kern", "user", "mail", "daemon",
44 "auth", "syslog", "lpr", "news",
45 "uucp", "cron", "auth2", "ftp",
46 "ntp", "audit", "alert", "cron2",
47 "local0", "local1", "local2", "local3",
48 "local4", "local5", "local6", "local7"
49};
50
51
52const char *log_levels[NB_LOG_LEVELS] = {
53 "emerg", "alert", "crit", "err",
54 "warning", "notice", "info", "debug"
55};
56
Willy Tarreau570f2212013-06-10 16:42:09 +020057const 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 +020058const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
William Lallemand723b73a2012-02-08 16:37:49 +010060
61/* log_format */
62struct logformat_type {
63 char *name;
64 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010065 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020066 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010067 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +010068 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +010069};
70
William Lallemandb7ff6a32012-03-02 14:35:21 +010071int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
72
William Lallemand723b73a2012-02-08 16:37:49 +010073/* log_format variable names */
74static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020075 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +010076
77 /* please keep these lines sorted ! */
78 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
79 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
80 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
81 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
82 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +020083 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +020084 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +010085 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +080086 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +020087 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +020088 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +010089 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +020090 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +010091 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
92 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +020093 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010094 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +020095 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010096 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
97 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +020098 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +010099 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
100 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
101 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
102 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
103 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
104 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
105 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200106 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
107 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
108 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
109 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200110 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100111 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200112 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100113 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
114 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100115 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100116 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
117 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
118 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
119 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
120 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200121 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
122 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100123 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
124 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
125 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
126
127 /* The following tags are deprecated and will be removed soon */
128 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
129 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
130 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
131 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
132 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
133 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
134 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
135 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
136 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
137 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
138 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200139 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100140};
141
Willy Tarreau2beef582012-12-20 17:22:52 +0100142char 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
143char 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";
144char 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 +0100145char *log_format = NULL;
146
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100147/* This is a global syslog line, common to all outgoing messages. It begins
148 * with the syslog tag and the date that are updated by update_log_hdr().
149 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200150char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100151
William Lallemand723b73a2012-02-08 16:37:49 +0100152struct logformat_var_args {
153 char *name;
154 int mask;
155};
156
157struct logformat_var_args var_args_list[] = {
158// global
159 { "M", LOG_OPT_MANDATORY },
160 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200161 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100162 { 0, 0 }
163};
164
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200165/* return the name of the directive used in the current proxy for which we're
166 * currently parsing a header, when it is known.
167 */
168static inline const char *fmt_directive(const struct proxy *curproxy)
169{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100170 switch (curproxy->conf.args.ctx) {
171 case ARGC_UIF:
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200172 return "unique-id-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100173 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100174 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100175 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100176 return "http-response";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100177 case ARGC_STK:
178 return "stick";
179 case ARGC_TRK:
180 return "track-sc"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100181 case ARGC_RDR:
182 return "redirect"; break;
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100183 case ARGC_ACL:
184 return "acl"; break;
185 default:
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200186 return "log-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100187 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200188}
189
William Lallemand723b73a2012-02-08 16:37:49 +0100190/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100191 * callback used to configure addr source retrieval
192 */
193int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
194{
195 curproxy->options2 |= PR_O2_SRC_ADDR;
196
197 return 0;
198}
199
200
201/*
William Lallemand723b73a2012-02-08 16:37:49 +0100202 * Parse args in a logformat_var
203 */
204int parse_logformat_var_args(char *args, struct logformat_node *node)
205{
206 int i = 0;
207 int end = 0;
208 int flags = 0; // 1 = + 2 = -
209 char *sp = NULL; // start pointer
210
211 if (args == NULL)
212 return 1;
213
214 while (1) {
215 if (*args == '\0')
216 end = 1;
217
218 if (*args == '+') {
219 // add flag
220 sp = args + 1;
221 flags = 1;
222 }
223 if (*args == '-') {
224 // delete flag
225 sp = args + 1;
226 flags = 2;
227 }
228
229 if (*args == '\0' || *args == ',') {
230 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100231 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100232 if (strcmp(sp, var_args_list[i].name) == 0) {
233 if (flags == 1) {
234 node->options |= var_args_list[i].mask;
235 break;
236 } else if (flags == 2) {
237 node->options &= ~var_args_list[i].mask;
238 break;
239 }
240 }
241 }
242 sp = NULL;
243 if (end)
244 break;
245 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100246 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100247 }
248 return 0;
249}
250
251/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100252 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
253 * must pass the args part in the <arg> pointer with its length in <arg_len>,
254 * and varname with its length in <var> and <var_len> respectively. <arg> is
255 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100256 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100257int 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 +0100258{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100259 int j;
260 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100261
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100262 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
263 if (strlen(logformat_keywords[j].name) == var_len &&
264 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
265 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
266 node = calloc(1, sizeof(struct logformat_node));
267 node->type = logformat_keywords[j].type;
268 node->options = *defoptions;
269 if (arg_len) {
270 node->arg = my_strndup(arg, arg_len);
271 parse_logformat_var_args(node->arg, node);
272 }
273 if (node->type == LOG_FMT_GLOBAL) {
274 *defoptions = node->options;
275 free(node->arg);
276 free(node);
277 } else {
278 if (logformat_keywords[j].config_callback &&
279 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100280 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100281 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100282 curproxy->to_log |= logformat_keywords[j].lw;
283 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100284 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100285 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100286 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200287 curproxy->conf.args.file, curproxy->conf.args.line,
288 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100289 return 0;
290 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100291 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200292 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
293 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100294 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100295 }
William Lallemand723b73a2012-02-08 16:37:49 +0100296 }
297 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100298
299 j = var[var_len];
300 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100301 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 +0200302 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100303 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100304 return -1;
305}
306
307/*
308 * push to the logformat linked list
309 *
310 * start: start pointer
311 * end: end text pointer
312 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100313 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100314 *
315 * LOG_TEXT: copy chars from start to end excluding end.
316 *
317*/
William Lallemand1d705562012-03-12 12:46:41 +0100318void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100319{
320 char *str;
321
Willy Tarreaua3571662012-12-20 21:59:12 +0100322 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100323 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100324 str = calloc(end - start + 1, 1);
325 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100326 str[end - start] = '\0';
327 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100328 node->type = LOG_FMT_TEXT; // type string
329 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100330 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100331 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100332 node->type = LOG_FMT_SEPARATOR;
333 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100334 }
335}
336
337/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100338 * Parse the sample fetch expression <text> and add a node to <list_format> upon
339 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200340 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100341 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100342void 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 +0100343{
344 char *cmd[2];
345 struct sample_expr *expr;
346 struct logformat_node *node;
347 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100348 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100349
350 cmd[0] = text;
351 cmd[1] = "";
352 cmd_arg = 0;
353
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100354 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100355 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200356 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
357 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100358 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100359 return;
360 }
361
362 node = calloc(1, sizeof(struct logformat_node));
363 node->type = LOG_FMT_EXPR;
364 node->expr = expr;
365 node->options = options;
366
367 if (arg_len) {
368 node->arg = my_strndup(arg, arg_len);
369 parse_logformat_var_args(node->arg, node);
370 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100371 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100372 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
373
Willy Tarreau434c57c2013-01-08 01:10:24 +0100374 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100375 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
376
Willy Tarreau434c57c2013-01-08 01:10:24 +0100377 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200378 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
379 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
380 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100381
Willy Tarreauc8368452012-12-21 00:09:23 +0100382 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
383 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100384 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100385
William Lallemand65ad6e12014-01-31 15:08:02 +0100386 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
387 * needed with some sample fetches (eg: ssl*). We always set it for
388 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100389 */
390 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100391 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100392 LIST_ADDQ(list_format, &node->list);
393}
394
395/*
William Lallemand723b73a2012-02-08 16:37:49 +0100396 * Parse the log_format string and fill a linked list.
397 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200398 * You can set arguments using { } : %{many arguments}varname.
399 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100400 *
401 * str: the string to parse
402 * curproxy: the proxy affected
403 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100404 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100405 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100406 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100407void 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 +0100408{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100409 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100410 char *arg = NULL; /* start pointer for args */
411 char *var = NULL; /* start pointer for vars */
412 int arg_len = 0;
413 int var_len = 0;
414 int cformat; /* current token format */
415 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100416 struct logformat_node *tmplf, *back;
417
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100418 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200419 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200420
William Lallemand723b73a2012-02-08 16:37:49 +0100421 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100422 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100423 LIST_DEL(&tmplf->list);
424 free(tmplf);
425 }
426
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100427 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100428 pformat = cformat;
429
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100430 if (!*str)
431 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100432
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100433 /* The prinicple of the two-step state machine below is to first detect a change, and
434 * second have all common paths processed at one place. The common paths are the ones
435 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
436 * We use the common LF_INIT state to dispatch to the different final states.
437 */
438 switch (pformat) {
439 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100440 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100441 arg_len = var_len = 0;
442 if (*str == '{') { // optional argument
443 cformat = LF_STARG;
444 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100445 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100446 else if (*str == '[') {
447 cformat = LF_STEXPR;
448 var = str + 1; // store expr in variable name
449 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100450 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100451 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100452 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100453 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100454 else if (*str == '%')
455 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100456 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100457 /* single '%' followed by blank or digit, send them both */
458 cformat = LF_TEXT;
459 pformat = LF_TEXT; /* finally we include the previous char as well */
460 sp = str - 1; /* send both the '%' and the current char */
461 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",
462 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
463
464 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100465 else
466 cformat = LF_INIT; // handle other cases of litterals
467 break;
468
469 case LF_STARG: // text immediately following '%{'
470 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100471 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100472 arg_len = str - arg;
473 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100474 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100475 break;
476
477 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100478 if (*str == '[') {
479 cformat = LF_STEXPR;
480 var = str + 1; // store expr in variable name
481 break;
482 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100483 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100484 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100485 var = str;
486 break;
487 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200488 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
489 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100490 cformat = LF_INIT;
491 break;
492
Willy Tarreauc8368452012-12-21 00:09:23 +0100493 case LF_STEXPR: // text immediately following '%['
494 if (*str == ']') { // end of arg
495 cformat = LF_EDEXPR;
496 var_len = str - var;
497 *str = 0; // needed for parsing the expression
498 }
499 break;
500
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100501 case LF_VAR: // text part of a variable name
502 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100503 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100504 cformat = LF_INIT; // not variable name anymore
505 break;
506
Willy Tarreauc8368452012-12-21 00:09:23 +0100507 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100508 cformat = LF_INIT;
509 }
510
511 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
512 switch (*str) {
513 case '%': cformat = LF_STARTVAR; break;
514 case ' ': cformat = LF_SEPARATOR; break;
515 case 0 : cformat = LF_END; break;
516 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100517 }
518 }
519
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100520 if (cformat != pformat || pformat == LF_SEPARATOR) {
521 switch (pformat) {
522 case LF_VAR:
523 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
524 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100525 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100526 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100527 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100528 case LF_TEXT:
529 case LF_SEPARATOR:
530 add_to_logformat_list(sp, str, pformat, list_format);
531 break;
532 }
533 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100534 }
535 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100536
Willy Tarreauc8368452012-12-21 00:09:23 +0100537 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200538 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
539 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
540 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100541
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100542 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100543}
544
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545/*
546 * Displays the message on stderr with the date and pid. Overrides the quiet
547 * mode during startup.
548 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200549void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550{
551 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200552 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553
554 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
555 va_start(argp, fmt);
556
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200557 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200559 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200560 vfprintf(stderr, fmt, argp);
561 fflush(stderr);
562 va_end(argp);
563 }
564}
565
566
567/*
568 * Displays the message on stderr with the date and pid.
569 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200570void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571{
572 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200573 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574
575 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
576 va_start(argp, fmt);
577
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200578 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200580 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200581 vfprintf(stderr, fmt, argp);
582 fflush(stderr);
583 va_end(argp);
584 }
585}
586
587/*
588 * Displays the message on <out> only if quiet mode is not set.
589 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200590void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200591{
592 va_list argp;
593
594 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
595 va_start(argp, fmt);
596 vfprintf(out, fmt, argp);
597 fflush(out);
598 va_end(argp);
599 }
600}
601
602/*
603 * returns log level for <lev> or -1 if not found.
604 */
605int get_log_level(const char *lev)
606{
607 int level;
608
609 level = NB_LOG_LEVELS - 1;
610 while (level >= 0 && strcmp(log_levels[level], lev))
611 level--;
612
613 return level;
614}
615
616
617/*
618 * returns log facility for <fac> or -1 if not found.
619 */
620int get_log_facility(const char *fac)
621{
622 int facility;
623
624 facility = NB_LOG_FACILITIES - 1;
625 while (facility >= 0 && strcmp(log_facilities[facility], fac))
626 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100627
Willy Tarreaubaaee002006-06-26 02:48:02 +0200628 return facility;
629}
630
William Lallemanda1cc3812012-02-08 16:38:44 +0100631/*
632 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200633 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100634 *
635 * Return the adress of the \0 character, or NULL on error
636 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100637char *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 +0100638{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100639 if (size < 2)
640 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100641
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100642 if (node->options & LOG_OPT_QUOTE) {
643 *(dst++) = '"';
644 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100645 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100646
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100647 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100648 if (++len > size)
649 len = size;
650 len = strlcpy2(dst, src, len);
651
652 size -= len;
653 dst += len;
654 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100655 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
656 if (size < 2)
657 return NULL;
658 *(dst++) = '-';
659 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100660
661 if (node->options & LOG_OPT_QUOTE) {
662 if (size < 2)
663 return NULL;
664 *(dst++) = '"';
665 }
666
667 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100668 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100669}
670
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100671static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
672{
673 return lf_text_len(dst, src, size, size, node);
674}
675
William Lallemand5f232402012-04-05 18:02:55 +0200676/*
677 * Write a IP adress to the log string
678 * +X option write in hexadecimal notation, most signifant byte on the left
679 */
680char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
681{
682 char *ret = dst;
683 int iret;
684 char pn[INET6_ADDRSTRLEN];
685
686 if (node->options & LOG_OPT_HEXA) {
687 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
688 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
689 if (iret < 0 || iret > size)
690 return NULL;
691 ret += iret;
692 } else {
693 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
694 ret = lf_text(dst, pn, size, node);
695 if (ret == NULL)
696 return NULL;
697 }
698 return ret;
699}
700
701/*
702 * Write a port to the log
703 * +X option write in hexadecimal notation, most signifant byte on the left
704 */
705char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
706{
707 char *ret = dst;
708 int iret;
709
710 if (node->options & LOG_OPT_HEXA) {
711 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
712 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
713 if (iret < 0 || iret > size)
714 return NULL;
715 ret += iret;
716 } else {
717 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
718 if (ret == NULL)
719 return NULL;
720 }
721 return ret;
722}
723
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100724/* Re-generate the syslog header at the beginning of logline once a second and
725 * return the pointer to the first character after the header.
726 */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100727static char *update_log_hdr(const char *log_tag)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100729 static long tvsec;
730 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100731 int tag_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200733 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200734 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200735 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100736 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200737
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200738 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200739 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200740
Willy Tarreau18324f52014-06-27 18:10:07 +0200741 hdr_len = snprintf(logline, global.max_syslog_len,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100742 "<<<<>%s %2d %02d:%02d:%02d %s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200743 monthname[tm.tm_mon],
744 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100745 global.log_send_hostname ? global.log_send_hostname : "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200746 /* WARNING: depending upon implementations, snprintf may return
747 * either -1 or the number of bytes that would be needed to store
748 * the total message. In both cases, we must adjust it.
749 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200750 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
751 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200752
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100753 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754 }
755
Willy Tarreau094af4e2015-01-07 15:03:42 +0100756 dataptr[0] = 0; // ensure we get rid of any previous attempt
757
758 tag_len = snprintf(dataptr, logline + global.max_syslog_len - dataptr, "%s[%d]: ", log_tag, pid);
759 if (tag_len < 0 || tag_len > logline + global.max_syslog_len - dataptr)
760 tag_len = logline + global.max_syslog_len - dataptr;
761
762 return dataptr + tag_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100763}
764
765/*
766 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100767 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100768 */
769void send_log(struct proxy *p, int level, const char *format, ...)
770{
771 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100772 char *dataptr;
773 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100774
Willy Tarreau8c97ab52015-01-15 16:29:53 +0100775 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100776 return;
777
Thierry FOURNIERe83766a2015-03-09 17:10:29 +0100778 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 +0100779 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100780
781 va_start(argp, format);
Willy Tarreau18324f52014-06-27 18:10:07 +0200782 data_len += vsnprintf(dataptr, logline + global.max_syslog_len - dataptr, format, argp);
783 if (data_len < 0 || data_len > global.max_syslog_len)
784 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200785 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100786
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100787 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100788}
789
790/*
791 * This function sends a syslog message.
792 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100793 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100794 */
795void __send_log(struct proxy *p, int level, char *message, size_t size)
796{
797 static int logfdunix = -1; /* syslog to AF_UNIX socket */
798 static int logfdinet = -1; /* syslog to AF_INET socket */
799 static char *dataptr = NULL;
800 int fac_level;
801 struct list *logsrvs = NULL;
802 struct logsrv *tmp = NULL;
803 int nblogger;
804 char *log_ptr;
805
806 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807
808 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200809 if (!LIST_ISEMPTY(&global.logsrvs)) {
810 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811 }
812 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200813 if (!LIST_ISEMPTY(&p->logsrvs)) {
814 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100816 }
817
William Lallemand0f99e342011-10-12 17:50:54 +0200818 if (!logsrvs)
819 return;
820
Robert Tsai81ae1952007-12-05 10:47:29 +0100821 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200822 nblogger = 0;
823 list_for_each_entry(tmp, logsrvs, list) {
824 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100825 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100826 &logfdunix : &logfdinet;
827 int sent;
Willy Tarreau18324f52014-06-27 18:10:07 +0200828 int max;
829 char backup;
Robert Tsai81ae1952007-12-05 10:47:29 +0100830
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200831 nblogger++;
832
Willy Tarreaubaaee002006-06-26 02:48:02 +0200833 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200834 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100836
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200837 if (unlikely(*plogfd < 0)) {
838 /* socket not successfully initialized yet */
839 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
840
841 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
842 Alert("socket for logger #%d failed: %s (errno=%d)\n",
843 nblogger, strerror(errno), errno);
844 continue;
845 }
846 /* we don't want to receive anything on this socket */
847 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
848 /* does nothing under Linux, maybe needed for others */
849 shutdown(*plogfd, SHUT_RD);
850 }
851
Willy Tarreaubaaee002006-06-26 02:48:02 +0200852 /* For each target, we may have a different facility.
853 * We can also have a different log level for each message.
854 * This induces variations in the message header length.
855 * Since we don't want to recompute it each time, nor copy it every
856 * time, we only change the facility in the pre-computed header,
857 * and we change the pointer to the header accordingly.
858 */
William Lallemand0f99e342011-10-12 17:50:54 +0200859 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100860 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200861 do {
862 *log_ptr = '0' + fac_level % 10;
863 fac_level /= 10;
864 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100865 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100867
Willy Tarreau18324f52014-06-27 18:10:07 +0200868 max = size - (log_ptr - dataptr);
869 if (max > logsrv->maxlen)
870 max = logsrv->maxlen;
871
872 /* insert a \n at the end of the message, but save what was
873 * there first because we could have different max lengths
874 * for different log targets.
875 */
876 backup = log_ptr[max - 1];
877 log_ptr[max - 1] = '\n';
878
879 sent = sendto(*plogfd, log_ptr, max,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200880 MSG_DONTWAIT | MSG_NOSIGNAL,
881 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Willy Tarreau18324f52014-06-27 18:10:07 +0200882
883 log_ptr[max - 1] = backup;
884
Robert Tsai81ae1952007-12-05 10:47:29 +0100885 if (sent < 0) {
886 Alert("sendto logger #%d failed: %s (errno=%d)\n",
887 nblogger, strerror(errno), errno);
888 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200889 }
890}
891
William Lallemandbddd4fd2012-02-27 11:23:10 +0100892extern fd_set hdr_encode_map[];
893extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100894extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +0100895
Willy Tarreaubaaee002006-06-26 02:48:02 +0200896
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200897const 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 +0100898const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
899 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
900 Set-cookie Updated, unknown, unknown */
901
William Lallemand1d705562012-03-12 12:46:41 +0100902/*
903 * try to write a character if there is enough space, or goto out
904 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100905#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100906 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100907 *(tmplog++) = (x); \
908 } else { \
909 goto out; \
910 } \
911 } while(0)
912
William Lallemand1d705562012-03-12 12:46:41 +0100913
Willy Tarreaudf974472012-12-28 02:44:01 +0100914/* Builds a log line in <dst> based on <list_format>, and stops before reaching
915 * <maxsize> characters. Returns the size of the output string in characters,
916 * not counting the trailing zero which is always added if the resulting size
917 * is not zero.
918 */
William Lallemand1d705562012-03-12 12:46:41 +0100919int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920{
Willy Tarreau73de9892006-11-30 11:40:23 +0100921 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100922 struct proxy *be = s->be;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100923 struct http_txn *txn = &s->txn;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100924 char *uri;
Willy Tarreaufe944602007-10-25 10:34:16 +0200925 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100926 int t_request;
927 int hdr;
928 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100929 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100930 char *ret;
931 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100932 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933
William Lallemandbddd4fd2012-02-27 11:23:10 +0100934 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935
William Lallemandbddd4fd2012-02-27 11:23:10 +0100936 t_request = -1;
937 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
938 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
939
William Lallemand1d705562012-03-12 12:46:41 +0100940 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200941
William Lallemandbddd4fd2012-02-27 11:23:10 +0100942 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100943 if (LIST_ISEMPTY(list_format))
944 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100945
William Lallemand1d705562012-03-12 12:46:41 +0100946 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200947 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +0200948 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100949 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100950 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +0100951
Willy Tarreauc8368452012-12-21 00:09:23 +0100952 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +0100953 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100954 if (!last_isspace) {
955 LOGCHAR(' ');
956 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100957 }
958 break;
959
William Lallemand1d705562012-03-12 12:46:41 +0100960 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100961 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200962 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100963 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100964 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100965 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100966 last_isspace = 0;
967 break;
968
Willy Tarreauc8368452012-12-21 00:09:23 +0100969 case LOG_FMT_EXPR: // sample expression, may be request or response
970 key = NULL;
971 if (tmp->options & LOG_OPT_REQ_CAP)
972 key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr);
973 if (!key && (tmp->options & LOG_OPT_RES_CAP))
974 key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100975 if (tmp->options & LOG_OPT_HTTP)
976 ret = encode_chunk(tmplog, dst + maxsize,
977 '%', http_encode_map, key ? &key->data.str : &empty);
978 else
979 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 +0100980 if (ret == 0)
981 goto out;
982 tmplog = ret;
983 last_isspace = 0;
984 break;
985
Willy Tarreau2beef582012-12-20 17:22:52 +0100986 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau350f4872014-11-28 14:42:25 +0100987 conn = objt_conn(s->si[0].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200988 if (conn)
989 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
990 else
991 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100992 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100993 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100994 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100995 last_isspace = 0;
996 break;
997
Willy Tarreau2beef582012-12-20 17:22:52 +0100998 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau350f4872014-11-28 14:42:25 +0100999 conn = objt_conn(s->si[0].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001000 if (conn) {
1001 if (conn->addr.from.ss_family == AF_UNIX) {
1002 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
1003 } else {
1004 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1005 dst + maxsize - tmplog, tmp);
1006 }
William Lallemand5f232402012-04-05 18:02:55 +02001007 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001008 else
1009 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1010
William Lallemand5f232402012-04-05 18:02:55 +02001011 if (ret == NULL)
1012 goto out;
1013 tmplog = ret;
1014 last_isspace = 0;
1015 break;
1016
Willy Tarreau2beef582012-12-20 17:22:52 +01001017 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau350f4872014-11-28 14:42:25 +01001018 conn = objt_conn(s->si[0].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001019 if (conn) {
1020 conn_get_to_addr(conn);
1021 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1022 }
1023 else
1024 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1025
William Lallemand1d705562012-03-12 12:46:41 +01001026 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001027 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001028 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001029 last_isspace = 0;
1030 break;
1031
Willy Tarreau2beef582012-12-20 17:22:52 +01001032 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau350f4872014-11-28 14:42:25 +01001033 conn = objt_conn(s->si[0].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001034 if (conn) {
1035 conn_get_to_addr(conn);
1036 if (conn->addr.to.ss_family == AF_UNIX)
1037 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
1038 else
1039 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001040 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001041 else
1042 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1043
William Lallemand5f232402012-04-05 18:02:55 +02001044 if (ret == NULL)
1045 goto out;
1046 tmplog = ret;
1047 last_isspace = 0;
1048 break;
1049
Willy Tarreau2beef582012-12-20 17:22:52 +01001050 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001051 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001052 if (conn)
1053 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1054 else
1055 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1056
William Lallemand1d705562012-03-12 12:46:41 +01001057 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001058 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001059 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001060 last_isspace = 0;
1061 break;
1062
Willy Tarreau2beef582012-12-20 17:22:52 +01001063 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001064 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001065 if (conn)
1066 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1067 else
1068 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1069
William Lallemand5f232402012-04-05 18:02:55 +02001070 if (ret == NULL)
1071 goto out;
1072 tmplog = ret;
1073 last_isspace = 0;
1074 break;
1075
Willy Tarreau2beef582012-12-20 17:22:52 +01001076 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001077 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001078 if (conn)
1079 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1080 else
1081 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1082
William Lallemand5f232402012-04-05 18:02:55 +02001083 if (ret == NULL)
1084 goto out;
1085 tmplog = ret;
1086 last_isspace = 0;
1087 break;
1088
Willy Tarreau2beef582012-12-20 17:22:52 +01001089 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001090 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001091 if (conn)
1092 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1093 else
1094 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1095
William Lallemand1d705562012-03-12 12:46:41 +01001096 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001097 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001098 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001099 last_isspace = 0;
1100 break;
1101
William Lallemand1d705562012-03-12 12:46:41 +01001102 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001103 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001104 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1105 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001106 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001107 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001108 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001109 last_isspace = 0;
1110 break;
1111
William Lallemand1d705562012-03-12 12:46:41 +01001112 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001113 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001114 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001115 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001116 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001117 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001118 last_isspace = 0;
1119 break;
1120
Yuxans Yao4e25b012012-10-19 10:36:09 +08001121 case LOG_FMT_DATELOCAL: // %Tl
1122 get_localtime(s->logs.accept_date.tv_sec, &tm);
1123 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1124 if (ret == NULL)
1125 goto out;
1126 tmplog = ret;
1127 last_isspace = 0;
1128 break;
1129
William Lallemand5f232402012-04-05 18:02:55 +02001130 case LOG_FMT_TS: // %Ts
1131 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1132 if (tmp->options & LOG_OPT_HEXA) {
1133 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1134 if (iret < 0 || iret > dst + maxsize - tmplog)
1135 goto out;
1136 last_isspace = 0;
1137 tmplog += iret;
1138 } else {
1139 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1140 if (ret == NULL)
1141 goto out;
1142 tmplog = ret;
1143 last_isspace = 0;
1144 }
1145 break;
1146
William Lallemand1d705562012-03-12 12:46:41 +01001147 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001148 if (tmp->options & LOG_OPT_HEXA) {
1149 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1150 if (iret < 0 || iret > dst + maxsize - tmplog)
1151 goto out;
1152 last_isspace = 0;
1153 tmplog += iret;
1154 } else {
1155 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001156 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001157 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1158 tmplog, 4);
1159 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001160 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001161 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001162 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001163 }
1164 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001165
William Lallemand1d705562012-03-12 12:46:41 +01001166 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001167 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001168 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001169 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001170 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001171 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001172 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001173 break;
1174
Willy Tarreau773d65f2012-10-12 14:56:11 +02001175 case LOG_FMT_FRONTEND_XPRT: // %ft
1176 src = fe->id;
1177 if (tmp->options & LOG_OPT_QUOTE)
1178 LOGCHAR('"');
1179 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1180 if (iret == 0)
1181 goto out;
1182 tmplog += iret;
1183#ifdef USE_OPENSSL
1184 if (s->listener->xprt == &ssl_sock)
1185 LOGCHAR('~');
1186#endif
1187 if (tmp->options & LOG_OPT_QUOTE)
1188 LOGCHAR('"');
1189 last_isspace = 0;
1190 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001191#ifdef USE_OPENSSL
1192 case LOG_FMT_SSL_CIPHER: // %sslc
1193 src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001194 conn = objt_conn(s->si[0].end);
1195 if (conn) {
1196 if (s->listener->xprt == &ssl_sock)
1197 src = ssl_sock_get_cipher_name(conn);
1198 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001199 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1200 if (ret == NULL)
1201 goto out;
1202 tmplog = ret;
1203 last_isspace = 0;
1204 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001205
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001206 case LOG_FMT_SSL_VERSION: // %sslv
1207 src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001208 conn = objt_conn(s->si[0].end);
1209 if (conn) {
1210 if (s->listener->xprt == &ssl_sock)
1211 src = ssl_sock_get_proto_version(conn);
1212 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001213 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1214 if (ret == NULL)
1215 goto out;
1216 tmplog = ret;
1217 last_isspace = 0;
1218 break;
1219#endif
William Lallemand1d705562012-03-12 12:46:41 +01001220 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001221 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001222 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001223 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001224 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001225 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001226 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001227 break;
1228
William Lallemand1d705562012-03-12 12:46:41 +01001229 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001230 switch (obj_type(s->target)) {
1231 case OBJ_TYPE_SERVER:
1232 src = objt_server(s->target)->id;
1233 break;
1234 case OBJ_TYPE_APPLET:
1235 src = objt_applet(s->target)->name;
1236 break;
1237 default:
1238 src = "<NOSRV>";
1239 break;
1240 }
William Lallemand5f232402012-04-05 18:02:55 +02001241 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001242 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001243 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001244 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001245 last_isspace = 0;
1246 break;
1247
William Lallemand1d705562012-03-12 12:46:41 +01001248 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001249 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001250 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001251 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001252 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001253 last_isspace = 0;
1254 break;
1255
William Lallemand1d705562012-03-12 12:46:41 +01001256 case LOG_FMT_TW: // %Tw
1257 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001258 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001259 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001260 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001261 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001262 last_isspace = 0;
1263 break;
1264
William Lallemand1d705562012-03-12 12:46:41 +01001265 case LOG_FMT_TC: // %Tc
1266 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001267 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001268 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001269 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001270 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001271 last_isspace = 0;
1272 break;
1273
William Lallemand1d705562012-03-12 12:46:41 +01001274 case LOG_FMT_TR: // %Tr
1275 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001276 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001277 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001278 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001279 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001280 last_isspace = 0;
1281 break;
1282
William Lallemand1d705562012-03-12 12:46:41 +01001283 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001284 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001285 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001286 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001287 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001288 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001289 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001290 last_isspace = 0;
1291 break;
1292
Willy Tarreau2beef582012-12-20 17:22:52 +01001293 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001294 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001295 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001296 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001297 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001298 last_isspace = 0;
1299 break;
1300
William Lallemand1d705562012-03-12 12:46:41 +01001301 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001302 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001303 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001304 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001305 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001306 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001307 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001308 last_isspace = 0;
1309 break;
1310
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001311 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001312 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1313 if (ret == NULL)
1314 goto out;
1315 tmplog = ret;
1316 last_isspace = 0;
1317 break;
1318
Willy Tarreau2beef582012-12-20 17:22:52 +01001319 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001320 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001321 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001322 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001323 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001324 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001325 last_isspace = 0;
1326 break;
1327
Willy Tarreau2beef582012-12-20 17:22:52 +01001328 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001329 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001330 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001331 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001332 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001333 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001334 last_isspace = 0;
1335 break;
1336
William Lallemand1d705562012-03-12 12:46:41 +01001337 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +01001338 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1339 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
1340 *tmplog = '\0';
1341 last_isspace = 0;
1342 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001343
William Lallemand1d705562012-03-12 12:46:41 +01001344 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001345 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1346 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001347 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1348 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 +01001349 last_isspace = 0;
1350 break;
1351
William Lallemand1d705562012-03-12 12:46:41 +01001352 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001353 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001354 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001355 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001356 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001357 last_isspace = 0;
1358 break;
1359
William Lallemand1d705562012-03-12 12:46:41 +01001360 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001361 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001362 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001363 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001364 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001365 last_isspace = 0;
1366 break;
1367
William Lallemand1d705562012-03-12 12:46:41 +01001368 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001369 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001370 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001371 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001372 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001373 last_isspace = 0;
1374 break;
1375
William Lallemand1d705562012-03-12 12:46:41 +01001376 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001377 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001378 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001379 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001380 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001381 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001382 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001383 last_isspace = 0;
1384 break;
1385
William Lallemand1d705562012-03-12 12:46:41 +01001386 case LOG_FMT_RETRIES: // %rq
William Lallemandbddd4fd2012-02-27 11:23:10 +01001387 if (s->flags & SN_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001388 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001389 ret = ltoa_o((s->si[1].conn_retries>0) ?
1390 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001391 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001392 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001393 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001394 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001395 last_isspace = 0;
1396 break;
1397
William Lallemand1d705562012-03-12 12:46:41 +01001398 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001399 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001400 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001401 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001402 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001403 last_isspace = 0;
1404 break;
1405
William Lallemand1d705562012-03-12 12:46:41 +01001406 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001407 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001408 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001409 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001410 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001411 last_isspace = 0;
1412 break;
1413
William Lallemand1d705562012-03-12 12:46:41 +01001414 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001415 /* request header */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001416 if (fe->nb_req_cap && txn->req.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001417 if (tmp->options & LOG_OPT_QUOTE)
1418 LOGCHAR('"');
1419 LOGCHAR('{');
1420 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1421 if (hdr)
1422 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001423 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001424 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001425 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001426 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001427 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001428 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001429 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001430 }
1431 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001432 if (tmp->options & LOG_OPT_QUOTE)
1433 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001434 last_isspace = 0;
1435 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001436 break;
1437
William Lallemand1d705562012-03-12 12:46:41 +01001438 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001439 /* request header list */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001440 if (fe->nb_req_cap && txn->req.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001441 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1442 if (hdr > 0)
1443 LOGCHAR(' ');
1444 if (tmp->options & LOG_OPT_QUOTE)
1445 LOGCHAR('"');
1446 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001447 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001448 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001449 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001450 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001451 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001452 } else if (!(tmp->options & LOG_OPT_QUOTE))
1453 LOGCHAR('-');
1454 if (tmp->options & LOG_OPT_QUOTE)
1455 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001456 last_isspace = 0;
1457 }
1458 }
1459 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001460
William Lallemand1d705562012-03-12 12:46:41 +01001461
1462 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001463 /* response header */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001464 if (fe->nb_rsp_cap && txn->rsp.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001465 if (tmp->options & LOG_OPT_QUOTE)
1466 LOGCHAR('"');
1467 LOGCHAR('{');
1468 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1469 if (hdr)
1470 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001471 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001472 ret = encode_string(tmplog, dst + maxsize,
1473 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1474 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001475 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001476 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001477 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001478 }
1479 LOGCHAR('}');
1480 last_isspace = 0;
1481 if (tmp->options & LOG_OPT_QUOTE)
1482 LOGCHAR('"');
1483 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001484 break;
1485
William Lallemand1d705562012-03-12 12:46:41 +01001486 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001487 /* response header list */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001488 if (fe->nb_rsp_cap && txn->rsp.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001489 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1490 if (hdr > 0)
1491 LOGCHAR(' ');
1492 if (tmp->options & LOG_OPT_QUOTE)
1493 LOGCHAR('"');
1494 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001495 ret = encode_string(tmplog, dst + maxsize,
1496 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1497 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001498 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001499 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001500 } else if (!(tmp->options & LOG_OPT_QUOTE))
1501 LOGCHAR('-');
1502 if (tmp->options & LOG_OPT_QUOTE)
1503 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001504 last_isspace = 0;
1505 }
1506 }
1507 break;
1508
William Lallemand1d705562012-03-12 12:46:41 +01001509 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001510 /* Request */
1511 if (tmp->options & LOG_OPT_QUOTE)
1512 LOGCHAR('"');
1513 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001514 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001515 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001516 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001517 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001518 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001519 if (tmp->options & LOG_OPT_QUOTE)
1520 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001521 last_isspace = 0;
1522 break;
William Lallemand5f232402012-04-05 18:02:55 +02001523
1524 case LOG_FMT_COUNTER: // %rt
1525 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001526 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001527 if (iret < 0 || iret > dst + maxsize - tmplog)
1528 goto out;
1529 last_isspace = 0;
1530 tmplog += iret;
1531 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001532 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001533 if (ret == NULL)
1534 goto out;
1535 tmplog = ret;
1536 last_isspace = 0;
1537 }
1538 break;
1539
Willy Tarreau7346acb2014-08-28 15:03:15 +02001540 case LOG_FMT_LOGCNT: // %lc
1541 if (tmp->options & LOG_OPT_HEXA) {
1542 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->fe->log_count);
1543 if (iret < 0 || iret > dst + maxsize - tmplog)
1544 goto out;
1545 last_isspace = 0;
1546 tmplog += iret;
1547 } else {
1548 ret = ultoa_o(s->fe->log_count, tmplog, dst + maxsize - tmplog);
1549 if (ret == NULL)
1550 goto out;
1551 tmplog = ret;
1552 last_isspace = 0;
1553 }
1554 break;
1555
William Lallemand5f232402012-04-05 18:02:55 +02001556 case LOG_FMT_HOSTNAME: // %H
1557 src = hostname;
1558 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1559 if (ret == NULL)
1560 goto out;
1561 tmplog = ret;
1562 last_isspace = 0;
1563 break;
1564
1565 case LOG_FMT_PID: // %pid
1566 if (tmp->options & LOG_OPT_HEXA) {
1567 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1568 if (iret < 0 || iret > dst + maxsize - tmplog)
1569 goto out;
1570 last_isspace = 0;
1571 tmplog += iret;
1572 } else {
1573 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1574 if (ret == NULL)
1575 goto out;
1576 tmplog = ret;
1577 last_isspace = 0;
1578 }
1579 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001580
1581 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001582 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001583 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02001584 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001585 if (ret == NULL)
1586 goto out;
1587 tmplog = ret;
1588 last_isspace = 0;
1589 break;
1590
William Lallemandbddd4fd2012-02-27 11:23:10 +01001591 }
1592 }
1593
1594out:
William Lallemand1d705562012-03-12 12:46:41 +01001595 /* *tmplog is a unused character */
1596 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001597 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001598
Willy Tarreaubaaee002006-06-26 02:48:02 +02001599}
1600
William Lallemand1d705562012-03-12 12:46:41 +01001601/*
1602 * send a log for the session when we have enough info about it.
1603 * Will not log if the frontend has no log defined.
1604 */
1605void sess_log(struct session *s)
1606{
1607 char *tmplog;
1608 int size, err, level;
1609
1610 /* if we don't want to log normal traffic, return now */
Willy Tarreau570f2212013-06-10 16:42:09 +02001611 err = (s->flags & SN_REDISP) ||
1612 ((s->flags & SN_ERR_MASK) > SN_ERR_LOCAL) ||
1613 (((s->flags & SN_ERR_MASK) == SN_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01001614 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreau570f2212013-06-10 16:42:09 +02001615 ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001616
William Lallemand1d705562012-03-12 12:46:41 +01001617 if (!err && (s->fe->options2 & PR_O2_NOLOGNORM))
1618 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001619
William Lallemand1d705562012-03-12 12:46:41 +01001620 if (LIST_ISEMPTY(&s->fe->logsrvs))
1621 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001622
Willy Tarreauabcd5142013-06-11 17:18:02 +02001623 if (s->logs.level) { /* loglevel was overridden */
1624 if (s->logs.level == -1) {
1625 s->logs.logwait = 0; /* logs disabled */
1626 return;
1627 }
1628 level = s->logs.level - 1;
1629 }
1630 else {
1631 level = LOG_INFO;
1632 if (err && (s->fe->options2 & PR_O2_LOGERRORS))
1633 level = LOG_ERR;
1634 }
William Lallemand1d705562012-03-12 12:46:41 +01001635
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001636 /* if unique-id was not generated */
1637 if (!s->unique_id && !LIST_ISEMPTY(&s->fe->format_unique_id)) {
1638 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
1639 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
1640 }
1641
Willy Tarreau094af4e2015-01-07 15:03:42 +01001642 tmplog = update_log_hdr(s->fe->log_tag ? s->fe->log_tag : global.log_tag);
William Lallemand1d705562012-03-12 12:46:41 +01001643 size = tmplog - logline;
Willy Tarreau18324f52014-06-27 18:10:07 +02001644 size += build_logline(s, tmplog, global.max_syslog_len - size, &s->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01001645 if (size > 0) {
Willy Tarreau7346acb2014-08-28 15:03:15 +02001646 s->fe->log_count++;
Willy Tarreaudf974472012-12-28 02:44:01 +01001647 __send_log(s->fe, level, logline, size + 1);
William Lallemand1d705562012-03-12 12:46:41 +01001648 s->logs.logwait = 0;
1649 }
1650}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001651
Willy Tarreaubaaee002006-06-26 02:48:02 +02001652/*
1653 * Local variables:
1654 * c-indent-level: 8
1655 * c-basic-offset: 8
1656 * End:
1657 */