Skip to content

Commit

Permalink
Merge pull request #3841 from Kokan/rewrite-rename
Browse files Browse the repository at this point in the history
Rewrite rename
  • Loading branch information
bazsi committed Nov 25, 2021
2 parents 4ffcd16 + d3af25a commit 8a467d9
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/logmsg/logmsg.c
Expand Up @@ -509,6 +509,21 @@ _value_invalidates_legacy_header(NVHandle handle)
return handle == LM_V_PROGRAM || handle == LM_V_PID;
}

void
log_msg_rename_value(LogMessage *self, NVHandle from, NVHandle to)
{
if (from == to)
return;

gssize value_len = 0;
const gchar *value = log_msg_get_value_if_set(self, from, &value_len);
if (!value)
return;

log_msg_set_value(self, to, value, value_len);
log_msg_unset_value(self, from);
}

void
log_msg_set_value_with_type(LogMessage *self, NVHandle handle, const gchar *value, gssize value_len, NVType type)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/logmsg/logmsg.h
Expand Up @@ -354,6 +354,8 @@ log_msg_set_value_by_name(LogMessage *self, const gchar *name, const gchar *valu
log_msg_set_value(self, handle, value, length);
}

void log_msg_rename_value(LogMessage *self, NVHandle from, NVHandle to);

void log_msg_append_format_sdata(const LogMessage *self, GString *result, guint32 seq_num);
void log_msg_format_sdata(const LogMessage *self, GString *result, guint32 seq_num);

Expand Down
2 changes: 2 additions & 0 deletions lib/rewrite/CMakeLists.txt
Expand Up @@ -6,6 +6,7 @@ set(REWRITE_HEADERS
rewrite/rewrite-expr-parser.h
rewrite/rewrite-groupset.h
rewrite/rewrite-unset.h
rewrite/rewrite-rename.h
rewrite/rewrite-set-pri.h
rewrite/rewrite-set-severity.h
rewrite/rewrite-set-facility.h
Expand All @@ -20,6 +21,7 @@ set(REWRITE_SOURCES
rewrite/rewrite-expr-parser.c
rewrite/rewrite-groupset.c
rewrite/rewrite-unset.c
rewrite/rewrite-rename.c
rewrite/rewrite-set-pri.c
rewrite/rewrite-set-severity.c
rewrite/rewrite-set-facility.c
Expand Down
2 changes: 2 additions & 0 deletions lib/rewrite/Makefile.am
Expand Up @@ -7,6 +7,7 @@ rewriteinclude_HEADERS = \
lib/rewrite/rewrite-set-tag.h \
lib/rewrite/rewrite-set.h \
lib/rewrite/rewrite-unset.h \
lib/rewrite/rewrite-rename.h \
lib/rewrite/rewrite-subst.h \
lib/rewrite/rewrite-expr-parser.h \
lib/rewrite/rewrite-groupset.h \
Expand All @@ -20,6 +21,7 @@ rewrite_sources = \
lib/rewrite/rewrite-set-tag.c \
lib/rewrite/rewrite-set.c \
lib/rewrite/rewrite-unset.c \
lib/rewrite/rewrite-rename.c \
lib/rewrite/rewrite-subst.c \
lib/rewrite/rewrite-expr-parser.c \
lib/rewrite/rewrite-expr-grammar.y \
Expand Down
21 changes: 21 additions & 0 deletions lib/rewrite/rewrite-expr-grammar.ym
Expand Up @@ -33,6 +33,7 @@
#include "rewrite/rewrite-set-tag.h"
#include "rewrite/rewrite-set.h"
#include "rewrite/rewrite-unset.h"
#include "rewrite/rewrite-rename.h"
#include "rewrite/rewrite-subst.h"
#include "rewrite/rewrite-groupset.h"
#include "rewrite/rewrite-set-pri.h"
Expand Down Expand Up @@ -65,6 +66,7 @@
%token KW_GROUP_UNSET
%token KW_SET
%token KW_UNSET
%token KW_RENAME
%token KW_SUBST
%token KW_VALUES
%token KW_SET_SEVERITY
Expand Down Expand Up @@ -108,6 +110,16 @@ rewrite_expr
last_rewrite = log_rewrite_unset_new(configuration);
}
'(' rewrite_set_opts ')' { $$ = last_rewrite; }
| KW_RENAME '(' string string
{
NVHandle from = log_msg_get_value_handle($3);
NVHandle to = log_msg_get_value_handle($4);
free($3);
free($4);

last_rewrite = log_rewrite_rename_new(configuration, from, to);
}
rewrite_rename_opts ')' { $$ = last_rewrite; }
| KW_SET_TAG '(' string
{
last_rewrite = log_rewrite_set_tag_new($3, TRUE, configuration);
Expand Down Expand Up @@ -196,6 +208,15 @@ rewrite_subst_opt
| rewrite_expr_opt
;

rewrite_rename_opts
: rewrite_rename_opt rewrite_rename_opts
|
;

rewrite_rename_opt
: rewrite_condition_opt
;

rewrite_set_opts
: rewrite_set_opt rewrite_set_opts
|
Expand Down
1 change: 1 addition & 0 deletions lib/rewrite/rewrite-expr-parser.c
Expand Up @@ -39,6 +39,7 @@ static CfgLexerKeyword rewrite_expr_keywords[] =
{ "set_pri", KW_SET_PRI },
{ "set_severity", KW_SET_SEVERITY },
{ "set_facility", KW_SET_FACILITY },
{ "rename", KW_RENAME },

{ "groupset", KW_GROUP_SET },
{ "groupunset", KW_GROUP_UNSET },
Expand Down
95 changes: 95 additions & 0 deletions lib/rewrite/rewrite-rename.c
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2021 Balabit
* Copyright (c) 2021 Kokan <kokaipeter@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; 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 "rewrite-rename.h"

typedef struct _LogRewriteRename LogRewriteRename;

struct _LogRewriteRename
{
LogRewrite super;
NVHandle source_handle;
NVHandle destination_handle;
};

static void
log_rewrite_rename_process(LogRewrite *s, LogMessage **pmsg, const LogPathOptions *path_options)
{
LogRewriteRename *self = (LogRewriteRename *) s;

if (self->source_handle == self->destination_handle)
return;

log_msg_make_writable(pmsg, path_options);

log_msg_rename_value(*pmsg, self->source_handle, self->destination_handle);
}

static LogPipe *
log_rewrite_rename_clone(LogPipe *s)
{
LogRewriteRename *self = (LogRewriteRename *) s;
LogRewriteRename *cloned;

cloned = (LogRewriteRename *) log_rewrite_rename_new(s->cfg, self->source_handle, self->destination_handle);

if (self->super.condition)
cloned->super.condition = filter_expr_clone(self->super.condition);

return &cloned->super.super;
}

static gboolean
log_rewrite_rename_init(LogPipe *s)
{
LogRewriteRename *self = (LogRewriteRename *) s;
if (!self->source_handle)
{
msg_error("rename(): source() option is mandatory", log_pipe_location_tag(s));
return FALSE;
}

if (!self->destination_handle)
{
msg_error("rename(): destination() option is mandatory", log_pipe_location_tag(s));
return FALSE;
}

return TRUE;
}

LogRewrite *
log_rewrite_rename_new(GlobalConfig *cfg, NVHandle source, NVHandle destination)
{
LogRewriteRename *self = g_new0(LogRewriteRename, 1);

self->source_handle = source;
self->destination_handle = destination;

log_rewrite_init_instance(&self->super, cfg);
self->super.super.init = log_rewrite_rename_init;
self->super.super.clone = log_rewrite_rename_clone;
self->super.process = log_rewrite_rename_process;
return &self->super;
}
32 changes: 32 additions & 0 deletions lib/rewrite/rewrite-rename.h
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2021 Balabit
* Copyright (c) 2021 Kokan <kokaipeter@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; 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.
*
*/

#ifndef REWRITE_RENAME_H_INCLUDED
#define REWRITE_RENAME_H_INCLUDED

#include "rewrite/rewrite-expr.h"

LogRewrite *log_rewrite_rename_new(GlobalConfig *cfg, NVHandle source, NVHandle destination);

#endif
1 change: 1 addition & 0 deletions lib/rewrite/tests/CMakeLists.txt
@@ -1,4 +1,5 @@
add_unit_test(LIBTEST CRITERION TARGET test_rewrite)
add_unit_test(LIBTEST CRITERION TARGET test_set_pri)
add_unit_test(LIBTEST CRITERION TARGET test_rename)
add_unit_test(LIBTEST CRITERION TARGET test_set_severity)
add_unit_test(LIBTEST CRITERION TARGET test_set_facility)
6 changes: 6 additions & 0 deletions lib/rewrite/tests/Makefile.am
@@ -1,6 +1,7 @@
lib_rewrite_tests_TESTS = \
lib/rewrite/tests/test_rewrite \
lib/rewrite/tests/test_set_pri \
lib/rewrite/tests/test_rename \
lib/rewrite/tests/test_set_severity \
lib/rewrite/tests/test_set_facility

Expand All @@ -14,6 +15,11 @@ lib_rewrite_tests_test_rewrite_LDADD = $(TEST_LDADD) \
lib_rewrite_tests_test_rewrite_SOURCES = \
lib/rewrite/tests/test_rewrite.c

lib_rewrite_tests_test_rename_CFLAGS = $(TEST_CFLAGS)
lib_rewrite_tests_test_rename_LDADD = $(TEST_LDADD)
lib_rewrite_tests_test_rename_SOURCES = \
lib/rewrite/tests/test_rename.c

lib_rewrite_tests_test_set_severity_CFLAGS = $(TEST_CFLAGS)
lib_rewrite_tests_test_set_severity_LDADD = $(TEST_LDADD)
lib_rewrite_tests_test_set_severity_SOURCES = \
Expand Down

0 comments on commit 8a467d9

Please sign in to comment.