blob: 10eb68d0e420324016b33b31728b75f3cd7a1863 [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 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 */
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200149char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100150
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 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100341void 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 +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
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100353 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &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
William Lallemand65ad6e12014-01-31 15:08:02 +0100385 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
386 * needed with some sample fetches (eg: ssl*). We always set it for
387 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100388 */
389 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100390 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100391 LIST_ADDQ(list_format, &node->list);
392}
393
394/*
William Lallemand723b73a2012-02-08 16:37:49 +0100395 * Parse the log_format string and fill a linked list.
396 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200397 * You can set arguments using { } : %{many arguments}varname.
398 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100399 *
400 * str: the string to parse
401 * curproxy: the proxy affected
402 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100403 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100404 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100405 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100406void 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 +0100407{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100408 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100409 char *arg = NULL; /* start pointer for args */
410 char *var = NULL; /* start pointer for vars */
411 int arg_len = 0;
412 int var_len = 0;
413 int cformat; /* current token format */
414 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100415 struct logformat_node *tmplf, *back;
416
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100417 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200418 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200419
William Lallemand723b73a2012-02-08 16:37:49 +0100420 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100421 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100422 LIST_DEL(&tmplf->list);
423 free(tmplf);
424 }
425
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100426 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100427 pformat = cformat;
428
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100429 if (!*str)
430 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100431
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100432 /* The prinicple of the two-step state machine below is to first detect a change, and
433 * second have all common paths processed at one place. The common paths are the ones
434 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
435 * We use the common LF_INIT state to dispatch to the different final states.
436 */
437 switch (pformat) {
438 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100439 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100440 arg_len = var_len = 0;
441 if (*str == '{') { // optional argument
442 cformat = LF_STARG;
443 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100444 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100445 else if (*str == '[') {
446 cformat = LF_STEXPR;
447 var = str + 1; // store expr in variable name
448 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100449 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100450 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100451 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100452 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100453 else if (*str == '%')
454 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100455 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100456 /* single '%' followed by blank or digit, send them both */
457 cformat = LF_TEXT;
458 pformat = LF_TEXT; /* finally we include the previous char as well */
459 sp = str - 1; /* send both the '%' and the current char */
460 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",
461 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
462
463 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100464 else
465 cformat = LF_INIT; // handle other cases of litterals
466 break;
467
468 case LF_STARG: // text immediately following '%{'
469 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100470 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100471 arg_len = str - arg;
472 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100473 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100474 break;
475
476 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100477 if (*str == '[') {
478 cformat = LF_STEXPR;
479 var = str + 1; // store expr in variable name
480 break;
481 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100482 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100483 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100484 var = str;
485 break;
486 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200487 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
488 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100489 cformat = LF_INIT;
490 break;
491
Willy Tarreauc8368452012-12-21 00:09:23 +0100492 case LF_STEXPR: // text immediately following '%['
493 if (*str == ']') { // end of arg
494 cformat = LF_EDEXPR;
495 var_len = str - var;
496 *str = 0; // needed for parsing the expression
497 }
498 break;
499
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100500 case LF_VAR: // text part of a variable name
501 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100502 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100503 cformat = LF_INIT; // not variable name anymore
504 break;
505
Willy Tarreauc8368452012-12-21 00:09:23 +0100506 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100507 cformat = LF_INIT;
508 }
509
510 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
511 switch (*str) {
512 case '%': cformat = LF_STARTVAR; break;
513 case ' ': cformat = LF_SEPARATOR; break;
514 case 0 : cformat = LF_END; break;
515 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100516 }
517 }
518
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100519 if (cformat != pformat || pformat == LF_SEPARATOR) {
520 switch (pformat) {
521 case LF_VAR:
522 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
523 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100524 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100525 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100526 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100527 case LF_TEXT:
528 case LF_SEPARATOR:
529 add_to_logformat_list(sp, str, pformat, list_format);
530 break;
531 }
532 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100533 }
534 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100535
Willy Tarreauc8368452012-12-21 00:09:23 +0100536 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200537 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
538 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
539 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100540
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100541 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100542}
543
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544/*
545 * Displays the message on stderr with the date and pid. Overrides the quiet
546 * mode during startup.
547 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200548void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200549{
550 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200551 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552
553 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
554 va_start(argp, fmt);
555
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200556 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200557 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200558 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559 vfprintf(stderr, fmt, argp);
560 fflush(stderr);
561 va_end(argp);
562 }
563}
564
565
566/*
567 * Displays the message on stderr with the date and pid.
568 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200569void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570{
571 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200572 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573
574 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
575 va_start(argp, fmt);
576
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200577 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200579 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200580 vfprintf(stderr, fmt, argp);
581 fflush(stderr);
582 va_end(argp);
583 }
584}
585
natalie.chen7ac0ca72015-04-07 15:06:01 +0800586void make_literal(char const *input, char *output) {
587 // the following two arrays must be maintained in matching order:
588 static char inputs[] = "\a\b\f\n\r\t\v\\\"\'";
589 static char outputs[] = "abfnrtv\\\"\'";
590
591 char *p, *pos;
592
593 for (;*input;input++) {
594 if (NULL!= (pos=strchr(inputs, *input))) {
595 *output++ = '\\';
596 *output++ = outputs[pos-inputs];
597 }
598 else
599 *output++ = *input;
600 }
601 *output = '\0';
602}
natalie.chen7c92a9b2015-04-07 11:27:02 +0800603
604void Emaila(const char *fmt, ...)
605{
606 va_list argp;
natalie.chen7ac0ca72015-04-07 15:06:01 +0800607 char tmp[128];
608 char buf[256];
609 char alertcmd[500];
natalie.chen7c92a9b2015-04-07 11:27:02 +0800610 int sysreturn;
611
612 if (global.email_alert) {
613 va_start(argp, fmt);
natalie.chen7ac0ca72015-04-07 15:06:01 +0800614 vsnprintf(tmp,128,fmt, argp);
615 make_literal(tmp, buf);
natalie.chen7c92a9b2015-04-07 11:27:02 +0800616 /*if no from address is set use default */
617 if (!strcmp(global.email_from,"na")) {
natalie.chen7ac0ca72015-04-07 15:06:01 +0800618 snprintf(alertcmd,500,"echo \" %s\" | mail -s \"HAProxy alert\" %s &",buf,global.email_to);
natalie.chen7c92a9b2015-04-07 11:27:02 +0800619 }
620 else {
natalie.chen7ac0ca72015-04-07 15:06:01 +0800621 snprintf(alertcmd,500,"echo \" %s\" | mail -s \"$(echo -e \"HAProxy alert\\nFrom: HAProxy <%s>\\n\")\" %s &",buf,global.email_from,global.email_to);
622 }
623 sysreturn = system(alertcmd);
624 if (sysreturn == -1) {
625 Warning("There was an error sending the email alert");
natalie.chen7c92a9b2015-04-07 11:27:02 +0800626 }
627 vfprintf(stderr, fmt, argp);
628 fflush(stderr);
629 va_end(argp);
630 }
631}
632
633
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634/*
635 * Displays the message on <out> only if quiet mode is not set.
636 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200637void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200638{
639 va_list argp;
640
641 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
642 va_start(argp, fmt);
643 vfprintf(out, fmt, argp);
644 fflush(out);
645 va_end(argp);
646 }
647}
648
649/*
650 * returns log level for <lev> or -1 if not found.
651 */
652int get_log_level(const char *lev)
653{
654 int level;
655
656 level = NB_LOG_LEVELS - 1;
657 while (level >= 0 && strcmp(log_levels[level], lev))
658 level--;
659
660 return level;
661}
662
663
664/*
665 * returns log facility for <fac> or -1 if not found.
666 */
667int get_log_facility(const char *fac)
668{
669 int facility;
670
671 facility = NB_LOG_FACILITIES - 1;
672 while (facility >= 0 && strcmp(log_facilities[facility], fac))
673 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100674
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 return facility;
676}
677
William Lallemanda1cc3812012-02-08 16:38:44 +0100678/*
679 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200680 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100681 *
682 * Return the adress of the \0 character, or NULL on error
683 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100684char *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 +0100685{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100686 if (size < 2)
687 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100688
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100689 if (node->options & LOG_OPT_QUOTE) {
690 *(dst++) = '"';
691 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100692 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100693
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100694 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100695 if (++len > size)
696 len = size;
697 len = strlcpy2(dst, src, len);
698
699 size -= len;
700 dst += len;
701 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100702 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
703 if (size < 2)
704 return NULL;
705 *(dst++) = '-';
706 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100707
708 if (node->options & LOG_OPT_QUOTE) {
709 if (size < 2)
710 return NULL;
711 *(dst++) = '"';
712 }
713
714 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100715 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100716}
717
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100718static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
719{
720 return lf_text_len(dst, src, size, size, node);
721}
722
William Lallemand5f232402012-04-05 18:02:55 +0200723/*
724 * Write a IP adress to the log string
725 * +X option write in hexadecimal notation, most signifant byte on the left
726 */
727char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
728{
729 char *ret = dst;
730 int iret;
731 char pn[INET6_ADDRSTRLEN];
732
733 if (node->options & LOG_OPT_HEXA) {
734 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
735 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
736 if (iret < 0 || iret > size)
737 return NULL;
738 ret += iret;
739 } else {
740 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
741 ret = lf_text(dst, pn, size, node);
742 if (ret == NULL)
743 return NULL;
744 }
745 return ret;
746}
747
748/*
749 * Write a port to the log
750 * +X option write in hexadecimal notation, most signifant byte on the left
751 */
752char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
753{
754 char *ret = dst;
755 int iret;
756
757 if (node->options & LOG_OPT_HEXA) {
758 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
759 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
760 if (iret < 0 || iret > size)
761 return NULL;
762 ret += iret;
763 } else {
764 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
765 if (ret == NULL)
766 return NULL;
767 }
768 return ret;
769}
770
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100771/* Re-generate the syslog header at the beginning of logline once a second and
772 * return the pointer to the first character after the header.
773 */
774static char *update_log_hdr()
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100776 static long tvsec;
777 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200779 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200780 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200781 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100782 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200783
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200784 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200785 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200786
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200787 hdr_len = snprintf(logline, global.max_syslog_len,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100788 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200789 monthname[tm.tm_mon],
790 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100791 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000792 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200793 /* WARNING: depending upon implementations, snprintf may return
794 * either -1 or the number of bytes that would be needed to store
795 * the total message. In both cases, we must adjust it.
796 */
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200797 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
798 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100800 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 }
802
William Lallemand2a4a44f2012-02-06 16:00:33 +0100803 return dataptr;
804}
805
806/*
807 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100808 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100809 */
810void send_log(struct proxy *p, int level, const char *format, ...)
811{
812 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100813 char *dataptr;
814 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100815
Willy Tarreauab34c8f2015-01-15 16:29:53 +0100816 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100817 return;
818
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100819 dataptr = update_log_hdr(); /* update log header and skip it */
820 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100821
822 va_start(argp, format);
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200823 data_len += vsnprintf(dataptr, logline + global.max_syslog_len - dataptr, format, argp);
824 if (data_len < 0 || data_len > global.max_syslog_len)
825 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100827
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100828 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100829}
830
831/*
832 * This function sends a syslog message.
833 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100834 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100835 */
836void __send_log(struct proxy *p, int level, char *message, size_t size)
837{
838 static int logfdunix = -1; /* syslog to AF_UNIX socket */
839 static int logfdinet = -1; /* syslog to AF_INET socket */
840 static char *dataptr = NULL;
841 int fac_level;
842 struct list *logsrvs = NULL;
843 struct logsrv *tmp = NULL;
844 int nblogger;
845 char *log_ptr;
846
847 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848
849 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200850 if (!LIST_ISEMPTY(&global.logsrvs)) {
851 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200852 }
853 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200854 if (!LIST_ISEMPTY(&p->logsrvs)) {
855 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100857 }
858
William Lallemand0f99e342011-10-12 17:50:54 +0200859 if (!logsrvs)
860 return;
861
Robert Tsai81ae1952007-12-05 10:47:29 +0100862 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200863 nblogger = 0;
864 list_for_each_entry(tmp, logsrvs, list) {
865 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100866 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100867 &logfdunix : &logfdinet;
868 int sent;
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200869 int max;
870 char backup;
Robert Tsai81ae1952007-12-05 10:47:29 +0100871
Willy Tarreau53045692014-06-23 18:07:15 +0200872 nblogger++;
873
Willy Tarreaubaaee002006-06-26 02:48:02 +0200874 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200875 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200876 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100877
Willy Tarreau53045692014-06-23 18:07:15 +0200878 if (unlikely(*plogfd < 0)) {
879 /* socket not successfully initialized yet */
880 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
881
882 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
883 Alert("socket for logger #%d failed: %s (errno=%d)\n",
884 nblogger, strerror(errno), errno);
885 continue;
886 }
887 /* we don't want to receive anything on this socket */
888 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
889 /* does nothing under Linux, maybe needed for others */
890 shutdown(*plogfd, SHUT_RD);
891 }
892
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893 /* For each target, we may have a different facility.
894 * We can also have a different log level for each message.
895 * This induces variations in the message header length.
896 * Since we don't want to recompute it each time, nor copy it every
897 * time, we only change the facility in the pre-computed header,
898 * and we change the pointer to the header accordingly.
899 */
William Lallemand0f99e342011-10-12 17:50:54 +0200900 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100901 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200902 do {
903 *log_ptr = '0' + fac_level % 10;
904 fac_level /= 10;
905 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100906 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200907 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100908
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200909 max = size - (log_ptr - dataptr);
910 if (max > logsrv->maxlen)
911 max = logsrv->maxlen;
912
913 /* insert a \n at the end of the message, but save what was
914 * there first because we could have different max lengths
915 * for different log targets.
916 */
917 backup = log_ptr[max - 1];
918 log_ptr[max - 1] = '\n';
919
920 sent = sendto(*plogfd, log_ptr, max,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200921 MSG_DONTWAIT | MSG_NOSIGNAL,
922 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Willy Tarreaudc2695c2014-06-27 18:10:07 +0200923
924 log_ptr[max - 1] = backup;
925
Robert Tsai81ae1952007-12-05 10:47:29 +0100926 if (sent < 0) {
927 Alert("sendto logger #%d failed: %s (errno=%d)\n",
928 nblogger, strerror(errno), errno);
929 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930 }
931}
932
William Lallemandbddd4fd2012-02-27 11:23:10 +0100933extern fd_set hdr_encode_map[];
934extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100935extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +0100936
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200938const 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 +0100939const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
940 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
941 Set-cookie Updated, unknown, unknown */
942
William Lallemand1d705562012-03-12 12:46:41 +0100943/*
944 * try to write a character if there is enough space, or goto out
945 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100946#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100947 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100948 *(tmplog++) = (x); \
949 } else { \
950 goto out; \
951 } \
952 } while(0)
953
William Lallemand1d705562012-03-12 12:46:41 +0100954
Willy Tarreaudf974472012-12-28 02:44:01 +0100955/* Builds a log line in <dst> based on <list_format>, and stops before reaching
956 * <maxsize> characters. Returns the size of the output string in characters,
957 * not counting the trailing zero which is always added if the resulting size
958 * is not zero.
959 */
William Lallemand1d705562012-03-12 12:46:41 +0100960int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200961{
Willy Tarreau73de9892006-11-30 11:40:23 +0100962 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100963 struct proxy *be = s->be;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100964 struct http_txn *txn = &s->txn;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100965 char *uri;
Willy Tarreaufe944602007-10-25 10:34:16 +0200966 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100967 int t_request;
968 int hdr;
969 int last_isspace = 1;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100970 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100971 char *ret;
972 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100973 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200974
William Lallemandbddd4fd2012-02-27 11:23:10 +0100975 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200976
William Lallemandbddd4fd2012-02-27 11:23:10 +0100977 t_request = -1;
978 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
979 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
980
William Lallemand1d705562012-03-12 12:46:41 +0100981 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200982
William Lallemandbddd4fd2012-02-27 11:23:10 +0100983 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100984 if (LIST_ISEMPTY(list_format))
985 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100986
William Lallemand1d705562012-03-12 12:46:41 +0100987 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200988 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +0200989 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100990 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100991 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +0100992
Willy Tarreauc8368452012-12-21 00:09:23 +0100993 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +0100994 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100995 if (!last_isspace) {
996 LOGCHAR(' ');
997 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100998 }
999 break;
1000
William Lallemand1d705562012-03-12 12:46:41 +01001001 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +01001002 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +02001003 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001004 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001005 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001006 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001007 last_isspace = 0;
1008 break;
1009
Willy Tarreauc8368452012-12-21 00:09:23 +01001010 case LOG_FMT_EXPR: // sample expression, may be request or response
1011 key = NULL;
1012 if (tmp->options & LOG_OPT_REQ_CAP)
1013 key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr);
1014 if (!key && (tmp->options & LOG_OPT_RES_CAP))
1015 key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01001016 if (tmp->options & LOG_OPT_HTTP)
1017 ret = encode_chunk(tmplog, dst + maxsize,
1018 '%', http_encode_map, key ? &key->data.str : &empty);
1019 else
1020 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 +01001021 if (ret == 0)
1022 goto out;
1023 tmplog = ret;
1024 last_isspace = 0;
1025 break;
1026
Willy Tarreau2beef582012-12-20 17:22:52 +01001027 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001028 conn = objt_conn(s->req->prod->end);
1029 if (conn)
1030 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1031 else
1032 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001033 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001034 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001035 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001036 last_isspace = 0;
1037 break;
1038
Willy Tarreau2beef582012-12-20 17:22:52 +01001039 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001040 conn = objt_conn(s->req->prod->end);
1041 if (conn) {
1042 if (conn->addr.from.ss_family == AF_UNIX) {
1043 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
1044 } else {
1045 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1046 dst + maxsize - tmplog, tmp);
1047 }
William Lallemand5f232402012-04-05 18:02:55 +02001048 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001049 else
1050 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1051
William Lallemand5f232402012-04-05 18:02:55 +02001052 if (ret == NULL)
1053 goto out;
1054 tmplog = ret;
1055 last_isspace = 0;
1056 break;
1057
Willy Tarreau2beef582012-12-20 17:22:52 +01001058 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001059 conn = objt_conn(s->req->prod->end);
1060 if (conn) {
1061 conn_get_to_addr(conn);
1062 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1063 }
1064 else
1065 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1066
William Lallemand1d705562012-03-12 12:46:41 +01001067 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001068 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001069 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001070 last_isspace = 0;
1071 break;
1072
Willy Tarreau2beef582012-12-20 17:22:52 +01001073 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001074 conn = objt_conn(s->req->prod->end);
1075 if (conn) {
1076 conn_get_to_addr(conn);
1077 if (conn->addr.to.ss_family == AF_UNIX)
1078 ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog);
1079 else
1080 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001081 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001082 else
1083 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1084
William Lallemand5f232402012-04-05 18:02:55 +02001085 if (ret == NULL)
1086 goto out;
1087 tmplog = ret;
1088 last_isspace = 0;
1089 break;
1090
Willy Tarreau2beef582012-12-20 17:22:52 +01001091 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001092 conn = objt_conn(s->req->cons->end);
1093 if (conn)
1094 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1095 else
1096 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1097
William Lallemand1d705562012-03-12 12:46:41 +01001098 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001099 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001100 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001101 last_isspace = 0;
1102 break;
1103
Willy Tarreau2beef582012-12-20 17:22:52 +01001104 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001105 conn = objt_conn(s->req->cons->end);
1106 if (conn)
1107 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1108 else
1109 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1110
William Lallemand5f232402012-04-05 18:02:55 +02001111 if (ret == NULL)
1112 goto out;
1113 tmplog = ret;
1114 last_isspace = 0;
1115 break;
1116
Willy Tarreau2beef582012-12-20 17:22:52 +01001117 case LOG_FMT_SERVERIP: // %si
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001118 conn = objt_conn(s->req->cons->end);
1119 if (conn)
1120 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1121 else
1122 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1123
William Lallemand5f232402012-04-05 18:02:55 +02001124 if (ret == NULL)
1125 goto out;
1126 tmplog = ret;
1127 last_isspace = 0;
1128 break;
1129
Willy Tarreau2beef582012-12-20 17:22:52 +01001130 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001131 conn = objt_conn(s->req->cons->end);
1132 if (conn)
1133 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1134 else
1135 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1136
William Lallemand1d705562012-03-12 12:46:41 +01001137 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001138 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001139 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001140 last_isspace = 0;
1141 break;
1142
William Lallemand1d705562012-03-12 12:46:41 +01001143 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001144 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001145 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1146 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001147 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001148 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001149 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001150 last_isspace = 0;
1151 break;
1152
William Lallemand1d705562012-03-12 12:46:41 +01001153 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001154 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001155 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
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 Lallemandbddd4fd2012-02-27 11:23:10 +01001159 last_isspace = 0;
1160 break;
1161
Yuxans Yao4e25b012012-10-19 10:36:09 +08001162 case LOG_FMT_DATELOCAL: // %Tl
1163 get_localtime(s->logs.accept_date.tv_sec, &tm);
1164 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1165 if (ret == NULL)
1166 goto out;
1167 tmplog = ret;
1168 last_isspace = 0;
1169 break;
1170
William Lallemand5f232402012-04-05 18:02:55 +02001171 case LOG_FMT_TS: // %Ts
1172 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1173 if (tmp->options & LOG_OPT_HEXA) {
1174 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1175 if (iret < 0 || iret > dst + maxsize - tmplog)
1176 goto out;
1177 last_isspace = 0;
1178 tmplog += iret;
1179 } else {
1180 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1181 if (ret == NULL)
1182 goto out;
1183 tmplog = ret;
1184 last_isspace = 0;
1185 }
1186 break;
1187
William Lallemand1d705562012-03-12 12:46:41 +01001188 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001189 if (tmp->options & LOG_OPT_HEXA) {
1190 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1191 if (iret < 0 || iret > dst + maxsize - tmplog)
1192 goto out;
1193 last_isspace = 0;
1194 tmplog += iret;
1195 } else {
1196 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001197 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001198 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1199 tmplog, 4);
1200 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001201 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001202 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001203 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001204 }
1205 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001206
William Lallemand1d705562012-03-12 12:46:41 +01001207 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001208 src = fe->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
Willy Tarreau773d65f2012-10-12 14:56:11 +02001216 case LOG_FMT_FRONTEND_XPRT: // %ft
1217 src = fe->id;
1218 if (tmp->options & LOG_OPT_QUOTE)
1219 LOGCHAR('"');
1220 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1221 if (iret == 0)
1222 goto out;
1223 tmplog += iret;
1224#ifdef USE_OPENSSL
1225 if (s->listener->xprt == &ssl_sock)
1226 LOGCHAR('~');
1227#endif
1228 if (tmp->options & LOG_OPT_QUOTE)
1229 LOGCHAR('"');
1230 last_isspace = 0;
1231 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001232#ifdef USE_OPENSSL
1233 case LOG_FMT_SSL_CIPHER: // %sslc
1234 src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001235 conn = objt_conn(s->si[0].end);
1236 if (conn) {
1237 if (s->listener->xprt == &ssl_sock)
1238 src = ssl_sock_get_cipher_name(conn);
1239 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001240 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1241 if (ret == NULL)
1242 goto out;
1243 tmplog = ret;
1244 last_isspace = 0;
1245 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001246
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001247 case LOG_FMT_SSL_VERSION: // %sslv
1248 src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001249 conn = objt_conn(s->si[0].end);
1250 if (conn) {
1251 if (s->listener->xprt == &ssl_sock)
1252 src = ssl_sock_get_proto_version(conn);
1253 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001254 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1255 if (ret == NULL)
1256 goto out;
1257 tmplog = ret;
1258 last_isspace = 0;
1259 break;
1260#endif
William Lallemand1d705562012-03-12 12:46:41 +01001261 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001262 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001263 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
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 Lallemand51b5dca2012-03-26 17:52:55 +02001267 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001268 break;
1269
William Lallemand1d705562012-03-12 12:46:41 +01001270 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001271 switch (obj_type(s->target)) {
1272 case OBJ_TYPE_SERVER:
1273 src = objt_server(s->target)->id;
1274 break;
1275 case OBJ_TYPE_APPLET:
1276 src = objt_applet(s->target)->name;
1277 break;
1278 default:
1279 src = "<NOSRV>";
1280 break;
1281 }
William Lallemand5f232402012-04-05 18:02:55 +02001282 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001283 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001284 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001285 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001286 last_isspace = 0;
1287 break;
1288
William Lallemand1d705562012-03-12 12:46:41 +01001289 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001290 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001291 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001292 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001293 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001294 last_isspace = 0;
1295 break;
1296
William Lallemand1d705562012-03-12 12:46:41 +01001297 case LOG_FMT_TW: // %Tw
1298 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001299 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001300 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001301 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001302 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001303 last_isspace = 0;
1304 break;
1305
William Lallemand1d705562012-03-12 12:46:41 +01001306 case LOG_FMT_TC: // %Tc
1307 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001308 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001309 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001310 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
William Lallemand1d705562012-03-12 12:46:41 +01001315 case LOG_FMT_TR: // %Tr
1316 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001317 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001318 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001319 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_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001325 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001326 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001327 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001328 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001329 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001330 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001331 last_isspace = 0;
1332 break;
1333
Willy Tarreau2beef582012-12-20 17:22:52 +01001334 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001335 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001336 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001337 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001338 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001339 last_isspace = 0;
1340 break;
1341
William Lallemand1d705562012-03-12 12:46:41 +01001342 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001343 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001344 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001345 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001346 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001347 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001348 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001349 last_isspace = 0;
1350 break;
1351
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001352 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001353 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1354 if (ret == NULL)
1355 goto out;
1356 tmplog = ret;
1357 last_isspace = 0;
1358 break;
1359
Willy Tarreau2beef582012-12-20 17:22:52 +01001360 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001361 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001362 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001363 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001364 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001365 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001366 last_isspace = 0;
1367 break;
1368
Willy Tarreau2beef582012-12-20 17:22:52 +01001369 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001370 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001371 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001372 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001373 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001374 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001375 last_isspace = 0;
1376 break;
1377
William Lallemand1d705562012-03-12 12:46:41 +01001378 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreau6580c062012-03-12 15:09:42 +01001379 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1380 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
1381 *tmplog = '\0';
1382 last_isspace = 0;
1383 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001384
William Lallemand1d705562012-03-12 12:46:41 +01001385 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001386 LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]);
1387 LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001388 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1389 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 +01001390 last_isspace = 0;
1391 break;
1392
William Lallemand1d705562012-03-12 12:46:41 +01001393 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001394 ret = ltoa_o(actconn, 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_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001402 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001403 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001404 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001405 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001406 last_isspace = 0;
1407 break;
1408
William Lallemand1d705562012-03-12 12:46:41 +01001409 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001410 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001411 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001412 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001413 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001414 last_isspace = 0;
1415 break;
1416
William Lallemand1d705562012-03-12 12:46:41 +01001417 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001418 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001419 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001420 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001421 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001422 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001423 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001424 last_isspace = 0;
1425 break;
1426
William Lallemand1d705562012-03-12 12:46:41 +01001427 case LOG_FMT_RETRIES: // %rq
William Lallemandbddd4fd2012-02-27 11:23:10 +01001428 if (s->flags & SN_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001429 LOGCHAR('+');
1430 ret = ltoa_o((s->req->cons->conn_retries>0) ?
1431 (be->conn_retries - s->req->cons->conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001432 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001433 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001434 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001435 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001436 last_isspace = 0;
1437 break;
1438
William Lallemand1d705562012-03-12 12:46:41 +01001439 case LOG_FMT_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001440 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001441 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001442 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001443 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001444 last_isspace = 0;
1445 break;
1446
William Lallemand1d705562012-03-12 12:46:41 +01001447 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001448 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001449 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001450 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001451 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001452 last_isspace = 0;
1453 break;
1454
William Lallemand1d705562012-03-12 12:46:41 +01001455 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001456 /* request header */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001457 if (fe->nb_req_cap && txn->req.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001458 if (tmp->options & LOG_OPT_QUOTE)
1459 LOGCHAR('"');
1460 LOGCHAR('{');
1461 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1462 if (hdr)
1463 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001464 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001465 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001466 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001467 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001468 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001469 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001470 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001471 }
1472 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001473 if (tmp->options & LOG_OPT_QUOTE)
1474 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001475 last_isspace = 0;
1476 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001477 break;
1478
William Lallemand1d705562012-03-12 12:46:41 +01001479 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001480 /* request header list */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001481 if (fe->nb_req_cap && txn->req.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001482 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1483 if (hdr > 0)
1484 LOGCHAR(' ');
1485 if (tmp->options & LOG_OPT_QUOTE)
1486 LOGCHAR('"');
1487 if (txn->req.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001488 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001489 '#', hdr_encode_map, txn->req.cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001490 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001491 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001492 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001493 } else if (!(tmp->options & LOG_OPT_QUOTE))
1494 LOGCHAR('-');
1495 if (tmp->options & LOG_OPT_QUOTE)
1496 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001497 last_isspace = 0;
1498 }
1499 }
1500 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001501
William Lallemand1d705562012-03-12 12:46:41 +01001502
1503 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001504 /* response header */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001505 if (fe->nb_rsp_cap && txn->rsp.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001506 if (tmp->options & LOG_OPT_QUOTE)
1507 LOGCHAR('"');
1508 LOGCHAR('{');
1509 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1510 if (hdr)
1511 LOGCHAR('|');
William Lallemand51b5dca2012-03-26 17:52:55 +02001512 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001513 ret = encode_string(tmplog, dst + maxsize,
1514 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1515 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001516 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001517 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001518 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001519 }
1520 LOGCHAR('}');
1521 last_isspace = 0;
1522 if (tmp->options & LOG_OPT_QUOTE)
1523 LOGCHAR('"');
1524 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001525 break;
1526
William Lallemand1d705562012-03-12 12:46:41 +01001527 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001528 /* response header list */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001529 if (fe->nb_rsp_cap && txn->rsp.cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001530 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1531 if (hdr > 0)
1532 LOGCHAR(' ');
1533 if (tmp->options & LOG_OPT_QUOTE)
1534 LOGCHAR('"');
1535 if (txn->rsp.cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001536 ret = encode_string(tmplog, dst + maxsize,
1537 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1538 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001539 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001540 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001541 } else if (!(tmp->options & LOG_OPT_QUOTE))
1542 LOGCHAR('-');
1543 if (tmp->options & LOG_OPT_QUOTE)
1544 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001545 last_isspace = 0;
1546 }
1547 }
1548 break;
1549
William Lallemand1d705562012-03-12 12:46:41 +01001550 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001551 /* Request */
1552 if (tmp->options & LOG_OPT_QUOTE)
1553 LOGCHAR('"');
1554 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001555 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001556 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001557 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001558 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001559 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001560 if (tmp->options & LOG_OPT_QUOTE)
1561 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001562 last_isspace = 0;
1563 break;
William Lallemand5f232402012-04-05 18:02:55 +02001564
1565 case LOG_FMT_COUNTER: // %rt
1566 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001567 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001568 if (iret < 0 || iret > dst + maxsize - tmplog)
1569 goto out;
1570 last_isspace = 0;
1571 tmplog += iret;
1572 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001573 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001574 if (ret == NULL)
1575 goto out;
1576 tmplog = ret;
1577 last_isspace = 0;
1578 }
1579 break;
1580
1581 case LOG_FMT_HOSTNAME: // %H
1582 src = hostname;
1583 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1584 if (ret == NULL)
1585 goto out;
1586 tmplog = ret;
1587 last_isspace = 0;
1588 break;
1589
1590 case LOG_FMT_PID: // %pid
1591 if (tmp->options & LOG_OPT_HEXA) {
1592 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1593 if (iret < 0 || iret > dst + maxsize - tmplog)
1594 goto out;
1595 last_isspace = 0;
1596 tmplog += iret;
1597 } else {
1598 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1599 if (ret == NULL)
1600 goto out;
1601 tmplog = ret;
1602 last_isspace = 0;
1603 }
1604 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001605
1606 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001607 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001608 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02001609 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001610 if (ret == NULL)
1611 goto out;
1612 tmplog = ret;
1613 last_isspace = 0;
1614 break;
1615
William Lallemandbddd4fd2012-02-27 11:23:10 +01001616 }
1617 }
1618
1619out:
William Lallemand1d705562012-03-12 12:46:41 +01001620 /* *tmplog is a unused character */
1621 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001622 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001623
Willy Tarreaubaaee002006-06-26 02:48:02 +02001624}
1625
William Lallemand1d705562012-03-12 12:46:41 +01001626/*
1627 * send a log for the session when we have enough info about it.
1628 * Will not log if the frontend has no log defined.
1629 */
1630void sess_log(struct session *s)
1631{
1632 char *tmplog;
1633 int size, err, level;
1634
1635 /* if we don't want to log normal traffic, return now */
Willy Tarreau570f2212013-06-10 16:42:09 +02001636 err = (s->flags & SN_REDISP) ||
1637 ((s->flags & SN_ERR_MASK) > SN_ERR_LOCAL) ||
1638 (((s->flags & SN_ERR_MASK) == SN_ERR_NONE) &&
1639 (s->req->cons->conn_retries != s->be->conn_retries)) ||
1640 ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001641
William Lallemand1d705562012-03-12 12:46:41 +01001642 if (!err && (s->fe->options2 & PR_O2_NOLOGNORM))
1643 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001644
William Lallemand1d705562012-03-12 12:46:41 +01001645 if (LIST_ISEMPTY(&s->fe->logsrvs))
1646 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001647
Willy Tarreauabcd5142013-06-11 17:18:02 +02001648 if (s->logs.level) { /* loglevel was overridden */
1649 if (s->logs.level == -1) {
1650 s->logs.logwait = 0; /* logs disabled */
1651 return;
1652 }
1653 level = s->logs.level - 1;
1654 }
1655 else {
1656 level = LOG_INFO;
1657 if (err && (s->fe->options2 & PR_O2_LOGERRORS))
1658 level = LOG_ERR;
1659 }
William Lallemand1d705562012-03-12 12:46:41 +01001660
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001661 /* if unique-id was not generated */
1662 if (!s->unique_id && !LIST_ISEMPTY(&s->fe->format_unique_id)) {
1663 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
1664 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
1665 }
1666
William Lallemand1d705562012-03-12 12:46:41 +01001667 tmplog = update_log_hdr();
1668 size = tmplog - logline;
Willy Tarreaudc2695c2014-06-27 18:10:07 +02001669 size += build_logline(s, tmplog, global.max_syslog_len - size, &s->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01001670 if (size > 0) {
Willy Tarreaudf974472012-12-28 02:44:01 +01001671 __send_log(s->fe, level, logline, size + 1);
William Lallemand1d705562012-03-12 12:46:41 +01001672 s->logs.logwait = 0;
1673 }
1674}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001675
Willy Tarreaubaaee002006-06-26 02:48:02 +02001676/*
1677 * Local variables:
1678 * c-indent-level: 8
1679 * c-basic-offset: 8
1680 * End:
1681 */