blob: caddd95b87b381a5c1c9ea6d903175f82e213a3b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreauaeed4a82020-06-04 22:01:04 +02002 * include/haproxy/log.h
3 * This file contains definitions of log-related functions.
4 *
5 * Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreauaeed4a82020-06-04 22:01:04 +020022#ifndef _HAPROXY_LOG_H
23#define _HAPROXY_LOG_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <syslog.h>
26
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log-t.h>
29#include <haproxy/pool-t.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020030#include <haproxy/thread-t.h>
Willy Tarreau43c538e2018-09-05 14:58:15 +020031#include <proto/stream.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020032#include <types/proxy.h>
Willy Tarreau43c538e2018-09-05 14:58:15 +020033
Willy Tarreaubafbe012017-11-24 17:34:44 +010034extern struct pool_head *pool_head_requri;
35extern struct pool_head *pool_head_uniqueid;
Willy Tarreau332f8bf2007-05-13 21:36:56 +020036
Willy Tarreauaeed4a82020-06-04 22:01:04 +020037extern const char *log_levels[];
William Lallemand723b73a2012-02-08 16:37:49 +010038extern char *log_format;
William Lallemandbddd4fd2012-02-27 11:23:10 +010039extern char default_tcp_log_format[];
William Lallemand723b73a2012-02-08 16:37:49 +010040extern char default_http_log_format[];
41extern char clf_http_log_format[];
Dragan Dosen1322d092015-09-22 16:05:32 +020042
Dragan Dosen0b85ece2015-09-25 19:17:44 +020043extern char default_rfc5424_sd_log_format[];
44
Willy Tarreau13ef7732018-11-12 07:25:28 +010045extern unsigned int dropped_logs;
46
Christopher Fauletf8188c62017-06-02 16:20:16 +020047extern THREAD_LOCAL char *logheader;
48extern THREAD_LOCAL char *logheader_rfc5424;
49extern THREAD_LOCAL char *logline;
50extern THREAD_LOCAL char *logline_rfc5424;
William Lallemand723b73a2012-02-08 16:37:49 +010051
William Lallemandbddd4fd2012-02-27 11:23:10 +010052
Christopher Faulet0132d062017-07-26 15:33:35 +020053/* Initialize/Deinitialize log buffers used for syslog messages */
54int init_log_buffers();
55void deinit_log_buffers();
56
Willy Tarreauaeed4a82020-06-04 22:01:04 +020057/* build a log line for the session and an optional stream */
Willy Tarreau43c538e2018-09-05 14:58:15 +020058int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t maxsize, struct list *list_format);
59
William Lallemandbddd4fd2012-02-27 11:23:10 +010060/*
Willy Tarreau87b09662015-04-03 00:22:06 +020061 * send a log for the stream when we have enough info about it.
William Lallemandbddd4fd2012-02-27 11:23:10 +010062 * Will not log if the frontend has no log defined.
63 */
Willy Tarreau87b09662015-04-03 00:22:06 +020064void strm_log(struct stream *s);
Willy Tarreau53839352018-09-05 19:51:10 +020065void sess_log(struct session *sess);
William Lallemandbddd4fd2012-02-27 11:23:10 +010066
Christopher Faulet5c6fefc2019-08-11 19:40:12 +020067/* send a applicative log with custom list of log servers */
68void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
69 __attribute__ ((format(printf, 4, 5)));
70
William Lallemand723b73a2012-02-08 16:37:49 +010071/*
William Lallemand723b73a2012-02-08 16:37:49 +010072 * add to the logformat linked list
73 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010074int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err);
William Lallemand723b73a2012-02-08 16:37:49 +010075
76/*
77 * Parse the log_format string and fill a linked list.
78 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
79 * You can set arguments using { } : %{many arguments}varname
80 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010081int parse_logformat_string(const char *str, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +020082
83/* Parse "log" keyword and update the linked list. */
84int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err);
85
Willy Tarreaubaaee002006-06-26 02:48:02 +020086/*
87 * Displays the message on stderr with the date and pid. Overrides the quiet
88 * mode during startup.
89 */
Christopher Faulet767a84b2017-11-24 16:50:31 +010090void ha_alert(const char *fmt, ...)
Willy Tarreau40d25162009-04-03 12:01:47 +020091 __attribute__ ((format(printf, 1, 2)));
Willy Tarreaubaaee002006-06-26 02:48:02 +020092
93/*
94 * Displays the message on stderr with the date and pid.
95 */
Christopher Faulet767a84b2017-11-24 16:50:31 +010096void ha_warning(const char *fmt, ...)
Willy Tarreau40d25162009-04-03 12:01:47 +020097 __attribute__ ((format(printf, 1, 2)));
Willy Tarreaubaaee002006-06-26 02:48:02 +020098
99/*
William Lallemand9c56a222018-11-21 18:04:52 +0100100 * Displays the message on stderr with the date and pid.
101 */
102void ha_notice(const char *fmt, ...)
103 __attribute__ ((format(printf, 1, 2)));
104
105/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106 * Displays the message on <out> only if quiet mode is not set.
107 */
Willy Tarreau40d25162009-04-03 12:01:47 +0200108void qfprintf(FILE *out, const char *fmt, ...)
109 __attribute__ ((format(printf, 2, 3)));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110
William Lallemand2a4a44f2012-02-06 16:00:33 +0100111/*
112 * This function adds a header to the message and sends the syslog message
113 * using a printf format string
114 */
115void send_log(struct proxy *p, int level, const char *format, ...)
116 __attribute__ ((format(printf, 3, 4)));
117
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118/*
119 * This function sends a syslog message to both log servers of a proxy,
120 * or to global log servers if the proxy is NULL.
121 * It also tries not to waste too much time computing the message header.
122 * It doesn't care about errors nor does it report them.
123 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124
Christopher Faulet5c6fefc2019-08-11 19:40:12 +0200125void __send_log(struct list *logsrvs, struct buffer *tag, int level, char *message, size_t size, char *sd, size_t sd_size);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126
127/*
Dragan Dosen1322d092015-09-22 16:05:32 +0200128 * returns log format for <fmt> or -1 if not found.
129 */
130int get_log_format(const char *fmt);
131
132/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133 * returns log level for <lev> or -1 if not found.
134 */
135int get_log_level(const char *lev);
136
137/*
138 * returns log facility for <fac> or -1 if not found.
139 */
140int get_log_facility(const char *fac);
141
William Lallemanda1cc3812012-02-08 16:38:44 +0100142/*
143 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200144 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100145 *
Joseph Herlant85b40592018-11-15 12:10:04 -0800146 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +0100147 */
Olivier Houchard54620522018-09-06 18:14:09 +0200148char *lf_text_len(char *dst, const char *src, size_t len, size_t size, const struct logformat_node *node);
William Lallemand5f232402012-04-05 18:02:55 +0200149
150/*
Joseph Herlant85b40592018-11-15 12:10:04 -0800151 * Write a IP address to the log string
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500152 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +0200153 */
Willy Tarreau26ffa852018-09-05 15:23:10 +0200154char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node);
William Lallemand5f232402012-04-05 18:02:55 +0200155
156/*
157 * Write a port to the log
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500158 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +0200159 */
Willy Tarreau26ffa852018-09-05 15:23:10 +0200160char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node);
William Lallemand5f232402012-04-05 18:02:55 +0200161
William Lallemanda1cc3812012-02-08 16:38:44 +0100162
Emeric Brunbd163812020-05-06 14:33:46 +0200163/*
164 * Function to handle log header building (exported for sinks)
165 */
166char *update_log_hdr_rfc5424(const time_t time);
167char *update_log_hdr(const time_t time);
168char * get_format_pid_sep1(int format, size_t *len);
169char * get_format_pid_sep2(int format, size_t *len);
170
Willy Tarreauaeed4a82020-06-04 22:01:04 +0200171/*
172 * Test if <idx> index numbered from 0 is in <rg> range with low and high
173 * limits of indexes numbered from 1.
174 */
175static inline int in_smp_log_range(struct smp_log_range *rg, unsigned int idx)
176{
177 if (idx + 1 <= rg->high && idx + 1 >= rg->low)
178 return 1;
179 return 0;
180}
181
182/*
183 * Builds a log line for the stream (must be valid).
184 */
185static inline int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
186{
187 return sess_build_logline(strm_sess(s), s, dst, maxsize, list_format);
188}
189
190#endif /* _HAPROXY_LOG_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191
192/*
193 * Local variables:
194 * c-indent-level: 8
195 * c-basic-offset: 8
196 * End:
197 */