blob: ef7005aedeb08176c69674b0c45c24111cf8c492 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau8a3f52f2012-12-20 21:23:42 +010013#include <ctype.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010022#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
24#include <sys/time.h>
25
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020027#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020029#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010032#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020033
William Lallemand5f232402012-04-05 18:02:55 +020034#include <proto/frontend.h>
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +000035#include <proto/proto_http.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020036#include <proto/log.h>
Willy Tarreauc8368452012-12-21 00:09:23 +010037#include <proto/sample.h>
Willy Tarreaufb0afa72015-04-03 14:46:27 +020038#include <proto/stream.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010039#include <proto/stream_interface.h>
Willy Tarreau773d65f2012-10-12 14:56:11 +020040#ifdef USE_OPENSSL
41#include <proto/ssl_sock.h>
42#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreaubaaee002006-06-26 02:48:02 +020044const char *log_facilities[NB_LOG_FACILITIES] = {
45 "kern", "user", "mail", "daemon",
46 "auth", "syslog", "lpr", "news",
47 "uucp", "cron", "auth2", "ftp",
48 "ntp", "audit", "alert", "cron2",
49 "local0", "local1", "local2", "local3",
50 "local4", "local5", "local6", "local7"
51};
52
53
54const char *log_levels[NB_LOG_LEVELS] = {
55 "emerg", "alert", "crit", "err",
56 "warning", "notice", "info", "debug"
57};
58
Willy Tarreau570f2212013-06-10 16:42:09 +020059const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */
Willy Tarreaub8750a82006-09-03 09:56:00 +020060const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
William Lallemand723b73a2012-02-08 16:37:49 +010062
63/* log_format */
64struct logformat_type {
65 char *name;
66 int type;
William Lallemandbddd4fd2012-02-27 11:23:10 +010067 int mode;
William Lallemand5e19a282012-04-02 16:22:10 +020068 int lw; /* logwait bitsfield */
William Lallemandb7ff6a32012-03-02 14:35:21 +010069 int (*config_callback)(struct logformat_node *node, struct proxy *curproxy);
Willy Tarreau2beef582012-12-20 17:22:52 +010070 const char *replace_by; /* new option to use instead of old one */
William Lallemand723b73a2012-02-08 16:37:49 +010071};
72
William Lallemandb7ff6a32012-03-02 14:35:21 +010073int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy);
74
William Lallemand723b73a2012-02-08 16:37:49 +010075/* log_format variable names */
76static const struct logformat_type logformat_keywords[] = {
William Lallemand5e19a282012-04-02 16:22:10 +020077 { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */
Willy Tarreau2beef582012-12-20 17:22:52 +010078
79 /* please keep these lines sorted ! */
80 { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */
81 { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */
82 { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */
83 { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */
84 { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */
Willy Tarreau4bf99632014-06-13 12:21:40 +020085 { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +020086 { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */
Willy Tarreau2beef582012-12-20 17:22:52 +010087 { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */
Yuxans Yao4e25b012012-10-19 10:36:09 +080088 { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */
William Lallemand5e19a282012-04-02 16:22:10 +020089 { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */
William Lallemand5e19a282012-04-02 16:22:10 +020090 { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */
Willy Tarreau2beef582012-12-20 17:22:52 +010091 { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */
William Lallemand5e19a282012-04-02 16:22:10 +020092 { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */
Willy Tarreau2beef582012-12-20 17:22:52 +010093 { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */
94 { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */
William Lallemand5e19a282012-04-02 16:22:10 +020095 { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010096 { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */
William Lallemand5e19a282012-04-02 16:22:10 +020097 { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */
Willy Tarreau2beef582012-12-20 17:22:52 +010098 { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */
99 { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */
William Lallemand5e19a282012-04-02 16:22:10 +0200100 { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */
Willy Tarreau2beef582012-12-20 17:22:52 +0100101 { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */
102 { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */
103 { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */
104 { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */
105 { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */
106 { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */
107 { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */
Willy Tarreaud9ed3d22014-06-13 12:23:06 +0200108 { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */
109 { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */
110 { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */
111 { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000112 { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */
113 { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000114 { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000115 { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */
116 { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */
Willy Tarreau7346acb2014-08-28 15:03:15 +0200117 { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */
Willy Tarreau2beef582012-12-20 17:22:52 +0100118 { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */
William Lallemand5e19a282012-04-02 16:22:10 +0200119 { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */
Willy Tarreau2beef582012-12-20 17:22:52 +0100120 { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */
121 { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100122 { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */
Willy Tarreau2beef582012-12-20 17:22:52 +0100123 { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */
124 { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */
125 { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
126 { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */
127 { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +0200128 { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */
129 { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */
Willy Tarreau2beef582012-12-20 17:22:52 +0100130 { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */
131 { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */
132 { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */
133
134 /* The following tags are deprecated and will be removed soon */
135 { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */
136 { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */
137 { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */
138 { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */
139 { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */
140 { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */
141 { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */
142 { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */
143 { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */
144 { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */
145 { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */
William Lallemand5e19a282012-04-02 16:22:10 +0200146 { 0, 0, 0, 0, NULL }
William Lallemand723b73a2012-02-08 16:37:49 +0100147};
148
Willy Tarreau2beef582012-12-20 17:22:52 +0100149char default_http_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
150char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%T] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %Tq %Tw %Tc %Tr %Tt %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl";
151char default_tcp_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq";
William Lallemand723b73a2012-02-08 16:37:49 +0100152char *log_format = NULL;
153
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100154/* This is a global syslog line, common to all outgoing messages. It begins
155 * with the syslog tag and the date that are updated by update_log_hdr().
156 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200157char *logline = NULL;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100158
William Lallemand723b73a2012-02-08 16:37:49 +0100159struct logformat_var_args {
160 char *name;
161 int mask;
162};
163
164struct logformat_var_args var_args_list[] = {
165// global
166 { "M", LOG_OPT_MANDATORY },
167 { "Q", LOG_OPT_QUOTE },
William Lallemand5f232402012-04-05 18:02:55 +0200168 { "X", LOG_OPT_HEXA },
William Lallemand723b73a2012-02-08 16:37:49 +0100169 { 0, 0 }
170};
171
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200172/* return the name of the directive used in the current proxy for which we're
173 * currently parsing a header, when it is known.
174 */
175static inline const char *fmt_directive(const struct proxy *curproxy)
176{
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100177 switch (curproxy->conf.args.ctx) {
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200178 case ARGC_ACL:
179 return "acl";
180 case ARGC_STK:
181 return "stick";
182 case ARGC_TRK:
183 return "track-sc";
184 case ARGC_LOG:
185 return "log-format";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100186 case ARGC_HRQ:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100187 return "http-request";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100188 case ARGC_HRS:
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100189 return "http-response";
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200190 case ARGC_UIF:
191 return "unique-id-format";
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100192 case ARGC_RDR:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200193 return "redirect";
194 case ARGC_CAP:
195 return "capture";
Willy Tarreau28d976d2015-07-09 11:39:33 +0200196 case ARGC_SRV:
197 return "server";
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100198 default:
Willy Tarreau53e1a6d2015-07-09 11:20:00 +0200199 return "undefined(please report this bug)"; /* must never happen */
Willy Tarreaubf0addb2013-12-02 12:24:54 +0100200 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200201}
202
William Lallemand723b73a2012-02-08 16:37:49 +0100203/*
William Lallemandb7ff6a32012-03-02 14:35:21 +0100204 * callback used to configure addr source retrieval
205 */
206int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy)
207{
208 curproxy->options2 |= PR_O2_SRC_ADDR;
209
210 return 0;
211}
212
213
214/*
William Lallemand723b73a2012-02-08 16:37:49 +0100215 * Parse args in a logformat_var
216 */
217int parse_logformat_var_args(char *args, struct logformat_node *node)
218{
219 int i = 0;
220 int end = 0;
221 int flags = 0; // 1 = + 2 = -
222 char *sp = NULL; // start pointer
223
224 if (args == NULL)
225 return 1;
226
227 while (1) {
228 if (*args == '\0')
229 end = 1;
230
231 if (*args == '+') {
232 // add flag
233 sp = args + 1;
234 flags = 1;
235 }
236 if (*args == '-') {
237 // delete flag
238 sp = args + 1;
239 flags = 2;
240 }
241
242 if (*args == '\0' || *args == ',') {
243 *args = '\0';
Willy Tarreau254d44c2012-12-20 18:19:26 +0100244 for (i = 0; sp && var_args_list[i].name; i++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100245 if (strcmp(sp, var_args_list[i].name) == 0) {
246 if (flags == 1) {
247 node->options |= var_args_list[i].mask;
248 break;
249 } else if (flags == 2) {
250 node->options &= ~var_args_list[i].mask;
251 break;
252 }
253 }
254 }
255 sp = NULL;
256 if (end)
257 break;
258 }
Willy Tarreau254d44c2012-12-20 18:19:26 +0100259 args++;
William Lallemand723b73a2012-02-08 16:37:49 +0100260 }
261 return 0;
262}
263
264/*
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100265 * Parse a variable '%varname' or '%{args}varname' in log-format. The caller
266 * must pass the args part in the <arg> pointer with its length in <arg_len>,
267 * and varname with its length in <var> and <var_len> respectively. <arg> is
268 * ignored when arg_len is 0. Neither <var> nor <var_len> may be null.
William Lallemand723b73a2012-02-08 16:37:49 +0100269 */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100270int 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 +0100271{
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100272 int j;
273 struct logformat_node *node;
William Lallemand723b73a2012-02-08 16:37:49 +0100274
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100275 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
276 if (strlen(logformat_keywords[j].name) == var_len &&
277 strncmp(var, logformat_keywords[j].name, var_len) == 0) {
278 if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) {
279 node = calloc(1, sizeof(struct logformat_node));
280 node->type = logformat_keywords[j].type;
281 node->options = *defoptions;
282 if (arg_len) {
283 node->arg = my_strndup(arg, arg_len);
284 parse_logformat_var_args(node->arg, node);
285 }
286 if (node->type == LOG_FMT_GLOBAL) {
287 *defoptions = node->options;
288 free(node->arg);
289 free(node);
290 } else {
291 if (logformat_keywords[j].config_callback &&
292 logformat_keywords[j].config_callback(node, curproxy) != 0) {
William Lallemandbddd4fd2012-02-27 11:23:10 +0100293 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100294 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100295 curproxy->to_log |= logformat_keywords[j].lw;
296 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100297 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100298 if (logformat_keywords[j].replace_by)
Willy Tarreau06d97f92013-12-02 17:45:48 +0100299 Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200300 curproxy->conf.args.file, curproxy->conf.args.line,
301 logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100302 return 0;
303 } else {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100304 Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n",
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200305 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
306 logformat_keywords[j].name);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100307 return -1;
William Lallemand723b73a2012-02-08 16:37:49 +0100308 }
William Lallemand723b73a2012-02-08 16:37:49 +0100309 }
310 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100311
312 j = var[var_len];
313 var[var_len] = 0;
Willy Tarreau06d97f92013-12-02 17:45:48 +0100314 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 +0200315 curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy));
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100316 var[var_len] = j;
William Lallemand723b73a2012-02-08 16:37:49 +0100317 return -1;
318}
319
320/*
321 * push to the logformat linked list
322 *
323 * start: start pointer
324 * end: end text pointer
325 * type: string type
William Lallemand1d705562012-03-12 12:46:41 +0100326 * list_format: destination list
William Lallemand723b73a2012-02-08 16:37:49 +0100327 *
328 * LOG_TEXT: copy chars from start to end excluding end.
329 *
330*/
William Lallemand1d705562012-03-12 12:46:41 +0100331void add_to_logformat_list(char *start, char *end, int type, struct list *list_format)
William Lallemand723b73a2012-02-08 16:37:49 +0100332{
333 char *str;
334
Willy Tarreaua3571662012-12-20 21:59:12 +0100335 if (type == LF_TEXT) { /* type text */
William Lallemand723b73a2012-02-08 16:37:49 +0100336 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand723b73a2012-02-08 16:37:49 +0100337 str = calloc(end - start + 1, 1);
338 strncpy(str, start, end - start);
William Lallemand723b73a2012-02-08 16:37:49 +0100339 str[end - start] = '\0';
340 node->arg = str;
William Lallemand1d705562012-03-12 12:46:41 +0100341 node->type = LOG_FMT_TEXT; // type string
342 LIST_ADDQ(list_format, &node->list);
Willy Tarreaua3571662012-12-20 21:59:12 +0100343 } else if (type == LF_SEPARATOR) {
William Lallemand723b73a2012-02-08 16:37:49 +0100344 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
William Lallemand1d705562012-03-12 12:46:41 +0100345 node->type = LOG_FMT_SEPARATOR;
346 LIST_ADDQ(list_format, &node->list);
William Lallemand723b73a2012-02-08 16:37:49 +0100347 }
348}
349
350/*
Willy Tarreauc8368452012-12-21 00:09:23 +0100351 * Parse the sample fetch expression <text> and add a node to <list_format> upon
352 * success. At the moment, sample converters are not yet supported but fetch arguments
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200353 * should work. The curpx->conf.args.ctx must be set by the caller.
Willy Tarreauc8368452012-12-21 00:09:23 +0100354 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100355void 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 +0100356{
357 char *cmd[2];
358 struct sample_expr *expr;
359 struct logformat_node *node;
360 int cmd_arg;
Willy Tarreau975c1782013-12-12 23:16:54 +0100361 char *errmsg = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100362
363 cmd[0] = text;
364 cmd[1] = "";
365 cmd_arg = 0;
366
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100367 expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args);
Willy Tarreauc8368452012-12-21 00:09:23 +0100368 if (!expr) {
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200369 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n",
370 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
Willy Tarreau975c1782013-12-12 23:16:54 +0100371 text, errmsg);
Willy Tarreauc8368452012-12-21 00:09:23 +0100372 return;
373 }
374
375 node = calloc(1, sizeof(struct logformat_node));
376 node->type = LOG_FMT_EXPR;
377 node->expr = expr;
378 node->options = options;
379
380 if (arg_len) {
381 node->arg = my_strndup(arg, arg_len);
382 parse_logformat_var_args(node->arg, node);
383 }
Willy Tarreau434c57c2013-01-08 01:10:24 +0100384 if (expr->fetch->val & cap & SMP_VAL_REQUEST)
Willy Tarreauc8368452012-12-21 00:09:23 +0100385 node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */
386
Willy Tarreau434c57c2013-01-08 01:10:24 +0100387 if (expr->fetch->val & cap & SMP_VAL_RESPONSE)
Willy Tarreauc8368452012-12-21 00:09:23 +0100388 node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */
389
Willy Tarreau434c57c2013-01-08 01:10:24 +0100390 if (!(expr->fetch->val & cap))
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200391 Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n",
392 curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx),
393 text, sample_src_names(expr->fetch->use));
Willy Tarreau434c57c2013-01-08 01:10:24 +0100394
Willy Tarreauc8368452012-12-21 00:09:23 +0100395 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
396 /* Note, we may also need to set curpx->to_log with certain fetches */
Willy Tarreau25320b22013-03-24 07:22:08 +0100397 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
Willy Tarreauc8368452012-12-21 00:09:23 +0100398
William Lallemand65ad6e12014-01-31 15:08:02 +0100399 /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags
400 * needed with some sample fetches (eg: ssl*). We always set it for
401 * now on, but this will leave with sample capabilities soon.
Willy Tarreau1f31c732013-01-10 16:22:27 +0100402 */
403 curpx->to_log |= LW_XPRT;
William Lallemand65ad6e12014-01-31 15:08:02 +0100404 curpx->to_log |= LW_REQ;
Willy Tarreauc8368452012-12-21 00:09:23 +0100405 LIST_ADDQ(list_format, &node->list);
406}
407
408/*
William Lallemand723b73a2012-02-08 16:37:49 +0100409 * Parse the log_format string and fill a linked list.
410 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200411 * You can set arguments using { } : %{many arguments}varname.
412 * The curproxy->conf.args.ctx must be set by the caller.
William Lallemand1d705562012-03-12 12:46:41 +0100413 *
414 * str: the string to parse
415 * curproxy: the proxy affected
416 * list_format: the destination list
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100417 * options: LOG_OPT_* to force on every node
Willy Tarreau434c57c2013-01-08 01:10:24 +0100418 * cap: all SMP_VAL_* flags supported by the consumer
William Lallemand723b73a2012-02-08 16:37:49 +0100419 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100420void 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 +0100421{
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100422 char *sp, *str, *backfmt; /* start pointer for text parts */
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100423 char *arg = NULL; /* start pointer for args */
424 char *var = NULL; /* start pointer for vars */
425 int arg_len = 0;
426 int var_len = 0;
427 int cformat; /* current token format */
428 int pformat; /* previous token format */
William Lallemand723b73a2012-02-08 16:37:49 +0100429 struct logformat_node *tmplf, *back;
430
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100431 sp = str = backfmt = strdup(fmt);
William Lallemand1dc00ef2012-08-09 16:41:35 +0200432 curproxy->to_log |= LW_INIT;
William Lallemand5e19a282012-04-02 16:22:10 +0200433
William Lallemand723b73a2012-02-08 16:37:49 +0100434 /* flush the list first. */
William Lallemand1d705562012-03-12 12:46:41 +0100435 list_for_each_entry_safe(tmplf, back, list_format, list) {
William Lallemand723b73a2012-02-08 16:37:49 +0100436 LIST_DEL(&tmplf->list);
437 free(tmplf);
438 }
439
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100440 for (cformat = LF_INIT; cformat != LF_END; str++) {
William Lallemand723b73a2012-02-08 16:37:49 +0100441 pformat = cformat;
442
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100443 if (!*str)
444 cformat = LF_END; // preset it to save all states from doing this
William Lallemand723b73a2012-02-08 16:37:49 +0100445
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100446 /* The prinicple of the two-step state machine below is to first detect a change, and
447 * second have all common paths processed at one place. The common paths are the ones
448 * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END).
449 * We use the common LF_INIT state to dispatch to the different final states.
450 */
451 switch (pformat) {
452 case LF_STARTVAR: // text immediately following a '%'
Willy Tarreauc8368452012-12-21 00:09:23 +0100453 arg = NULL; var = NULL;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100454 arg_len = var_len = 0;
455 if (*str == '{') { // optional argument
456 cformat = LF_STARG;
457 arg = str + 1;
William Lallemand723b73a2012-02-08 16:37:49 +0100458 }
Willy Tarreauc8368452012-12-21 00:09:23 +0100459 else if (*str == '[') {
460 cformat = LF_STEXPR;
461 var = str + 1; // store expr in variable name
462 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100463 else if (isalpha((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100464 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100465 var = str;
William Lallemand723b73a2012-02-08 16:37:49 +0100466 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100467 else if (*str == '%')
468 cformat = LF_TEXT; // convert this character to a litteral (useful for '%')
Willy Tarreau0f28f822013-12-16 01:38:33 +0100469 else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') {
Willy Tarreau06d97f92013-12-02 17:45:48 +0100470 /* single '%' followed by blank or digit, send them both */
471 cformat = LF_TEXT;
472 pformat = LF_TEXT; /* finally we include the previous char as well */
473 sp = str - 1; /* send both the '%' and the current char */
474 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",
475 curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt);
476
477 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100478 else
479 cformat = LF_INIT; // handle other cases of litterals
480 break;
481
482 case LF_STARG: // text immediately following '%{'
483 if (*str == '}') { // end of arg
William Lallemand723b73a2012-02-08 16:37:49 +0100484 cformat = LF_EDARG;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100485 arg_len = str - arg;
486 *str = 0; // used for reporting errors
William Lallemand723b73a2012-02-08 16:37:49 +0100487 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100488 break;
489
490 case LF_EDARG: // text immediately following '%{arg}'
Willy Tarreauc8368452012-12-21 00:09:23 +0100491 if (*str == '[') {
492 cformat = LF_STEXPR;
493 var = str + 1; // store expr in variable name
494 break;
495 }
Willy Tarreau0f28f822013-12-16 01:38:33 +0100496 else if (isalnum((unsigned char)*str)) { // variable name
William Lallemand723b73a2012-02-08 16:37:49 +0100497 cformat = LF_VAR;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100498 var = str;
499 break;
500 }
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200501 Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n",
502 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg);
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100503 cformat = LF_INIT;
504 break;
505
Willy Tarreauc8368452012-12-21 00:09:23 +0100506 case LF_STEXPR: // text immediately following '%['
507 if (*str == ']') { // end of arg
508 cformat = LF_EDEXPR;
509 var_len = str - var;
510 *str = 0; // needed for parsing the expression
511 }
512 break;
513
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100514 case LF_VAR: // text part of a variable name
515 var_len = str - var;
Willy Tarreau0f28f822013-12-16 01:38:33 +0100516 if (!isalnum((unsigned char)*str))
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100517 cformat = LF_INIT; // not variable name anymore
518 break;
519
Willy Tarreauc8368452012-12-21 00:09:23 +0100520 default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100521 cformat = LF_INIT;
522 }
523
524 if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */
525 switch (*str) {
526 case '%': cformat = LF_STARTVAR; break;
527 case ' ': cformat = LF_SEPARATOR; break;
528 case 0 : cformat = LF_END; break;
529 default : cformat = LF_TEXT; break;
William Lallemand723b73a2012-02-08 16:37:49 +0100530 }
531 }
532
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100533 if (cformat != pformat || pformat == LF_SEPARATOR) {
534 switch (pformat) {
535 case LF_VAR:
536 parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options);
537 break;
Willy Tarreauc8368452012-12-21 00:09:23 +0100538 case LF_STEXPR:
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100539 add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line);
Willy Tarreauc8368452012-12-21 00:09:23 +0100540 break;
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100541 case LF_TEXT:
542 case LF_SEPARATOR:
543 add_to_logformat_list(sp, str, pformat, list_format);
544 break;
545 }
546 sp = str; /* new start of text at every state switch and at every separator */
William Lallemand723b73a2012-02-08 16:37:49 +0100547 }
548 }
Willy Tarreau8a3f52f2012-12-20 21:23:42 +0100549
Willy Tarreauc8368452012-12-21 00:09:23 +0100550 if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR)
Willy Tarreaub1f3af22013-04-12 18:30:32 +0200551 Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n",
552 curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy),
553 var ? var : arg ? arg : "%");
Willy Tarreau3ed22a42012-12-23 17:29:07 +0100554
Willy Tarreaub83bc1e2012-12-24 12:36:33 +0100555 free(backfmt);
William Lallemand723b73a2012-02-08 16:37:49 +0100556}
557
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558/*
559 * Displays the message on stderr with the date and pid. Overrides the quiet
560 * mode during startup.
561 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200562void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563{
564 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200565 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566
567 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
568 va_start(argp, fmt);
569
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200570 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200572 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573 vfprintf(stderr, fmt, argp);
574 fflush(stderr);
575 va_end(argp);
576 }
577}
578
579
580/*
581 * Displays the message on stderr with the date and pid.
582 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200583void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200584{
585 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200586 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200587
588 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
589 va_start(argp, fmt);
590
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200591 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200592 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200593 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200594 vfprintf(stderr, fmt, argp);
595 fflush(stderr);
596 va_end(argp);
597 }
598}
599
600/*
601 * Displays the message on <out> only if quiet mode is not set.
602 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200603void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200604{
605 va_list argp;
606
607 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
608 va_start(argp, fmt);
609 vfprintf(out, fmt, argp);
610 fflush(out);
611 va_end(argp);
612 }
613}
614
615/*
616 * returns log level for <lev> or -1 if not found.
617 */
618int get_log_level(const char *lev)
619{
620 int level;
621
622 level = NB_LOG_LEVELS - 1;
623 while (level >= 0 && strcmp(log_levels[level], lev))
624 level--;
625
626 return level;
627}
628
629
630/*
631 * returns log facility for <fac> or -1 if not found.
632 */
633int get_log_facility(const char *fac)
634{
635 int facility;
636
637 facility = NB_LOG_FACILITIES - 1;
638 while (facility >= 0 && strcmp(log_facilities[facility], fac))
639 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100640
Willy Tarreaubaaee002006-06-26 02:48:02 +0200641 return facility;
642}
643
William Lallemanda1cc3812012-02-08 16:38:44 +0100644/*
645 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200646 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100647 *
648 * Return the adress of the \0 character, or NULL on error
649 */
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100650char *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 +0100651{
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100652 if (size < 2)
653 return NULL;
William Lallemanda1cc3812012-02-08 16:38:44 +0100654
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100655 if (node->options & LOG_OPT_QUOTE) {
656 *(dst++) = '"';
657 size--;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100658 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100659
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100660 if (src && len) {
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100661 if (++len > size)
662 len = size;
663 len = strlcpy2(dst, src, len);
664
665 size -= len;
666 dst += len;
667 }
Willy Tarreau6cbbdbf2013-02-05 18:52:25 +0100668 else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) {
669 if (size < 2)
670 return NULL;
671 *(dst++) = '-';
672 }
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100673
674 if (node->options & LOG_OPT_QUOTE) {
675 if (size < 2)
676 return NULL;
677 *(dst++) = '"';
678 }
679
680 *dst = '\0';
William Lallemandbddd4fd2012-02-27 11:23:10 +0100681 return dst;
William Lallemanda1cc3812012-02-08 16:38:44 +0100682}
683
Willy Tarreau2b0108a2012-12-21 19:23:44 +0100684static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
685{
686 return lf_text_len(dst, src, size, size, node);
687}
688
William Lallemand5f232402012-04-05 18:02:55 +0200689/*
690 * Write a IP adress to the log string
691 * +X option write in hexadecimal notation, most signifant byte on the left
692 */
693char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
694{
695 char *ret = dst;
696 int iret;
697 char pn[INET6_ADDRSTRLEN];
698
699 if (node->options & LOG_OPT_HEXA) {
700 const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr;
701 iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]);
702 if (iret < 0 || iret > size)
703 return NULL;
704 ret += iret;
705 } else {
706 addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn));
707 ret = lf_text(dst, pn, size, node);
708 if (ret == NULL)
709 return NULL;
710 }
711 return ret;
712}
713
714/*
715 * Write a port to the log
716 * +X option write in hexadecimal notation, most signifant byte on the left
717 */
718char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node)
719{
720 char *ret = dst;
721 int iret;
722
723 if (node->options & LOG_OPT_HEXA) {
724 const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port;
725 iret = snprintf(dst, size, "%02X%02X", port[0], port[1]);
726 if (iret < 0 || iret > size)
727 return NULL;
728 ret += iret;
729 } else {
730 ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size);
731 if (ret == NULL)
732 return NULL;
733 }
734 return ret;
735}
736
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100737/* Re-generate the syslog header at the beginning of logline once a second and
738 * return the pointer to the first character after the header.
739 */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100740static char *update_log_hdr(const char *log_tag)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741{
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100742 static long tvsec;
743 static char *dataptr = NULL; /* backup of last end of header, NULL first time */
Willy Tarreau094af4e2015-01-07 15:03:42 +0100744 int tag_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200746 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200748 struct tm tm;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100749 int hdr_len;
Willy Tarreaufe944602007-10-25 10:34:16 +0200750
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200751 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200752 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753
Willy Tarreau18324f52014-06-27 18:10:07 +0200754 hdr_len = snprintf(logline, global.max_syslog_len,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100755 "<<<<>%s %2d %02d:%02d:%02d %s",
Willy Tarreaufe944602007-10-25 10:34:16 +0200756 monthname[tm.tm_mon],
757 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau094af4e2015-01-07 15:03:42 +0100758 global.log_send_hostname ? global.log_send_hostname : "");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759 /* WARNING: depending upon implementations, snprintf may return
760 * either -1 or the number of bytes that would be needed to store
761 * the total message. In both cases, we must adjust it.
762 */
Willy Tarreau18324f52014-06-27 18:10:07 +0200763 if (hdr_len < 0 || hdr_len > global.max_syslog_len)
764 hdr_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100766 dataptr = logline + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200767 }
768
Willy Tarreau094af4e2015-01-07 15:03:42 +0100769 dataptr[0] = 0; // ensure we get rid of any previous attempt
770
771 tag_len = snprintf(dataptr, logline + global.max_syslog_len - dataptr, "%s[%d]: ", log_tag, pid);
772 if (tag_len < 0 || tag_len > logline + global.max_syslog_len - dataptr)
773 tag_len = logline + global.max_syslog_len - dataptr;
774
775 return dataptr + tag_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100776}
777
778/*
779 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100780 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100781 */
782void send_log(struct proxy *p, int level, const char *format, ...)
783{
784 va_list argp;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100785 char *dataptr;
786 int data_len;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100787
Willy Tarreau8c97ab52015-01-15 16:29:53 +0100788 if (level < 0 || format == NULL || logline == NULL)
William Lallemand2a4a44f2012-02-06 16:00:33 +0100789 return;
790
Thierry FOURNIERe83766a2015-03-09 17:10:29 +0100791 dataptr = update_log_hdr(p && p->log_tag ? p->log_tag : global.log_tag); /* update log header and skip it */
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100792 data_len = dataptr - logline;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100793
794 va_start(argp, format);
Willy Tarreau18324f52014-06-27 18:10:07 +0200795 data_len += vsnprintf(dataptr, logline + global.max_syslog_len - dataptr, format, argp);
796 if (data_len < 0 || data_len > global.max_syslog_len)
797 data_len = global.max_syslog_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200798 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100799
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100800 __send_log(p, level, logline, data_len);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100801}
802
803/*
804 * This function sends a syslog message.
805 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100806 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100807 */
808void __send_log(struct proxy *p, int level, char *message, size_t size)
809{
810 static int logfdunix = -1; /* syslog to AF_UNIX socket */
811 static int logfdinet = -1; /* syslog to AF_INET socket */
812 static char *dataptr = NULL;
813 int fac_level;
814 struct list *logsrvs = NULL;
815 struct logsrv *tmp = NULL;
816 int nblogger;
817 char *log_ptr;
818
819 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200820
821 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200822 if (!LIST_ISEMPTY(&global.logsrvs)) {
823 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200824 }
825 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200826 if (!LIST_ISEMPTY(&p->logsrvs)) {
827 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100829 }
830
William Lallemand0f99e342011-10-12 17:50:54 +0200831 if (!logsrvs)
832 return;
833
Robert Tsai81ae1952007-12-05 10:47:29 +0100834 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200835 nblogger = 0;
836 list_for_each_entry(tmp, logsrvs, list) {
837 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100838 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100839 &logfdunix : &logfdinet;
840 int sent;
Willy Tarreau18324f52014-06-27 18:10:07 +0200841 int max;
842 char backup;
Robert Tsai81ae1952007-12-05 10:47:29 +0100843
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200844 nblogger++;
845
Willy Tarreaubaaee002006-06-26 02:48:02 +0200846 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200847 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848 continue;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100849
Willy Tarreauc7c7be22014-06-23 18:07:15 +0200850 if (unlikely(*plogfd < 0)) {
851 /* socket not successfully initialized yet */
852 int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP;
853
854 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) {
855 Alert("socket for logger #%d failed: %s (errno=%d)\n",
856 nblogger, strerror(errno), errno);
857 continue;
858 }
859 /* we don't want to receive anything on this socket */
860 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
861 /* does nothing under Linux, maybe needed for others */
862 shutdown(*plogfd, SHUT_RD);
863 }
864
Willy Tarreaubaaee002006-06-26 02:48:02 +0200865 /* For each target, we may have a different facility.
866 * We can also have a different log level for each message.
867 * This induces variations in the message header length.
868 * Since we don't want to recompute it each time, nor copy it every
869 * time, we only change the facility in the pre-computed header,
870 * and we change the pointer to the header accordingly.
871 */
William Lallemand0f99e342011-10-12 17:50:54 +0200872 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100873 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200874 do {
875 *log_ptr = '0' + fac_level % 10;
876 fac_level /= 10;
877 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100878 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200879 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100880
Willy Tarreau18324f52014-06-27 18:10:07 +0200881 max = size - (log_ptr - dataptr);
882 if (max > logsrv->maxlen)
883 max = logsrv->maxlen;
884
885 /* insert a \n at the end of the message, but save what was
886 * there first because we could have different max lengths
887 * for different log targets.
888 */
889 backup = log_ptr[max - 1];
890 log_ptr[max - 1] = '\n';
891
892 sent = sendto(*plogfd, log_ptr, max,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200893 MSG_DONTWAIT | MSG_NOSIGNAL,
894 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Willy Tarreau18324f52014-06-27 18:10:07 +0200895
896 log_ptr[max - 1] = backup;
897
Robert Tsai81ae1952007-12-05 10:47:29 +0100898 if (sent < 0) {
899 Alert("sendto logger #%d failed: %s (errno=%d)\n",
900 nblogger, strerror(errno), errno);
901 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200902 }
903}
904
William Lallemandbddd4fd2012-02-27 11:23:10 +0100905extern fd_set hdr_encode_map[];
906extern fd_set url_encode_map[];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100907extern fd_set http_encode_map[];
William Lallemandbddd4fd2012-02-27 11:23:10 +0100908
Willy Tarreaubaaee002006-06-26 02:48:02 +0200909
Willy Tarreauc89ccb62012-04-05 21:18:22 +0200910const 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 +0100911const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
912 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
913 Set-cookie Updated, unknown, unknown */
914
William Lallemand1d705562012-03-12 12:46:41 +0100915/*
916 * try to write a character if there is enough space, or goto out
917 */
William Lallemandbddd4fd2012-02-27 11:23:10 +0100918#define LOGCHAR(x) do { \
William Lallemand1d705562012-03-12 12:46:41 +0100919 if (tmplog < dst + maxsize - 1) { \
William Lallemandbddd4fd2012-02-27 11:23:10 +0100920 *(tmplog++) = (x); \
921 } else { \
922 goto out; \
923 } \
924 } while(0)
925
William Lallemand1d705562012-03-12 12:46:41 +0100926
Willy Tarreaudf974472012-12-28 02:44:01 +0100927/* Builds a log line in <dst> based on <list_format>, and stops before reaching
928 * <maxsize> characters. Returns the size of the output string in characters,
929 * not counting the trailing zero which is always added if the resulting size
930 * is not zero.
931 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200932int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200934 struct session *sess = strm_sess(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200935 struct proxy *fe = sess->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100936 struct proxy *be = s->be;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200937 struct http_txn *txn = s->txn;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000938 struct chunk chunk;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100939 char *uri;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000940 char *spc;
Andrew Hayworthe63ac872015-07-31 16:14:16 +0000941 char *qmark;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000942 char *end;
Willy Tarreaufe944602007-10-25 10:34:16 +0200943 struct tm tm;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100944 int t_request;
945 int hdr;
946 int last_isspace = 1;
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +0000947 int nspaces = 0;
Willy Tarreaub1a2faf2012-03-19 16:51:53 +0100948 char *tmplog;
William Lallemand1d705562012-03-12 12:46:41 +0100949 char *ret;
950 int iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100951 struct logformat_node *tmp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952
William Lallemandbddd4fd2012-02-27 11:23:10 +0100953 /* FIXME: let's limit ourselves to frontend logging for now. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200954
William Lallemandbddd4fd2012-02-27 11:23:10 +0100955 t_request = -1;
956 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
957 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
958
William Lallemand1d705562012-03-12 12:46:41 +0100959 tmplog = dst;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200960
William Lallemandbddd4fd2012-02-27 11:23:10 +0100961 /* fill logbuffer */
William Lallemand1d705562012-03-12 12:46:41 +0100962 if (LIST_ISEMPTY(list_format))
963 return 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100964
William Lallemand1d705562012-03-12 12:46:41 +0100965 list_for_each_entry(tmp, list_format, list) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200966 struct connection *conn;
Willy Tarreau4f653562012-10-12 19:48:16 +0200967 const char *src = NULL;
Willy Tarreauc8368452012-12-21 00:09:23 +0100968 struct sample *key;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100969 const struct chunk empty = { NULL, 0, 0 };
William Lallemandbddd4fd2012-02-27 11:23:10 +0100970
Willy Tarreauc8368452012-12-21 00:09:23 +0100971 switch (tmp->type) {
William Lallemand1d705562012-03-12 12:46:41 +0100972 case LOG_FMT_SEPARATOR:
William Lallemandbddd4fd2012-02-27 11:23:10 +0100973 if (!last_isspace) {
974 LOGCHAR(' ');
975 last_isspace = 1;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100976 }
977 break;
978
William Lallemand1d705562012-03-12 12:46:41 +0100979 case LOG_FMT_TEXT: // text
William Lallemandbddd4fd2012-02-27 11:23:10 +0100980 src = tmp->arg;
William Lallemand5f232402012-04-05 18:02:55 +0200981 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +0100982 if (iret == 0)
William Lallemandbddd4fd2012-02-27 11:23:10 +0100983 goto out;
William Lallemand1d705562012-03-12 12:46:41 +0100984 tmplog += iret;
William Lallemandbddd4fd2012-02-27 11:23:10 +0100985 last_isspace = 0;
986 break;
987
Willy Tarreauc8368452012-12-21 00:09:23 +0100988 case LOG_FMT_EXPR: // sample expression, may be request or response
989 key = NULL;
990 if (tmp->options & LOG_OPT_REQ_CAP)
Adis Nezirovic79beb242015-07-06 15:41:02 +0200991 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Willy Tarreauc8368452012-12-21 00:09:23 +0100992 if (!key && (tmp->options & LOG_OPT_RES_CAP))
Adis Nezirovic79beb242015-07-06 15:41:02 +0200993 key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100994 if (tmp->options & LOG_OPT_HTTP)
995 ret = encode_chunk(tmplog, dst + maxsize,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200996 '%', http_encode_map, key ? &key->data.u.str : &empty);
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100997 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200998 ret = lf_text_len(tmplog, key ? key->data.u.str.str : NULL, key ? key->data.u.str.len : 0, dst + maxsize - tmplog, tmp);
Willy Tarreauc8368452012-12-21 00:09:23 +0100999 if (ret == 0)
1000 goto out;
1001 tmplog = ret;
1002 last_isspace = 0;
1003 break;
1004
Willy Tarreau2beef582012-12-20 17:22:52 +01001005 case LOG_FMT_CLIENTIP: // %ci
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001006 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001007 if (conn)
1008 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1009 else
1010 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001011 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001012 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001013 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001014 last_isspace = 0;
1015 break;
1016
Willy Tarreau2beef582012-12-20 17:22:52 +01001017 case LOG_FMT_CLIENTPORT: // %cp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001018 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001019 if (conn) {
1020 if (conn->addr.from.ss_family == AF_UNIX) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001021 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001022 } else {
1023 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from,
1024 dst + maxsize - tmplog, tmp);
1025 }
William Lallemand5f232402012-04-05 18:02:55 +02001026 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001027 else
1028 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1029
William Lallemand5f232402012-04-05 18:02:55 +02001030 if (ret == NULL)
1031 goto out;
1032 tmplog = ret;
1033 last_isspace = 0;
1034 break;
1035
Willy Tarreau2beef582012-12-20 17:22:52 +01001036 case LOG_FMT_FRONTENDIP: // %fi
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001037 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001038 if (conn) {
1039 conn_get_to_addr(conn);
1040 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1041 }
1042 else
1043 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1044
William Lallemand1d705562012-03-12 12:46:41 +01001045 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001046 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001047 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001048 last_isspace = 0;
1049 break;
1050
Willy Tarreau2beef582012-12-20 17:22:52 +01001051 case LOG_FMT_FRONTENDPORT: // %fp
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001052 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001053 if (conn) {
1054 conn_get_to_addr(conn);
1055 if (conn->addr.to.ss_family == AF_UNIX)
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001056 ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001057 else
1058 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
William Lallemand5f232402012-04-05 18:02:55 +02001059 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001060 else
1061 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1062
William Lallemand5f232402012-04-05 18:02:55 +02001063 if (ret == NULL)
1064 goto out;
1065 tmplog = ret;
1066 last_isspace = 0;
1067 break;
1068
Willy Tarreau2beef582012-12-20 17:22:52 +01001069 case LOG_FMT_BACKENDIP: // %bi
Willy Tarreau350f4872014-11-28 14:42:25 +01001070 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001071 if (conn)
1072 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1073 else
1074 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1075
William Lallemand1d705562012-03-12 12:46:41 +01001076 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001077 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001078 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001079 last_isspace = 0;
1080 break;
1081
Willy Tarreau2beef582012-12-20 17:22:52 +01001082 case LOG_FMT_BACKENDPORT: // %bp
Willy Tarreau350f4872014-11-28 14:42:25 +01001083 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001084 if (conn)
1085 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp);
1086 else
1087 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1088
William Lallemand5f232402012-04-05 18:02:55 +02001089 if (ret == NULL)
1090 goto out;
1091 tmplog = ret;
1092 last_isspace = 0;
1093 break;
1094
Willy Tarreau2beef582012-12-20 17:22:52 +01001095 case LOG_FMT_SERVERIP: // %si
Willy Tarreau350f4872014-11-28 14:42:25 +01001096 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001097 if (conn)
1098 ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1099 else
1100 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1101
William Lallemand5f232402012-04-05 18:02:55 +02001102 if (ret == NULL)
1103 goto out;
1104 tmplog = ret;
1105 last_isspace = 0;
1106 break;
1107
Willy Tarreau2beef582012-12-20 17:22:52 +01001108 case LOG_FMT_SERVERPORT: // %sp
Willy Tarreau350f4872014-11-28 14:42:25 +01001109 conn = objt_conn(s->si[1].end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001110 if (conn)
1111 ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp);
1112 else
1113 ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp);
1114
William Lallemand1d705562012-03-12 12:46:41 +01001115 if (ret == NULL)
William Lallemandb7ff6a32012-03-02 14:35:21 +01001116 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001117 tmplog = ret;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001118 last_isspace = 0;
1119 break;
1120
William Lallemand1d705562012-03-12 12:46:41 +01001121 case LOG_FMT_DATE: // %t
William Lallemandbddd4fd2012-02-27 11:23:10 +01001122 get_localtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001123 ret = date2str_log(tmplog, &tm, &(s->logs.accept_date),
1124 dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001125 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001126 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001127 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001128 last_isspace = 0;
1129 break;
1130
William Lallemand1d705562012-03-12 12:46:41 +01001131 case LOG_FMT_DATEGMT: // %T
William Lallemandbddd4fd2012-02-27 11:23:10 +01001132 get_gmtime(s->logs.accept_date.tv_sec, &tm);
William Lallemand5f232402012-04-05 18:02:55 +02001133 ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001134 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001135 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001136 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001137 last_isspace = 0;
1138 break;
1139
Yuxans Yao4e25b012012-10-19 10:36:09 +08001140 case LOG_FMT_DATELOCAL: // %Tl
1141 get_localtime(s->logs.accept_date.tv_sec, &tm);
1142 ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
1143 if (ret == NULL)
1144 goto out;
1145 tmplog = ret;
1146 last_isspace = 0;
1147 break;
1148
William Lallemand5f232402012-04-05 18:02:55 +02001149 case LOG_FMT_TS: // %Ts
1150 get_gmtime(s->logs.accept_date.tv_sec, &tm);
1151 if (tmp->options & LOG_OPT_HEXA) {
1152 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec);
1153 if (iret < 0 || iret > dst + maxsize - tmplog)
1154 goto out;
1155 last_isspace = 0;
1156 tmplog += iret;
1157 } else {
1158 ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog);
1159 if (ret == NULL)
1160 goto out;
1161 tmplog = ret;
1162 last_isspace = 0;
1163 }
1164 break;
1165
William Lallemand1d705562012-03-12 12:46:41 +01001166 case LOG_FMT_MS: // %ms
William Lallemand5f232402012-04-05 18:02:55 +02001167 if (tmp->options & LOG_OPT_HEXA) {
1168 iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000);
1169 if (iret < 0 || iret > dst + maxsize - tmplog)
1170 goto out;
1171 last_isspace = 0;
1172 tmplog += iret;
1173 } else {
1174 if ((dst + maxsize - tmplog) < 4)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001175 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001176 ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000,
1177 tmplog, 4);
1178 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001179 goto out;
Willy Tarreau9e60cd82013-01-24 01:18:16 +01001180 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001181 last_isspace = 0;
William Lallemand5f232402012-04-05 18:02:55 +02001182 }
1183 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001184
William Lallemand1d705562012-03-12 12:46:41 +01001185 case LOG_FMT_FRONTEND: // %f
William Lallemandbddd4fd2012-02-27 11:23:10 +01001186 src = fe->id;
William Lallemand5f232402012-04-05 18:02:55 +02001187 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001188 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001189 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001190 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001191 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001192 break;
1193
Willy Tarreau773d65f2012-10-12 14:56:11 +02001194 case LOG_FMT_FRONTEND_XPRT: // %ft
1195 src = fe->id;
1196 if (tmp->options & LOG_OPT_QUOTE)
1197 LOGCHAR('"');
1198 iret = strlcpy2(tmplog, src, dst + maxsize - tmplog);
1199 if (iret == 0)
1200 goto out;
1201 tmplog += iret;
1202#ifdef USE_OPENSSL
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001203 if (sess->listener->xprt == &ssl_sock)
Willy Tarreau773d65f2012-10-12 14:56:11 +02001204 LOGCHAR('~');
1205#endif
1206 if (tmp->options & LOG_OPT_QUOTE)
1207 LOGCHAR('"');
1208 last_isspace = 0;
1209 break;
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001210#ifdef USE_OPENSSL
1211 case LOG_FMT_SSL_CIPHER: // %sslc
1212 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001213 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001214 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001215 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001216 src = ssl_sock_get_cipher_name(conn);
1217 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001218 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1219 if (ret == NULL)
1220 goto out;
1221 tmplog = ret;
1222 last_isspace = 0;
1223 break;
Willy Tarreau773d65f2012-10-12 14:56:11 +02001224
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001225 case LOG_FMT_SSL_VERSION: // %sslv
1226 src = NULL;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001227 conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001228 if (conn) {
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001229 if (sess->listener->xprt == &ssl_sock)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001230 src = ssl_sock_get_proto_version(conn);
1231 }
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02001232 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1233 if (ret == NULL)
1234 goto out;
1235 tmplog = ret;
1236 last_isspace = 0;
1237 break;
1238#endif
William Lallemand1d705562012-03-12 12:46:41 +01001239 case LOG_FMT_BACKEND: // %b
William Lallemandbddd4fd2012-02-27 11:23:10 +01001240 src = be->id;
William Lallemand5f232402012-04-05 18:02:55 +02001241 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001242 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001243 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001244 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001245 last_isspace = 0;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001246 break;
1247
William Lallemand1d705562012-03-12 12:46:41 +01001248 case LOG_FMT_SERVER: // %s
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001249 switch (obj_type(s->target)) {
1250 case OBJ_TYPE_SERVER:
1251 src = objt_server(s->target)->id;
1252 break;
1253 case OBJ_TYPE_APPLET:
1254 src = objt_applet(s->target)->name;
1255 break;
1256 default:
1257 src = "<NOSRV>";
1258 break;
1259 }
William Lallemand5f232402012-04-05 18:02:55 +02001260 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001261 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001262 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001263 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001264 last_isspace = 0;
1265 break;
1266
William Lallemand1d705562012-03-12 12:46:41 +01001267 case LOG_FMT_TQ: // %Tq
William Lallemand5f232402012-04-05 18:02:55 +02001268 ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001269 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001270 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001271 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001272 last_isspace = 0;
1273 break;
1274
William Lallemand1d705562012-03-12 12:46:41 +01001275 case LOG_FMT_TW: // %Tw
1276 ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001277 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001278 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001279 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001280 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001281 last_isspace = 0;
1282 break;
1283
William Lallemand1d705562012-03-12 12:46:41 +01001284 case LOG_FMT_TC: // %Tc
1285 ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001286 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001287 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001288 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001289 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001290 last_isspace = 0;
1291 break;
1292
William Lallemand1d705562012-03-12 12:46:41 +01001293 case LOG_FMT_TR: // %Tr
1294 ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
William Lallemand5f232402012-04-05 18:02:55 +02001295 tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001296 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001297 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001298 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001299 last_isspace = 0;
1300 break;
1301
William Lallemand1d705562012-03-12 12:46:41 +01001302 case LOG_FMT_TT: // %Tt
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001303 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001304 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001305 ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001306 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001307 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001308 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001309 last_isspace = 0;
1310 break;
1311
Willy Tarreau2beef582012-12-20 17:22:52 +01001312 case LOG_FMT_STATUS: // %ST
William Lallemand5f232402012-04-05 18:02:55 +02001313 ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001314 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001315 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001316 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001317 last_isspace = 0;
1318 break;
1319
William Lallemand1d705562012-03-12 12:46:41 +01001320 case LOG_FMT_BYTES: // %B
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001321 if (!(fe->to_log & LW_BYTES))
William Lallemand1d705562012-03-12 12:46:41 +01001322 LOGCHAR('+');
William Lallemand5f232402012-04-05 18:02:55 +02001323 ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001324 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001325 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001326 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001327 last_isspace = 0;
1328 break;
1329
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001330 case LOG_FMT_BYTES_UP: // %U
Willy Tarreauc5259fd2012-12-20 15:38:04 +01001331 ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog);
1332 if (ret == NULL)
1333 goto out;
1334 tmplog = ret;
1335 last_isspace = 0;
1336 break;
1337
Willy Tarreau2beef582012-12-20 17:22:52 +01001338 case LOG_FMT_CCLIENT: // %CC
William Lallemandbddd4fd2012-02-27 11:23:10 +01001339 src = txn->cli_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001340 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001341 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001342 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
Willy Tarreau2beef582012-12-20 17:22:52 +01001347 case LOG_FMT_CSERVER: // %CS
William Lallemandbddd4fd2012-02-27 11:23:10 +01001348 src = txn->srv_cookie;
William Lallemand5f232402012-04-05 18:02:55 +02001349 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
William Lallemand1d705562012-03-12 12:46:41 +01001350 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001351 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001352 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001353 last_isspace = 0;
1354 break;
1355
William Lallemand1d705562012-03-12 12:46:41 +01001356 case LOG_FMT_TERMSTATE: // %ts
Willy Tarreaue7dff022015-04-03 01:14:29 +02001357 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1358 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau6580c062012-03-12 15:09:42 +01001359 *tmplog = '\0';
1360 last_isspace = 0;
1361 break;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001362
William Lallemand1d705562012-03-12 12:46:41 +01001363 case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001364 LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]);
1365 LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]);
Willy Tarreau67402132012-05-31 20:40:20 +02001366 LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-');
1367 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 +01001368 last_isspace = 0;
1369 break;
1370
William Lallemand1d705562012-03-12 12:46:41 +01001371 case LOG_FMT_ACTCONN: // %ac
William Lallemand5f232402012-04-05 18:02:55 +02001372 ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001373 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001374 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001375 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001376 last_isspace = 0;
1377 break;
1378
William Lallemand1d705562012-03-12 12:46:41 +01001379 case LOG_FMT_FECONN: // %fc
William Lallemand5f232402012-04-05 18:02:55 +02001380 ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001381 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001382 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001383 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001384 last_isspace = 0;
1385 break;
1386
William Lallemand1d705562012-03-12 12:46:41 +01001387 case LOG_FMT_BECONN: // %bc
William Lallemand5f232402012-04-05 18:02:55 +02001388 ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001389 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001390 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001391 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001392 last_isspace = 0;
1393 break;
1394
William Lallemand1d705562012-03-12 12:46:41 +01001395 case LOG_FMT_SRVCONN: // %sc
Willy Tarreau54a08d32012-11-12 01:14:56 +01001396 ret = ultoa_o(objt_server(s->target) ?
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001397 objt_server(s->target)->cur_sess :
William Lallemand5f232402012-04-05 18:02:55 +02001398 0, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001399 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001400 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001401 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001402 last_isspace = 0;
1403 break;
1404
William Lallemand1d705562012-03-12 12:46:41 +01001405 case LOG_FMT_RETRIES: // %rq
Willy Tarreaue7dff022015-04-03 01:14:29 +02001406 if (s->flags & SF_REDISP)
William Lallemand1d705562012-03-12 12:46:41 +01001407 LOGCHAR('+');
Willy Tarreau350f4872014-11-28 14:42:25 +01001408 ret = ltoa_o((s->si[1].conn_retries>0) ?
1409 (be->conn_retries - s->si[1].conn_retries) :
William Lallemand5f232402012-04-05 18:02:55 +02001410 be->conn_retries, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001411 if (ret == NULL)
William Lallemand51b5dca2012-03-26 17:52:55 +02001412 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_SRVQUEUE: // %sq
William Lallemand5f232402012-04-05 18:02:55 +02001418 ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001419 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001420 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001421 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001422 last_isspace = 0;
1423 break;
1424
William Lallemand1d705562012-03-12 12:46:41 +01001425 case LOG_FMT_BCKQUEUE: // %bq
William Lallemand5f232402012-04-05 18:02:55 +02001426 ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog);
William Lallemand1d705562012-03-12 12:46:41 +01001427 if (ret == NULL)
William Lallemandbddd4fd2012-02-27 11:23:10 +01001428 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001429 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001430 last_isspace = 0;
1431 break;
1432
William Lallemand1d705562012-03-12 12:46:41 +01001433 case LOG_FMT_HDRREQUEST: // %hr
William Lallemandbddd4fd2012-02-27 11:23:10 +01001434 /* request header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001435 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001436 if (tmp->options & LOG_OPT_QUOTE)
1437 LOGCHAR('"');
1438 LOGCHAR('{');
1439 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1440 if (hdr)
1441 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001442 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001443 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001444 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001445 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001446 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001447 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001448 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001449 }
1450 LOGCHAR('}');
William Lallemand51b5dca2012-03-26 17:52:55 +02001451 if (tmp->options & LOG_OPT_QUOTE)
1452 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001453 last_isspace = 0;
1454 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001455 break;
1456
William Lallemand1d705562012-03-12 12:46:41 +01001457 case LOG_FMT_HDRREQUESTLIST: // %hrl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001458 /* request header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001459 if (fe->nb_req_cap && s->req_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001460 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1461 if (hdr > 0)
1462 LOGCHAR(' ');
1463 if (tmp->options & LOG_OPT_QUOTE)
1464 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001465 if (s->req_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001466 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001467 '#', hdr_encode_map, s->req_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001468 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001469 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001470 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001471 } else if (!(tmp->options & LOG_OPT_QUOTE))
1472 LOGCHAR('-');
1473 if (tmp->options & LOG_OPT_QUOTE)
1474 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001475 last_isspace = 0;
1476 }
1477 }
1478 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001479
William Lallemand1d705562012-03-12 12:46:41 +01001480
1481 case LOG_FMT_HDRRESPONS: // %hs
William Lallemandbddd4fd2012-02-27 11:23:10 +01001482 /* response header */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001483 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001484 if (tmp->options & LOG_OPT_QUOTE)
1485 LOGCHAR('"');
1486 LOGCHAR('{');
1487 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1488 if (hdr)
1489 LOGCHAR('|');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001490 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001491 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001492 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001493 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001494 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001495 tmplog = ret;
William Lallemand51b5dca2012-03-26 17:52:55 +02001496 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001497 }
1498 LOGCHAR('}');
1499 last_isspace = 0;
1500 if (tmp->options & LOG_OPT_QUOTE)
1501 LOGCHAR('"');
1502 }
William Lallemandbddd4fd2012-02-27 11:23:10 +01001503 break;
1504
William Lallemand1d705562012-03-12 12:46:41 +01001505 case LOG_FMT_HDRRESPONSLIST: // %hsl
William Lallemandbddd4fd2012-02-27 11:23:10 +01001506 /* response header list */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001507 if (fe->nb_rsp_cap && s->res_cap) {
William Lallemandbddd4fd2012-02-27 11:23:10 +01001508 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1509 if (hdr > 0)
1510 LOGCHAR(' ');
1511 if (tmp->options & LOG_OPT_QUOTE)
1512 LOGCHAR('"');
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001513 if (s->res_cap[hdr] != NULL) {
William Lallemand1d705562012-03-12 12:46:41 +01001514 ret = encode_string(tmplog, dst + maxsize,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001515 '#', hdr_encode_map, s->res_cap[hdr]);
William Lallemand1d705562012-03-12 12:46:41 +01001516 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001517 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001518 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001519 } else if (!(tmp->options & LOG_OPT_QUOTE))
1520 LOGCHAR('-');
1521 if (tmp->options & LOG_OPT_QUOTE)
1522 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001523 last_isspace = 0;
1524 }
1525 }
1526 break;
1527
William Lallemand1d705562012-03-12 12:46:41 +01001528 case LOG_FMT_REQ: // %r
William Lallemandbddd4fd2012-02-27 11:23:10 +01001529 /* Request */
1530 if (tmp->options & LOG_OPT_QUOTE)
1531 LOGCHAR('"');
1532 uri = txn->uri ? txn->uri : "<BADREQ>";
William Lallemand1d705562012-03-12 12:46:41 +01001533 ret = encode_string(tmplog, dst + maxsize,
William Lallemandbddd4fd2012-02-27 11:23:10 +01001534 '#', url_encode_map, uri);
William Lallemand1d705562012-03-12 12:46:41 +01001535 if (ret == NULL || *ret != '\0')
William Lallemand51b5dca2012-03-26 17:52:55 +02001536 goto out;
William Lallemand1d705562012-03-12 12:46:41 +01001537 tmplog = ret;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001538 if (tmp->options & LOG_OPT_QUOTE)
1539 LOGCHAR('"');
William Lallemandbddd4fd2012-02-27 11:23:10 +01001540 last_isspace = 0;
1541 break;
William Lallemand5f232402012-04-05 18:02:55 +02001542
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001543 case LOG_FMT_HTTP_PATH: // %HP
1544 uri = txn->uri ? txn->uri : "<BADREQ>";
1545
Willy Tarreaub7636d12015-06-17 19:58:02 +02001546 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001547 LOGCHAR('"');
1548
1549 end = uri + strlen(uri);
1550 // look for the first whitespace character
1551 while (uri < end && !HTTP_IS_SPHT(*uri))
1552 uri++;
1553
1554 // keep advancing past multiple spaces
1555 while (uri < end && HTTP_IS_SPHT(*uri)) {
1556 uri++; nspaces++;
1557 }
1558
1559 // look for first space or question mark after url
1560 spc = uri;
1561 while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc))
1562 spc++;
1563
1564 if (!txn->uri || nspaces == 0) {
1565 chunk.str = "<BADREQ>";
1566 chunk.len = strlen("<BADREQ>");
1567 } else {
1568 chunk.str = uri;
1569 chunk.len = spc - uri;
1570 }
1571
1572 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1573 if (ret == NULL || *ret != '\0')
1574 goto out;
1575
1576 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001577 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001578 LOGCHAR('"');
1579
1580 last_isspace = 0;
1581 break;
1582
Andrew Hayworthe63ac872015-07-31 16:14:16 +00001583 case LOG_FMT_HTTP_QUERY: // %HQ
1584 if (tmp->options & LOG_OPT_QUOTE)
1585 LOGCHAR('"');
1586
1587 if (!txn->uri) {
1588 chunk.str = "<BADREQ>";
1589 chunk.len = strlen("<BADREQ>");
1590 } else {
1591 uri = txn->uri;
1592 end = uri + strlen(uri);
1593 // look for the first question mark
1594 while (uri < end && *uri != '?')
1595 uri++;
1596
1597 qmark = uri;
1598 // look for first space or question mark after url
1599 while (uri < end && !HTTP_IS_SPHT(*uri))
1600 uri++;
1601
1602 chunk.str = qmark;
1603 chunk.len = uri - qmark;
1604 }
1605
1606 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1607 if (ret == NULL || *ret != '\0')
1608 goto out;
1609
1610 tmplog = ret;
1611 if (tmp->options & LOG_OPT_QUOTE)
1612 LOGCHAR('"');
1613
1614 last_isspace = 0;
1615 break;
1616
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001617 case LOG_FMT_HTTP_URI: // %HU
1618 uri = txn->uri ? txn->uri : "<BADREQ>";
1619
Willy Tarreaub7636d12015-06-17 19:58:02 +02001620 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001621 LOGCHAR('"');
1622
1623 end = uri + strlen(uri);
1624 // look for the first whitespace character
1625 while (uri < end && !HTTP_IS_SPHT(*uri))
1626 uri++;
1627
1628 // keep advancing past multiple spaces
1629 while (uri < end && HTTP_IS_SPHT(*uri)) {
1630 uri++; nspaces++;
1631 }
1632
1633 // look for first space after url
1634 spc = uri;
1635 while (spc < end && !HTTP_IS_SPHT(*spc))
1636 spc++;
1637
1638 if (!txn->uri || nspaces == 0) {
1639 chunk.str = "<BADREQ>";
1640 chunk.len = strlen("<BADREQ>");
1641 } else {
1642 chunk.str = uri;
1643 chunk.len = spc - uri;
1644 }
1645
1646 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1647 if (ret == NULL || *ret != '\0')
1648 goto out;
1649
1650 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001651 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001652 LOGCHAR('"');
1653
1654 last_isspace = 0;
1655 break;
1656
1657 case LOG_FMT_HTTP_METHOD: // %HM
1658 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001659 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001660 LOGCHAR('"');
1661
1662 end = uri + strlen(uri);
1663 // look for the first whitespace character
1664 spc = uri;
1665 while (spc < end && !HTTP_IS_SPHT(*spc))
1666 spc++;
1667
1668 if (spc == end) { // odd case, we have txn->uri, but we only got a verb
1669 chunk.str = "<BADREQ>";
1670 chunk.len = strlen("<BADREQ>");
1671 } else {
1672 chunk.str = uri;
1673 chunk.len = spc - uri;
1674 }
1675
1676 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1677 if (ret == NULL || *ret != '\0')
1678 goto out;
1679
1680 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001681 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001682 LOGCHAR('"');
1683
1684 last_isspace = 0;
1685 break;
1686
1687 case LOG_FMT_HTTP_VERSION: // %HV
1688 uri = txn->uri ? txn->uri : "<BADREQ>";
Willy Tarreaub7636d12015-06-17 19:58:02 +02001689 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001690 LOGCHAR('"');
1691
1692 end = uri + strlen(uri);
1693 // look for the first whitespace character
1694 while (uri < end && !HTTP_IS_SPHT(*uri))
1695 uri++;
1696
1697 // keep advancing past multiple spaces
1698 while (uri < end && HTTP_IS_SPHT(*uri)) {
1699 uri++; nspaces++;
1700 }
1701
1702 // look for the next whitespace character
1703 while (uri < end && !HTTP_IS_SPHT(*uri))
1704 uri++;
1705
1706 // keep advancing past multiple spaces
1707 while (uri < end && HTTP_IS_SPHT(*uri))
1708 uri++;
1709
1710 if (!txn->uri || nspaces == 0) {
1711 chunk.str = "<BADREQ>";
1712 chunk.len = strlen("<BADREQ>");
1713 } else if (uri == end) {
1714 chunk.str = "HTTP/0.9";
1715 chunk.len = strlen("HTTP/0.9");
1716 } else {
1717 chunk.str = uri;
1718 chunk.len = end - uri;
1719 }
1720
1721 ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk);
1722 if (ret == NULL || *ret != '\0')
1723 goto out;
1724
1725 tmplog = ret;
Willy Tarreaub7636d12015-06-17 19:58:02 +02001726 if (tmp->options & LOG_OPT_QUOTE)
Andrew Hayworth0ebc55f2015-04-27 21:37:03 +00001727 LOGCHAR('"');
1728
1729 last_isspace = 0;
1730 break;
1731
William Lallemand5f232402012-04-05 18:02:55 +02001732 case LOG_FMT_COUNTER: // %rt
1733 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001734 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id);
William Lallemand5f232402012-04-05 18:02:55 +02001735 if (iret < 0 || iret > dst + maxsize - tmplog)
1736 goto out;
1737 last_isspace = 0;
1738 tmplog += iret;
1739 } else {
Willy Tarreau1f0da242014-01-25 11:01:50 +01001740 ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog);
William Lallemand5f232402012-04-05 18:02:55 +02001741 if (ret == NULL)
1742 goto out;
1743 tmplog = ret;
1744 last_isspace = 0;
1745 }
1746 break;
1747
Willy Tarreau7346acb2014-08-28 15:03:15 +02001748 case LOG_FMT_LOGCNT: // %lc
1749 if (tmp->options & LOG_OPT_HEXA) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001750 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001751 if (iret < 0 || iret > dst + maxsize - tmplog)
1752 goto out;
1753 last_isspace = 0;
1754 tmplog += iret;
1755 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001756 ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog);
Willy Tarreau7346acb2014-08-28 15:03:15 +02001757 if (ret == NULL)
1758 goto out;
1759 tmplog = ret;
1760 last_isspace = 0;
1761 }
1762 break;
1763
William Lallemand5f232402012-04-05 18:02:55 +02001764 case LOG_FMT_HOSTNAME: // %H
1765 src = hostname;
1766 ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp);
1767 if (ret == NULL)
1768 goto out;
1769 tmplog = ret;
1770 last_isspace = 0;
1771 break;
1772
1773 case LOG_FMT_PID: // %pid
1774 if (tmp->options & LOG_OPT_HEXA) {
1775 iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid);
1776 if (iret < 0 || iret > dst + maxsize - tmplog)
1777 goto out;
1778 last_isspace = 0;
1779 tmplog += iret;
1780 } else {
1781 ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog);
1782 if (ret == NULL)
1783 goto out;
1784 tmplog = ret;
1785 last_isspace = 0;
1786 }
1787 break;
William Lallemanda73203e2012-03-12 12:48:57 +01001788
1789 case LOG_FMT_UNIQUEID: // %ID
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001790 ret = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001791 src = s->unique_id;
Thierry FOURNIER1be69102014-04-15 01:38:48 +02001792 ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp);
William Lallemanda73203e2012-03-12 12:48:57 +01001793 if (ret == NULL)
1794 goto out;
1795 tmplog = ret;
1796 last_isspace = 0;
1797 break;
1798
William Lallemandbddd4fd2012-02-27 11:23:10 +01001799 }
1800 }
1801
1802out:
William Lallemand1d705562012-03-12 12:46:41 +01001803 /* *tmplog is a unused character */
1804 *tmplog = '\0';
Willy Tarreaudf974472012-12-28 02:44:01 +01001805 return tmplog - dst;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001806
Willy Tarreaubaaee002006-06-26 02:48:02 +02001807}
1808
William Lallemand1d705562012-03-12 12:46:41 +01001809/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001810 * send a log for the stream when we have enough info about it.
William Lallemand1d705562012-03-12 12:46:41 +01001811 * Will not log if the frontend has no log defined.
1812 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001813void strm_log(struct stream *s)
William Lallemand1d705562012-03-12 12:46:41 +01001814{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001815 struct session *sess = s->sess;
William Lallemand1d705562012-03-12 12:46:41 +01001816 char *tmplog;
1817 int size, err, level;
1818
1819 /* if we don't want to log normal traffic, return now */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001820 err = (s->flags & SF_REDISP) ||
1821 ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) ||
1822 (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) &&
Willy Tarreau350f4872014-11-28 14:42:25 +01001823 (s->si[1].conn_retries != s->be->conn_retries)) ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02001824 ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001825
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001826 if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM))
William Lallemand1d705562012-03-12 12:46:41 +01001827 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001828
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001829 if (LIST_ISEMPTY(&sess->fe->logsrvs))
William Lallemand1d705562012-03-12 12:46:41 +01001830 return;
William Lallemandbddd4fd2012-02-27 11:23:10 +01001831
Willy Tarreauabcd5142013-06-11 17:18:02 +02001832 if (s->logs.level) { /* loglevel was overridden */
1833 if (s->logs.level == -1) {
1834 s->logs.logwait = 0; /* logs disabled */
1835 return;
1836 }
1837 level = s->logs.level - 1;
1838 }
1839 else {
1840 level = LOG_INFO;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001841 if (err && (sess->fe->options2 & PR_O2_LOGERRORS))
Willy Tarreauabcd5142013-06-11 17:18:02 +02001842 level = LOG_ERR;
1843 }
William Lallemand1d705562012-03-12 12:46:41 +01001844
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001845 /* if unique-id was not generated */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001846 if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001847 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001848 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02001849 }
1850
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001851 tmplog = update_log_hdr(sess->fe->log_tag ? sess->fe->log_tag : global.log_tag);
William Lallemand1d705562012-03-12 12:46:41 +01001852 size = tmplog - logline;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001853 size += build_logline(s, tmplog, global.max_syslog_len - size, &sess->fe->logformat);
William Lallemand1d705562012-03-12 12:46:41 +01001854 if (size > 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001855 sess->fe->log_count++;
1856 __send_log(sess->fe, level, logline, size + 1);
William Lallemand1d705562012-03-12 12:46:41 +01001857 s->logs.logwait = 0;
1858 }
1859}
William Lallemandbddd4fd2012-02-27 11:23:10 +01001860
Willy Tarreaubaaee002006-06-26 02:48:02 +02001861/*
1862 * Local variables:
1863 * c-indent-level: 8
1864 * c-basic-offset: 8
1865 * End:
1866 */