syslog-ng source
multi-line-logic.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Balabit
3  * Copyright (c) 2013 Balazs Scheidler <bazsi@balabit.hu>
4  * Copyright (c) 2022 Balazs Scheidler <bazsi77@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  * As an additional exemption you are allowed to compile & link against the
21  * OpenSSL libraries as published by the OpenSSL project. See the file
22  * COPYING for details.
23  *
24  */
25 
26 #ifndef MULTI_LINE_LOGIC_H_INCLUDED
27 #define MULTI_LINE_LOGIC_H_INCLUDED
28 
29 #include "syslog-ng.h"
30 
31 enum
32 {
33  MLL_EXTRACTED = 0x0001,
34  MLL_WAITING = 0x0002,
37 };
38 
39 #define MLL_CONSUME_PARTIAL_AMOUNT_SHIFT 8
40 #define MLL_CONSUME_PARTIAL_AMOUNT_MASK ~0xFF
41 #define MLL_CONSUME_PARTIALLY(drop_length) (MLL_CONSUME_SEGMENT | ((drop_length) << MLL_CONSUME_PARTIAL_AMOUNT_SHIFT))
42 
43 typedef struct _MultiLineLogic MultiLineLogic;
45 {
46  gint (*accumulate_line)(MultiLineLogic *self,
47  const guchar *consumed,
48  gsize consumed_len,
49  const guchar *segment,
50  gsize segment_len);
51  void (*free_fn)(MultiLineLogic *s);
53 };
54 
55 void multi_line_logic_init_instance(MultiLineLogic *self, gboolean keep_trailing_newline);
56 void multi_line_logic_free_method(MultiLineLogic *s);
57 
58 /*
59  * multi_line_logic_accumulate_line():
60  *
61  * Accumulate a multi-line message into an internal buffer.
62  * consumed
63  * points to the buffer containing our consumed data so far
64  *
65  * consumed_len
66  * The number of bytes in @consumed
67  *
68  * segment
69  * new data to be considered part of consumed
70  *
71  * segment_len
72  * The number of bytes in @segment
73  *
74  * The accumulator should return a set of bitfields that indicate what to do
75  * with the data presented:
76  *
77  * What we want to do with the new data
78  * MLL_CONSUME_SEGMENT -- add the new segment to data consumed
79  * MLL_CONSUME_PARTIALLY(n) -- add the new segment to the data consumed,
80  * but dropping (n) characters from the end
81  * MLL_REWIND_SEGMENT -- the new data is NOT part of the consumed
82  * data so far, and should be considered
83  * part of the next line.
84  *
85  * What we want to perform once the accumulation is finished:
86  * MLL_EXTRACTED -- the accumulation is finished, return the
87  * consumed data as a complete line for higher
88  * layers.
89  * MLL_WAITING -- we still need more data
90  *
91  */
92 static inline gint
93 multi_line_logic_accumulate_line(MultiLineLogic *self,
94  const guchar *msg,
95  gsize msg_len,
96  const guchar *segment,
97  gsize segment_len)
98 {
99  return self->accumulate_line(self, msg, msg_len, segment, segment_len);
100 }
101 
102 static inline void
103 multi_line_logic_free(MultiLineLogic *self)
104 {
105  self->free_fn(self);
106 }
107 
108 static inline gboolean
109 multi_line_logic_keep_trailing_newline(MultiLineLogic *self)
110 {
111  return self->keep_trailing_newline;
112 }
113 
114 #endif
@ MLL_CONSUME_SEGMENT
Definition: multi-line-logic.h:35
@ MLL_EXTRACTED
Definition: multi-line-logic.h:33
@ MLL_WAITING
Definition: multi-line-logic.h:34
@ MLL_REWIND_SEGMENT
Definition: multi-line-logic.h:36
void multi_line_logic_free_method(MultiLineLogic *s)
Definition: multi-line-logic.c:29
void multi_line_logic_init_instance(MultiLineLogic *self, gboolean keep_trailing_newline)
Definition: multi-line-logic.c:35
Definition: multi-line-logic.h:45
gint(* accumulate_line)(MultiLineLogic *self, const guchar *consumed, gsize consumed_len, const guchar *segment, gsize segment_len)
Definition: multi-line-logic.h:46
void(* free_fn)(MultiLineLogic *s)
Definition: multi-line-logic.h:51
gboolean keep_trailing_newline
Definition: multi-line-logic.h:52
LogMessage * msg
Definition: test_rename.c:35