1 /*
2   include/proto/log.h
3   This file contains definitions of log-related functions, structures,
4   and macros.
5 
6   Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
7 
8   This library is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Lesser General Public
10   License as published by the Free Software Foundation, version 2.1
11   exclusively.
12 
13   This library is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   Lesser General Public License for more details.
17 
18   You should have received a copy of the GNU Lesser General Public
19   License along with this library; if not, write to the Free Software
20   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 */
22 
23 #ifndef _PROTO_LOG_H
24 #define _PROTO_LOG_H
25 
26 #include <stdio.h>
27 #include <syslog.h>
28 
29 #include <common/config.h>
30 #include <common/memory.h>
31 #include <common/hathreads.h>
32 
33 #include <types/log.h>
34 #include <types/proxy.h>
35 #include <types/stream.h>
36 
37 #include <proto/stream.h>
38 
39 extern struct pool_head *pool_head_requri;
40 extern struct pool_head *pool_head_uniqueid;
41 
42 extern char *log_format;
43 extern char default_tcp_log_format[];
44 extern char default_http_log_format[];
45 extern char clf_http_log_format[];
46 
47 extern char default_rfc5424_sd_log_format[];
48 
49 extern unsigned int dropped_logs;
50 
51 extern THREAD_LOCAL char *logheader;
52 extern THREAD_LOCAL char *logheader_rfc5424;
53 extern THREAD_LOCAL char *logline;
54 extern THREAD_LOCAL char *logline_rfc5424;
55 
56 
57 /*
58  * Test if <idx> index numbered from 0 is in <rg> range with low and high
59  * limits of indexes numbered from 1.
60  */
in_smp_log_range(struct smp_log_range * rg,unsigned int idx)61 static inline int in_smp_log_range(struct smp_log_range *rg, unsigned int idx)
62 {
63        if (idx + 1 <= rg->high && idx + 1 >= rg->low)
64                return 1;
65        return 0;
66 }
67 
68 /* Initialize/Deinitialize log buffers used for syslog messages */
69 int init_log_buffers();
70 void deinit_log_buffers();
71 
72 /*
73  * Builds a log line.
74  */
75 int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t maxsize, struct list *list_format);
76 
build_logline(struct stream * s,char * dst,size_t maxsize,struct list * list_format)77 static inline int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
78 {
79 	return sess_build_logline(strm_sess(s), s, dst, maxsize, list_format);
80 }
81 
82 
83 /*
84  * send a log for the stream when we have enough info about it.
85  * Will not log if the frontend has no log defined.
86  */
87 void strm_log(struct stream *s);
88 void sess_log(struct session *sess);
89 
90 /*
91  * add to the logformat linked list
92  */
93 int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err);
94 
95 /*
96  * Parse the log_format string and fill a linked list.
97  * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
98  * You can set arguments using { } : %{many arguments}varname
99  */
100 int parse_logformat_string(const char *str, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err);
101 
102 /* Parse "log" keyword and update the linked list. */
103 int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err);
104 
105 /*
106  * Displays the message on stderr with the date and pid. Overrides the quiet
107  * mode during startup.
108  */
109 void ha_alert(const char *fmt, ...)
110 	__attribute__ ((format(printf, 1, 2)));
111 
112 /*
113  * Displays the message on stderr with the date and pid.
114  */
115 void ha_warning(const char *fmt, ...)
116 	__attribute__ ((format(printf, 1, 2)));
117 
118 /*
119  * Displays the message on stderr with the date and pid.
120  */
121 void ha_notice(const char *fmt, ...)
122 	__attribute__ ((format(printf, 1, 2)));
123 
124 /*
125  * Displays the message on <out> only if quiet mode is not set.
126  */
127 void qfprintf(FILE *out, const char *fmt, ...)
128 	__attribute__ ((format(printf, 2, 3)));
129 
130 /*
131  * This function adds a header to the message and sends the syslog message
132  * using a printf format string
133  */
134 void send_log(struct proxy *p, int level, const char *format, ...)
135 	__attribute__ ((format(printf, 3, 4)));
136 
137 /*
138  * This function sends a syslog message to both log servers of a proxy,
139  * or to global log servers if the proxy is NULL.
140  * It also tries not to waste too much time computing the message header.
141  * It doesn't care about errors nor does it report them.
142  */
143 
144 void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size);
145 
146 /*
147  * returns log format for <fmt> or -1 if not found.
148  */
149 int get_log_format(const char *fmt);
150 
151 /*
152  * returns log level for <lev> or -1 if not found.
153  */
154 int get_log_level(const char *lev);
155 
156 /*
157  * returns log facility for <fac> or -1 if not found.
158  */
159 int get_log_facility(const char *fac);
160 
161 /*
162  * Write a string in the log string
163  * Take cares of quote options
164  *
165  * Return the address of the \0 character, or NULL on error
166  */
167 char *lf_text_len(char *dst, const char *src, size_t len, size_t size, const struct logformat_node *node);
168 
169 /*
170  * Write a IP address to the log string
171  * +X option write in hexadecimal notation, most signifant byte on the left
172  */
173 char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node);
174 
175 /*
176  * Write a port to the log
177  * +X option write in hexadecimal notation, most signifant byte on the left
178  */
179 char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node);
180 
181 
182 #endif /* _PROTO_LOG_H */
183 
184 /*
185  * Local variables:
186  *  c-indent-level: 8
187  *  c-basic-offset: 8
188  * End:
189  */
190