Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 25 | #include <haproxy/list-t.h> |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 26 | #include <types/sink.h> |
| 27 | |
| 28 | extern struct list sink_list; |
| 29 | |
| 30 | struct sink *sink_find(const char *name); |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 31 | struct sink *sink_new_fd(const char *name, const char *desc, enum sink_fmt fmt, int fd); |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 32 | ssize_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); |
| 35 | int sink_announce_dropped(struct sink *sink, int facility, struct ist *pid); |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 36 | |
| 37 | |
| 38 | /* tries to send <nmsg> message parts (up to 8, ignored above) from message |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 39 | * array <msg> to sink <sink>. Formatting according to the sink's preference is |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 40 | * 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 Brun | e709e1e | 2020-05-06 17:23:59 +0200 | [diff] [blame] | 42 | * The function returns the number of Bytes effectively sent or announced. |
| 43 | * or <= 0 in other cases. |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 44 | */ |
Emeric Brun | e709e1e | 2020-05-06 17:23:59 +0200 | [diff] [blame] | 45 | static inline ssize_t sink_write(struct sink *sink, const struct ist msg[], size_t nmsg, |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 46 | int level, int facility, struct ist * tag, |
| 47 | struct ist *pid, struct ist *sd) |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 48 | { |
| 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 Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 59 | sent = sink_announce_dropped(sink, facility, pid); |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 60 | 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 Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 71 | sent = __sink_write(sink, msg, nmsg, level, facility, tag, pid, sd); |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 72 | 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 Brun | e709e1e | 2020-05-06 17:23:59 +0200 | [diff] [blame] | 77 | |
| 78 | return sent; |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 79 | } |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 80 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 81 | #endif /* _PROTO_SINK_H */ |
| 82 | |
| 83 | /* |
| 84 | * Local variables: |
| 85 | * c-indent-level: 8 |
| 86 | * c-basic-offset: 8 |
| 87 | * End: |
| 88 | */ |