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