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
|
/*
* Copyright (c) 2015 Balabit
*
* 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 "kv-parser.h"
#include "kv-scanner.h"
typedef struct _KVParser
{
LogParser super;
gchar *prefix;
gsize prefix_len;
GString *formatted_key;
KVScanner *kv_scanner;
} KVParser;
void
kv_parser_set_prefix(LogParser *p, const gchar *prefix)
{
KVParser *self = (KVParser *)p;
g_free(self->prefix);
if (prefix)
{
self->prefix = g_strdup(prefix);
self->prefix_len = strlen(prefix);
}
else
{
self->prefix = NULL;
self->prefix_len = 0;
}
}
void
kv_parser_set_value_separator(LogParser *s, gchar value_separator)
{
KVParser *self = (KVParser *) s;
kv_scanner_set_value_separator(self->kv_scanner, value_separator);
}
static const gchar *
_get_formatted_key(KVParser *self, const gchar *key)
{
if (!self->prefix)
return key;
else if (self->formatted_key->len > 0)
g_string_truncate(self->formatted_key, self->prefix_len);
else
g_string_assign(self->formatted_key, self->prefix);
g_string_append(self->formatted_key, key);
return self->formatted_key->str;
}
static gboolean
kv_parser_process(LogParser *s, LogMessage **pmsg, const LogPathOptions *path_options, const gchar *input, gsize input_len)
{
KVParser *self = (KVParser *) s;
log_msg_make_writable(pmsg, path_options);
/* FIXME: input length */
kv_scanner_input(self->kv_scanner, input);
while (kv_scanner_scan_next(self->kv_scanner))
{
/* FIXME: value length */
log_msg_set_value_by_name(*pmsg,
_get_formatted_key(self, kv_scanner_get_current_key(self->kv_scanner)),
kv_scanner_get_current_value(self->kv_scanner), -1);
}
return TRUE;
}
static LogPipe *
kv_parser_clone(LogPipe *s)
{
KVParser *self = (KVParser *) s;
LogParser *cloned;
cloned = kv_parser_new(s->cfg, kv_scanner_clone(self->kv_scanner));
kv_parser_set_prefix(cloned, self->prefix);
log_parser_set_template(cloned, log_template_ref(self->super.template));
return &cloned->super;
}
static void
kv_parser_free(LogPipe *s)
{
KVParser *self = (KVParser *)s;
kv_scanner_free(self->kv_scanner);
g_string_free(self->formatted_key, TRUE);
g_free(self->prefix);
log_parser_free_method(s);
}
static gboolean
kv_parser_process_threaded(LogParser *s, LogMessage **pmsg, const LogPathOptions *path_options, const gchar *input, gsize input_len)
{
LogParser *self = (LogParser *)log_pipe_clone(&s->super);
gboolean ok = kv_parser_process(self, pmsg, path_options, input, input_len);
log_pipe_unref(&self->super);
return ok;
}
LogParser *
kv_parser_new(GlobalConfig *cfg, KVScanner *kv_scanner)
{
KVParser *self = g_new0(KVParser, 1);
log_parser_init_instance(&self->super, cfg);
self->super.super.free_fn = kv_parser_free;
self->super.super.clone = kv_parser_clone;
self->super.process = kv_parser_process_threaded;
self->kv_scanner = kv_scanner;
self->formatted_key = g_string_sized_new(32);
return &self->super;
}
|