File: add-contextual-data-glob-selector.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (181 lines) | stat: -rw-r--r-- 5,409 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2019 Balazs Scheidler <bazsi77@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "add-contextual-data-glob-selector.h"
#include "scratch-buffers.h"
#include "messages.h"

typedef struct _GlobExpression
{
  gchar *pattern;
  GPatternSpec *expr;
} GlobExpression;

static void
glob_expression_populate(GlobExpression *gs, const gchar *pattern)
{
  gs->pattern = g_strdup(pattern);
  gs->expr = g_pattern_spec_new(pattern);
}

static void
glob_expression_clear(GlobExpression *gs)
{
  g_free(gs->pattern);
  g_pattern_spec_free(gs->expr);
}

typedef struct _AddContextualDataGlobSelector
{
  AddContextualDataSelector super;
  GArray *globs;
  LogTemplate *glob_template;
} AddContextualDataGlobSelector;

static GArray *
_create_globs_array(void)
{
  GArray *array = g_array_new(FALSE, TRUE, sizeof(GlobExpression));
  return array;
}

static void
_populate_globs_array(AddContextualDataGlobSelector *self, GList *ordered_selectors)
{
  for (GList *l = ordered_selectors; l; l = l->next)
    {
      const gchar *selector = (const gchar *) l->data;
      GlobExpression gs;

      glob_expression_populate(&gs, selector);
      g_array_append_val(self->globs, gs);
    }
}

static GArray *
_clone_globs_array(GArray *src)
{
  GArray *dst = _create_globs_array();

  for (gint i = 0; i < src->len; i++)
    {
      GlobExpression *src_expr = &g_array_index(src, GlobExpression, i);
      GlobExpression dst_expr;

      glob_expression_populate(&dst_expr, src_expr->pattern);
      g_array_append_val(dst, dst_expr);
    }
  return dst;
}

static const gchar *
_find_first_matching_glob(AddContextualDataGlobSelector *self, LogMessage *msg)
{
  GString *string = scratch_buffers_alloc();
  GString *string_reversed = scratch_buffers_alloc();

  log_template_format(self->glob_template, msg, &DEFAULT_TEMPLATE_EVAL_OPTIONS, string);
  g_string_assign(string_reversed, string->str);
  g_strreverse(string_reversed->str);
  for (gint i = 0; i < self->globs->len; i++)
    {
      GlobExpression *gs = &g_array_index(self->globs, GlobExpression, i);
      gboolean result = g_pattern_spec_match(gs->expr, string->len, string->str, string_reversed->str);

      msg_trace("add-contextual-data(): Evaluating glob against message",
                evt_tag_str("glob-template", self->glob_template->template_str),
                evt_tag_str("string", string->str),
                evt_tag_str("pattern", gs->pattern),
                evt_tag_int("matched", result));
      if (result)
        return gs->pattern;
    }

  return NULL;
}

static gboolean
_init(AddContextualDataSelector *s, GList *ordered_selectors)
{
  AddContextualDataGlobSelector *self = (AddContextualDataGlobSelector *)s;

  _populate_globs_array(self, ordered_selectors);

  return TRUE;
}

static gchar *
_resolve(AddContextualDataSelector *s, LogMessage *msg)
{
  AddContextualDataGlobSelector *self = (AddContextualDataGlobSelector *)s;

  return g_strdup(_find_first_matching_glob(self, msg));
}

static void
_free(AddContextualDataSelector *s)
{
  AddContextualDataGlobSelector *self = (AddContextualDataGlobSelector *)s;

  log_template_unref(self->glob_template);
  for (int i = 0; i < self->globs->len; i++)
    {
      GlobExpression *gs = &g_array_index(self->globs, GlobExpression, i);
      glob_expression_clear(gs);
    }
  g_array_free(self->globs, TRUE);
}

static AddContextualDataSelector *_clone(AddContextualDataSelector *s,
                                         GlobalConfig *cfg);

static void
add_contextual_data_glob_selector_init_instance(AddContextualDataGlobSelector *self, LogTemplate *glob_template)
{
  self->super.ordering_required = TRUE;
  self->super.resolve = _resolve;
  self->super.free = _free;
  self->super.init = _init;
  self->super.clone = _clone;
  self->glob_template = glob_template;
}

static AddContextualDataSelector *
_clone(AddContextualDataSelector *s, GlobalConfig *cfg)
{
  AddContextualDataGlobSelector *self = (AddContextualDataGlobSelector *) s;
  AddContextualDataGlobSelector *cloned = g_new0(AddContextualDataGlobSelector, 1);

  add_contextual_data_glob_selector_init_instance(cloned, log_template_ref(self->glob_template));
  cloned->globs = _clone_globs_array(self->globs);
  return &cloned->super;
}

AddContextualDataSelector *
add_contextual_data_glob_selector_new(LogTemplate *glob_template)
{
  AddContextualDataGlobSelector *self = g_new0(AddContextualDataGlobSelector, 1);

  add_contextual_data_glob_selector_init_instance(self, glob_template);
  self->globs = _create_globs_array();
  return &self->super;
}