Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * General logging functions. |
| 3 | * |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 14 | #include <fcntl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 15 | #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 Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 22 | #include <errno.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | |
| 24 | #include <sys/time.h> |
Willy Tarreau | 077edcb | 2016-08-10 18:30:56 +0200 | [diff] [blame] | 25 | #include <sys/uio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 27 | #include <common/config.h> |
Willy Tarreau | d6d0690 | 2009-08-19 11:22:33 +0200 | [diff] [blame] | 28 | #include <common/compat.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 29 | #include <common/standard.h> |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 30 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <types/global.h> |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 33 | #include <types/log.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 34 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 35 | #include <proto/frontend.h> |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 36 | #include <proto/proto_http.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 37 | #include <proto/log.h> |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 38 | #include <proto/sample.h> |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 39 | #include <proto/stream.h> |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 40 | #include <proto/stream_interface.h> |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 41 | #ifdef USE_OPENSSL |
| 42 | #include <proto/ssl_sock.h> |
| 43 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 45 | struct log_fmt { |
| 46 | char *name; |
| 47 | struct { |
| 48 | struct chunk sep1; /* first pid separator */ |
| 49 | struct chunk sep2; /* second pid separator */ |
| 50 | } pid; |
| 51 | }; |
| 52 | |
| 53 | static const struct log_fmt log_formats[LOG_FORMATS] = { |
| 54 | [LOG_FORMAT_RFC3164] = { |
| 55 | .name = "rfc3164", |
| 56 | .pid = { |
| 57 | .sep1 = { .str = "[", .len = 1 }, |
| 58 | .sep2 = { .str = "]: ", .len = 3 } |
| 59 | } |
| 60 | }, |
| 61 | [LOG_FORMAT_RFC5424] = { |
| 62 | .name = "rfc5424", |
| 63 | .pid = { |
| 64 | .sep1 = { .str = " ", .len = 1 }, |
| 65 | .sep2 = { .str = " - ", .len = 3 } |
| 66 | } |
| 67 | } |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 70 | #define FD_SETS_ARE_BITFIELDS |
| 71 | #ifdef FD_SETS_ARE_BITFIELDS |
| 72 | /* |
| 73 | * This map is used with all the FD_* macros to check whether a particular bit |
| 74 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 75 | * which should be escaped. When FD_ISSET() returns non-zero, it means that the |
| 76 | * byte should be escaped. Be careful to always pass bytes from 0 to 255 |
| 77 | * exclusively to the macros. |
| 78 | */ |
| 79 | fd_set rfc5424_escape_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 80 | |
| 81 | #else |
| 82 | #error "Check if your OS uses bitfields for fd_sets" |
| 83 | #endif |
| 84 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 85 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 86 | "kern", "user", "mail", "daemon", |
| 87 | "auth", "syslog", "lpr", "news", |
| 88 | "uucp", "cron", "auth2", "ftp", |
| 89 | "ntp", "audit", "alert", "cron2", |
| 90 | "local0", "local1", "local2", "local3", |
| 91 | "local4", "local5", "local6", "local7" |
| 92 | }; |
| 93 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | const char *log_levels[NB_LOG_LEVELS] = { |
| 95 | "emerg", "alert", "crit", "err", |
| 96 | "warning", "notice", "info", "debug" |
| 97 | }; |
| 98 | |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 99 | const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */ |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 100 | const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 102 | |
| 103 | /* log_format */ |
| 104 | struct logformat_type { |
| 105 | char *name; |
| 106 | int type; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 107 | int mode; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 108 | int lw; /* logwait bitsfield */ |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 109 | int (*config_callback)(struct logformat_node *node, struct proxy *curproxy); |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 110 | const char *replace_by; /* new option to use instead of old one */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 111 | }; |
| 112 | |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 113 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy); |
| 114 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 115 | /* log_format variable names */ |
| 116 | static const struct logformat_type logformat_keywords[] = { |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 117 | { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 118 | |
| 119 | /* please keep these lines sorted ! */ |
| 120 | { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */ |
| 121 | { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */ |
| 122 | { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */ |
| 123 | { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */ |
| 124 | { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */ |
Willy Tarreau | 4bf9963 | 2014-06-13 12:21:40 +0200 | [diff] [blame] | 125 | { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 126 | { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 127 | { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 128 | { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 129 | { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */ |
| 130 | { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */ |
| 131 | { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */ |
| 132 | { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */ |
| 133 | { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */ |
| 134 | { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */ |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 135 | { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 136 | { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 137 | { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 138 | { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */ |
| 139 | { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 140 | { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 141 | { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 142 | { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 143 | { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */ |
| 144 | { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 145 | { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 146 | { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */ |
| 147 | { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */ |
| 148 | { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */ |
| 149 | { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */ |
| 150 | { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */ |
| 151 | { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */ |
| 152 | { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */ |
Willy Tarreau | d9ed3d2 | 2014-06-13 12:23:06 +0200 | [diff] [blame] | 153 | { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */ |
| 154 | { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */ |
| 155 | { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */ |
| 156 | { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */ |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 157 | { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */ |
| 158 | { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */ |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 159 | { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */ |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 160 | { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */ |
| 161 | { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */ |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 162 | { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 163 | { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 164 | { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 165 | { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */ |
| 166 | { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */ |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 167 | { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 168 | { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */ |
| 169 | { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */ |
| 170 | { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */ |
| 171 | { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */ |
| 172 | { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 173 | { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */ |
| 174 | { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 175 | { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 176 | { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */ |
| 177 | { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */ |
| 178 | { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 179 | { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */ |
| 180 | { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */ |
| 181 | |
| 182 | /* The following tags are deprecated and will be removed soon */ |
| 183 | { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */ |
| 184 | { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */ |
| 185 | { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */ |
| 186 | { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */ |
| 187 | { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */ |
| 188 | { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */ |
| 189 | { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */ |
| 190 | { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */ |
| 191 | { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */ |
| 192 | { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */ |
| 193 | { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 194 | { 0, 0, 0, 0, NULL } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 195 | }; |
| 196 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 197 | char default_http_log_format[] = "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format |
| 198 | char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%trg] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl"; |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 199 | char default_tcp_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq"; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 200 | char *log_format = NULL; |
| 201 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 202 | /* Default string used for structured-data part in RFC5424 formatted |
| 203 | * syslog messages. |
| 204 | */ |
| 205 | char default_rfc5424_sd_log_format[] = "- "; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 206 | |
| 207 | /* This is a global syslog header, common to all outgoing messages in |
| 208 | * RFC3164 format. It begins with time-based part and is updated by |
| 209 | * update_log_hdr(). |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 210 | */ |
| 211 | char *logheader = NULL; |
| 212 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 213 | /* This is a global syslog header for messages in RFC5424 format. It is |
| 214 | * updated by update_log_hdr_rfc5424(). |
| 215 | */ |
| 216 | char *logheader_rfc5424 = NULL; |
| 217 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 218 | /* This is a global syslog message buffer, common to all outgoing |
| 219 | * messages. It contains only the data part. |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 220 | */ |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 221 | char *logline = NULL; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 222 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 223 | /* A global syslog message buffer, common to all RFC5424 syslog messages. |
| 224 | * Currently, it is used for generating the structured-data part. |
| 225 | */ |
| 226 | char *logline_rfc5424 = NULL; |
| 227 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 228 | struct logformat_var_args { |
| 229 | char *name; |
| 230 | int mask; |
| 231 | }; |
| 232 | |
| 233 | struct logformat_var_args var_args_list[] = { |
| 234 | // global |
| 235 | { "M", LOG_OPT_MANDATORY }, |
| 236 | { "Q", LOG_OPT_QUOTE }, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 237 | { "X", LOG_OPT_HEXA }, |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 238 | { "E", LOG_OPT_ESC }, |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 239 | { 0, 0 } |
| 240 | }; |
| 241 | |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 242 | /* return the name of the directive used in the current proxy for which we're |
| 243 | * currently parsing a header, when it is known. |
| 244 | */ |
| 245 | static inline const char *fmt_directive(const struct proxy *curproxy) |
| 246 | { |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 247 | switch (curproxy->conf.args.ctx) { |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 248 | case ARGC_ACL: |
| 249 | return "acl"; |
| 250 | case ARGC_STK: |
| 251 | return "stick"; |
| 252 | case ARGC_TRK: |
| 253 | return "track-sc"; |
| 254 | case ARGC_LOG: |
| 255 | return "log-format"; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 256 | case ARGC_LOGSD: |
| 257 | return "log-format-sd"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 258 | case ARGC_HRQ: |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 259 | return "http-request"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 260 | case ARGC_HRS: |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 261 | return "http-response"; |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 262 | case ARGC_UIF: |
| 263 | return "unique-id-format"; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 264 | case ARGC_RDR: |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 265 | return "redirect"; |
| 266 | case ARGC_CAP: |
| 267 | return "capture"; |
Willy Tarreau | 28d976d | 2015-07-09 11:39:33 +0200 | [diff] [blame] | 268 | case ARGC_SRV: |
| 269 | return "server"; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 270 | case ARGC_SPOE: |
| 271 | return "spoe-message"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 272 | default: |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 273 | return "undefined(please report this bug)"; /* must never happen */ |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 274 | } |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 275 | } |
| 276 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 277 | /* |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 278 | * callback used to configure addr source retrieval |
| 279 | */ |
| 280 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy) |
| 281 | { |
| 282 | curproxy->options2 |= PR_O2_SRC_ADDR; |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /* |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 289 | * Parse args in a logformat_var |
| 290 | */ |
| 291 | int parse_logformat_var_args(char *args, struct logformat_node *node) |
| 292 | { |
| 293 | int i = 0; |
| 294 | int end = 0; |
| 295 | int flags = 0; // 1 = + 2 = - |
| 296 | char *sp = NULL; // start pointer |
| 297 | |
| 298 | if (args == NULL) |
| 299 | return 1; |
| 300 | |
| 301 | while (1) { |
| 302 | if (*args == '\0') |
| 303 | end = 1; |
| 304 | |
| 305 | if (*args == '+') { |
| 306 | // add flag |
| 307 | sp = args + 1; |
| 308 | flags = 1; |
| 309 | } |
| 310 | if (*args == '-') { |
| 311 | // delete flag |
| 312 | sp = args + 1; |
| 313 | flags = 2; |
| 314 | } |
| 315 | |
| 316 | if (*args == '\0' || *args == ',') { |
| 317 | *args = '\0'; |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 318 | for (i = 0; sp && var_args_list[i].name; i++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 319 | if (strcmp(sp, var_args_list[i].name) == 0) { |
| 320 | if (flags == 1) { |
| 321 | node->options |= var_args_list[i].mask; |
| 322 | break; |
| 323 | } else if (flags == 2) { |
| 324 | node->options &= ~var_args_list[i].mask; |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | sp = NULL; |
| 330 | if (end) |
| 331 | break; |
| 332 | } |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 333 | args++; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | /* |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 339 | * Parse a variable '%varname' or '%{args}varname' in log-format. The caller |
| 340 | * must pass the args part in the <arg> pointer with its length in <arg_len>, |
| 341 | * and varname with its length in <var> and <var_len> respectively. <arg> is |
| 342 | * ignored when arg_len is 0. Neither <var> nor <var_len> may be null. |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 343 | */ |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 344 | int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 345 | { |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 346 | int j; |
| 347 | struct logformat_node *node; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 348 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 349 | for (j = 0; logformat_keywords[j].name; j++) { // search a log type |
| 350 | if (strlen(logformat_keywords[j].name) == var_len && |
| 351 | strncmp(var, logformat_keywords[j].name, var_len) == 0) { |
| 352 | if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 353 | node = calloc(1, sizeof(*node)); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 354 | node->type = logformat_keywords[j].type; |
| 355 | node->options = *defoptions; |
| 356 | if (arg_len) { |
| 357 | node->arg = my_strndup(arg, arg_len); |
| 358 | parse_logformat_var_args(node->arg, node); |
| 359 | } |
| 360 | if (node->type == LOG_FMT_GLOBAL) { |
| 361 | *defoptions = node->options; |
| 362 | free(node->arg); |
| 363 | free(node); |
| 364 | } else { |
| 365 | if (logformat_keywords[j].config_callback && |
| 366 | logformat_keywords[j].config_callback(node, curproxy) != 0) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 367 | return -1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 368 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 369 | curproxy->to_log |= logformat_keywords[j].lw; |
| 370 | LIST_ADDQ(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 371 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 372 | if (logformat_keywords[j].replace_by) |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 373 | Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n", |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 374 | curproxy->conf.args.file, curproxy->conf.args.line, |
| 375 | logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 376 | return 0; |
| 377 | } else { |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 378 | Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n", |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 379 | curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), |
| 380 | logformat_keywords[j].name); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 381 | return -1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 382 | } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 383 | } |
| 384 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 385 | |
| 386 | j = var[var_len]; |
| 387 | var[var_len] = 0; |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 388 | 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 Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 389 | curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy)); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 390 | var[var_len] = j; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * push to the logformat linked list |
| 396 | * |
| 397 | * start: start pointer |
| 398 | * end: end text pointer |
| 399 | * type: string type |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 400 | * list_format: destination list |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 401 | * |
| 402 | * LOG_TEXT: copy chars from start to end excluding end. |
| 403 | * |
| 404 | */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 405 | void add_to_logformat_list(char *start, char *end, int type, struct list *list_format) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 406 | { |
| 407 | char *str; |
| 408 | |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 409 | if (type == LF_TEXT) { /* type text */ |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 410 | struct logformat_node *node = calloc(1, sizeof(*node)); |
| 411 | str = calloc(1, end - start + 1); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 412 | strncpy(str, start, end - start); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 413 | str[end - start] = '\0'; |
| 414 | node->arg = str; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 415 | node->type = LOG_FMT_TEXT; // type string |
| 416 | LIST_ADDQ(list_format, &node->list); |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 417 | } else if (type == LF_SEPARATOR) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 418 | struct logformat_node *node = calloc(1, sizeof(*node)); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 419 | node->type = LOG_FMT_SEPARATOR; |
| 420 | LIST_ADDQ(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | /* |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 425 | * Parse the sample fetch expression <text> and add a node to <list_format> upon |
| 426 | * success. At the moment, sample converters are not yet supported but fetch arguments |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 427 | * should work. The curpx->conf.args.ctx must be set by the caller. |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 428 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 429 | void 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 Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 430 | { |
| 431 | char *cmd[2]; |
| 432 | struct sample_expr *expr; |
| 433 | struct logformat_node *node; |
| 434 | int cmd_arg; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 435 | char *errmsg = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 436 | |
| 437 | cmd[0] = text; |
| 438 | cmd[1] = ""; |
| 439 | cmd_arg = 0; |
| 440 | |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 441 | expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 442 | if (!expr) { |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 443 | Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n", |
| 444 | curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx), |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 445 | text, errmsg); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 449 | node = calloc(1, sizeof(*node)); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 450 | node->type = LOG_FMT_EXPR; |
| 451 | node->expr = expr; |
| 452 | node->options = options; |
| 453 | |
| 454 | if (arg_len) { |
| 455 | node->arg = my_strndup(arg, arg_len); |
| 456 | parse_logformat_var_args(node->arg, node); |
| 457 | } |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 458 | if (expr->fetch->val & cap & SMP_VAL_REQUEST) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 459 | node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */ |
| 460 | |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 461 | if (expr->fetch->val & cap & SMP_VAL_RESPONSE) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 462 | node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */ |
| 463 | |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 464 | if (!(expr->fetch->val & cap)) |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 465 | Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n", |
| 466 | curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx), |
| 467 | text, sample_src_names(expr->fetch->use)); |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 468 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 469 | /* check if we need to allocate an hdr_idx struct for HTTP parsing */ |
| 470 | /* Note, we may also need to set curpx->to_log with certain fetches */ |
Willy Tarreau | 25320b2 | 2013-03-24 07:22:08 +0100 | [diff] [blame] | 471 | curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 472 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 473 | /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags |
| 474 | * needed with some sample fetches (eg: ssl*). We always set it for |
| 475 | * now on, but this will leave with sample capabilities soon. |
Willy Tarreau | 1f31c73 | 2013-01-10 16:22:27 +0100 | [diff] [blame] | 476 | */ |
| 477 | curpx->to_log |= LW_XPRT; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 478 | curpx->to_log |= LW_REQ; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 479 | LIST_ADDQ(list_format, &node->list); |
| 480 | } |
| 481 | |
| 482 | /* |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 483 | * Parse the log_format string and fill a linked list. |
| 484 | * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 485 | * You can set arguments using { } : %{many arguments}varname. |
| 486 | * The curproxy->conf.args.ctx must be set by the caller. |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 487 | * |
| 488 | * str: the string to parse |
| 489 | * curproxy: the proxy affected |
| 490 | * list_format: the destination list |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 491 | * options: LOG_OPT_* to force on every node |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 492 | * cap: all SMP_VAL_* flags supported by the consumer |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 493 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 494 | void parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, const char *file, int line) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 495 | { |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 496 | char *sp, *str, *backfmt; /* start pointer for text parts */ |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 497 | char *arg = NULL; /* start pointer for args */ |
| 498 | char *var = NULL; /* start pointer for vars */ |
| 499 | int arg_len = 0; |
| 500 | int var_len = 0; |
| 501 | int cformat; /* current token format */ |
| 502 | int pformat; /* previous token format */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 503 | struct logformat_node *tmplf, *back; |
| 504 | |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 505 | sp = str = backfmt = strdup(fmt); |
William Lallemand | 1dc00ef | 2012-08-09 16:41:35 +0200 | [diff] [blame] | 506 | curproxy->to_log |= LW_INIT; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 507 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 508 | /* flush the list first. */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 509 | list_for_each_entry_safe(tmplf, back, list_format, list) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 510 | LIST_DEL(&tmplf->list); |
| 511 | free(tmplf); |
| 512 | } |
| 513 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 514 | for (cformat = LF_INIT; cformat != LF_END; str++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 515 | pformat = cformat; |
| 516 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 517 | if (!*str) |
| 518 | cformat = LF_END; // preset it to save all states from doing this |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 519 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 520 | /* The prinicple of the two-step state machine below is to first detect a change, and |
| 521 | * second have all common paths processed at one place. The common paths are the ones |
| 522 | * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END). |
| 523 | * We use the common LF_INIT state to dispatch to the different final states. |
| 524 | */ |
| 525 | switch (pformat) { |
| 526 | case LF_STARTVAR: // text immediately following a '%' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 527 | arg = NULL; var = NULL; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 528 | arg_len = var_len = 0; |
| 529 | if (*str == '{') { // optional argument |
| 530 | cformat = LF_STARG; |
| 531 | arg = str + 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 532 | } |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 533 | else if (*str == '[') { |
| 534 | cformat = LF_STEXPR; |
| 535 | var = str + 1; // store expr in variable name |
| 536 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 537 | else if (isalpha((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 538 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 539 | var = str; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 540 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 541 | else if (*str == '%') |
| 542 | cformat = LF_TEXT; // convert this character to a litteral (useful for '%') |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 543 | else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') { |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 544 | /* single '%' followed by blank or digit, send them both */ |
| 545 | cformat = LF_TEXT; |
| 546 | pformat = LF_TEXT; /* finally we include the previous char as well */ |
| 547 | sp = str - 1; /* send both the '%' and the current char */ |
| 548 | 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", |
| 549 | curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt); |
| 550 | |
| 551 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 552 | else |
| 553 | cformat = LF_INIT; // handle other cases of litterals |
| 554 | break; |
| 555 | |
| 556 | case LF_STARG: // text immediately following '%{' |
| 557 | if (*str == '}') { // end of arg |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 558 | cformat = LF_EDARG; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 559 | arg_len = str - arg; |
| 560 | *str = 0; // used for reporting errors |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 561 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 562 | break; |
| 563 | |
| 564 | case LF_EDARG: // text immediately following '%{arg}' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 565 | if (*str == '[') { |
| 566 | cformat = LF_STEXPR; |
| 567 | var = str + 1; // store expr in variable name |
| 568 | break; |
| 569 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 570 | else if (isalnum((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 571 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 572 | var = str; |
| 573 | break; |
| 574 | } |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 575 | Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n", |
| 576 | curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 577 | cformat = LF_INIT; |
| 578 | break; |
| 579 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 580 | case LF_STEXPR: // text immediately following '%[' |
| 581 | if (*str == ']') { // end of arg |
| 582 | cformat = LF_EDEXPR; |
| 583 | var_len = str - var; |
| 584 | *str = 0; // needed for parsing the expression |
| 585 | } |
| 586 | break; |
| 587 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 588 | case LF_VAR: // text part of a variable name |
| 589 | var_len = str - var; |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 590 | if (!isalnum((unsigned char)*str)) |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 591 | cformat = LF_INIT; // not variable name anymore |
| 592 | break; |
| 593 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 594 | default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 595 | cformat = LF_INIT; |
| 596 | } |
| 597 | |
| 598 | if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */ |
| 599 | switch (*str) { |
| 600 | case '%': cformat = LF_STARTVAR; break; |
| 601 | case ' ': cformat = LF_SEPARATOR; break; |
| 602 | case 0 : cformat = LF_END; break; |
| 603 | default : cformat = LF_TEXT; break; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 607 | if (cformat != pformat || pformat == LF_SEPARATOR) { |
| 608 | switch (pformat) { |
| 609 | case LF_VAR: |
| 610 | parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options); |
| 611 | break; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 612 | case LF_STEXPR: |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 613 | add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 614 | break; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 615 | case LF_TEXT: |
| 616 | case LF_SEPARATOR: |
| 617 | add_to_logformat_list(sp, str, pformat, list_format); |
| 618 | break; |
| 619 | } |
| 620 | sp = str; /* new start of text at every state switch and at every separator */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 621 | } |
| 622 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 623 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 624 | if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 625 | Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n", |
| 626 | curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), |
| 627 | var ? var : arg ? arg : "%"); |
Willy Tarreau | 3ed22a4 | 2012-12-23 17:29:07 +0100 | [diff] [blame] | 628 | |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 629 | free(backfmt); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 632 | /* |
| 633 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 634 | * mode during startup. |
| 635 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 636 | void Alert(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 637 | { |
| 638 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 639 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 640 | |
| 641 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 642 | va_start(argp, fmt); |
| 643 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 644 | get_localtime(date.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 645 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 646 | tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid()); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 647 | vfprintf(stderr, fmt, argp); |
| 648 | fflush(stderr); |
| 649 | va_end(argp); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | |
| 654 | /* |
| 655 | * Displays the message on stderr with the date and pid. |
| 656 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 657 | void Warning(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 658 | { |
| 659 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 660 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 661 | |
| 662 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 663 | va_start(argp, fmt); |
| 664 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 665 | get_localtime(date.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 666 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 667 | tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid()); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 668 | vfprintf(stderr, fmt, argp); |
| 669 | fflush(stderr); |
| 670 | va_end(argp); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Displays the message on <out> only if quiet mode is not set. |
| 676 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 677 | void qfprintf(FILE *out, const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 678 | { |
| 679 | va_list argp; |
| 680 | |
| 681 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 682 | va_start(argp, fmt); |
| 683 | vfprintf(out, fmt, argp); |
| 684 | fflush(out); |
| 685 | va_end(argp); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 690 | * returns log format for <fmt> or -1 if not found. |
| 691 | */ |
| 692 | int get_log_format(const char *fmt) |
| 693 | { |
| 694 | int format; |
| 695 | |
| 696 | format = LOG_FORMATS - 1; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 697 | while (format >= 0 && strcmp(log_formats[format].name, fmt)) |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 698 | format--; |
| 699 | |
| 700 | return format; |
| 701 | } |
| 702 | |
| 703 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 704 | * returns log level for <lev> or -1 if not found. |
| 705 | */ |
| 706 | int get_log_level(const char *lev) |
| 707 | { |
| 708 | int level; |
| 709 | |
| 710 | level = NB_LOG_LEVELS - 1; |
| 711 | while (level >= 0 && strcmp(log_levels[level], lev)) |
| 712 | level--; |
| 713 | |
| 714 | return level; |
| 715 | } |
| 716 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 717 | /* |
| 718 | * returns log facility for <fac> or -1 if not found. |
| 719 | */ |
| 720 | int get_log_facility(const char *fac) |
| 721 | { |
| 722 | int facility; |
| 723 | |
| 724 | facility = NB_LOG_FACILITIES - 1; |
| 725 | while (facility >= 0 && strcmp(log_facilities[facility], fac)) |
| 726 | facility--; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 727 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 728 | return facility; |
| 729 | } |
| 730 | |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 731 | /* |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 732 | * Encode the string. |
| 733 | * |
| 734 | * When using the +E log format option, it will try to escape '"\]' |
| 735 | * characters with '\' as prefix. The same prefix should not be used as |
| 736 | * <escape>. |
| 737 | */ |
| 738 | static char *lf_encode_string(char *start, char *stop, |
| 739 | const char escape, const fd_set *map, |
| 740 | const char *string, |
| 741 | struct logformat_node *node) |
| 742 | { |
| 743 | if (node->options & LOG_OPT_ESC) { |
| 744 | if (start < stop) { |
| 745 | stop--; /* reserve one byte for the final '\0' */ |
| 746 | while (start < stop && *string != '\0') { |
| 747 | if (!FD_ISSET((unsigned char)(*string), map)) { |
| 748 | if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map)) |
| 749 | *start++ = *string; |
| 750 | else { |
| 751 | if (start + 2 >= stop) |
| 752 | break; |
| 753 | *start++ = '\\'; |
| 754 | *start++ = *string; |
| 755 | } |
| 756 | } |
| 757 | else { |
| 758 | if (start + 3 >= stop) |
| 759 | break; |
| 760 | *start++ = escape; |
| 761 | *start++ = hextab[(*string >> 4) & 15]; |
| 762 | *start++ = hextab[*string & 15]; |
| 763 | } |
| 764 | string++; |
| 765 | } |
| 766 | *start = '\0'; |
| 767 | } |
| 768 | } |
| 769 | else { |
| 770 | return encode_string(start, stop, escape, map, string); |
| 771 | } |
| 772 | |
| 773 | return start; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Encode the chunk. |
| 778 | * |
| 779 | * When using the +E log format option, it will try to escape '"\]' |
| 780 | * characters with '\' as prefix. The same prefix should not be used as |
| 781 | * <escape>. |
| 782 | */ |
| 783 | static char *lf_encode_chunk(char *start, char *stop, |
| 784 | const char escape, const fd_set *map, |
| 785 | const struct chunk *chunk, |
| 786 | struct logformat_node *node) |
| 787 | { |
| 788 | char *str, *end; |
| 789 | |
| 790 | if (node->options & LOG_OPT_ESC) { |
| 791 | if (start < stop) { |
| 792 | str = chunk->str; |
| 793 | end = chunk->str + chunk->len; |
| 794 | |
| 795 | stop--; /* reserve one byte for the final '\0' */ |
| 796 | while (start < stop && str < end) { |
| 797 | if (!FD_ISSET((unsigned char)(*str), map)) { |
| 798 | if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map)) |
| 799 | *start++ = *str; |
| 800 | else { |
| 801 | if (start + 2 >= stop) |
| 802 | break; |
| 803 | *start++ = '\\'; |
| 804 | *start++ = *str; |
| 805 | } |
| 806 | } |
| 807 | else { |
| 808 | if (start + 3 >= stop) |
| 809 | break; |
| 810 | *start++ = escape; |
| 811 | *start++ = hextab[(*str >> 4) & 15]; |
| 812 | *start++ = hextab[*str & 15]; |
| 813 | } |
| 814 | str++; |
| 815 | } |
| 816 | *start = '\0'; |
| 817 | } |
| 818 | } |
| 819 | else { |
| 820 | return encode_chunk(start, stop, escape, map, chunk); |
| 821 | } |
| 822 | |
| 823 | return start; |
| 824 | } |
| 825 | |
| 826 | /* |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 827 | * Write a string in the log string |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 828 | * Take cares of quote and escape options |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 829 | * |
| 830 | * Return the adress of the \0 character, or NULL on error |
| 831 | */ |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 832 | char *lf_text_len(char *dst, const char *src, size_t len, size_t size, struct logformat_node *node) |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 833 | { |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 834 | if (size < 2) |
| 835 | return NULL; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 836 | |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 837 | if (node->options & LOG_OPT_QUOTE) { |
| 838 | *(dst++) = '"'; |
| 839 | size--; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 840 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 841 | |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 842 | if (src && len) { |
Dragan Dosen | db1b6f9 | 2016-07-25 11:35:02 +0200 | [diff] [blame] | 843 | if (++len > size) |
| 844 | len = size; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 845 | if (node->options & LOG_OPT_ESC) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 846 | char *ret; |
| 847 | |
Dragan Dosen | db1b6f9 | 2016-07-25 11:35:02 +0200 | [diff] [blame] | 848 | ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src); |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 849 | if (ret == NULL || *ret != '\0') |
| 850 | return NULL; |
| 851 | len = ret - dst; |
| 852 | } |
| 853 | else { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 854 | len = strlcpy2(dst, src, len); |
| 855 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 856 | |
| 857 | size -= len; |
| 858 | dst += len; |
| 859 | } |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 860 | else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) { |
| 861 | if (size < 2) |
| 862 | return NULL; |
| 863 | *(dst++) = '-'; |
| 864 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 865 | |
| 866 | if (node->options & LOG_OPT_QUOTE) { |
| 867 | if (size < 2) |
| 868 | return NULL; |
| 869 | *(dst++) = '"'; |
| 870 | } |
| 871 | |
| 872 | *dst = '\0'; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 873 | return dst; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 874 | } |
| 875 | |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 876 | static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node) |
| 877 | { |
| 878 | return lf_text_len(dst, src, size, size, node); |
| 879 | } |
| 880 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 881 | /* |
| 882 | * Write a IP adress to the log string |
| 883 | * +X option write in hexadecimal notation, most signifant byte on the left |
| 884 | */ |
| 885 | char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node) |
| 886 | { |
| 887 | char *ret = dst; |
| 888 | int iret; |
| 889 | char pn[INET6_ADDRSTRLEN]; |
| 890 | |
| 891 | if (node->options & LOG_OPT_HEXA) { |
| 892 | const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr; |
| 893 | iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]); |
| 894 | if (iret < 0 || iret > size) |
| 895 | return NULL; |
| 896 | ret += iret; |
| 897 | } else { |
| 898 | addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn)); |
| 899 | ret = lf_text(dst, pn, size, node); |
| 900 | if (ret == NULL) |
| 901 | return NULL; |
| 902 | } |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Write a port to the log |
| 908 | * +X option write in hexadecimal notation, most signifant byte on the left |
| 909 | */ |
| 910 | char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node) |
| 911 | { |
| 912 | char *ret = dst; |
| 913 | int iret; |
| 914 | |
| 915 | if (node->options & LOG_OPT_HEXA) { |
| 916 | const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port; |
| 917 | iret = snprintf(dst, size, "%02X%02X", port[0], port[1]); |
| 918 | if (iret < 0 || iret > size) |
| 919 | return NULL; |
| 920 | ret += iret; |
| 921 | } else { |
| 922 | ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size); |
| 923 | if (ret == NULL) |
| 924 | return NULL; |
| 925 | } |
| 926 | return ret; |
| 927 | } |
| 928 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 929 | /* Re-generate time-based part of the syslog header in RFC3164 format at |
| 930 | * the beginning of logheader once a second and return the pointer to the |
| 931 | * first character after it. |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 932 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 933 | static char *update_log_hdr(const time_t time) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 934 | { |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 935 | static long tvsec; |
| 936 | static char *dataptr = NULL; /* backup of last end of header, NULL first time */ |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 937 | static struct chunk host = { NULL, 0, 0 }; |
| 938 | static int sep = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 939 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 940 | if (unlikely(time != tvsec || dataptr == NULL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 941 | /* this string is rebuild only once a second */ |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 942 | struct tm tm; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 943 | int hdr_len; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 944 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 945 | tvsec = time; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 946 | get_localtime(tvsec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 947 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 948 | if (unlikely(global.log_send_hostname != host.str)) { |
| 949 | host.str = global.log_send_hostname; |
| 950 | host.len = host.str ? strlen(host.str) : 0; |
| 951 | sep = host.len ? 1 : 0; |
| 952 | } |
| 953 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 954 | hdr_len = snprintf(logheader, global.max_syslog_len, |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 955 | "<<<<>%s %2d %02d:%02d:%02d %.*s%*s", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 956 | monthname[tm.tm_mon], |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 957 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 958 | host.len, host.str, sep, ""); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 959 | /* WARNING: depending upon implementations, snprintf may return |
| 960 | * either -1 or the number of bytes that would be needed to store |
| 961 | * the total message. In both cases, we must adjust it. |
| 962 | */ |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 963 | if (hdr_len < 0 || hdr_len > global.max_syslog_len) |
| 964 | hdr_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 965 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 966 | dataptr = logheader + hdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 967 | } |
| 968 | |
Willy Tarreau | 094af4e | 2015-01-07 15:03:42 +0100 | [diff] [blame] | 969 | dataptr[0] = 0; // ensure we get rid of any previous attempt |
| 970 | |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 971 | return dataptr; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 972 | } |
| 973 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 974 | /* Re-generate time-based part of the syslog header in RFC5424 format at |
| 975 | * the beginning of logheader_rfc5424 once a second and return the pointer |
| 976 | * to the first character after it. |
| 977 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 978 | static char *update_log_hdr_rfc5424(const time_t time) |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 979 | { |
| 980 | static long tvsec; |
| 981 | static char *dataptr = NULL; /* backup of last end of header, NULL first time */ |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 982 | const char *gmt_offset; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 983 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 984 | if (unlikely(time != tvsec || dataptr == NULL)) { |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 985 | /* this string is rebuild only once a second */ |
| 986 | struct tm tm; |
| 987 | int hdr_len; |
| 988 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 989 | tvsec = time; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 990 | get_localtime(tvsec, &tm); |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 991 | gmt_offset = get_gmt_offset(time, &tm); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 992 | |
| 993 | hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len, |
Dragan Dosen | 17def46 | 2015-10-09 21:31:43 +0200 | [diff] [blame] | 994 | "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ", |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 995 | tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, |
Dragan Dosen | 17def46 | 2015-10-09 21:31:43 +0200 | [diff] [blame] | 996 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 997 | gmt_offset, gmt_offset+3, |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 998 | global.log_send_hostname ? global.log_send_hostname : hostname); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 999 | /* WARNING: depending upon implementations, snprintf may return |
| 1000 | * either -1 or the number of bytes that would be needed to store |
| 1001 | * the total message. In both cases, we must adjust it. |
| 1002 | */ |
| 1003 | if (hdr_len < 0 || hdr_len > global.max_syslog_len) |
| 1004 | hdr_len = global.max_syslog_len; |
| 1005 | |
| 1006 | dataptr = logheader_rfc5424 + hdr_len; |
| 1007 | } |
| 1008 | |
| 1009 | dataptr[0] = 0; // ensure we get rid of any previous attempt |
| 1010 | |
| 1011 | return dataptr; |
| 1012 | } |
| 1013 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1014 | /* |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1015 | * This function sends the syslog message using a printf format string. It |
| 1016 | * expects an LF-terminated message. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1017 | */ |
| 1018 | void send_log(struct proxy *p, int level, const char *format, ...) |
| 1019 | { |
| 1020 | va_list argp; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1021 | int data_len; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1022 | |
Willy Tarreau | 8c97ab5 | 2015-01-15 16:29:53 +0100 | [diff] [blame] | 1023 | if (level < 0 || format == NULL || logline == NULL) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1024 | return; |
| 1025 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1026 | va_start(argp, format); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1027 | data_len = vsnprintf(logline, global.max_syslog_len, format, argp); |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1028 | if (data_len < 0 || data_len > global.max_syslog_len) |
| 1029 | data_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1030 | va_end(argp); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1031 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1032 | __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * This function sends a syslog message. |
| 1037 | * It doesn't care about errors nor does it report them. |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1038 | * It overrides the last byte of the message vector with an LF character. |
| 1039 | * The arguments <sd> and <sd_size> are used for the structured-data part |
| 1040 | * in RFC5424 formatted syslog messages. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1041 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1042 | void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1043 | { |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1044 | static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { }; |
| 1045 | static struct msghdr msghdr = { |
| 1046 | .msg_iov = iovec, |
| 1047 | .msg_iovlen = NB_MSG_IOVEC_ELEMENTS |
| 1048 | }; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1049 | static int logfdunix = -1; /* syslog to AF_UNIX socket */ |
| 1050 | static int logfdinet = -1; /* syslog to AF_INET socket */ |
| 1051 | static char *dataptr = NULL; |
| 1052 | int fac_level; |
| 1053 | struct list *logsrvs = NULL; |
| 1054 | struct logsrv *tmp = NULL; |
| 1055 | int nblogger; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1056 | char *hdr, *hdr_ptr; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1057 | size_t hdr_size; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1058 | time_t time = date.tv_sec; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1059 | struct chunk *tag = &global.log_tag; |
| 1060 | static int curr_pid; |
| 1061 | static char pidstr[100]; |
| 1062 | static struct chunk pid; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1063 | |
| 1064 | dataptr = message; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1065 | |
| 1066 | if (p == NULL) { |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1067 | if (!LIST_ISEMPTY(&global.logsrvs)) { |
| 1068 | logsrvs = &global.logsrvs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1069 | } |
| 1070 | } else { |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1071 | if (!LIST_ISEMPTY(&p->logsrvs)) { |
| 1072 | logsrvs = &p->logsrvs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1073 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1074 | if (p->log_tag.str) { |
| 1075 | tag = &p->log_tag; |
| 1076 | } |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1079 | if (!logsrvs) |
| 1080 | return; |
| 1081 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1082 | if (unlikely(curr_pid != getpid())) { |
| 1083 | curr_pid = getpid(); |
| 1084 | ltoa_o(curr_pid, pidstr, sizeof(pidstr)); |
| 1085 | chunk_initstr(&pid, pidstr); |
| 1086 | } |
| 1087 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1088 | /* Send log messages to syslog server. */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1089 | nblogger = 0; |
| 1090 | list_for_each_entry(tmp, logsrvs, list) { |
| 1091 | const struct logsrv *logsrv = tmp; |
David du Colombier | 11bcb6c | 2011-03-24 12:23:00 +0100 | [diff] [blame] | 1092 | int *plogfd = logsrv->addr.ss_family == AF_UNIX ? |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1093 | &logfdunix : &logfdinet; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1094 | char *pid_sep1 = NULL, *pid_sep2 = NULL; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1095 | int sent; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1096 | int maxlen; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1097 | int hdr_max = 0; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1098 | int tag_max = 0; |
| 1099 | int pid_sep1_max = 0; |
| 1100 | int pid_max = 0; |
| 1101 | int pid_sep2_max = 0; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1102 | int sd_max = 0; |
Dragan Dosen | 5b78d9b | 2015-09-28 16:01:03 +0200 | [diff] [blame] | 1103 | int max = 0; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1104 | |
Willy Tarreau | c7c7be2 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 1105 | nblogger++; |
| 1106 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1107 | /* we can filter the level of the messages that are sent to each logger */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1108 | if (level > logsrv->level) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1109 | continue; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1110 | |
Willy Tarreau | c7c7be2 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 1111 | if (unlikely(*plogfd < 0)) { |
| 1112 | /* socket not successfully initialized yet */ |
| 1113 | int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP; |
| 1114 | |
| 1115 | if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) { |
| 1116 | Alert("socket for logger #%d failed: %s (errno=%d)\n", |
| 1117 | nblogger, strerror(errno), errno); |
| 1118 | continue; |
| 1119 | } |
| 1120 | /* we don't want to receive anything on this socket */ |
| 1121 | setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero)); |
| 1122 | /* does nothing under Linux, maybe needed for others */ |
| 1123 | shutdown(*plogfd, SHUT_RD); |
| 1124 | } |
| 1125 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1126 | switch (logsrv->format) { |
| 1127 | case LOG_FORMAT_RFC3164: |
| 1128 | hdr = logheader; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1129 | hdr_ptr = update_log_hdr(time); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1130 | break; |
| 1131 | |
| 1132 | case LOG_FORMAT_RFC5424: |
| 1133 | hdr = logheader_rfc5424; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1134 | hdr_ptr = update_log_hdr_rfc5424(time); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1135 | sd_max = sd_size; /* the SD part allowed only in RFC5424 */ |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1136 | break; |
| 1137 | |
| 1138 | default: |
| 1139 | continue; /* must never happen */ |
| 1140 | } |
| 1141 | |
| 1142 | hdr_size = hdr_ptr - hdr; |
| 1143 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1144 | /* For each target, we may have a different facility. |
| 1145 | * We can also have a different log level for each message. |
| 1146 | * This induces variations in the message header length. |
| 1147 | * Since we don't want to recompute it each time, nor copy it every |
| 1148 | * time, we only change the facility in the pre-computed header, |
| 1149 | * and we change the pointer to the header accordingly. |
| 1150 | */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1151 | fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1152 | hdr_ptr = hdr + 3; /* last digit of the log level */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1153 | do { |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1154 | *hdr_ptr = '0' + fac_level % 10; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1155 | fac_level /= 10; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1156 | hdr_ptr--; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1157 | } while (fac_level && hdr_ptr > hdr); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1158 | *hdr_ptr = '<'; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1159 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1160 | hdr_max = hdr_size - (hdr_ptr - hdr); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1161 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1162 | /* time-based header */ |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1163 | if (unlikely(hdr_size >= logsrv->maxlen)) { |
| 1164 | hdr_max = MIN(hdr_max, logsrv->maxlen) - 1; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1165 | sd_max = 0; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1166 | goto send; |
| 1167 | } |
| 1168 | |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1169 | maxlen = logsrv->maxlen - hdr_max; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1170 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1171 | /* tag */ |
| 1172 | tag_max = tag->len; |
| 1173 | if (unlikely(tag_max >= maxlen)) { |
| 1174 | tag_max = maxlen - 1; |
| 1175 | sd_max = 0; |
| 1176 | goto send; |
| 1177 | } |
| 1178 | |
| 1179 | maxlen -= tag_max; |
| 1180 | |
| 1181 | /* first pid separator */ |
| 1182 | pid_sep1_max = log_formats[logsrv->format].pid.sep1.len; |
| 1183 | if (unlikely(pid_sep1_max >= maxlen)) { |
| 1184 | pid_sep1_max = maxlen - 1; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1185 | sd_max = 0; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1186 | goto send; |
| 1187 | } |
| 1188 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1189 | pid_sep1 = log_formats[logsrv->format].pid.sep1.str; |
| 1190 | maxlen -= pid_sep1_max; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1191 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1192 | /* pid */ |
| 1193 | pid_max = pid.len; |
| 1194 | if (unlikely(pid_max >= maxlen)) { |
| 1195 | pid_max = maxlen - 1; |
| 1196 | sd_max = 0; |
| 1197 | goto send; |
| 1198 | } |
| 1199 | |
| 1200 | maxlen -= pid_max; |
| 1201 | |
| 1202 | /* second pid separator */ |
| 1203 | pid_sep2_max = log_formats[logsrv->format].pid.sep2.len; |
| 1204 | if (unlikely(pid_sep2_max >= maxlen)) { |
| 1205 | pid_sep2_max = maxlen - 1; |
| 1206 | sd_max = 0; |
| 1207 | goto send; |
| 1208 | } |
| 1209 | |
| 1210 | pid_sep2 = log_formats[logsrv->format].pid.sep2.str; |
| 1211 | maxlen -= pid_sep2_max; |
| 1212 | |
| 1213 | /* structured-data */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1214 | if (sd_max >= maxlen) { |
| 1215 | sd_max = maxlen - 1; |
| 1216 | goto send; |
| 1217 | } |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1218 | |
Dragan Dosen | 5b78d9b | 2015-09-28 16:01:03 +0200 | [diff] [blame] | 1219 | max = MIN(size, maxlen - sd_max) - 1; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1220 | send: |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1221 | iovec[0].iov_base = hdr_ptr; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1222 | iovec[0].iov_len = hdr_max; |
| 1223 | iovec[1].iov_base = tag->str; |
| 1224 | iovec[1].iov_len = tag_max; |
| 1225 | iovec[2].iov_base = pid_sep1; |
| 1226 | iovec[2].iov_len = pid_sep1_max; |
| 1227 | iovec[3].iov_base = pid.str; |
| 1228 | iovec[3].iov_len = pid_max; |
| 1229 | iovec[4].iov_base = pid_sep2; |
| 1230 | iovec[4].iov_len = pid_sep2_max; |
| 1231 | iovec[5].iov_base = sd; |
| 1232 | iovec[5].iov_len = sd_max; |
| 1233 | iovec[6].iov_base = dataptr; |
| 1234 | iovec[6].iov_len = max; |
| 1235 | iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */ |
| 1236 | iovec[7].iov_len = 1; |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1237 | |
| 1238 | msghdr.msg_name = (struct sockaddr *)&logsrv->addr; |
| 1239 | msghdr.msg_namelen = get_addr_len(&logsrv->addr); |
| 1240 | |
| 1241 | sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL); |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1242 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1243 | if (sent < 0) { |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1244 | Alert("sendmsg logger #%d failed: %s (errno=%d)\n", |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1245 | nblogger, strerror(errno), errno); |
| 1246 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1250 | extern fd_set hdr_encode_map[]; |
| 1251 | extern fd_set url_encode_map[]; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1252 | extern fd_set http_encode_map[]; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1253 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1254 | |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 1255 | const char sess_cookie[8] = "NIDVEOU7"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, Unused, unknown */ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1256 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 1257 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 1258 | Set-cookie Updated, unknown, unknown */ |
| 1259 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1260 | /* |
| 1261 | * try to write a character if there is enough space, or goto out |
| 1262 | */ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1263 | #define LOGCHAR(x) do { \ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1264 | if (tmplog < dst + maxsize - 1) { \ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1265 | *(tmplog++) = (x); \ |
| 1266 | } else { \ |
| 1267 | goto out; \ |
| 1268 | } \ |
| 1269 | } while(0) |
| 1270 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1271 | |
| 1272 | /* Initializes some log data. |
| 1273 | */ |
| 1274 | void init_log() |
| 1275 | { |
| 1276 | char *tmp; |
| 1277 | |
| 1278 | /* Initialize the escape map for the RFC5424 structured-data : '"\]' |
| 1279 | * inside PARAM-VALUE should be escaped with '\' as prefix. |
| 1280 | * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more |
| 1281 | * details. |
| 1282 | */ |
| 1283 | memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map)); |
| 1284 | |
| 1285 | tmp = "\"\\]"; |
| 1286 | while (*tmp) { |
| 1287 | FD_SET(*tmp, rfc5424_escape_map); |
| 1288 | tmp++; |
| 1289 | } |
| 1290 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1291 | |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1292 | /* Builds a log line in <dst> based on <list_format>, and stops before reaching |
| 1293 | * <maxsize> characters. Returns the size of the output string in characters, |
| 1294 | * not counting the trailing zero which is always added if the resulting size |
| 1295 | * is not zero. |
| 1296 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1297 | int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1298 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1299 | struct session *sess = strm_sess(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1300 | struct proxy *fe = sess->fe; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 1301 | struct proxy *be = s->be; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1302 | struct http_txn *txn = s->txn; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1303 | struct chunk chunk; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1304 | char *uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1305 | char *spc; |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 1306 | char *qmark; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1307 | char *end; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1308 | struct tm tm; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1309 | int t_request; |
| 1310 | int hdr; |
| 1311 | int last_isspace = 1; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1312 | int nspaces = 0; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1313 | char *tmplog; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1314 | char *ret; |
| 1315 | int iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1316 | struct logformat_node *tmp; |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1317 | struct timeval tv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1318 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1319 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1320 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1321 | t_request = -1; |
| 1322 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1323 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1324 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1325 | tmplog = dst; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1326 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1327 | /* fill logbuffer */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1328 | if (LIST_ISEMPTY(list_format)) |
| 1329 | return 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1330 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1331 | list_for_each_entry(tmp, list_format, list) { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1332 | struct connection *conn; |
Willy Tarreau | 4f65356 | 2012-10-12 19:48:16 +0200 | [diff] [blame] | 1333 | const char *src = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1334 | struct sample *key; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1335 | const struct chunk empty = { NULL, 0, 0 }; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1336 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1337 | switch (tmp->type) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1338 | case LOG_FMT_SEPARATOR: |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1339 | if (!last_isspace) { |
| 1340 | LOGCHAR(' '); |
| 1341 | last_isspace = 1; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1342 | } |
| 1343 | break; |
| 1344 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1345 | case LOG_FMT_TEXT: // text |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1346 | src = tmp->arg; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1347 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1348 | if (iret == 0) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1349 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1350 | tmplog += iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1351 | last_isspace = 0; |
| 1352 | break; |
| 1353 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1354 | case LOG_FMT_EXPR: // sample expression, may be request or response |
| 1355 | key = NULL; |
| 1356 | if (tmp->options & LOG_OPT_REQ_CAP) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1357 | key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1358 | if (!key && (tmp->options & LOG_OPT_RES_CAP)) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1359 | key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1360 | if (tmp->options & LOG_OPT_HTTP) |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1361 | ret = lf_encode_chunk(tmplog, dst + maxsize, |
| 1362 | '%', http_encode_map, key ? &key->data.u.str : &empty, tmp); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1363 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1364 | ret = lf_text_len(tmplog, key ? key->data.u.str.str : NULL, key ? key->data.u.str.len : 0, dst + maxsize - tmplog, tmp); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1365 | if (ret == 0) |
| 1366 | goto out; |
| 1367 | tmplog = ret; |
| 1368 | last_isspace = 0; |
| 1369 | break; |
| 1370 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1371 | case LOG_FMT_CLIENTIP: // %ci |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1372 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1373 | if (conn) |
| 1374 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1375 | else |
| 1376 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1377 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1378 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1379 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1380 | last_isspace = 0; |
| 1381 | break; |
| 1382 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1383 | case LOG_FMT_CLIENTPORT: // %cp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1384 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1385 | if (conn) { |
| 1386 | if (conn->addr.from.ss_family == AF_UNIX) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1387 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1388 | } else { |
| 1389 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, |
| 1390 | dst + maxsize - tmplog, tmp); |
| 1391 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1392 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1393 | else |
| 1394 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1395 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1396 | if (ret == NULL) |
| 1397 | goto out; |
| 1398 | tmplog = ret; |
| 1399 | last_isspace = 0; |
| 1400 | break; |
| 1401 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1402 | case LOG_FMT_FRONTENDIP: // %fi |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1403 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1404 | if (conn) { |
| 1405 | conn_get_to_addr(conn); |
| 1406 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1407 | } |
| 1408 | else |
| 1409 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1410 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1411 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1412 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1413 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1414 | last_isspace = 0; |
| 1415 | break; |
| 1416 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1417 | case LOG_FMT_FRONTENDPORT: // %fp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1418 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1419 | if (conn) { |
| 1420 | conn_get_to_addr(conn); |
| 1421 | if (conn->addr.to.ss_family == AF_UNIX) |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1422 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1423 | else |
| 1424 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1425 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1426 | else |
| 1427 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1428 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1429 | if (ret == NULL) |
| 1430 | goto out; |
| 1431 | tmplog = ret; |
| 1432 | last_isspace = 0; |
| 1433 | break; |
| 1434 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1435 | case LOG_FMT_BACKENDIP: // %bi |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1436 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1437 | if (conn) |
| 1438 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1439 | else |
| 1440 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1441 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1442 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1443 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1444 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1445 | last_isspace = 0; |
| 1446 | break; |
| 1447 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1448 | case LOG_FMT_BACKENDPORT: // %bp |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1449 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1450 | if (conn) |
| 1451 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1452 | else |
| 1453 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1454 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1455 | if (ret == NULL) |
| 1456 | goto out; |
| 1457 | tmplog = ret; |
| 1458 | last_isspace = 0; |
| 1459 | break; |
| 1460 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1461 | case LOG_FMT_SERVERIP: // %si |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1462 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1463 | if (conn) |
| 1464 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1465 | else |
| 1466 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1467 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1468 | if (ret == NULL) |
| 1469 | goto out; |
| 1470 | tmplog = ret; |
| 1471 | last_isspace = 0; |
| 1472 | break; |
| 1473 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1474 | case LOG_FMT_SERVERPORT: // %sp |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1475 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1476 | if (conn) |
| 1477 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1478 | else |
| 1479 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1480 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1481 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1482 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1483 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1484 | last_isspace = 0; |
| 1485 | break; |
| 1486 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1487 | case LOG_FMT_DATE: // %t = accept date |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1488 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1489 | ret = date2str_log(tmplog, &tm, &(s->logs.accept_date), |
| 1490 | dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1491 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1492 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1493 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1494 | last_isspace = 0; |
| 1495 | break; |
| 1496 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1497 | case LOG_FMT_tr: // %tr = start of request date |
| 1498 | /* Note that the timers are valid if we get here */ |
| 1499 | tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0); |
| 1500 | get_localtime(tv.tv_sec, &tm); |
| 1501 | ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog); |
| 1502 | if (ret == NULL) |
| 1503 | goto out; |
| 1504 | tmplog = ret; |
| 1505 | last_isspace = 0; |
| 1506 | break; |
| 1507 | |
| 1508 | case LOG_FMT_DATEGMT: // %T = accept date, GMT |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1509 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1510 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1511 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1512 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1513 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1514 | last_isspace = 0; |
| 1515 | break; |
| 1516 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1517 | case LOG_FMT_trg: // %trg = start of request date, GMT |
| 1518 | tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0); |
| 1519 | get_gmtime(tv.tv_sec, &tm); |
| 1520 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
| 1521 | if (ret == NULL) |
| 1522 | goto out; |
| 1523 | tmplog = ret; |
| 1524 | last_isspace = 0; |
| 1525 | break; |
| 1526 | |
| 1527 | case LOG_FMT_DATELOCAL: // %Tl = accept date, local |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 1528 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 1529 | ret = localdate2str_log(tmplog, s->logs.accept_date.tv_sec, &tm, dst + maxsize - tmplog); |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 1530 | if (ret == NULL) |
| 1531 | goto out; |
| 1532 | tmplog = ret; |
| 1533 | last_isspace = 0; |
| 1534 | break; |
| 1535 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1536 | case LOG_FMT_trl: // %trl = start of request date, local |
| 1537 | tv_ms_add(&tv, &s->logs.accept_date, s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0); |
| 1538 | get_localtime(tv.tv_sec, &tm); |
| 1539 | ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog); |
| 1540 | if (ret == NULL) |
| 1541 | goto out; |
| 1542 | tmplog = ret; |
| 1543 | last_isspace = 0; |
| 1544 | break; |
| 1545 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1546 | case LOG_FMT_TS: // %Ts |
| 1547 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 1548 | if (tmp->options & LOG_OPT_HEXA) { |
| 1549 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec); |
| 1550 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1551 | goto out; |
| 1552 | last_isspace = 0; |
| 1553 | tmplog += iret; |
| 1554 | } else { |
| 1555 | ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog); |
| 1556 | if (ret == NULL) |
| 1557 | goto out; |
| 1558 | tmplog = ret; |
| 1559 | last_isspace = 0; |
| 1560 | } |
| 1561 | break; |
| 1562 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1563 | case LOG_FMT_MS: // %ms |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1564 | if (tmp->options & LOG_OPT_HEXA) { |
| 1565 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000); |
| 1566 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1567 | goto out; |
| 1568 | last_isspace = 0; |
| 1569 | tmplog += iret; |
| 1570 | } else { |
| 1571 | if ((dst + maxsize - tmplog) < 4) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1572 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 1573 | ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000, |
| 1574 | tmplog, 4); |
| 1575 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1576 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 1577 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1578 | last_isspace = 0; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1579 | } |
| 1580 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1581 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1582 | case LOG_FMT_FRONTEND: // %f |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1583 | src = fe->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1584 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1585 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1586 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1587 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1588 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1589 | break; |
| 1590 | |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1591 | case LOG_FMT_FRONTEND_XPRT: // %ft |
| 1592 | src = fe->id; |
| 1593 | if (tmp->options & LOG_OPT_QUOTE) |
| 1594 | LOGCHAR('"'); |
| 1595 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
| 1596 | if (iret == 0) |
| 1597 | goto out; |
| 1598 | tmplog += iret; |
| 1599 | #ifdef USE_OPENSSL |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1600 | if (sess->listener->xprt == &ssl_sock) |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1601 | LOGCHAR('~'); |
| 1602 | #endif |
| 1603 | if (tmp->options & LOG_OPT_QUOTE) |
| 1604 | LOGCHAR('"'); |
| 1605 | last_isspace = 0; |
| 1606 | break; |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1607 | #ifdef USE_OPENSSL |
| 1608 | case LOG_FMT_SSL_CIPHER: // %sslc |
| 1609 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1610 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1611 | if (conn) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1612 | if (sess->listener->xprt == &ssl_sock) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1613 | src = ssl_sock_get_cipher_name(conn); |
| 1614 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1615 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1616 | if (ret == NULL) |
| 1617 | goto out; |
| 1618 | tmplog = ret; |
| 1619 | last_isspace = 0; |
| 1620 | break; |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1621 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1622 | case LOG_FMT_SSL_VERSION: // %sslv |
| 1623 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1624 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1625 | if (conn) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1626 | if (sess->listener->xprt == &ssl_sock) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1627 | src = ssl_sock_get_proto_version(conn); |
| 1628 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1629 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1630 | if (ret == NULL) |
| 1631 | goto out; |
| 1632 | tmplog = ret; |
| 1633 | last_isspace = 0; |
| 1634 | break; |
| 1635 | #endif |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1636 | case LOG_FMT_BACKEND: // %b |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1637 | src = be->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1638 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1639 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1640 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1641 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1642 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1643 | break; |
| 1644 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1645 | case LOG_FMT_SERVER: // %s |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1646 | switch (obj_type(s->target)) { |
| 1647 | case OBJ_TYPE_SERVER: |
| 1648 | src = objt_server(s->target)->id; |
| 1649 | break; |
| 1650 | case OBJ_TYPE_APPLET: |
| 1651 | src = objt_applet(s->target)->name; |
| 1652 | break; |
| 1653 | default: |
| 1654 | src = "<NOSRV>"; |
| 1655 | break; |
| 1656 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1657 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1658 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1659 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1660 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1661 | last_isspace = 0; |
| 1662 | break; |
| 1663 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1664 | case LOG_FMT_Th: // %Th = handshake time |
| 1665 | ret = ltoa_o(s->logs.t_handshake, tmplog, dst + maxsize - tmplog); |
| 1666 | if (ret == NULL) |
| 1667 | goto out; |
| 1668 | tmplog = ret; |
| 1669 | last_isspace = 0; |
| 1670 | break; |
| 1671 | |
| 1672 | case LOG_FMT_Ti: // %Ti = HTTP idle time |
| 1673 | ret = ltoa_o(s->logs.t_idle, tmplog, dst + maxsize - tmplog); |
| 1674 | if (ret == NULL) |
| 1675 | goto out; |
| 1676 | tmplog = ret; |
| 1677 | last_isspace = 0; |
| 1678 | break; |
| 1679 | |
| 1680 | case LOG_FMT_TR: // %TR = HTTP request time |
| 1681 | ret = ltoa_o((t_request >= 0) ? t_request - s->logs.t_idle - s->logs.t_handshake : -1, |
| 1682 | tmplog, dst + maxsize - tmplog); |
| 1683 | if (ret == NULL) |
| 1684 | goto out; |
| 1685 | tmplog = ret; |
| 1686 | last_isspace = 0; |
| 1687 | break; |
| 1688 | |
| 1689 | case LOG_FMT_TQ: // %Tq = Th + Ti + TR |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1690 | ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1691 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1692 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1693 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1694 | last_isspace = 0; |
| 1695 | break; |
| 1696 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1697 | case LOG_FMT_TW: // %Tw |
| 1698 | ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1699 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1700 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1701 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1702 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1703 | last_isspace = 0; |
| 1704 | break; |
| 1705 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1706 | case LOG_FMT_TC: // %Tc |
| 1707 | ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1708 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1709 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1710 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1711 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1712 | last_isspace = 0; |
| 1713 | break; |
| 1714 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1715 | case LOG_FMT_Tr: // %Tr |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1716 | ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1717 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1718 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1719 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1720 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1721 | last_isspace = 0; |
| 1722 | break; |
| 1723 | |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 1724 | case LOG_FMT_TD: // %Td |
| 1725 | if (s->be->mode == PR_MODE_HTTP) |
| 1726 | ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_close - s->logs.t_data : -1, |
| 1727 | tmplog, dst + maxsize - tmplog); |
| 1728 | else |
| 1729 | ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_close - s->logs.t_connect : -1, |
| 1730 | tmplog, dst + maxsize - tmplog); |
| 1731 | if (ret == NULL) |
| 1732 | goto out; |
| 1733 | tmplog = ret; |
| 1734 | last_isspace = 0; |
| 1735 | break; |
| 1736 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1737 | case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti |
| 1738 | if (!(fe->to_log & LW_BYTES)) |
| 1739 | LOGCHAR('+'); |
| 1740 | ret = ltoa_o(s->logs.t_close - (s->logs.t_idle >= 0 ? s->logs.t_idle + s->logs.t_handshake : 0), |
| 1741 | tmplog, dst + maxsize - tmplog); |
| 1742 | if (ret == NULL) |
| 1743 | goto out; |
| 1744 | tmplog = ret; |
| 1745 | last_isspace = 0; |
| 1746 | break; |
| 1747 | |
| 1748 | case LOG_FMT_TT: // %Tt = total time |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1749 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1750 | LOGCHAR('+'); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1751 | ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1752 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1753 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1754 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1755 | last_isspace = 0; |
| 1756 | break; |
| 1757 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1758 | case LOG_FMT_STATUS: // %ST |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 1759 | ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1760 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1761 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1762 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1763 | last_isspace = 0; |
| 1764 | break; |
| 1765 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1766 | case LOG_FMT_BYTES: // %B |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1767 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1768 | LOGCHAR('+'); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1769 | ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1770 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1771 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1772 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1773 | last_isspace = 0; |
| 1774 | break; |
| 1775 | |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 1776 | case LOG_FMT_BYTES_UP: // %U |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 1777 | ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog); |
| 1778 | if (ret == NULL) |
| 1779 | goto out; |
| 1780 | tmplog = ret; |
| 1781 | last_isspace = 0; |
| 1782 | break; |
| 1783 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1784 | case LOG_FMT_CCLIENT: // %CC |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 1785 | src = txn ? txn->cli_cookie : NULL; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1786 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1787 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1788 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1789 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1790 | last_isspace = 0; |
| 1791 | break; |
| 1792 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1793 | case LOG_FMT_CSERVER: // %CS |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 1794 | src = txn ? txn->srv_cookie : NULL; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1795 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1796 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1797 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1798 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1799 | last_isspace = 0; |
| 1800 | break; |
| 1801 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1802 | case LOG_FMT_TERMSTATE: // %ts |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1803 | LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 1804 | LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 6580c06 | 2012-03-12 15:09:42 +0100 | [diff] [blame] | 1805 | *tmplog = '\0'; |
| 1806 | last_isspace = 0; |
| 1807 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1808 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1809 | case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1810 | LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 1811 | LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 1812 | LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-'); |
| 1813 | LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1814 | last_isspace = 0; |
| 1815 | break; |
| 1816 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1817 | case LOG_FMT_ACTCONN: // %ac |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1818 | ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1819 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1820 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1821 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1822 | last_isspace = 0; |
| 1823 | break; |
| 1824 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1825 | case LOG_FMT_FECONN: // %fc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1826 | ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1827 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1828 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1829 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1830 | last_isspace = 0; |
| 1831 | break; |
| 1832 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1833 | case LOG_FMT_BECONN: // %bc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1834 | ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1835 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1836 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1837 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1838 | last_isspace = 0; |
| 1839 | break; |
| 1840 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1841 | case LOG_FMT_SRVCONN: // %sc |
Willy Tarreau | 54a08d3 | 2012-11-12 01:14:56 +0100 | [diff] [blame] | 1842 | ret = ultoa_o(objt_server(s->target) ? |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1843 | objt_server(s->target)->cur_sess : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1844 | 0, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1845 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1846 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1847 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1848 | last_isspace = 0; |
| 1849 | break; |
| 1850 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1851 | case LOG_FMT_RETRIES: // %rq |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1852 | if (s->flags & SF_REDISP) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1853 | LOGCHAR('+'); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1854 | ret = ltoa_o((s->si[1].conn_retries>0) ? |
| 1855 | (be->conn_retries - s->si[1].conn_retries) : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1856 | be->conn_retries, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1857 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1858 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1859 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1860 | last_isspace = 0; |
| 1861 | break; |
| 1862 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1863 | case LOG_FMT_SRVQUEUE: // %sq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1864 | ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1865 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1866 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1867 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1868 | last_isspace = 0; |
| 1869 | break; |
| 1870 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1871 | case LOG_FMT_BCKQUEUE: // %bq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1872 | ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1873 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1874 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1875 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1876 | last_isspace = 0; |
| 1877 | break; |
| 1878 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1879 | case LOG_FMT_HDRREQUEST: // %hr |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1880 | /* request header */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1881 | if (fe->nb_req_cap && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1882 | if (tmp->options & LOG_OPT_QUOTE) |
| 1883 | LOGCHAR('"'); |
| 1884 | LOGCHAR('{'); |
| 1885 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1886 | if (hdr) |
| 1887 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1888 | if (s->req_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1889 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 1890 | '#', hdr_encode_map, s->req_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1891 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1892 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1893 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1894 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1895 | } |
| 1896 | LOGCHAR('}'); |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1897 | if (tmp->options & LOG_OPT_QUOTE) |
| 1898 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1899 | last_isspace = 0; |
| 1900 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1901 | break; |
| 1902 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1903 | case LOG_FMT_HDRREQUESTLIST: // %hrl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1904 | /* request header list */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1905 | if (fe->nb_req_cap && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1906 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1907 | if (hdr > 0) |
| 1908 | LOGCHAR(' '); |
| 1909 | if (tmp->options & LOG_OPT_QUOTE) |
| 1910 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1911 | if (s->req_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1912 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 1913 | '#', hdr_encode_map, s->req_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1914 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1915 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1916 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1917 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 1918 | LOGCHAR('-'); |
| 1919 | if (tmp->options & LOG_OPT_QUOTE) |
| 1920 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1921 | last_isspace = 0; |
| 1922 | } |
| 1923 | } |
| 1924 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1925 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1926 | |
| 1927 | case LOG_FMT_HDRRESPONS: // %hs |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1928 | /* response header */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1929 | if (fe->nb_rsp_cap && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1930 | if (tmp->options & LOG_OPT_QUOTE) |
| 1931 | LOGCHAR('"'); |
| 1932 | LOGCHAR('{'); |
| 1933 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1934 | if (hdr) |
| 1935 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1936 | if (s->res_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1937 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 1938 | '#', hdr_encode_map, s->res_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1939 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1940 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1941 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1942 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1943 | } |
| 1944 | LOGCHAR('}'); |
| 1945 | last_isspace = 0; |
| 1946 | if (tmp->options & LOG_OPT_QUOTE) |
| 1947 | LOGCHAR('"'); |
| 1948 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1949 | break; |
| 1950 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1951 | case LOG_FMT_HDRRESPONSLIST: // %hsl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1952 | /* response header list */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1953 | if (fe->nb_rsp_cap && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1954 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1955 | if (hdr > 0) |
| 1956 | LOGCHAR(' '); |
| 1957 | if (tmp->options & LOG_OPT_QUOTE) |
| 1958 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1959 | if (s->res_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1960 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 1961 | '#', hdr_encode_map, s->res_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1962 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1963 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1964 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1965 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 1966 | LOGCHAR('-'); |
| 1967 | if (tmp->options & LOG_OPT_QUOTE) |
| 1968 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1969 | last_isspace = 0; |
| 1970 | } |
| 1971 | } |
| 1972 | break; |
| 1973 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1974 | case LOG_FMT_REQ: // %r |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1975 | /* Request */ |
| 1976 | if (tmp->options & LOG_OPT_QUOTE) |
| 1977 | LOGCHAR('"'); |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 1978 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1979 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 1980 | '#', url_encode_map, uri, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1981 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1982 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1983 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1984 | if (tmp->options & LOG_OPT_QUOTE) |
| 1985 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1986 | last_isspace = 0; |
| 1987 | break; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1988 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1989 | case LOG_FMT_HTTP_PATH: // %HP |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 1990 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1991 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1992 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1993 | LOGCHAR('"'); |
| 1994 | |
| 1995 | end = uri + strlen(uri); |
| 1996 | // look for the first whitespace character |
| 1997 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1998 | uri++; |
| 1999 | |
| 2000 | // keep advancing past multiple spaces |
| 2001 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2002 | uri++; nspaces++; |
| 2003 | } |
| 2004 | |
| 2005 | // look for first space or question mark after url |
| 2006 | spc = uri; |
| 2007 | while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc)) |
| 2008 | spc++; |
| 2009 | |
Nenad Merdanovic | 54e439f | 2016-04-26 01:39:02 +0200 | [diff] [blame] | 2010 | if (!txn || !txn->uri || nspaces == 0) { |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2011 | chunk.str = "<BADREQ>"; |
| 2012 | chunk.len = strlen("<BADREQ>"); |
| 2013 | } else { |
| 2014 | chunk.str = uri; |
| 2015 | chunk.len = spc - uri; |
| 2016 | } |
| 2017 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2018 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2019 | if (ret == NULL || *ret != '\0') |
| 2020 | goto out; |
| 2021 | |
| 2022 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2023 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2024 | LOGCHAR('"'); |
| 2025 | |
| 2026 | last_isspace = 0; |
| 2027 | break; |
| 2028 | |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2029 | case LOG_FMT_HTTP_QUERY: // %HQ |
| 2030 | if (tmp->options & LOG_OPT_QUOTE) |
| 2031 | LOGCHAR('"'); |
| 2032 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2033 | if (!txn || !txn->uri) { |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2034 | chunk.str = "<BADREQ>"; |
| 2035 | chunk.len = strlen("<BADREQ>"); |
| 2036 | } else { |
| 2037 | uri = txn->uri; |
| 2038 | end = uri + strlen(uri); |
| 2039 | // look for the first question mark |
| 2040 | while (uri < end && *uri != '?') |
| 2041 | uri++; |
| 2042 | |
| 2043 | qmark = uri; |
| 2044 | // look for first space or question mark after url |
| 2045 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2046 | uri++; |
| 2047 | |
| 2048 | chunk.str = qmark; |
| 2049 | chunk.len = uri - qmark; |
| 2050 | } |
| 2051 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2052 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2053 | if (ret == NULL || *ret != '\0') |
| 2054 | goto out; |
| 2055 | |
| 2056 | tmplog = ret; |
| 2057 | if (tmp->options & LOG_OPT_QUOTE) |
| 2058 | LOGCHAR('"'); |
| 2059 | |
| 2060 | last_isspace = 0; |
| 2061 | break; |
| 2062 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2063 | case LOG_FMT_HTTP_URI: // %HU |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2064 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2065 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2066 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2067 | LOGCHAR('"'); |
| 2068 | |
| 2069 | end = uri + strlen(uri); |
| 2070 | // look for the first whitespace character |
| 2071 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2072 | uri++; |
| 2073 | |
| 2074 | // keep advancing past multiple spaces |
| 2075 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2076 | uri++; nspaces++; |
| 2077 | } |
| 2078 | |
| 2079 | // look for first space after url |
| 2080 | spc = uri; |
| 2081 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 2082 | spc++; |
| 2083 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2084 | if (!txn || !txn->uri || nspaces == 0) { |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2085 | chunk.str = "<BADREQ>"; |
| 2086 | chunk.len = strlen("<BADREQ>"); |
| 2087 | } else { |
| 2088 | chunk.str = uri; |
| 2089 | chunk.len = spc - uri; |
| 2090 | } |
| 2091 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2092 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2093 | if (ret == NULL || *ret != '\0') |
| 2094 | goto out; |
| 2095 | |
| 2096 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2097 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2098 | LOGCHAR('"'); |
| 2099 | |
| 2100 | last_isspace = 0; |
| 2101 | break; |
| 2102 | |
| 2103 | case LOG_FMT_HTTP_METHOD: // %HM |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2104 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2105 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2106 | LOGCHAR('"'); |
| 2107 | |
| 2108 | end = uri + strlen(uri); |
| 2109 | // look for the first whitespace character |
| 2110 | spc = uri; |
| 2111 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 2112 | spc++; |
| 2113 | |
| 2114 | if (spc == end) { // odd case, we have txn->uri, but we only got a verb |
| 2115 | chunk.str = "<BADREQ>"; |
| 2116 | chunk.len = strlen("<BADREQ>"); |
| 2117 | } else { |
| 2118 | chunk.str = uri; |
| 2119 | chunk.len = spc - uri; |
| 2120 | } |
| 2121 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2122 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2123 | if (ret == NULL || *ret != '\0') |
| 2124 | goto out; |
| 2125 | |
| 2126 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2127 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2128 | LOGCHAR('"'); |
| 2129 | |
| 2130 | last_isspace = 0; |
| 2131 | break; |
| 2132 | |
| 2133 | case LOG_FMT_HTTP_VERSION: // %HV |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2134 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2135 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2136 | LOGCHAR('"'); |
| 2137 | |
| 2138 | end = uri + strlen(uri); |
| 2139 | // look for the first whitespace character |
| 2140 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2141 | uri++; |
| 2142 | |
| 2143 | // keep advancing past multiple spaces |
| 2144 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2145 | uri++; nspaces++; |
| 2146 | } |
| 2147 | |
| 2148 | // look for the next whitespace character |
| 2149 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2150 | uri++; |
| 2151 | |
| 2152 | // keep advancing past multiple spaces |
| 2153 | while (uri < end && HTTP_IS_SPHT(*uri)) |
| 2154 | uri++; |
| 2155 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2156 | if (!txn || !txn->uri || nspaces == 0) { |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2157 | chunk.str = "<BADREQ>"; |
| 2158 | chunk.len = strlen("<BADREQ>"); |
| 2159 | } else if (uri == end) { |
| 2160 | chunk.str = "HTTP/0.9"; |
| 2161 | chunk.len = strlen("HTTP/0.9"); |
| 2162 | } else { |
| 2163 | chunk.str = uri; |
| 2164 | chunk.len = end - uri; |
| 2165 | } |
| 2166 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2167 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2168 | if (ret == NULL || *ret != '\0') |
| 2169 | goto out; |
| 2170 | |
| 2171 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2172 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2173 | LOGCHAR('"'); |
| 2174 | |
| 2175 | last_isspace = 0; |
| 2176 | break; |
| 2177 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2178 | case LOG_FMT_COUNTER: // %rt |
| 2179 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 2180 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2181 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2182 | goto out; |
| 2183 | last_isspace = 0; |
| 2184 | tmplog += iret; |
| 2185 | } else { |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 2186 | ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2187 | if (ret == NULL) |
| 2188 | goto out; |
| 2189 | tmplog = ret; |
| 2190 | last_isspace = 0; |
| 2191 | } |
| 2192 | break; |
| 2193 | |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2194 | case LOG_FMT_LOGCNT: // %lc |
| 2195 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2196 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2197 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2198 | goto out; |
| 2199 | last_isspace = 0; |
| 2200 | tmplog += iret; |
| 2201 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2202 | ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2203 | if (ret == NULL) |
| 2204 | goto out; |
| 2205 | tmplog = ret; |
| 2206 | last_isspace = 0; |
| 2207 | } |
| 2208 | break; |
| 2209 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2210 | case LOG_FMT_HOSTNAME: // %H |
| 2211 | src = hostname; |
| 2212 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 2213 | if (ret == NULL) |
| 2214 | goto out; |
| 2215 | tmplog = ret; |
| 2216 | last_isspace = 0; |
| 2217 | break; |
| 2218 | |
| 2219 | case LOG_FMT_PID: // %pid |
| 2220 | if (tmp->options & LOG_OPT_HEXA) { |
| 2221 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid); |
| 2222 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2223 | goto out; |
| 2224 | last_isspace = 0; |
| 2225 | tmplog += iret; |
| 2226 | } else { |
| 2227 | ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog); |
| 2228 | if (ret == NULL) |
| 2229 | goto out; |
| 2230 | tmplog = ret; |
| 2231 | last_isspace = 0; |
| 2232 | } |
| 2233 | break; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2234 | |
| 2235 | case LOG_FMT_UNIQUEID: // %ID |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2236 | ret = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2237 | src = s->unique_id; |
Thierry FOURNIER | 1be6910 | 2014-04-15 01:38:48 +0200 | [diff] [blame] | 2238 | ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2239 | if (ret == NULL) |
| 2240 | goto out; |
| 2241 | tmplog = ret; |
| 2242 | last_isspace = 0; |
| 2243 | break; |
| 2244 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | out: |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2249 | /* *tmplog is a unused character */ |
| 2250 | *tmplog = '\0'; |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 2251 | return tmplog - dst; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2252 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2253 | } |
| 2254 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2255 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2256 | * send a log for the stream when we have enough info about it. |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2257 | * Will not log if the frontend has no log defined. |
| 2258 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2259 | void strm_log(struct stream *s) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2260 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2261 | struct session *sess = s->sess; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2262 | int size, err, level; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2263 | int sd_size = 0; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2264 | |
| 2265 | /* if we don't want to log normal traffic, return now */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2266 | err = (s->flags & SF_REDISP) || |
| 2267 | ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) || |
| 2268 | (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) && |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2269 | (s->si[1].conn_retries != s->be->conn_retries)) || |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2270 | ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2271 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2272 | if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2273 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2274 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2275 | if (LIST_ISEMPTY(&sess->fe->logsrvs)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2276 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2277 | |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2278 | if (s->logs.level) { /* loglevel was overridden */ |
| 2279 | if (s->logs.level == -1) { |
| 2280 | s->logs.logwait = 0; /* logs disabled */ |
| 2281 | return; |
| 2282 | } |
| 2283 | level = s->logs.level - 1; |
| 2284 | } |
| 2285 | else { |
| 2286 | level = LOG_INFO; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2287 | if (err && (sess->fe->options2 & PR_O2_LOGERRORS)) |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2288 | level = LOG_ERR; |
| 2289 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2290 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2291 | /* if unique-id was not generated */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2292 | if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2293 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2294 | build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2295 | } |
| 2296 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2297 | if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) { |
| 2298 | sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len, |
| 2299 | &sess->fe->logformat_sd); |
| 2300 | } |
| 2301 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 2302 | size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2303 | if (size > 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2304 | sess->fe->log_count++; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2305 | __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2306 | s->logs.logwait = 0; |
| 2307 | } |
| 2308 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2309 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2310 | /* |
| 2311 | * Local variables: |
| 2312 | * c-indent-level: 8 |
| 2313 | * c-basic-offset: 8 |
| 2314 | * End: |
| 2315 | */ |