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> |
| 25 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 26 | #include <common/config.h> |
Willy Tarreau | d6d0690 | 2009-08-19 11:22:33 +0200 | [diff] [blame] | 27 | #include <common/compat.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/standard.h> |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 29 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <types/global.h> |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 32 | #include <types/log.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 33 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 34 | #include <proto/frontend.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 35 | #include <proto/log.h> |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 36 | #include <proto/sample.h> |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 37 | #include <proto/stream_interface.h> |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 38 | #ifdef USE_OPENSSL |
| 39 | #include <proto/ssl_sock.h> |
| 40 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 43 | "kern", "user", "mail", "daemon", |
| 44 | "auth", "syslog", "lpr", "news", |
| 45 | "uucp", "cron", "auth2", "ftp", |
| 46 | "ntp", "audit", "alert", "cron2", |
| 47 | "local0", "local1", "local2", "local3", |
| 48 | "local4", "local5", "local6", "local7" |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | const char *log_levels[NB_LOG_LEVELS] = { |
| 53 | "emerg", "alert", "crit", "err", |
| 54 | "warning", "notice", "info", "debug" |
| 55 | }; |
| 56 | |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 57 | 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] | 58 | 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] | 59 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 60 | |
| 61 | /* log_format */ |
| 62 | struct logformat_type { |
| 63 | char *name; |
| 64 | int type; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 65 | int mode; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 66 | int lw; /* logwait bitsfield */ |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 67 | int (*config_callback)(struct logformat_node *node, struct proxy *curproxy); |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 68 | const char *replace_by; /* new option to use instead of old one */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 71 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy); |
| 72 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 73 | /* log_format variable names */ |
| 74 | static const struct logformat_type logformat_keywords[] = { |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 75 | { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 76 | |
| 77 | /* please keep these lines sorted ! */ |
| 78 | { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */ |
| 79 | { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */ |
| 80 | { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */ |
| 81 | { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */ |
| 82 | { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */ |
Willy Tarreau | 4bf9963 | 2014-06-13 12:21:40 +0200 | [diff] [blame] | 83 | { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 84 | { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 85 | { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */ |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 86 | { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 87 | { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 88 | { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 89 | { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 90 | { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 91 | { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */ |
| 92 | { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 93 | { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 94 | { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 95 | { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 96 | { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */ |
| 97 | { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 98 | { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 99 | { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */ |
| 100 | { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */ |
| 101 | { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */ |
| 102 | { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */ |
| 103 | { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */ |
| 104 | { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */ |
| 105 | { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */ |
Willy Tarreau | d9ed3d2 | 2014-06-13 12:23:06 +0200 | [diff] [blame] | 106 | { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */ |
| 107 | { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */ |
| 108 | { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */ |
| 109 | { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 110 | { "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] | 111 | { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 112 | { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */ |
| 113 | { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */ |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 114 | { "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] | 115 | { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */ |
| 116 | { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */ |
| 117 | { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */ |
| 118 | { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */ |
| 119 | { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 120 | { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */ |
| 121 | { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 122 | { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */ |
| 123 | { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */ |
| 124 | { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */ |
| 125 | |
| 126 | /* The following tags are deprecated and will be removed soon */ |
| 127 | { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */ |
| 128 | { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */ |
| 129 | { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */ |
| 130 | { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */ |
| 131 | { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */ |
| 132 | { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */ |
| 133 | { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */ |
| 134 | { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */ |
| 135 | { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */ |
| 136 | { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */ |
| 137 | { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 138 | { 0, 0, 0, 0, NULL } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 139 | }; |
| 140 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 141 | char default_http_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format |
| 142 | char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%T] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %Tq %Tw %Tc %Tr %Tt %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl"; |
| 143 | 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] | 144 | char *log_format = NULL; |
| 145 | |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 146 | /* This is a global syslog line, common to all outgoing messages. It begins |
| 147 | * with the syslog tag and the date that are updated by update_log_hdr(). |
| 148 | */ |
Willy Tarreau | dc2695c | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 149 | char *logline = NULL; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 150 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 151 | struct logformat_var_args { |
| 152 | char *name; |
| 153 | int mask; |
| 154 | }; |
| 155 | |
| 156 | struct logformat_var_args var_args_list[] = { |
| 157 | // global |
| 158 | { "M", LOG_OPT_MANDATORY }, |
| 159 | { "Q", LOG_OPT_QUOTE }, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 160 | { "X", LOG_OPT_HEXA }, |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 161 | { 0, 0 } |
| 162 | }; |
| 163 | |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 164 | /* return the name of the directive used in the current proxy for which we're |
| 165 | * currently parsing a header, when it is known. |
| 166 | */ |
| 167 | static inline const char *fmt_directive(const struct proxy *curproxy) |
| 168 | { |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 169 | switch (curproxy->conf.args.ctx) { |
Willy Tarreau | df0a596 | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 170 | case ARGC_ACL: |
| 171 | return "acl"; |
| 172 | case ARGC_STK: |
| 173 | return "stick"; |
| 174 | case ARGC_TRK: |
| 175 | return "track-sc"; |
| 176 | case ARGC_LOG: |
| 177 | return "log-format"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 178 | case ARGC_HRQ: |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 179 | return "http-request"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 180 | case ARGC_HRS: |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 181 | return "http-response"; |
Willy Tarreau | df0a596 | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 182 | case ARGC_UIF: |
| 183 | return "unique-id-format"; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 184 | case ARGC_RDR: |
Willy Tarreau | df0a596 | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 185 | return "redirect"; |
| 186 | case ARGC_CAP: |
| 187 | return "capture"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 188 | default: |
Willy Tarreau | df0a596 | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 189 | return "undefined(please report this bug)"; /* must never happen */ |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 190 | } |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 191 | } |
| 192 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 193 | /* |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 194 | * callback used to configure addr source retrieval |
| 195 | */ |
| 196 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy) |
| 197 | { |
| 198 | curproxy->options2 |= PR_O2_SRC_ADDR; |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /* |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 205 | * Parse args in a logformat_var |
| 206 | */ |
| 207 | int parse_logformat_var_args(char *args, struct logformat_node *node) |
| 208 | { |
| 209 | int i = 0; |
| 210 | int end = 0; |
| 211 | int flags = 0; // 1 = + 2 = - |
| 212 | char *sp = NULL; // start pointer |
| 213 | |
| 214 | if (args == NULL) |
| 215 | return 1; |
| 216 | |
| 217 | while (1) { |
| 218 | if (*args == '\0') |
| 219 | end = 1; |
| 220 | |
| 221 | if (*args == '+') { |
| 222 | // add flag |
| 223 | sp = args + 1; |
| 224 | flags = 1; |
| 225 | } |
| 226 | if (*args == '-') { |
| 227 | // delete flag |
| 228 | sp = args + 1; |
| 229 | flags = 2; |
| 230 | } |
| 231 | |
| 232 | if (*args == '\0' || *args == ',') { |
| 233 | *args = '\0'; |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 234 | for (i = 0; sp && var_args_list[i].name; i++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 235 | if (strcmp(sp, var_args_list[i].name) == 0) { |
| 236 | if (flags == 1) { |
| 237 | node->options |= var_args_list[i].mask; |
| 238 | break; |
| 239 | } else if (flags == 2) { |
| 240 | node->options &= ~var_args_list[i].mask; |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | sp = NULL; |
| 246 | if (end) |
| 247 | break; |
| 248 | } |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 249 | args++; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 250 | } |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | /* |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 255 | * Parse a variable '%varname' or '%{args}varname' in log-format. The caller |
| 256 | * must pass the args part in the <arg> pointer with its length in <arg_len>, |
| 257 | * and varname with its length in <var> and <var_len> respectively. <arg> is |
| 258 | * 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] | 259 | */ |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 260 | 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] | 261 | { |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 262 | int j; |
| 263 | struct logformat_node *node; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 264 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 265 | for (j = 0; logformat_keywords[j].name; j++) { // search a log type |
| 266 | if (strlen(logformat_keywords[j].name) == var_len && |
| 267 | strncmp(var, logformat_keywords[j].name, var_len) == 0) { |
| 268 | if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) { |
| 269 | node = calloc(1, sizeof(struct logformat_node)); |
| 270 | node->type = logformat_keywords[j].type; |
| 271 | node->options = *defoptions; |
| 272 | if (arg_len) { |
| 273 | node->arg = my_strndup(arg, arg_len); |
| 274 | parse_logformat_var_args(node->arg, node); |
| 275 | } |
| 276 | if (node->type == LOG_FMT_GLOBAL) { |
| 277 | *defoptions = node->options; |
| 278 | free(node->arg); |
| 279 | free(node); |
| 280 | } else { |
| 281 | if (logformat_keywords[j].config_callback && |
| 282 | logformat_keywords[j].config_callback(node, curproxy) != 0) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 283 | return -1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 284 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 285 | curproxy->to_log |= logformat_keywords[j].lw; |
| 286 | LIST_ADDQ(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 287 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 288 | if (logformat_keywords[j].replace_by) |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 289 | 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] | 290 | curproxy->conf.args.file, curproxy->conf.args.line, |
| 291 | logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 292 | return 0; |
| 293 | } else { |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 294 | 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] | 295 | curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), |
| 296 | logformat_keywords[j].name); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 297 | return -1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 298 | } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 299 | } |
| 300 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 301 | |
| 302 | j = var[var_len]; |
| 303 | var[var_len] = 0; |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 304 | 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] | 305 | curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy)); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 306 | var[var_len] = j; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * push to the logformat linked list |
| 312 | * |
| 313 | * start: start pointer |
| 314 | * end: end text pointer |
| 315 | * type: string type |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 316 | * list_format: destination list |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 317 | * |
| 318 | * LOG_TEXT: copy chars from start to end excluding end. |
| 319 | * |
| 320 | */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 321 | 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] | 322 | { |
| 323 | char *str; |
| 324 | |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 325 | if (type == LF_TEXT) { /* type text */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 326 | struct logformat_node *node = calloc(1, sizeof(struct logformat_node)); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 327 | str = calloc(end - start + 1, 1); |
| 328 | strncpy(str, start, end - start); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 329 | str[end - start] = '\0'; |
| 330 | node->arg = str; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 331 | node->type = LOG_FMT_TEXT; // type string |
| 332 | LIST_ADDQ(list_format, &node->list); |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 333 | } else if (type == LF_SEPARATOR) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 334 | struct logformat_node *node = calloc(1, sizeof(struct logformat_node)); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 335 | node->type = LOG_FMT_SEPARATOR; |
| 336 | LIST_ADDQ(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | /* |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 341 | * Parse the sample fetch expression <text> and add a node to <list_format> upon |
| 342 | * 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] | 343 | * 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] | 344 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 345 | 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] | 346 | { |
| 347 | char *cmd[2]; |
| 348 | struct sample_expr *expr; |
| 349 | struct logformat_node *node; |
| 350 | int cmd_arg; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 351 | char *errmsg = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 352 | |
| 353 | cmd[0] = text; |
| 354 | cmd[1] = ""; |
| 355 | cmd_arg = 0; |
| 356 | |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 357 | 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] | 358 | if (!expr) { |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 359 | Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n", |
| 360 | curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx), |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 361 | text, errmsg); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 362 | return; |
| 363 | } |
| 364 | |
| 365 | node = calloc(1, sizeof(struct logformat_node)); |
| 366 | node->type = LOG_FMT_EXPR; |
| 367 | node->expr = expr; |
| 368 | node->options = options; |
| 369 | |
| 370 | if (arg_len) { |
| 371 | node->arg = my_strndup(arg, arg_len); |
| 372 | parse_logformat_var_args(node->arg, node); |
| 373 | } |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 374 | if (expr->fetch->val & cap & SMP_VAL_REQUEST) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 375 | node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */ |
| 376 | |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 377 | if (expr->fetch->val & cap & SMP_VAL_RESPONSE) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 378 | node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */ |
| 379 | |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 380 | if (!(expr->fetch->val & cap)) |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 381 | Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n", |
| 382 | curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx), |
| 383 | text, sample_src_names(expr->fetch->use)); |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 384 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 385 | /* check if we need to allocate an hdr_idx struct for HTTP parsing */ |
| 386 | /* 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] | 387 | curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 388 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 389 | /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags |
| 390 | * needed with some sample fetches (eg: ssl*). We always set it for |
| 391 | * now on, but this will leave with sample capabilities soon. |
Willy Tarreau | 1f31c73 | 2013-01-10 16:22:27 +0100 | [diff] [blame] | 392 | */ |
| 393 | curpx->to_log |= LW_XPRT; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 394 | curpx->to_log |= LW_REQ; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 395 | LIST_ADDQ(list_format, &node->list); |
| 396 | } |
| 397 | |
| 398 | /* |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 399 | * Parse the log_format string and fill a linked list. |
| 400 | * 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] | 401 | * You can set arguments using { } : %{many arguments}varname. |
| 402 | * The curproxy->conf.args.ctx must be set by the caller. |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 403 | * |
| 404 | * str: the string to parse |
| 405 | * curproxy: the proxy affected |
| 406 | * list_format: the destination list |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 407 | * options: LOG_OPT_* to force on every node |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 408 | * cap: all SMP_VAL_* flags supported by the consumer |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 409 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 410 | 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] | 411 | { |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 412 | char *sp, *str, *backfmt; /* start pointer for text parts */ |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 413 | char *arg = NULL; /* start pointer for args */ |
| 414 | char *var = NULL; /* start pointer for vars */ |
| 415 | int arg_len = 0; |
| 416 | int var_len = 0; |
| 417 | int cformat; /* current token format */ |
| 418 | int pformat; /* previous token format */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 419 | struct logformat_node *tmplf, *back; |
| 420 | |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 421 | sp = str = backfmt = strdup(fmt); |
William Lallemand | 1dc00ef | 2012-08-09 16:41:35 +0200 | [diff] [blame] | 422 | curproxy->to_log |= LW_INIT; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 423 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 424 | /* flush the list first. */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 425 | list_for_each_entry_safe(tmplf, back, list_format, list) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 426 | LIST_DEL(&tmplf->list); |
| 427 | free(tmplf); |
| 428 | } |
| 429 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 430 | for (cformat = LF_INIT; cformat != LF_END; str++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 431 | pformat = cformat; |
| 432 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 433 | if (!*str) |
| 434 | cformat = LF_END; // preset it to save all states from doing this |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 435 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 436 | /* The prinicple of the two-step state machine below is to first detect a change, and |
| 437 | * second have all common paths processed at one place. The common paths are the ones |
| 438 | * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END). |
| 439 | * We use the common LF_INIT state to dispatch to the different final states. |
| 440 | */ |
| 441 | switch (pformat) { |
| 442 | case LF_STARTVAR: // text immediately following a '%' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 443 | arg = NULL; var = NULL; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 444 | arg_len = var_len = 0; |
| 445 | if (*str == '{') { // optional argument |
| 446 | cformat = LF_STARG; |
| 447 | arg = str + 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 448 | } |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 449 | else if (*str == '[') { |
| 450 | cformat = LF_STEXPR; |
| 451 | var = str + 1; // store expr in variable name |
| 452 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 453 | else if (isalpha((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 454 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 455 | var = str; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 456 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 457 | else if (*str == '%') |
| 458 | cformat = LF_TEXT; // convert this character to a litteral (useful for '%') |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 459 | else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') { |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 460 | /* single '%' followed by blank or digit, send them both */ |
| 461 | cformat = LF_TEXT; |
| 462 | pformat = LF_TEXT; /* finally we include the previous char as well */ |
| 463 | sp = str - 1; /* send both the '%' and the current char */ |
| 464 | 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", |
| 465 | curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt); |
| 466 | |
| 467 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 468 | else |
| 469 | cformat = LF_INIT; // handle other cases of litterals |
| 470 | break; |
| 471 | |
| 472 | case LF_STARG: // text immediately following '%{' |
| 473 | if (*str == '}') { // end of arg |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 474 | cformat = LF_EDARG; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 475 | arg_len = str - arg; |
| 476 | *str = 0; // used for reporting errors |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 477 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 478 | break; |
| 479 | |
| 480 | case LF_EDARG: // text immediately following '%{arg}' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 481 | if (*str == '[') { |
| 482 | cformat = LF_STEXPR; |
| 483 | var = str + 1; // store expr in variable name |
| 484 | break; |
| 485 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 486 | else if (isalnum((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 487 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 488 | var = str; |
| 489 | break; |
| 490 | } |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 491 | Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n", |
| 492 | curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 493 | cformat = LF_INIT; |
| 494 | break; |
| 495 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 496 | case LF_STEXPR: // text immediately following '%[' |
| 497 | if (*str == ']') { // end of arg |
| 498 | cformat = LF_EDEXPR; |
| 499 | var_len = str - var; |
| 500 | *str = 0; // needed for parsing the expression |
| 501 | } |
| 502 | break; |
| 503 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 504 | case LF_VAR: // text part of a variable name |
| 505 | var_len = str - var; |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 506 | if (!isalnum((unsigned char)*str)) |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 507 | cformat = LF_INIT; // not variable name anymore |
| 508 | break; |
| 509 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 510 | default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 511 | cformat = LF_INIT; |
| 512 | } |
| 513 | |
| 514 | if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */ |
| 515 | switch (*str) { |
| 516 | case '%': cformat = LF_STARTVAR; break; |
| 517 | case ' ': cformat = LF_SEPARATOR; break; |
| 518 | case 0 : cformat = LF_END; break; |
| 519 | default : cformat = LF_TEXT; break; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 523 | if (cformat != pformat || pformat == LF_SEPARATOR) { |
| 524 | switch (pformat) { |
| 525 | case LF_VAR: |
| 526 | parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options); |
| 527 | break; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 528 | case LF_STEXPR: |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 529 | 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] | 530 | break; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 531 | case LF_TEXT: |
| 532 | case LF_SEPARATOR: |
| 533 | add_to_logformat_list(sp, str, pformat, list_format); |
| 534 | break; |
| 535 | } |
| 536 | 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] | 537 | } |
| 538 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 539 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 540 | if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 541 | Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n", |
| 542 | curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), |
| 543 | var ? var : arg ? arg : "%"); |
Willy Tarreau | 3ed22a4 | 2012-12-23 17:29:07 +0100 | [diff] [blame] | 544 | |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 545 | free(backfmt); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 546 | } |
| 547 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 548 | /* |
| 549 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 550 | * mode during startup. |
| 551 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 552 | void Alert(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 553 | { |
| 554 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 555 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 556 | |
| 557 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 558 | va_start(argp, fmt); |
| 559 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 560 | get_localtime(date.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 561 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 562 | 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] | 563 | vfprintf(stderr, fmt, argp); |
| 564 | fflush(stderr); |
| 565 | va_end(argp); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | |
| 570 | /* |
| 571 | * Displays the message on stderr with the date and pid. |
| 572 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 573 | void Warning(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 574 | { |
| 575 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 576 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 577 | |
| 578 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 579 | va_start(argp, fmt); |
| 580 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 581 | get_localtime(date.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 582 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 583 | 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] | 584 | vfprintf(stderr, fmt, argp); |
| 585 | fflush(stderr); |
| 586 | va_end(argp); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Displays the message on <out> only if quiet mode is not set. |
| 592 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 593 | void qfprintf(FILE *out, const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 594 | { |
| 595 | va_list argp; |
| 596 | |
| 597 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 598 | va_start(argp, fmt); |
| 599 | vfprintf(out, fmt, argp); |
| 600 | fflush(out); |
| 601 | va_end(argp); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * returns log level for <lev> or -1 if not found. |
| 607 | */ |
| 608 | int get_log_level(const char *lev) |
| 609 | { |
| 610 | int level; |
| 611 | |
| 612 | level = NB_LOG_LEVELS - 1; |
| 613 | while (level >= 0 && strcmp(log_levels[level], lev)) |
| 614 | level--; |
| 615 | |
| 616 | return level; |
| 617 | } |
| 618 | |
| 619 | |
| 620 | /* |
| 621 | * returns log facility for <fac> or -1 if not found. |
| 622 | */ |
| 623 | int get_log_facility(const char *fac) |
| 624 | { |
| 625 | int facility; |
| 626 | |
| 627 | facility = NB_LOG_FACILITIES - 1; |
| 628 | while (facility >= 0 && strcmp(log_facilities[facility], fac)) |
| 629 | facility--; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 630 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 631 | return facility; |
| 632 | } |
| 633 | |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 634 | /* |
| 635 | * Write a string in the log string |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 636 | * Take cares of quote options |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 637 | * |
| 638 | * Return the adress of the \0 character, or NULL on error |
| 639 | */ |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 640 | 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] | 641 | { |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 642 | if (size < 2) |
| 643 | return NULL; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 644 | |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 645 | if (node->options & LOG_OPT_QUOTE) { |
| 646 | *(dst++) = '"'; |
| 647 | size--; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 648 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 649 | |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 650 | if (src && len) { |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 651 | if (++len > size) |
| 652 | len = size; |
| 653 | len = strlcpy2(dst, src, len); |
| 654 | |
| 655 | size -= len; |
| 656 | dst += len; |
| 657 | } |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 658 | else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) { |
| 659 | if (size < 2) |
| 660 | return NULL; |
| 661 | *(dst++) = '-'; |
| 662 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 663 | |
| 664 | if (node->options & LOG_OPT_QUOTE) { |
| 665 | if (size < 2) |
| 666 | return NULL; |
| 667 | *(dst++) = '"'; |
| 668 | } |
| 669 | |
| 670 | *dst = '\0'; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 671 | return dst; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 672 | } |
| 673 | |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 674 | static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node) |
| 675 | { |
| 676 | return lf_text_len(dst, src, size, size, node); |
| 677 | } |
| 678 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 679 | /* |
| 680 | * Write a IP adress to the log string |
| 681 | * +X option write in hexadecimal notation, most signifant byte on the left |
| 682 | */ |
| 683 | char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node) |
| 684 | { |
| 685 | char *ret = dst; |
| 686 | int iret; |
| 687 | char pn[INET6_ADDRSTRLEN]; |
| 688 | |
| 689 | if (node->options & LOG_OPT_HEXA) { |
| 690 | const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr; |
| 691 | iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]); |
| 692 | if (iret < 0 || iret > size) |
| 693 | return NULL; |
| 694 | ret += iret; |
| 695 | } else { |
| 696 | addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn)); |
| 697 | ret = lf_text(dst, pn, size, node); |
| 698 | if (ret == NULL) |
| 699 | return NULL; |
| 700 | } |
| 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Write a port to the log |
| 706 | * +X option write in hexadecimal notation, most signifant byte on the left |
| 707 | */ |
| 708 | char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node) |
| 709 | { |
| 710 | char *ret = dst; |
| 711 | int iret; |
| 712 | |
| 713 | if (node->options & LOG_OPT_HEXA) { |
| 714 | const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port; |
| 715 | iret = snprintf(dst, size, "%02X%02X", port[0], port[1]); |
| 716 | if (iret < 0 || iret > size) |
| 717 | return NULL; |
| 718 | ret += iret; |
| 719 | } else { |
| 720 | ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size); |
| 721 | if (ret == NULL) |
| 722 | return NULL; |
| 723 | } |
| 724 | return ret; |
| 725 | } |
| 726 | |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 727 | /* Re-generate the syslog header at the beginning of logline once a second and |
| 728 | * return the pointer to the first character after the header. |
| 729 | */ |
| 730 | static char *update_log_hdr() |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 731 | { |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 732 | static long tvsec; |
| 733 | static char *dataptr = NULL; /* backup of last end of header, NULL first time */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 734 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 735 | if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 736 | /* this string is rebuild only once a second */ |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 737 | struct tm tm; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 738 | int hdr_len; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 739 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 740 | tvsec = date.tv_sec; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 741 | get_localtime(tvsec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 742 | |
Willy Tarreau | dc2695c | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 743 | hdr_len = snprintf(logline, global.max_syslog_len, |
Joe Williams | df5b38f | 2010-12-29 17:05:48 +0100 | [diff] [blame] | 744 | "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 745 | monthname[tm.tm_mon], |
| 746 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Joe Williams | df5b38f | 2010-12-29 17:05:48 +0100 | [diff] [blame] | 747 | global.log_send_hostname ? global.log_send_hostname : "", |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 748 | global.log_tag, pid); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 749 | /* WARNING: depending upon implementations, snprintf may return |
| 750 | * either -1 or the number of bytes that would be needed to store |
| 751 | * the total message. In both cases, we must adjust it. |
| 752 | */ |
Willy Tarreau | dc2695c | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 753 | if (hdr_len < 0 || hdr_len > global.max_syslog_len) |
| 754 | hdr_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 755 | |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 756 | dataptr = logline + hdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 757 | } |
| 758 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 759 | return dataptr; |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * This function adds a header to the message and sends the syslog message |
Willy Tarreau | 53bf6af | 2012-02-24 11:46:54 +0100 | [diff] [blame] | 764 | * using a printf format string. It expects an LF-terminated message. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 765 | */ |
| 766 | void send_log(struct proxy *p, int level, const char *format, ...) |
| 767 | { |
| 768 | va_list argp; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 769 | char *dataptr; |
| 770 | int data_len; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 771 | |
Willy Tarreau | ab34c8f | 2015-01-15 16:29:53 +0100 | [diff] [blame] | 772 | if (level < 0 || format == NULL || logline == NULL) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 773 | return; |
| 774 | |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 775 | dataptr = update_log_hdr(); /* update log header and skip it */ |
| 776 | data_len = dataptr - logline; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 777 | |
| 778 | va_start(argp, format); |
Willy Tarreau | dc2695c | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 779 | data_len += vsnprintf(dataptr, logline + global.max_syslog_len - dataptr, format, argp); |
| 780 | if (data_len < 0 || data_len > global.max_syslog_len) |
| 781 | data_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 782 | va_end(argp); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 783 | |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 784 | __send_log(p, level, logline, data_len); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* |
| 788 | * This function sends a syslog message. |
| 789 | * It doesn't care about errors nor does it report them. |
Willy Tarreau | 53bf6af | 2012-02-24 11:46:54 +0100 | [diff] [blame] | 790 | * It overrides the last byte (message[size-1]) with an LF character. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 791 | */ |
| 792 | void __send_log(struct proxy *p, int level, char *message, size_t size) |
| 793 | { |
| 794 | static int logfdunix = -1; /* syslog to AF_UNIX socket */ |
| 795 | static int logfdinet = -1; /* syslog to AF_INET socket */ |
| 796 | static char *dataptr = NULL; |
| 797 | int fac_level; |
| 798 | struct list *logsrvs = NULL; |
| 799 | struct logsrv *tmp = NULL; |
| 800 | int nblogger; |
| 801 | char *log_ptr; |
| 802 | |
| 803 | dataptr = message; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 804 | |
| 805 | if (p == NULL) { |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 806 | if (!LIST_ISEMPTY(&global.logsrvs)) { |
| 807 | logsrvs = &global.logsrvs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 808 | } |
| 809 | } else { |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 810 | if (!LIST_ISEMPTY(&p->logsrvs)) { |
| 811 | logsrvs = &p->logsrvs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 812 | } |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 813 | } |
| 814 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 815 | if (!logsrvs) |
| 816 | return; |
| 817 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 818 | /* Send log messages to syslog server. */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 819 | nblogger = 0; |
| 820 | list_for_each_entry(tmp, logsrvs, list) { |
| 821 | const struct logsrv *logsrv = tmp; |
David du Colombier | 11bcb6c | 2011-03-24 12:23:00 +0100 | [diff] [blame] | 822 | int *plogfd = logsrv->addr.ss_family == AF_UNIX ? |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 823 | &logfdunix : &logfdinet; |
| 824 | int sent; |
Willy Tarreau | dc2695c | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 825 | int max; |
| 826 | char backup; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 827 | |
Willy Tarreau | 5304569 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 828 | nblogger++; |
| 829 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 830 | /* 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] | 831 | if (level > logsrv->level) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 832 | continue; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 833 | |
Willy Tarreau | 5304569 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 834 | if (unlikely(*plogfd < 0)) { |
| 835 | /* socket not successfully initialized yet */ |
| 836 | int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP; |
| 837 | |
| 838 | if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) { |
| 839 | Alert("socket for logger #%d failed: %s (errno=%d)\n", |
| 840 | nblogger, strerror(errno), errno); |
| 841 | continue; |
| 842 | } |
| 843 | /* we don't want to receive anything on this socket */ |
| 844 | setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero)); |
| 845 | /* does nothing under Linux, maybe needed for others */ |
| 846 | shutdown(*plogfd, SHUT_RD); |
| 847 | } |
| 848 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 849 | /* For each target, we may have a different facility. |
| 850 | * We can also have a different log level for each message. |
| 851 | * This induces variations in the message header length. |
| 852 | * Since we don't want to recompute it each time, nor copy it every |
| 853 | * time, we only change the facility in the pre-computed header, |
| 854 | * and we change the pointer to the header accordingly. |
| 855 | */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 856 | fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 857 | log_ptr = dataptr + 3; /* last digit of the log level */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 858 | do { |
| 859 | *log_ptr = '0' + fac_level % 10; |
| 860 | fac_level /= 10; |
| 861 | log_ptr--; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 862 | } while (fac_level && log_ptr > dataptr); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 863 | *log_ptr = '<'; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 864 | |
Willy Tarreau | dc2695c | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 865 | max = size - (log_ptr - dataptr); |
| 866 | if (max > logsrv->maxlen) |
| 867 | max = logsrv->maxlen; |
| 868 | |
| 869 | /* insert a \n at the end of the message, but save what was |
| 870 | * there first because we could have different max lengths |
| 871 | * for different log targets. |
| 872 | */ |
| 873 | backup = log_ptr[max - 1]; |
| 874 | log_ptr[max - 1] = '\n'; |
| 875 | |
| 876 | sent = sendto(*plogfd, log_ptr, max, |
Willy Tarreau | 1b4b7ce | 2011-04-05 16:56:50 +0200 | [diff] [blame] | 877 | MSG_DONTWAIT | MSG_NOSIGNAL, |
| 878 | (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr)); |
Willy Tarreau | dc2695c | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 879 | |
| 880 | log_ptr[max - 1] = backup; |
| 881 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 882 | if (sent < 0) { |
| 883 | Alert("sendto logger #%d failed: %s (errno=%d)\n", |
| 884 | nblogger, strerror(errno), errno); |
| 885 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 889 | extern fd_set hdr_encode_map[]; |
| 890 | extern fd_set url_encode_map[]; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 891 | extern fd_set http_encode_map[]; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 892 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 893 | |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 894 | 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] | 895 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 896 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 897 | Set-cookie Updated, unknown, unknown */ |
| 898 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 899 | /* |
| 900 | * try to write a character if there is enough space, or goto out |
| 901 | */ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 902 | #define LOGCHAR(x) do { \ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 903 | if (tmplog < dst + maxsize - 1) { \ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 904 | *(tmplog++) = (x); \ |
| 905 | } else { \ |
| 906 | goto out; \ |
| 907 | } \ |
| 908 | } while(0) |
| 909 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 910 | |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 911 | /* Builds a log line in <dst> based on <list_format>, and stops before reaching |
| 912 | * <maxsize> characters. Returns the size of the output string in characters, |
| 913 | * not counting the trailing zero which is always added if the resulting size |
| 914 | * is not zero. |
| 915 | */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 916 | int build_logline(struct session *s, char *dst, size_t maxsize, struct list *list_format) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 917 | { |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 918 | struct proxy *fe = s->fe; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 919 | struct proxy *be = s->be; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 920 | struct http_txn *txn = &s->txn; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 921 | char *uri; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 922 | struct tm tm; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 923 | int t_request; |
| 924 | int hdr; |
| 925 | int last_isspace = 1; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 926 | char *tmplog; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 927 | char *ret; |
| 928 | int iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 929 | struct logformat_node *tmp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 930 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 931 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 932 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 933 | t_request = -1; |
| 934 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 935 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 936 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 937 | tmplog = dst; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 938 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 939 | /* fill logbuffer */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 940 | if (LIST_ISEMPTY(list_format)) |
| 941 | return 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 942 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 943 | list_for_each_entry(tmp, list_format, list) { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 944 | struct connection *conn; |
Willy Tarreau | 4f65356 | 2012-10-12 19:48:16 +0200 | [diff] [blame] | 945 | const char *src = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 946 | struct sample *key; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 947 | const struct chunk empty = { NULL, 0, 0 }; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 948 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 949 | switch (tmp->type) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 950 | case LOG_FMT_SEPARATOR: |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 951 | if (!last_isspace) { |
| 952 | LOGCHAR(' '); |
| 953 | last_isspace = 1; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 954 | } |
| 955 | break; |
| 956 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 957 | case LOG_FMT_TEXT: // text |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 958 | src = tmp->arg; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 959 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 960 | if (iret == 0) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 961 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 962 | tmplog += iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 963 | last_isspace = 0; |
| 964 | break; |
| 965 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 966 | case LOG_FMT_EXPR: // sample expression, may be request or response |
| 967 | key = NULL; |
| 968 | if (tmp->options & LOG_OPT_REQ_CAP) |
| 969 | key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr); |
| 970 | if (!key && (tmp->options & LOG_OPT_RES_CAP)) |
| 971 | key = sample_fetch_string(be, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 972 | if (tmp->options & LOG_OPT_HTTP) |
| 973 | ret = encode_chunk(tmplog, dst + maxsize, |
| 974 | '%', http_encode_map, key ? &key->data.str : &empty); |
| 975 | else |
| 976 | ret = lf_text_len(tmplog, key ? key->data.str.str : NULL, key ? key->data.str.len : 0, dst + maxsize - tmplog, tmp); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 977 | if (ret == 0) |
| 978 | goto out; |
| 979 | tmplog = ret; |
| 980 | last_isspace = 0; |
| 981 | break; |
| 982 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 983 | case LOG_FMT_CLIENTIP: // %ci |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 984 | conn = objt_conn(s->req->prod->end); |
| 985 | if (conn) |
| 986 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 987 | else |
| 988 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 989 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 990 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 991 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 992 | last_isspace = 0; |
| 993 | break; |
| 994 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 995 | case LOG_FMT_CLIENTPORT: // %cp |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 996 | conn = objt_conn(s->req->prod->end); |
| 997 | if (conn) { |
| 998 | if (conn->addr.from.ss_family == AF_UNIX) { |
| 999 | ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog); |
| 1000 | } else { |
| 1001 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, |
| 1002 | dst + maxsize - tmplog, tmp); |
| 1003 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1004 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1005 | else |
| 1006 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1007 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1008 | if (ret == NULL) |
| 1009 | goto out; |
| 1010 | tmplog = ret; |
| 1011 | last_isspace = 0; |
| 1012 | break; |
| 1013 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1014 | case LOG_FMT_FRONTENDIP: // %fi |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1015 | conn = objt_conn(s->req->prod->end); |
| 1016 | if (conn) { |
| 1017 | conn_get_to_addr(conn); |
| 1018 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1019 | } |
| 1020 | else |
| 1021 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1022 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1023 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1024 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1025 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1026 | last_isspace = 0; |
| 1027 | break; |
| 1028 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1029 | case LOG_FMT_FRONTENDPORT: // %fp |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1030 | conn = objt_conn(s->req->prod->end); |
| 1031 | if (conn) { |
| 1032 | conn_get_to_addr(conn); |
| 1033 | if (conn->addr.to.ss_family == AF_UNIX) |
| 1034 | ret = ltoa_o(s->listener->luid, tmplog, dst + maxsize - tmplog); |
| 1035 | else |
| 1036 | 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] | 1037 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1038 | else |
| 1039 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1040 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1041 | if (ret == NULL) |
| 1042 | goto out; |
| 1043 | tmplog = ret; |
| 1044 | last_isspace = 0; |
| 1045 | break; |
| 1046 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1047 | case LOG_FMT_BACKENDIP: // %bi |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1048 | conn = objt_conn(s->req->cons->end); |
| 1049 | if (conn) |
| 1050 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1051 | else |
| 1052 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1053 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1054 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1055 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1056 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1057 | last_isspace = 0; |
| 1058 | break; |
| 1059 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1060 | case LOG_FMT_BACKENDPORT: // %bp |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1061 | conn = objt_conn(s->req->cons->end); |
| 1062 | if (conn) |
| 1063 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1064 | else |
| 1065 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1066 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1067 | if (ret == NULL) |
| 1068 | goto out; |
| 1069 | tmplog = ret; |
| 1070 | last_isspace = 0; |
| 1071 | break; |
| 1072 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1073 | case LOG_FMT_SERVERIP: // %si |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1074 | conn = objt_conn(s->req->cons->end); |
| 1075 | if (conn) |
| 1076 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1077 | else |
| 1078 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1079 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1080 | if (ret == NULL) |
| 1081 | goto out; |
| 1082 | tmplog = ret; |
| 1083 | last_isspace = 0; |
| 1084 | break; |
| 1085 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1086 | case LOG_FMT_SERVERPORT: // %sp |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1087 | conn = objt_conn(s->req->cons->end); |
| 1088 | if (conn) |
| 1089 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1090 | else |
| 1091 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1092 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1093 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1094 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1095 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1096 | last_isspace = 0; |
| 1097 | break; |
| 1098 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1099 | case LOG_FMT_DATE: // %t |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1100 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1101 | ret = date2str_log(tmplog, &tm, &(s->logs.accept_date), |
| 1102 | dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1103 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1104 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1105 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1106 | last_isspace = 0; |
| 1107 | break; |
| 1108 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1109 | case LOG_FMT_DATEGMT: // %T |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1110 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1111 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1112 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1113 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1114 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1115 | last_isspace = 0; |
| 1116 | break; |
| 1117 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 1118 | case LOG_FMT_DATELOCAL: // %Tl |
| 1119 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
| 1120 | ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog); |
| 1121 | if (ret == NULL) |
| 1122 | goto out; |
| 1123 | tmplog = ret; |
| 1124 | last_isspace = 0; |
| 1125 | break; |
| 1126 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1127 | case LOG_FMT_TS: // %Ts |
| 1128 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 1129 | if (tmp->options & LOG_OPT_HEXA) { |
| 1130 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec); |
| 1131 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1132 | goto out; |
| 1133 | last_isspace = 0; |
| 1134 | tmplog += iret; |
| 1135 | } else { |
| 1136 | ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog); |
| 1137 | if (ret == NULL) |
| 1138 | goto out; |
| 1139 | tmplog = ret; |
| 1140 | last_isspace = 0; |
| 1141 | } |
| 1142 | break; |
| 1143 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1144 | case LOG_FMT_MS: // %ms |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1145 | if (tmp->options & LOG_OPT_HEXA) { |
| 1146 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000); |
| 1147 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1148 | goto out; |
| 1149 | last_isspace = 0; |
| 1150 | tmplog += iret; |
| 1151 | } else { |
| 1152 | if ((dst + maxsize - tmplog) < 4) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1153 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 1154 | ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000, |
| 1155 | tmplog, 4); |
| 1156 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1157 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 1158 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1159 | last_isspace = 0; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1160 | } |
| 1161 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1162 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1163 | case LOG_FMT_FRONTEND: // %f |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1164 | src = fe->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1165 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1166 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1167 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1168 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1169 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1170 | break; |
| 1171 | |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1172 | case LOG_FMT_FRONTEND_XPRT: // %ft |
| 1173 | src = fe->id; |
| 1174 | if (tmp->options & LOG_OPT_QUOTE) |
| 1175 | LOGCHAR('"'); |
| 1176 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
| 1177 | if (iret == 0) |
| 1178 | goto out; |
| 1179 | tmplog += iret; |
| 1180 | #ifdef USE_OPENSSL |
| 1181 | if (s->listener->xprt == &ssl_sock) |
| 1182 | LOGCHAR('~'); |
| 1183 | #endif |
| 1184 | if (tmp->options & LOG_OPT_QUOTE) |
| 1185 | LOGCHAR('"'); |
| 1186 | last_isspace = 0; |
| 1187 | break; |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1188 | #ifdef USE_OPENSSL |
| 1189 | case LOG_FMT_SSL_CIPHER: // %sslc |
| 1190 | src = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1191 | conn = objt_conn(s->si[0].end); |
| 1192 | if (conn) { |
| 1193 | if (s->listener->xprt == &ssl_sock) |
| 1194 | src = ssl_sock_get_cipher_name(conn); |
| 1195 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1196 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1197 | if (ret == NULL) |
| 1198 | goto out; |
| 1199 | tmplog = ret; |
| 1200 | last_isspace = 0; |
| 1201 | break; |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1202 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1203 | case LOG_FMT_SSL_VERSION: // %sslv |
| 1204 | src = NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1205 | conn = objt_conn(s->si[0].end); |
| 1206 | if (conn) { |
| 1207 | if (s->listener->xprt == &ssl_sock) |
| 1208 | src = ssl_sock_get_proto_version(conn); |
| 1209 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1210 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1211 | if (ret == NULL) |
| 1212 | goto out; |
| 1213 | tmplog = ret; |
| 1214 | last_isspace = 0; |
| 1215 | break; |
| 1216 | #endif |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1217 | case LOG_FMT_BACKEND: // %b |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1218 | src = be->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1219 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1220 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1221 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1222 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1223 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1224 | break; |
| 1225 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1226 | case LOG_FMT_SERVER: // %s |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1227 | switch (obj_type(s->target)) { |
| 1228 | case OBJ_TYPE_SERVER: |
| 1229 | src = objt_server(s->target)->id; |
| 1230 | break; |
| 1231 | case OBJ_TYPE_APPLET: |
| 1232 | src = objt_applet(s->target)->name; |
| 1233 | break; |
| 1234 | default: |
| 1235 | src = "<NOSRV>"; |
| 1236 | break; |
| 1237 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1238 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1239 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1240 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1241 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1242 | last_isspace = 0; |
| 1243 | break; |
| 1244 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1245 | case LOG_FMT_TQ: // %Tq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1246 | ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1247 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1248 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1249 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1250 | last_isspace = 0; |
| 1251 | break; |
| 1252 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1253 | case LOG_FMT_TW: // %Tw |
| 1254 | 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] | 1255 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1256 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1257 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1258 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1259 | last_isspace = 0; |
| 1260 | break; |
| 1261 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1262 | case LOG_FMT_TC: // %Tc |
| 1263 | 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] | 1264 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1265 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1266 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1267 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1268 | last_isspace = 0; |
| 1269 | break; |
| 1270 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1271 | case LOG_FMT_TR: // %Tr |
| 1272 | 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] | 1273 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1274 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1275 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1276 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1277 | last_isspace = 0; |
| 1278 | break; |
| 1279 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1280 | case LOG_FMT_TT: // %Tt |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1281 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1282 | LOGCHAR('+'); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1283 | ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1284 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1285 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1286 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1287 | last_isspace = 0; |
| 1288 | break; |
| 1289 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1290 | case LOG_FMT_STATUS: // %ST |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1291 | ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1292 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1293 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1294 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1295 | last_isspace = 0; |
| 1296 | break; |
| 1297 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1298 | case LOG_FMT_BYTES: // %B |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1299 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1300 | LOGCHAR('+'); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1301 | ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1302 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1303 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1304 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1305 | last_isspace = 0; |
| 1306 | break; |
| 1307 | |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 1308 | case LOG_FMT_BYTES_UP: // %U |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 1309 | ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog); |
| 1310 | if (ret == NULL) |
| 1311 | goto out; |
| 1312 | tmplog = ret; |
| 1313 | last_isspace = 0; |
| 1314 | break; |
| 1315 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1316 | case LOG_FMT_CCLIENT: // %CC |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1317 | src = txn->cli_cookie; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1318 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1319 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1320 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1321 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1322 | last_isspace = 0; |
| 1323 | break; |
| 1324 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1325 | case LOG_FMT_CSERVER: // %CS |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1326 | src = txn->srv_cookie; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1327 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1328 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1329 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1330 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1331 | last_isspace = 0; |
| 1332 | break; |
| 1333 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1334 | case LOG_FMT_TERMSTATE: // %ts |
Willy Tarreau | 6580c06 | 2012-03-12 15:09:42 +0100 | [diff] [blame] | 1335 | LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]); |
| 1336 | LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]); |
| 1337 | *tmplog = '\0'; |
| 1338 | last_isspace = 0; |
| 1339 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1340 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1341 | case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1342 | LOGCHAR(sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]); |
| 1343 | LOGCHAR(sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]); |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 1344 | LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-'); |
| 1345 | LOGCHAR((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] | 1346 | last_isspace = 0; |
| 1347 | break; |
| 1348 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1349 | case LOG_FMT_ACTCONN: // %ac |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1350 | ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1351 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1352 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1353 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1354 | last_isspace = 0; |
| 1355 | break; |
| 1356 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1357 | case LOG_FMT_FECONN: // %fc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1358 | ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1359 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1360 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1361 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1362 | last_isspace = 0; |
| 1363 | break; |
| 1364 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1365 | case LOG_FMT_BECONN: // %bc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1366 | ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1367 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1368 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1369 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1370 | last_isspace = 0; |
| 1371 | break; |
| 1372 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1373 | case LOG_FMT_SRVCONN: // %sc |
Willy Tarreau | 54a08d3 | 2012-11-12 01:14:56 +0100 | [diff] [blame] | 1374 | ret = ultoa_o(objt_server(s->target) ? |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1375 | objt_server(s->target)->cur_sess : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1376 | 0, tmplog, dst + maxsize - tmplog); |
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 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1383 | case LOG_FMT_RETRIES: // %rq |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1384 | if (s->flags & SN_REDISP) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1385 | LOGCHAR('+'); |
| 1386 | ret = ltoa_o((s->req->cons->conn_retries>0) ? |
| 1387 | (be->conn_retries - s->req->cons->conn_retries) : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1388 | be->conn_retries, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1389 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1390 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1391 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1392 | last_isspace = 0; |
| 1393 | break; |
| 1394 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1395 | case LOG_FMT_SRVQUEUE: // %sq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1396 | ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1397 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1398 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1399 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1400 | last_isspace = 0; |
| 1401 | break; |
| 1402 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1403 | case LOG_FMT_BCKQUEUE: // %bq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1404 | ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1405 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1406 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1407 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1408 | last_isspace = 0; |
| 1409 | break; |
| 1410 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1411 | case LOG_FMT_HDRREQUEST: // %hr |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1412 | /* request header */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1413 | if (fe->nb_req_cap && txn->req.cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1414 | if (tmp->options & LOG_OPT_QUOTE) |
| 1415 | LOGCHAR('"'); |
| 1416 | LOGCHAR('{'); |
| 1417 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1418 | if (hdr) |
| 1419 | LOGCHAR('|'); |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1420 | if (txn->req.cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1421 | ret = encode_string(tmplog, dst + maxsize, |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1422 | '#', hdr_encode_map, txn->req.cap[hdr]); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1423 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1424 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1425 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1426 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1427 | } |
| 1428 | LOGCHAR('}'); |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1429 | if (tmp->options & LOG_OPT_QUOTE) |
| 1430 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1431 | last_isspace = 0; |
| 1432 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1433 | break; |
| 1434 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1435 | case LOG_FMT_HDRREQUESTLIST: // %hrl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1436 | /* request header list */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1437 | if (fe->nb_req_cap && txn->req.cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1438 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1439 | if (hdr > 0) |
| 1440 | LOGCHAR(' '); |
| 1441 | if (tmp->options & LOG_OPT_QUOTE) |
| 1442 | LOGCHAR('"'); |
| 1443 | if (txn->req.cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1444 | ret = encode_string(tmplog, dst + maxsize, |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1445 | '#', hdr_encode_map, txn->req.cap[hdr]); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1446 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1447 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1448 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1449 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 1450 | LOGCHAR('-'); |
| 1451 | if (tmp->options & LOG_OPT_QUOTE) |
| 1452 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1453 | last_isspace = 0; |
| 1454 | } |
| 1455 | } |
| 1456 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1457 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1458 | |
| 1459 | case LOG_FMT_HDRRESPONS: // %hs |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1460 | /* response header */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1461 | if (fe->nb_rsp_cap && txn->rsp.cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1462 | if (tmp->options & LOG_OPT_QUOTE) |
| 1463 | LOGCHAR('"'); |
| 1464 | LOGCHAR('{'); |
| 1465 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1466 | if (hdr) |
| 1467 | LOGCHAR('|'); |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1468 | if (txn->rsp.cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1469 | ret = encode_string(tmplog, dst + maxsize, |
| 1470 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1471 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1472 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1473 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1474 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1475 | } |
| 1476 | LOGCHAR('}'); |
| 1477 | last_isspace = 0; |
| 1478 | if (tmp->options & LOG_OPT_QUOTE) |
| 1479 | LOGCHAR('"'); |
| 1480 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1481 | break; |
| 1482 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1483 | case LOG_FMT_HDRRESPONSLIST: // %hsl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1484 | /* response header list */ |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1485 | if (fe->nb_rsp_cap && txn->rsp.cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1486 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1487 | if (hdr > 0) |
| 1488 | LOGCHAR(' '); |
| 1489 | if (tmp->options & LOG_OPT_QUOTE) |
| 1490 | LOGCHAR('"'); |
| 1491 | if (txn->rsp.cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1492 | ret = encode_string(tmplog, dst + maxsize, |
| 1493 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1494 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1495 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1496 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1497 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 1498 | LOGCHAR('-'); |
| 1499 | if (tmp->options & LOG_OPT_QUOTE) |
| 1500 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1501 | last_isspace = 0; |
| 1502 | } |
| 1503 | } |
| 1504 | break; |
| 1505 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1506 | case LOG_FMT_REQ: // %r |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1507 | /* Request */ |
| 1508 | if (tmp->options & LOG_OPT_QUOTE) |
| 1509 | LOGCHAR('"'); |
| 1510 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1511 | ret = encode_string(tmplog, dst + maxsize, |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1512 | '#', url_encode_map, uri); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1513 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1514 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1515 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1516 | if (tmp->options & LOG_OPT_QUOTE) |
| 1517 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1518 | last_isspace = 0; |
| 1519 | break; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1520 | |
| 1521 | case LOG_FMT_COUNTER: // %rt |
| 1522 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 1523 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1524 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1525 | goto out; |
| 1526 | last_isspace = 0; |
| 1527 | tmplog += iret; |
| 1528 | } else { |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 1529 | ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1530 | if (ret == NULL) |
| 1531 | goto out; |
| 1532 | tmplog = ret; |
| 1533 | last_isspace = 0; |
| 1534 | } |
| 1535 | break; |
| 1536 | |
| 1537 | case LOG_FMT_HOSTNAME: // %H |
| 1538 | src = hostname; |
| 1539 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1540 | if (ret == NULL) |
| 1541 | goto out; |
| 1542 | tmplog = ret; |
| 1543 | last_isspace = 0; |
| 1544 | break; |
| 1545 | |
| 1546 | case LOG_FMT_PID: // %pid |
| 1547 | if (tmp->options & LOG_OPT_HEXA) { |
| 1548 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid); |
| 1549 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1550 | goto out; |
| 1551 | last_isspace = 0; |
| 1552 | tmplog += iret; |
| 1553 | } else { |
| 1554 | ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog); |
| 1555 | if (ret == NULL) |
| 1556 | goto out; |
| 1557 | tmplog = ret; |
| 1558 | last_isspace = 0; |
| 1559 | } |
| 1560 | break; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1561 | |
| 1562 | case LOG_FMT_UNIQUEID: // %ID |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 1563 | ret = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1564 | src = s->unique_id; |
Thierry FOURNIER | 1be6910 | 2014-04-15 01:38:48 +0200 | [diff] [blame] | 1565 | ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1566 | if (ret == NULL) |
| 1567 | goto out; |
| 1568 | tmplog = ret; |
| 1569 | last_isspace = 0; |
| 1570 | break; |
| 1571 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | out: |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1576 | /* *tmplog is a unused character */ |
| 1577 | *tmplog = '\0'; |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1578 | return tmplog - dst; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1579 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1580 | } |
| 1581 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1582 | /* |
| 1583 | * send a log for the session when we have enough info about it. |
| 1584 | * Will not log if the frontend has no log defined. |
| 1585 | */ |
| 1586 | void sess_log(struct session *s) |
| 1587 | { |
| 1588 | char *tmplog; |
| 1589 | int size, err, level; |
| 1590 | |
| 1591 | /* if we don't want to log normal traffic, return now */ |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 1592 | err = (s->flags & SN_REDISP) || |
| 1593 | ((s->flags & SN_ERR_MASK) > SN_ERR_LOCAL) || |
| 1594 | (((s->flags & SN_ERR_MASK) == SN_ERR_NONE) && |
| 1595 | (s->req->cons->conn_retries != s->be->conn_retries)) || |
| 1596 | ((s->fe->mode == PR_MODE_HTTP) && s->txn.status >= 500); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1597 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1598 | if (!err && (s->fe->options2 & PR_O2_NOLOGNORM)) |
| 1599 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1600 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1601 | if (LIST_ISEMPTY(&s->fe->logsrvs)) |
| 1602 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1603 | |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 1604 | if (s->logs.level) { /* loglevel was overridden */ |
| 1605 | if (s->logs.level == -1) { |
| 1606 | s->logs.logwait = 0; /* logs disabled */ |
| 1607 | return; |
| 1608 | } |
| 1609 | level = s->logs.level - 1; |
| 1610 | } |
| 1611 | else { |
| 1612 | level = LOG_INFO; |
| 1613 | if (err && (s->fe->options2 & PR_O2_LOGERRORS)) |
| 1614 | level = LOG_ERR; |
| 1615 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1616 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 1617 | /* if unique-id was not generated */ |
| 1618 | if (!s->unique_id && !LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 1619 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL) |
| 1620 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
| 1621 | } |
| 1622 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1623 | tmplog = update_log_hdr(); |
| 1624 | size = tmplog - logline; |
Willy Tarreau | dc2695c | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1625 | size += build_logline(s, tmplog, global.max_syslog_len - size, &s->fe->logformat); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1626 | if (size > 0) { |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1627 | __send_log(s->fe, level, logline, size + 1); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1628 | s->logs.logwait = 0; |
| 1629 | } |
| 1630 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1631 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1632 | /* |
| 1633 | * Local variables: |
| 1634 | * c-indent-level: 8 |
| 1635 | * c-basic-offset: 8 |
| 1636 | * End: |
| 1637 | */ |