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