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