blob: bab968140796543584ff55c51855f56043a68c3f [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.
42 */
Emeric Brunbd163812020-05-06 14:33:46 +020043static inline void sink_write(struct sink *sink, const struct ist msg[], size_t nmsg,
44 int level, int facility, struct ist * tag,
45 struct ist *pid, struct ist *sd)
Willy Tarreau8f240232019-08-27 16:41:06 +020046{
47 ssize_t sent;
48
49 if (unlikely(sink->ctx.dropped > 0)) {
50 /* We need to take an exclusive lock so that other producers
51 * don't do the same thing at the same time and above all we
52 * want to be sure others have finished sending their messages
53 * so that the dropped event arrives exactly at the right
54 * position.
55 */
56 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &sink->ctx.lock);
Emeric Brunbd163812020-05-06 14:33:46 +020057 sent = sink_announce_dropped(sink, facility, pid);
Willy Tarreau8f240232019-08-27 16:41:06 +020058 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &sink->ctx.lock);
59
60 if (!sent) {
61 /* we failed, we don't try to send our log as if it
62 * would pass by chance, we'd get disordered events.
63 */
64 goto fail;
65 }
66 }
67
68 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &sink->ctx.lock);
Emeric Brunbd163812020-05-06 14:33:46 +020069 sent = __sink_write(sink, msg, nmsg, level, facility, tag, pid, sd);
Willy Tarreau8f240232019-08-27 16:41:06 +020070 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &sink->ctx.lock);
71
72 fail:
73 if (unlikely(sent <= 0))
74 HA_ATOMIC_ADD(&sink->ctx.dropped, 1);
75}
Willy Tarreau67b5a162019-08-11 16:38:56 +020076
77#endif /* _PROTO_SINK_H */
78
79/*
80 * Local variables:
81 * c-indent-level: 8
82 * c-basic-offset: 8
83 * End:
84 */