blob: 2a6acf4290dfc09909d0a9759651fa058c59ce25 [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 */
83 { "ST", LOG_FMT_STATUS, PR_MODE_HTTP, 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 */
William Lallemand5e19a282012-04-02 16:22:10 +0200106 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request */
William Lallemand5e19a282012-04-02 16:22:10 +0200107 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_HTTP, LW_REQHDR, NULL }, /* header request list */
Willy Tarreau2beef582012-12-20 17:22:52 +0100108 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response */
William Lallemand5e19a282012-04-02 16:22:10 +0200109 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* header response list */
Willy Tarreau2beef582012-12-20 17:22:52 +0100110 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200111 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100112 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
113 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100114 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100115 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
116 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
117 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
118 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
119 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200120 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
121 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100122 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
123 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
124 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
125
126 /* The following tags are deprecated and will be removed soon */
127 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
128 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
129 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
130 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
131 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
132 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
133 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
134 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
135 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
136 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
137 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200138 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100139};
140
Willy Tarreau2beef582012-12-20 17:22:52 +0100141char 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
142char 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";
143char 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 +0100144char *log_format = NULL;
145
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100146/* This is a global syslog line, common to all outgoing messages. It begins
147 * with the syslog tag and the date that are updated by update_log_hdr().
148 */
149static char logline[MAX_SYSLOG_LEN];
150
William Lallemand723b73a2012-02-08 16:37:49 +0100151struct logformat_var_args {
152 char *name;
153 int mask;
154};
155
156struct logformat_var_args var_args_list[] = {
157// global
158 { "M", LOG_OPT_MANDATORY },
159 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200160 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100161 { 0, 0 }
162};
163
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200164/* return the name of the directive used in the current proxy for which we're
165 * currently parsing a header, when it is known.
166 */
167static inline const char *fmt_directive(const struct proxy *curproxy)
168{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100169 switch (curproxy->conf.args.ctx) {
170 case ARGC_UIF:
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200171 return "unique-id-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100172 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100173 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100174 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100175 return "http-response";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100176 case ARGC_STK:
177 return "stick";
178 case ARGC_TRK:
179 return "track-sc"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100180 case ARGC_RDR:
181 return "redirect"; break;
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100182 case ARGC_ACL:
183 return "acl"; break;
184 default:
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200185 return "log-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100186 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200187}
188
William Lallemand723b73a2012-02-08 16:37:49 +0100189/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100190 * callback used to configure addr source retrieval
191 */
192int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
193{
194 curproxy->options2 |= PR_O2_SRC_ADDR;
195
196 return 0;
197}
198
199
200/*
William Lallemand723b73a2012-02-08 16:37:49 +0100201 * Parse args in a logformat_var
202 */
203int parse_logformat_var_args(char *args, struct logformat_node *node)
204{
205 int i = 0;
206 int end = 0;
207 int flags = 0; // 1 = + 2 = -
208 char *sp = NULL; // start pointer
209
210 if (args == NULL)
211 return 1;
212
213 while (1) {
214 if (*args == '\0')
215 end = 1;
216
217 if (*args == '+') {
218 // add flag
219 sp = args + 1;
220 flags = 1;
221 }
222 if (*args == '-') {
223 // delete flag
224 sp = args + 1;
225 flags = 2;
226 }
227
228 if (*args == '\0' || *args == ',') {
229 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100230 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100231 if (strcmp(sp, var_args_list[i].name) == 0) {
232 if (flags == 1) {
233 node->options |= var_args_list[i].mask;
234 break;
235 } else if (flags == 2) {
236 node->options &= ~var_args_list[i].mask;
237 break;
238 }
239 }
240 }
241 sp = NULL;
242 if (end)
243 break;
244 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100245 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100246 }
247 return 0;
248}
249
250/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100251 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
252 * must pass the args part in the <arg> pointer with its length in <arg_len>,
253 * and varname with its length in <var> and <var_len> respectively. <arg> is
254 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100255 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100256int 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 +0100257{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100258 int j;
259 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100260
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100261 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
262 if (strlen(logformat_keywords[j].name) == var_len &&
263 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
264 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
265 node = calloc(1, sizeof(struct logformat_node));
266 node->type = logformat_keywords[j].type;
267 node->options = *defoptions;
268 if (arg_len) {
269 node->arg = my_strndup(arg, arg_len);
270 parse_logformat_var_args(node->arg, node);
271 }
272 if (node->type == LOG_FMT_GLOBAL) {
273 *defoptions = node->options;
274 free(node->arg);
275 free(node);
276 } else {
277 if (logformat_keywords[j].config_callback &&
278 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100279 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100280 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100281 curproxy->to_log |= logformat_keywords[j].lw;
282 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100283 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100284 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100285 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200286 curproxy->conf.args.file, curproxy->conf.args.line,
287 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100288 return 0;
289 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100290 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200291 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
292 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100293 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100294 }
William Lallemand723b73a2012-02-08 16:37:49 +0100295 }
296 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100297
298 j = var[var_len];
299 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100300 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 +0200301 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100302 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100303 return -1;
304}
305
306/*
307 * push to the logformat linked list
308 *
309 * start: start pointer
310 * end: end text pointer
311 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100312 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100313 *
314 * LOG_TEXT: copy chars from start to end excluding end.
315 *
316*/
William Lallemand1d705562012-03-12 12:46:41 +0100317void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100318{
319 char *str;
320
Willy Tarreaua3571662012-12-20 21:59:12 +0100321 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100322 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100323 str = calloc(end - start + 1, 1);
324 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100325 str[end - start] = '\0';
326 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100327 node->type = LOG_FMT_TEXT; // type string
328 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100329 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100330 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100331 node->type = LOG_FMT_SEPARATOR;
332 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100333 }
334}
335
336/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100337 * Parse the sample fetch expression <text> and add a node to <list_format> upon
338 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200339 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100340 */
Willy Tarreau434c57c2013-01-08 01:10:24 +0100341void add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap)
Willy Tarreauc8368452012-12-21 00:09:23 +0100342{
343 char *cmd[2];
344 struct sample_expr *expr;
345 struct logformat_node *node;
346 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100347 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100348
349 cmd[0] = text;
350 cmd[1] = "";
351 cmd_arg = 0;
352
Willy Tarreau975c1782013-12-12 23:16:54 +0100353 expr = sample_parse_expr(cmd, &cmd_arg, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100354 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200355 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
356 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100357 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100358 return;
359 }
360
361 node = calloc(1, sizeof(struct logformat_node));
362 node->type = LOG_FMT_EXPR;
363 node->expr = expr;
364 node->options = options;
365
366 if (arg_len) {
367 node->arg = my_strndup(arg, arg_len);
368 parse_logformat_var_args(node->arg, node);
369 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100370 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100371 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
372
Willy Tarreau434c57c2013-01-08 01:10:24 +0100373 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100374 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
375
Willy Tarreau434c57c2013-01-08 01:10:24 +0100376 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200377 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
378 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
379 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100380
Willy Tarreauc8368452012-12-21 00:09:23 +0100381 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
382 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100383 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100384
Willy Tarreau1f31c732013-01-10 16:22:27 +0100385 /* FIXME: temporary workaround for missing LW_XPRT flag needed with some
386 * sample fetches (eg: ssl*). We always set it for now on, but this will
387 * leave with sample capabilities soon.
388 */
389 curpx->to_log |= LW_XPRT;
Willy Tarreauc8368452012-12-21 00:09:23 +0100390 LIST_ADDQ(list_format, &node->list);
391}
392
393/*
William Lallemand723b73a2012-02-08 16:37:49 +0100394 * Parse the log_format string and fill a linked list.
395 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200396 * You can set arguments using { } : %{many arguments}varname.
397 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100398 *
399 * str: the string to parse
400 * curproxy: the proxy affected
401 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100402 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100403 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100404 */
Willy Tarreau434c57c2013-01-08 01:10:24 +0100405void parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap)
William Lallemand723b73a2012-02-08 16:37:49 +0100406{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100407 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100408 char *arg = NULL; /* start pointer for args */
409 char *var = NULL; /* start pointer for vars */
410 int arg_len = 0;
411 int var_len = 0;
412 int cformat; /* current token format */
413 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100414 struct logformat_node *tmplf, *back;
415
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100416 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200417 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200418
William Lallemand723b73a2012-02-08 16:37:49 +0100419 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100420 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100421 LIST_DEL(&tmplf->list);
422 free(tmplf);
423 }
424
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100425 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100426 pformat = cformat;
427
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100428 if (!*str)
429 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100430
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100431 /* The prinicple of the two-step state machine below is to first detect a change, and
432 * second have all common paths processed at one place. The common paths are the ones
433 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
434 * We use the common LF_INIT state to dispatch to the different final states.
435 */
436 switch (pformat) {
437 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100438 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100439 arg_len = var_len = 0;
440 if (*str == '{') { // optional argument
441 cformat = LF_STARG;
442 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100443 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100444 else if (*str == '[') {
445 cformat = LF_STEXPR;
446 var = str + 1; // store expr in variable name
447 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100448 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100449 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100450 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100451 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100452 else if (*str == '%')
453 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100454 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100455 /* single '%' followed by blank or digit, send them both */
456 cformat = LF_TEXT;
457 pformat = LF_TEXT; /* finally we include the previous char as well */
458 sp = str - 1; /* send both the '%' and the current char */
459 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",
460 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
461
462 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100463 else
464 cformat = LF_INIT; // handle other cases of litterals
465 break;
466
467 case LF_STARG: // text immediately following '%{'
468 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100469 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100470 arg_len = str - arg;
471 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100472 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100473 break;
474
475 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100476 if (*str == '[') {
477 cformat = LF_STEXPR;
478 var = str + 1; // store expr in variable name
479 break;
480 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100481 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100482 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100483 var = str;
484 break;
485 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200486 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
487 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100488 cformat = LF_INIT;
489 break;
490
Willy Tarreauc8368452012-12-21 00:09:23 +0100491 case LF_STEXPR: // text immediately following '%['
492 if (*str == ']') { // end of arg
493 cformat = LF_EDEXPR;
494 var_len = str - var;
495 *str = 0; // needed for parsing the expression
496 }
497 break;
498
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100499 case LF_VAR: // text part of a variable name
500 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100501 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100502 cformat = LF_INIT; // not variable name anymore
503 break;
504
Willy Tarreauc8368452012-12-21 00:09:23 +0100505 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100506 cformat = LF_INIT;
507 }
508
509 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
510 switch (*str) {
511 case '%': cformat = LF_STARTVAR; break;
512 case ' ': cformat = LF_SEPARATOR; break;
513 case 0 : cformat = LF_END; break;
514 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100515 }
516 }
517
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100518 if (cformat != pformat || pformat == LF_SEPARATOR) {
519 switch (pformat) {
520 case LF_VAR:
521 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
522 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100523 case LF_STEXPR:
Willy Tarreau434c57c2013-01-08 01:10:24 +0100524 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap);
Willy Tarreauc8368452012-12-21 00:09:23 +0100525 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100526 case LF_TEXT:
527 case LF_SEPARATOR:
528 add_to_logformat_list(sp, str, pformat, list_format);
529 break;
530 }
531 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100532 }
533 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100534
Willy Tarreauc8368452012-12-21 00:09:23 +0100535 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200536 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
537 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
538 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100539
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100540 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100541}
542
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543/*
544 * Displays the message on stderr with the date and pid. Overrides the quiet
545 * mode during startup.
546 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200547void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200548{
549 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200550 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200551
552 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
553 va_start(argp, fmt);
554
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200555 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200556 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200557 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558 vfprintf(stderr, fmt, argp);
559 fflush(stderr);
560 va_end(argp);
561 }
562}
563
564
565/*
566 * Displays the message on stderr with the date and pid.
567 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200568void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569{
570 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200571 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572
573 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
574 va_start(argp, fmt);
575
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200576 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200578 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579 vfprintf(stderr, fmt, argp);
580 fflush(stderr);
581 va_end(argp);
582 }
583}
584
585/*
586 * Displays the message on <out> only if quiet mode is not set.
587 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200588void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200589{
590 va_list argp;
591
592 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
593 va_start(argp, fmt);
594 vfprintf(out, fmt, argp);
595 fflush(out);
596 va_end(argp);
597 }
598}
599
600/*
601 * returns log level for <lev> or -1 if not found.
602 */
603int get_log_level(const char *lev)
604{
605 int level;
606
607 level = NB_LOG_LEVELS - 1;
608 while (level >= 0 && strcmp(log_levels[level], lev))
609 level--;
610
611 return level;
612}
613
614
615/*
616 * returns log facility for <fac> or -1 if not found.
617 */
618int get_log_facility(const char *fac)
619{
620 int facility;
621
622 facility = NB_LOG_FACILITIES - 1;
623 while (facility >= 0 && strcmp(log_facilities[facility], fac))
624 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100625
Willy Tarreaubaaee002006-06-26 02:48:02 +0200626 return facility;
627}
628
William Lallemanda1cc3812012-02-08 16:38:44 +0100629/*
630 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200631 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100632 *
633 * Return the adress of the \0 character, or NULL on error
634 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100635char *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 +0100636{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100637 if (size < 2)
638 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100639
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100640 if (node->options & LOG_OPT_QUOTE) {
641 *(dst++) = '"';
642 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100643 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100644
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100645 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100646 if (++len > size)
647 len = size;
648 len = strlcpy2(dst, src, len);
649
650 size -= len;
651 dst += len;
652 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100653 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
654 if (size < 2)
655 return NULL;
656 *(dst++) = '-';
657 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100658
659 if (node->options & LOG_OPT_QUOTE) {
660 if (size < 2)
661 return NULL;
662 *(dst++) = '"';
663 }
664
665 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100666 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100667}
668
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100669static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
670{
671 return lf_text_len(dst, src, size, size, node);
672}
673
William Lallemand5f232402012-04-05 18:02:55 +0200674/*
675 * Write a IP adress to the log string
676 * +X option write in hexadecimal notation, most signifant byte on the left
677 */
678char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
679{
680 char *ret = dst;
681 int iret;
682 char pn[INET6_ADDRSTRLEN];
683
684 if (node->options & LOG_OPT_HEXA) {
685 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
686 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
687 if (iret < 0 || iret > size)
688 return NULL;
689 ret += iret;
690 } else {
691 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
692 ret = lf_text(dst, pn, size, node);
693 if (ret == NULL)
694 return NULL;
695 }
696 return ret;
697}
698
699/*
700 * Write a port to the log
701 * +X option write in hexadecimal notation, most signifant byte on the left
702 */
703char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
704{
705 char *ret = dst;
706 int iret;
707
708 if (node->options & LOG_OPT_HEXA) {
709 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
710 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
711 if (iret < 0 || iret > size)
712 return NULL;
713 ret += iret;
714 } else {
715 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
716 if (ret == NULL)
717 return NULL;
718 }
719 return ret;
720}
721
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100722/* Re-generate the syslog header at the beginning of logline once a second and
723 * return the pointer to the first character after the header.
724 */
725static char *update_log_hdr()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100727 static long tvsec;
728 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200730 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200732 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100733 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200734
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200735 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200736 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200737
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100738 hdr_len = snprintf(logline, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100739 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200740 monthname[tm.tm_mon],
741 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100742 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000743 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744 /* WARNING: depending upon implementations, snprintf may return
745 * either -1 or the number of bytes that would be needed to store
746 * the total message. In both cases, we must adjust it.
747 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100748 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
749 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200750
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100751 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200752 }
753
William Lallemand2a4a44f2012-02-06 16:00:33 +0100754 return dataptr;
755}
756
757/*
758 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100759 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100760 */
761void send_log(struct proxy *p, int level, const char *format, ...)
762{
763 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100764 char *dataptr;
765 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100766
767 if (level < 0 || format == NULL)
768 return;
769
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100770 dataptr = update_log_hdr(); /* update log header and skip it */
771 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100772
773 va_start(argp, format);
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100774 data_len += vsnprintf(dataptr, logline + sizeof(logline) - dataptr, format, argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100775 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
776 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100778
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100779 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100780}
781
782/*
783 * This function sends a syslog message.
784 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100785 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100786 */
787void __send_log(struct proxy *p, int level, char *message, size_t size)
788{
789 static int logfdunix = -1; /* syslog to AF_UNIX socket */
790 static int logfdinet = -1; /* syslog to AF_INET socket */
791 static char *dataptr = NULL;
792 int fac_level;
793 struct list *logsrvs = NULL;
794 struct logsrv *tmp = NULL;
795 int nblogger;
796 char *log_ptr;
797
798 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799
800 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200801 if (!LIST_ISEMPTY(&global.logsrvs)) {
802 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 }
804 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200805 if (!LIST_ISEMPTY(&p->logsrvs)) {
806 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100808 }
809
William Lallemand0f99e342011-10-12 17:50:54 +0200810 if (!logsrvs)
811 return;
812
William Lallemand2a4a44f2012-02-06 16:00:33 +0100813 message[size - 1] = '\n';
814
Robert Tsai81ae1952007-12-05 10:47:29 +0100815 /* Lazily set up syslog sockets for protocol families of configured
816 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200817 nblogger = 0;
818 list_for_each_entry(tmp, logsrvs, list) {
819 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100820 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200821
David du Colombier11bcb6c2011-03-24 12:23:00 +0100822 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100823 proto = 0;
824 plogfd = &logfdunix;
825 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100826 proto = IPPROTO_UDP;
827 plogfd = &logfdinet;
828 }
829 if (*plogfd >= 0) {
830 /* socket already created. */
831 continue;
832 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100833 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100834 proto)) < 0) {
835 Alert("socket for logger #%d failed: %s (errno=%d)\n",
836 nblogger + 1, strerror(errno), errno);
837 return;
838 }
839 /* we don't want to receive anything on this socket */
840 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
841 /* does nothing under Linux, maybe needed for others */
842 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200843 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 }
845
Robert Tsai81ae1952007-12-05 10:47:29 +0100846 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200847 nblogger = 0;
848 list_for_each_entry(tmp, logsrvs, list) {
849 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100850 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100851 &logfdunix : &logfdinet;
852 int sent;
853
Willy Tarreaubaaee002006-06-26 02:48:02 +0200854 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200855 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100857
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
William Lallemandafeb9872013-08-30 14:17:46 +0200874 sent = sendto(*plogfd, log_ptr, size - (log_ptr - dataptr),
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200875 MSG_DONTWAIT | MSG_NOSIGNAL,
876 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100877 if (sent < 0) {
878 Alert("sendto logger #%d failed: %s (errno=%d)\n",
879 nblogger, strerror(errno), errno);
880 }
William Lallemand0f99e342011-10-12 17:50:54 +0200881 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200882 }
883}
884
William Lallemandbddd4fd2012-02-27 11:23:10 +0100885extern fd_set hdr_encode_map[];
886extern fd_set url_encode_map[];
887
Willy Tarreaubaaee002006-06-26 02:48:02 +0200888
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200889const 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 +0100890const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
891 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
892 Set-cookie Updated, unknown, unknown */
893
William Lallemand1d705562012-03-12 12:46:41 +0100894/*
895 * try to write a character if there is enough space, or goto out
896 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100897#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100898 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100899 *(tmplog++) = (x); \
900 } else { \
901 goto out; \
902 } \
903 } while(0)
904
William Lallemand1d705562012-03-12 12:46:41 +0100905
Willy Tarreaudf974472012-12-28 02:44:01 +0100906/* Builds a log line in <dst> based on <list_format>, and stops before reaching
907 * <maxsize> characters. Returns the size of the output string in characters,
908 * not counting the trailing zero which is always added if the resulting size
909 * is not zero.
910 */
William Lallemand1d705562012-03-12 12:46:41 +0100911int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200912{
Willy Tarreau73de9892006-11-30 11:40:23 +0100913 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100914 struct proxy *be = s->be;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100915 struct http_txn *txn = &s->txn;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100916 char *uri;
Willy Tarreaufe944602007-10-25 10:34:16 +0200917 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100918 int t_request;
919 int hdr;
920 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100921 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100922 char *ret;
923 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100924 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925
William Lallemandbddd4fd2012-02-27 11:23:10 +0100926 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927
William Lallemandbddd4fd2012-02-27 11:23:10 +0100928 t_request = -1;
929 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
930 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
931
William Lallemand1d705562012-03-12 12:46:41 +0100932 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200933
William Lallemandbddd4fd2012-02-27 11:23:10 +0100934 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100935 if (LIST_ISEMPTY(list_format))
936 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100937
William Lallemand1d705562012-03-12 12:46:41 +0100938 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200939 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +0200940 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100941 struct sample *key;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100942
Willy Tarreauc8368452012-12-21 00:09:23 +0100943 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +0100944 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100945 if (!last_isspace) {
946 LOGCHAR(' ');
947 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100948 }
949 break;
950
William Lallemand1d705562012-03-12 12:46:41 +0100951 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100952 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200953 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100954 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100955 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100956 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100957 last_isspace = 0;
958 break;
959
Willy Tarreauc8368452012-12-21 00:09:23 +0100960 case LOG_FMT_EXPR: // sample expression, may be request or response
961 key = NULL;
962 if (tmp->options & LOG_OPT_REQ_CAP)
963 key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr);
964 if (!key && (tmp->options & LOG_OPT_RES_CAP))
965 key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr);
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100966 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 +0100967 if (ret == 0)
968 goto out;
969 tmplog = ret;
970 last_isspace = 0;
971 break;
972
Willy Tarreau2beef582012-12-20 17:22:52 +0100973 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200974 conn = objt_conn(s->req->prod->end);
975 if (conn)
976 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
977 else
978 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +0100979 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100980 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100981 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100982 last_isspace = 0;
983 break;
984
Willy Tarreau2beef582012-12-20 17:22:52 +0100985 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200986 conn = objt_conn(s->req->prod->end);
987 if (conn) {
988 if (conn->addr.from.ss_family == AF_UNIX) {
989 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
990 } else {
991 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
992 dst + maxsize - tmplog, tmp);
993 }
William Lallemand5f232402012-04-05 18:02:55 +0200994 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200995 else
996 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
997
William Lallemand5f232402012-04-05 18:02:55 +0200998 if (ret == NULL)
999 goto out;
1000 tmplog = ret;
1001 last_isspace = 0;
1002 break;
1003
Willy Tarreau2beef582012-12-20 17:22:52 +01001004 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001005 conn = objt_conn(s->req->prod->end);
1006 if (conn) {
1007 conn_get_to_addr(conn);
1008 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1009 }
1010 else
1011 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1012
William Lallemand1d705562012-03-12 12:46:41 +01001013 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001014 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001015 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001016 last_isspace = 0;
1017 break;
1018
Willy Tarreau2beef582012-12-20 17:22:52 +01001019 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001020 conn = objt_conn(s->req->prod->end);
1021 if (conn) {
1022 conn_get_to_addr(conn);
1023 if (conn->addr.to.ss_family == AF_UNIX)
1024 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
1025 else
1026 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001027 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001028 else
1029 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1030
William Lallemand5f232402012-04-05 18:02:55 +02001031 if (ret == NULL)
1032 goto out;
1033 tmplog = ret;
1034 last_isspace = 0;
1035 break;
1036
Willy Tarreau2beef582012-12-20 17:22:52 +01001037 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001038 conn = objt_conn(s->req->cons->end);
1039 if (conn)
1040 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1041 else
1042 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1043
William Lallemand1d705562012-03-12 12:46:41 +01001044 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001045 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001046 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001047 last_isspace = 0;
1048 break;
1049
Willy Tarreau2beef582012-12-20 17:22:52 +01001050 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001051 conn = objt_conn(s->req->cons->end);
1052 if (conn)
1053 ret = lf_port(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 Lallemand5f232402012-04-05 18:02:55 +02001057 if (ret == NULL)
1058 goto out;
1059 tmplog = ret;
1060 last_isspace = 0;
1061 break;
1062
Willy Tarreau2beef582012-12-20 17:22:52 +01001063 case LOG_FMT_SERVERIP: // %si
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001064 conn = objt_conn(s->req->cons->end);
1065 if (conn)
1066 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, 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_SERVERPORT: // %sp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001077 conn = objt_conn(s->req->cons->end);
1078 if (conn)
1079 ret = lf_port(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 Lallemand1d705562012-03-12 12:46:41 +01001083 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001084 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001085 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001086 last_isspace = 0;
1087 break;
1088
William Lallemand1d705562012-03-12 12:46:41 +01001089 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001090 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001091 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1092 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001093 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001094 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001095 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001096 last_isspace = 0;
1097 break;
1098
William Lallemand1d705562012-03-12 12:46:41 +01001099 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001100 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001101 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001102 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001103 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001104 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001105 last_isspace = 0;
1106 break;
1107
Yuxans Yao4e25b012012-10-19 10:36:09 +08001108 case LOG_FMT_DATELOCAL: // %Tl
1109 get_localtime(s->logs.accept_date.tv_sec, &tm);
1110 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1111 if (ret == NULL)
1112 goto out;
1113 tmplog = ret;
1114 last_isspace = 0;
1115 break;
1116
William Lallemand5f232402012-04-05 18:02:55 +02001117 case LOG_FMT_TS: // %Ts
1118 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1119 if (tmp->options & LOG_OPT_HEXA) {
1120 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1121 if (iret < 0 || iret > dst + maxsize - tmplog)
1122 goto out;
1123 last_isspace = 0;
1124 tmplog += iret;
1125 } else {
1126 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1127 if (ret == NULL)
1128 goto out;
1129 tmplog = ret;
1130 last_isspace = 0;
1131 }
1132 break;
1133
William Lallemand1d705562012-03-12 12:46:41 +01001134 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001135 if (tmp->options & LOG_OPT_HEXA) {
1136 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1137 if (iret < 0 || iret > dst + maxsize - tmplog)
1138 goto out;
1139 last_isspace = 0;
1140 tmplog += iret;
1141 } else {
1142 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001143 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001144 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1145 tmplog, 4);
1146 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001147 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001148 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001149 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001150 }
1151 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001152
William Lallemand1d705562012-03-12 12:46:41 +01001153 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001154 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001155 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001156 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001157 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001158 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001159 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001160 break;
1161
Willy Tarreau773d65f2012-10-12 14:56:11 +02001162 case LOG_FMT_FRONTEND_XPRT: // %ft
1163 src = fe->id;
1164 if (tmp->options & LOG_OPT_QUOTE)
1165 LOGCHAR('"');
1166 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1167 if (iret == 0)
1168 goto out;
1169 tmplog += iret;
1170#ifdef USE_OPENSSL
1171 if (s->listener->xprt == &ssl_sock)
1172 LOGCHAR('~');
1173#endif
1174 if (tmp->options & LOG_OPT_QUOTE)
1175 LOGCHAR('"');
1176 last_isspace = 0;
1177 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001178#ifdef USE_OPENSSL
1179 case LOG_FMT_SSL_CIPHER: // %sslc
1180 src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001181 conn = objt_conn(s->si[0].end);
1182 if (conn) {
1183 if (s->listener->xprt == &ssl_sock)
1184 src = ssl_sock_get_cipher_name(conn);
1185 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001186 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1187 if (ret == NULL)
1188 goto out;
1189 tmplog = ret;
1190 last_isspace = 0;
1191 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001192
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001193 case LOG_FMT_SSL_VERSION: // %sslv
1194 src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001195 conn = objt_conn(s->si[0].end);
1196 if (conn) {
1197 if (s->listener->xprt == &ssl_sock)
1198 src = ssl_sock_get_proto_version(conn);
1199 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001200 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1201 if (ret == NULL)
1202 goto out;
1203 tmplog = ret;
1204 last_isspace = 0;
1205 break;
1206#endif
William Lallemand1d705562012-03-12 12:46:41 +01001207 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001208 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001209 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001210 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001211 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001212 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001213 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001214 break;
1215
William Lallemand1d705562012-03-12 12:46:41 +01001216 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001217 switch (obj_type(s->target)) {
1218 case OBJ_TYPE_SERVER:
1219 src = objt_server(s->target)->id;
1220 break;
1221 case OBJ_TYPE_APPLET:
1222 src = objt_applet(s->target)->name;
1223 break;
1224 default:
1225 src = "<NOSRV>";
1226 break;
1227 }
William Lallemand5f232402012-04-05 18:02:55 +02001228 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001229 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001230 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001231 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001232 last_isspace = 0;
1233 break;
1234
William Lallemand1d705562012-03-12 12:46:41 +01001235 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001236 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001237 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001238 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001239 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001240 last_isspace = 0;
1241 break;
1242
William Lallemand1d705562012-03-12 12:46:41 +01001243 case LOG_FMT_TW: // %Tw
1244 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001245 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001246 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001247 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001248 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001249 last_isspace = 0;
1250 break;
1251
William Lallemand1d705562012-03-12 12:46:41 +01001252 case LOG_FMT_TC: // %Tc
1253 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001254 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001255 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001256 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001257 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001258 last_isspace = 0;
1259 break;
1260
William Lallemand1d705562012-03-12 12:46:41 +01001261 case LOG_FMT_TR: // %Tr
1262 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001263 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001264 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001265 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001266 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001267 last_isspace = 0;
1268 break;
1269
William Lallemand1d705562012-03-12 12:46:41 +01001270 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001271 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001272 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001273 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001274 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001275 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001276 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001277 last_isspace = 0;
1278 break;
1279
Willy Tarreau2beef582012-12-20 17:22:52 +01001280 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001281 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001282 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001283 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001284 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001285 last_isspace = 0;
1286 break;
1287
William Lallemand1d705562012-03-12 12:46:41 +01001288 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001289 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001290 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001291 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001292 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001293 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001294 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001295 last_isspace = 0;
1296 break;
1297
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001298 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001299 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1300 if (ret == NULL)
1301 goto out;
1302 tmplog = ret;
1303 last_isspace = 0;
1304 break;
1305
Willy Tarreau2beef582012-12-20 17:22:52 +01001306 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001307 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001308 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001309 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001310 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001311 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001312 last_isspace = 0;
1313 break;
1314
Willy Tarreau2beef582012-12-20 17:22:52 +01001315 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001316 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001317 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001318 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001319 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001320 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001321 last_isspace = 0;
1322 break;
1323
William Lallemand1d705562012-03-12 12:46:41 +01001324 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +01001325 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1326 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
1327 *tmplog = '\0';
1328 last_isspace = 0;
1329 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001330
William Lallemand1d705562012-03-12 12:46:41 +01001331 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001332 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1333 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001334 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1335 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 +01001336 last_isspace = 0;
1337 break;
1338
William Lallemand1d705562012-03-12 12:46:41 +01001339 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001340 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001341 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001342 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001343 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001344 last_isspace = 0;
1345 break;
1346
William Lallemand1d705562012-03-12 12:46:41 +01001347 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001348 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001349 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001350 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001351 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001352 last_isspace = 0;
1353 break;
1354
William Lallemand1d705562012-03-12 12:46:41 +01001355 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001356 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001357 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001358 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001359 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001360 last_isspace = 0;
1361 break;
1362
William Lallemand1d705562012-03-12 12:46:41 +01001363 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001364 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001365 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001366 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001367 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001368 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001369 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001370 last_isspace = 0;
1371 break;
1372
William Lallemand1d705562012-03-12 12:46:41 +01001373 case LOG_FMT_RETRIES: // %rq
William Lallemandbddd4fd2012-02-27 11:23:10 +01001374 if (s->flags & SN_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001375 LOGCHAR('+');
1376 ret = ltoa_o((s->req->cons->conn_retries>0) ?
1377 (be->conn_retries - s->req->cons->conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001378 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001379 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001380 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001381 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001382 last_isspace = 0;
1383 break;
1384
William Lallemand1d705562012-03-12 12:46:41 +01001385 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001386 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001387 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001388 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001389 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001390 last_isspace = 0;
1391 break;
1392
William Lallemand1d705562012-03-12 12:46:41 +01001393 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001394 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001395 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001396 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001397 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001398 last_isspace = 0;
1399 break;
1400
William Lallemand1d705562012-03-12 12:46:41 +01001401 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001402 /* request header */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001403 if (fe->nb_req_cap && txn->req.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001404 if (tmp->options & LOG_OPT_QUOTE)
1405 LOGCHAR('"');
1406 LOGCHAR('{');
1407 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1408 if (hdr)
1409 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001410 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001411 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001412 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001413 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001414 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001415 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001416 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001417 }
1418 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001419 if (tmp->options & LOG_OPT_QUOTE)
1420 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001421 last_isspace = 0;
William Lallemand1d705562012-03-12 12:46:41 +01001422 if (tmp->options & LOG_OPT_QUOTE)
1423 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001424 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001425 break;
1426
William Lallemand1d705562012-03-12 12:46:41 +01001427 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001428 /* request header list */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001429 if (fe->nb_req_cap && txn->req.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001430 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1431 if (hdr > 0)
1432 LOGCHAR(' ');
1433 if (tmp->options & LOG_OPT_QUOTE)
1434 LOGCHAR('"');
1435 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001436 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001437 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001438 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001439 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001440 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001441 } else if (!(tmp->options & LOG_OPT_QUOTE))
1442 LOGCHAR('-');
1443 if (tmp->options & LOG_OPT_QUOTE)
1444 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001445 last_isspace = 0;
1446 }
1447 }
1448 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001449
William Lallemand1d705562012-03-12 12:46:41 +01001450
1451 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001452 /* response header */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001453 if (fe->nb_rsp_cap && txn->rsp.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001454 if (tmp->options & LOG_OPT_QUOTE)
1455 LOGCHAR('"');
1456 LOGCHAR('{');
1457 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1458 if (hdr)
1459 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001460 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001461 ret = encode_string(tmplog, dst + maxsize,
1462 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1463 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001464 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001465 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001466 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001467 }
1468 LOGCHAR('}');
1469 last_isspace = 0;
1470 if (tmp->options & LOG_OPT_QUOTE)
1471 LOGCHAR('"');
1472 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001473 break;
1474
William Lallemand1d705562012-03-12 12:46:41 +01001475 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001476 /* response header list */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001477 if (fe->nb_rsp_cap && txn->rsp.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001478 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1479 if (hdr > 0)
1480 LOGCHAR(' ');
1481 if (tmp->options & LOG_OPT_QUOTE)
1482 LOGCHAR('"');
1483 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001484 ret = encode_string(tmplog, dst + maxsize,
1485 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1486 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001487 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001488 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001489 } else if (!(tmp->options & LOG_OPT_QUOTE))
1490 LOGCHAR('-');
1491 if (tmp->options & LOG_OPT_QUOTE)
1492 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001493 last_isspace = 0;
1494 }
1495 }
1496 break;
1497
William Lallemand1d705562012-03-12 12:46:41 +01001498 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001499 /* Request */
1500 if (tmp->options & LOG_OPT_QUOTE)
1501 LOGCHAR('"');
1502 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001503 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001504 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001505 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001506 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001507 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001508 if (tmp->options & LOG_OPT_QUOTE)
1509 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001510 last_isspace = 0;
1511 break;
William Lallemand5f232402012-04-05 18:02:55 +02001512
1513 case LOG_FMT_COUNTER: // %rt
1514 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001515 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001516 if (iret < 0 || iret > dst + maxsize - tmplog)
1517 goto out;
1518 last_isspace = 0;
1519 tmplog += iret;
1520 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001521 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001522 if (ret == NULL)
1523 goto out;
1524 tmplog = ret;
1525 last_isspace = 0;
1526 }
1527 break;
1528
1529 case LOG_FMT_HOSTNAME: // %H
1530 src = hostname;
1531 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1532 if (ret == NULL)
1533 goto out;
1534 tmplog = ret;
1535 last_isspace = 0;
1536 break;
1537
1538 case LOG_FMT_PID: // %pid
1539 if (tmp->options & LOG_OPT_HEXA) {
1540 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1541 if (iret < 0 || iret > dst + maxsize - tmplog)
1542 goto out;
1543 last_isspace = 0;
1544 tmplog += iret;
1545 } else {
1546 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1547 if (ret == NULL)
1548 goto out;
1549 tmplog = ret;
1550 last_isspace = 0;
1551 }
1552 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001553
1554 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001555 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001556 src = s->unique_id;
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001557 if (src)
1558 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001559 if (ret == NULL)
1560 goto out;
1561 tmplog = ret;
1562 last_isspace = 0;
1563 break;
1564
William Lallemandbddd4fd2012-02-27 11:23:10 +01001565 }
1566 }
1567
1568out:
William Lallemand1d705562012-03-12 12:46:41 +01001569 /* *tmplog is a unused character */
1570 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001571 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001572
Willy Tarreaubaaee002006-06-26 02:48:02 +02001573}
1574
William Lallemand1d705562012-03-12 12:46:41 +01001575/*
1576 * send a log for the session when we have enough info about it.
1577 * Will not log if the frontend has no log defined.
1578 */
1579void sess_log(struct session *s)
1580{
1581 char *tmplog;
1582 int size, err, level;
1583
1584 /* if we don't want to log normal traffic, return now */
Willy Tarreau570f2212013-06-10 16:42:09 +02001585 err = (s->flags & SN_REDISP) ||
1586 ((s->flags & SN_ERR_MASK) > SN_ERR_LOCAL) ||
1587 (((s->flags & SN_ERR_MASK) == SN_ERR_NONE) &&
1588 (s->req->cons->conn_retries != s->be->conn_retries)) ||
1589 ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001590
William Lallemand1d705562012-03-12 12:46:41 +01001591 if (!err && (s->fe->options2 & PR_O2_NOLOGNORM))
1592 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001593
William Lallemand1d705562012-03-12 12:46:41 +01001594 if (LIST_ISEMPTY(&s->fe->logsrvs))
1595 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001596
Willy Tarreauabcd5142013-06-11 17:18:02 +02001597 if (s->logs.level) { /* loglevel was overridden */
1598 if (s->logs.level == -1) {
1599 s->logs.logwait = 0; /* logs disabled */
1600 return;
1601 }
1602 level = s->logs.level - 1;
1603 }
1604 else {
1605 level = LOG_INFO;
1606 if (err && (s->fe->options2 & PR_O2_LOGERRORS))
1607 level = LOG_ERR;
1608 }
William Lallemand1d705562012-03-12 12:46:41 +01001609
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001610 /* if unique-id was not generated */
1611 if (!s->unique_id && !LIST_ISEMPTY(&s->fe->format_unique_id)) {
1612 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
1613 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
1614 }
1615
William Lallemand1d705562012-03-12 12:46:41 +01001616 tmplog = update_log_hdr();
1617 size = tmplog - logline;
1618 size += build_logline(s, tmplog, sizeof(logline) - size, &s->fe->logformat);
1619 if (size > 0) {
Willy Tarreaudf974472012-12-28 02:44:01 +01001620 __send_log(s->fe, level, logline, size + 1);
William Lallemand1d705562012-03-12 12:46:41 +01001621 s->logs.logwait = 0;
1622 }
1623}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001624
Willy Tarreaubaaee002006-06-26 02:48:02 +02001625/*
1626 * Local variables:
1627 * c-indent-level: 8
1628 * c-basic-offset: 8
1629 * End:
1630 */