blob: d30f30a742791ed8aca034ac69e0542282bf5f51 [file] [log] [blame]
Willy Tarreau67b5a162019-08-11 16:38:56 +02001/*
2 * include/proto/sink.h
3 * This file provides declarations for event sinks management
4 *
5 * Copyright (C) 2000-2019 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 */
21
22#ifndef _PROTO_SINK_H
23#define _PROTO_SINK_H
24
25#include <common/mini-clist.h>
26#include <types/sink.h>
27
28extern struct list sink_list;
29
30struct sink *sink_find(const char *name);
Willy Tarreau973e6622019-08-20 11:57:52 +020031struct sink *sink_new_fd(const char *name, const char *desc, enum sink_fmt fmt, int fd);
Emeric Brunbd163812020-05-06 14:33:46 +020032ssize_t __sink_write(struct sink *sink, const struct ist msg[], size_t nmsg,
33 int level, int facility, struct ist * tag,
34 struct ist *pid, struct ist *sd);
35int sink_announce_dropped(struct sink *sink, int facility, struct ist *pid);
Willy Tarreau8f240232019-08-27 16:41:06 +020036
37
38/* tries to send <nmsg> message parts (up to 8, ignored above) from message
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050039 * array <msg> to sink <sink>. Formatting according to the sink's preference is
Willy Tarreau8f240232019-08-27 16:41:06 +020040 * done here. Lost messages are accounted for in the sink's counter. If there
41 * were lost messages, an attempt is first made to indicate it.
Emeric Brune709e1e2020-05-06 17:23:59 +020042 * The function returns the number of Bytes effectively sent or announced.
43 * or <= 0 in other cases.
Willy Tarreau8f240232019-08-27 16:41:06 +020044 */
Emeric Brune709e1e2020-05-06 17:23:59 +020045static inline ssize_t sink_write(struct sink *sink, const struct ist msg[], size_t nmsg,
Emeric Brunbd163812020-05-06 14:33:46 +020046 int level, int facility, struct ist * tag,
47 struct ist *pid, struct ist *sd)
Willy Tarreau8f240232019-08-27 16:41:06 +020048{
49 ssize_t sent;
50
51 if (unlikely(sink->ctx.dropped > 0)) {
52 /* We need to take an exclusive lock so that other producers
53 * don't do the same thing at the same time and above all we
54 * want to be sure others have finished sending their messages
55 * so that the dropped event arrives exactly at the right
56 * position.
57 */
58 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &sink->ctx.lock);
Emeric Brunbd163812020-05-06 14:33:46 +020059 sent = sink_announce_dropped(sink, facility, pid);
Willy Tarreau8f240232019-08-27 16:41:06 +020060 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &sink->ctx.lock);
61
62 if (!sent) {
63 /* we failed, we don't try to send our log as if it
64 * would pass by chance, we'd get disordered events.
65 */
66 goto fail;
67 }
68 }
69
70 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &sink->ctx.lock);
Emeric Brunbd163812020-05-06 14:33:46 +020071 sent = __sink_write(sink, msg, nmsg, level, facility, tag, pid, sd);
Willy Tarreau8f240232019-08-27 16:41:06 +020072 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &sink->ctx.lock);
73
74 fail:
75 if (unlikely(sent <= 0))
76 HA_ATOMIC_ADD(&sink->ctx.dropped, 1);
Emeric Brune709e1e2020-05-06 17:23:59 +020077
78 return sent;
Willy Tarreau8f240232019-08-27 16:41:06 +020079}
Willy Tarreau67b5a162019-08-11 16:38:56 +020080
Willy Tarreau67b5a162019-08-11 16:38:56 +020081#endif /* _PROTO_SINK_H */
82
83/*
84 * Local variables:
85 * c-indent-level: 8
86 * c-basic-offset: 8
87 * End:
88 */