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