blob: 8bd448697efd039b2283aee3b4474c199d9572ee [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 Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020031#include <haproxy/stream.h>
Willy Tarreau43c538e2018-09-05 14:58:15 +020032
Willy Tarreaubafbe012017-11-24 17:34:44 +010033extern struct pool_head *pool_head_requri;
34extern struct pool_head *pool_head_uniqueid;
Willy Tarreau332f8bf2007-05-13 21:36:56 +020035
Willy Tarreauaeed4a82020-06-04 22:01:04 +020036extern const char *log_levels[];
William Lallemand723b73a2012-02-08 16:37:49 +010037extern char *log_format;
William Lallemandbddd4fd2012-02-27 11:23:10 +010038extern char default_tcp_log_format[];
William Lallemand723b73a2012-02-08 16:37:49 +010039extern char default_http_log_format[];
40extern char clf_http_log_format[];
Dragan Dosen1322d092015-09-22 16:05:32 +020041
Dragan Dosen0b85ece2015-09-25 19:17:44 +020042extern char default_rfc5424_sd_log_format[];
43
Willy Tarreau13ef7732018-11-12 07:25:28 +010044extern unsigned int dropped_logs;
45
Emeric Brun12941c82020-07-07 14:19:42 +020046/* lof forward proxy list */
47extern struct proxy *cfg_log_forward;
48
Christopher Fauletf8188c62017-06-02 16:20:16 +020049extern THREAD_LOCAL char *logline;
50extern THREAD_LOCAL char *logline_rfc5424;
William Lallemand723b73a2012-02-08 16:37:49 +010051
Emeric Brun45c457a2020-07-09 23:23:34 +020052/* global syslog message counter */
53extern int cum_log_messages;
54
Emeric Brun54932b42020-07-07 09:43:24 +020055/* syslog UDP message handler */
56void syslog_fd_handler(int fd);
William Lallemandbddd4fd2012-02-27 11:23:10 +010057
Christopher Faulet0132d062017-07-26 15:33:35 +020058/* Initialize/Deinitialize log buffers used for syslog messages */
59int init_log_buffers();
60void deinit_log_buffers();
61
Willy Tarreauaeed4a82020-06-04 22:01:04 +020062/* build a log line for the session and an optional stream */
Willy Tarreau43c538e2018-09-05 14:58:15 +020063int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t maxsize, struct list *list_format);
64
William Lallemandbddd4fd2012-02-27 11:23:10 +010065/*
Willy Tarreau87b09662015-04-03 00:22:06 +020066 * send a log for the stream when we have enough info about it.
William Lallemandbddd4fd2012-02-27 11:23:10 +010067 * Will not log if the frontend has no log defined.
68 */
Willy Tarreau87b09662015-04-03 00:22:06 +020069void strm_log(struct stream *s);
Willy Tarreau53839352018-09-05 19:51:10 +020070void sess_log(struct session *sess);
William Lallemandbddd4fd2012-02-27 11:23:10 +010071
Christopher Faulet5c6fefc2019-08-11 19:40:12 +020072/* send a applicative log with custom list of log servers */
73void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...)
74 __attribute__ ((format(printf, 4, 5)));
75
William Lallemand723b73a2012-02-08 16:37:49 +010076/*
William Lallemand723b73a2012-02-08 16:37:49 +010077 * add to the logformat linked list
78 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010079int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err);
William Lallemand723b73a2012-02-08 16:37:49 +010080
81/*
82 * Parse the log_format string and fill a linked list.
83 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
84 * You can set arguments using { } : %{many arguments}varname
85 */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010086int 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 +020087
88/* Parse "log" keyword and update the linked list. */
Emeric Brun9533a702021-04-02 10:13:43 +020089int parse_logsrv(char **args, struct list *logsrvs, int do_del, const char *file, int linenum, char **err);
Christopher Faulet4b0b79d2018-03-26 15:54:32 +020090
Willy Tarreaubaaee002006-06-26 02:48:02 +020091/*
William Lallemand2a4a44f2012-02-06 16:00:33 +010092 * This function adds a header to the message and sends the syslog message
93 * using a printf format string
94 */
95void send_log(struct proxy *p, int level, const char *format, ...)
96 __attribute__ ((format(printf, 3, 4)));
97
Willy Tarreaubaaee002006-06-26 02:48:02 +020098/*
99 * This function sends a syslog message to both log servers of a proxy,
100 * or to global log servers if the proxy is NULL.
101 * It also tries not to waste too much time computing the message header.
102 * It doesn't care about errors nor does it report them.
103 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104
Christopher Faulet5c6fefc2019-08-11 19:40:12 +0200105void __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 +0200106
107/*
Emeric Brun54648852020-07-06 15:54:06 +0200108 * returns log format for <fmt> or LOG_FORMAT_UNSPEC if not found.
Dragan Dosen1322d092015-09-22 16:05:32 +0200109 */
Emeric Brun54648852020-07-06 15:54:06 +0200110enum log_fmt get_log_format(const char *fmt);
Dragan Dosen1322d092015-09-22 16:05:32 +0200111
112/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 * returns log level for <lev> or -1 if not found.
114 */
115int get_log_level(const char *lev);
116
117/*
118 * returns log facility for <fac> or -1 if not found.
119 */
120int get_log_facility(const char *fac);
121
William Lallemanda1cc3812012-02-08 16:38:44 +0100122/*
123 * Write a string in the log string
William Lallemand5f232402012-04-05 18:02:55 +0200124 * Take cares of quote options
William Lallemanda1cc3812012-02-08 16:38:44 +0100125 *
Joseph Herlant85b40592018-11-15 12:10:04 -0800126 * Return the address of the \0 character, or NULL on error
William Lallemanda1cc3812012-02-08 16:38:44 +0100127 */
Olivier Houchard54620522018-09-06 18:14:09 +0200128char *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 +0200129
130/*
Joseph Herlant85b40592018-11-15 12:10:04 -0800131 * Write a IP address to the log string
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500132 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +0200133 */
Willy Tarreau26ffa852018-09-05 15:23:10 +0200134char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node);
William Lallemand5f232402012-04-05 18:02:55 +0200135
136/*
137 * Write a port to the log
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500138 * +X option write in hexadecimal notation, most significant byte on the left
William Lallemand5f232402012-04-05 18:02:55 +0200139 */
Willy Tarreau26ffa852018-09-05 15:23:10 +0200140char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node);
William Lallemand5f232402012-04-05 18:02:55 +0200141
William Lallemanda1cc3812012-02-08 16:38:44 +0100142
Emeric Brunbd163812020-05-06 14:33:46 +0200143/*
144 * Function to handle log header building (exported for sinks)
145 */
Emeric Brun9f9b22c2020-07-02 16:16:59 +0200146char *update_log_hdr_rfc5424(const time_t time, suseconds_t frac);
Emeric Brunbd163812020-05-06 14:33:46 +0200147char *update_log_hdr(const time_t time);
148char * get_format_pid_sep1(int format, size_t *len);
149char * get_format_pid_sep2(int format, size_t *len);
150
Willy Tarreauaeed4a82020-06-04 22:01:04 +0200151/*
152 * Test if <idx> index numbered from 0 is in <rg> range with low and high
153 * limits of indexes numbered from 1.
154 */
155static inline int in_smp_log_range(struct smp_log_range *rg, unsigned int idx)
156{
157 if (idx + 1 <= rg->high && idx + 1 >= rg->low)
158 return 1;
159 return 0;
160}
161
162/*
163 * Builds a log line for the stream (must be valid).
164 */
165static inline int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format)
166{
167 return sess_build_logline(strm_sess(s), s, dst, maxsize, list_format);
168}
169
Emeric Brun54648852020-07-06 15:54:06 +0200170struct ist *build_log_header(enum log_fmt format, int level, int facility, struct ist *metadata, size_t *nbelem);
Emeric Brun45c457a2020-07-09 23:23:34 +0200171
Emeric Brunb0c331f2020-10-07 17:05:59 +0200172/*
173 * lookup log forward proxy by name
174 * Returns NULL if no proxy found.
175 */
176static inline struct proxy *log_forward_by_name(const char *name)
177{
178 struct proxy *px = cfg_log_forward;
179
180 while (px) {
181 if (strcmp(px->id, name) == 0)
182 return px;
183 px = px->next;
184 }
185 return NULL;
186}
187
Willy Tarreauaeed4a82020-06-04 22:01:04 +0200188#endif /* _HAPROXY_LOG_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189
190/*
191 * Local variables:
192 * c-indent-level: 8
193 * c-basic-offset: 8
194 * End:
195 */