Skip to content

Commit

Permalink
Merge pull request #4763 from bazsi/add-severity-aliases
Browse files Browse the repository at this point in the history
Add severity aliases
  • Loading branch information
alltilla committed Jan 4, 2024
2 parents 054eda7 + 7866aea commit 8deaa37
Show file tree
Hide file tree
Showing 17 changed files with 243 additions and 39 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -40,6 +40,7 @@ include(ExternalProject)
include(external_or_find_package)
include(add_module)
include(module_switch)
include(FindGperf)

find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
Expand Down
73 changes: 73 additions & 0 deletions cmake/Modules/FindGperf.cmake
@@ -0,0 +1,73 @@
# SPDX-FileCopyrightText: 2016-2017 Pino Toscano <pino@kde.org>
#
# SPDX-License-Identifier: BSD-3-Clause
#
# Adapted from https://invent.kde.org/frameworks/extra-cmake-modules/-/blob/master/find-modules/FindGperf.cmake?ref_type=heads
#

find_program(Gperf_EXECUTABLE NAMES gperf)

if (Gperf_EXECUTABLE)
execute_process(COMMAND ${Gperf_EXECUTABLE} -v
OUTPUT_VARIABLE _version_string
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(_version_string MATCHES "^GNU gperf ([-0-9\\.]+)")
set(Gperf_VERSION "${CMAKE_MATCH_1}")
endif()
unset(_version_string)
else()
set(Gperf_VERSION)
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Gperf
FOUND_VAR
Gperf_FOUND
REQUIRED_VARS
Gperf_EXECUTABLE
VERSION_VAR
Gperf_VERSION
)

if (Gperf_FOUND)
if (NOT TARGET GPerf::Gperf)
add_executable(GPerf::Gperf IMPORTED)
set_target_properties(GPerf::Gperf PROPERTIES
IMPORTED_LOCATION "${Gperf_EXECUTABLE}"
)
endif()
endif()

function(gperf_generate input_file output_file _target_or_sources_var)
# Parse arguments
set(oneValueArgs GENERATION_FLAGS)
cmake_parse_arguments(ARGS "" "${oneValueArgs}" "" ${ARGN})

if(ARGS_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown keywords given to gperf_generate(): \"${ARGS_UNPARSED_ARGUMENTS}\"")
endif()
if (TARGET ${_target_or_sources_var})
get_target_property(aliased_target ${_target_or_sources_var} ALIASED_TARGET)
if(aliased_target)
message(FATAL_ERROR "Target argument passed to gperf_generate must not be an alias: ${_target_or_sources_var}")
endif()
endif()

get_filename_component(_infile ${input_file} ABSOLUTE)
set(_extraopts "${ARGS_GENERATION_FLAGS}")
separate_arguments(_extraopts)
add_custom_command(OUTPUT ${output_file}
COMMAND ${Gperf_EXECUTABLE} ${_extraopts} --output-file=${output_file} ${_infile}
DEPENDS ${_infile}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
VERBATIM
)
set_property(SOURCE ${output_file} PROPERTY SKIP_AUTOMOC ON)

if (TARGET ${_target_or_sources_var})
target_sources(${_target_or_sources_var} PRIVATE ${output_file})
else()
set(${_target_or_sources_var} ${${_target_or_sources_var}} ${output_file} PARENT_SCOPE)
endif()
endfunction()
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -459,6 +459,7 @@ if test "x$ac_cv_prog_cc_c99" = "xno"; then
fi
AC_PROG_YACC
AM_PROG_LEX
AM_MISSING_PROG([GPERF], [gperf])
AC_PROG_MAKE_SET
PKG_PROG_PKG_CONFIG
LT_INIT([dlopen disable-static])
Expand Down
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Expand Up @@ -381,6 +381,8 @@ set_target_properties(syslog-ng
PROPERTIES VERSION ${SYSLOG_NG_VERSION}
SOVERSION ${SYSLOG_NG_VERSION})

gperf_generate(severity-aliases.table ${CMAKE_CURRENT_BINARY_DIR}/severity-aliases.h syslog-ng)

if (${IVYKIS_INTERNAL})
add_dependencies(syslog-ng IVYKIS)
endif()
Expand Down
8 changes: 7 additions & 1 deletion lib/Makefile.am
Expand Up @@ -311,16 +311,22 @@ lib_libsyslog_ng_la_LIBADD += @OPENSSL_LIBS@
BUILT_SOURCES += lib/cfg-lex.c lib/cfg-lex.h \
lib/cfg-grammar.c lib/cfg-grammar.h \
lib/block-ref-grammar.y lib/block-ref-grammar.c lib/block-ref-grammar.h \
lib/pragma-grammar.y lib/pragma-grammar.h lib/pragma-grammar.c
lib/pragma-grammar.y lib/pragma-grammar.h lib/pragma-grammar.c \
lib/severity-aliases.h

EXTRA_DIST += \
lib/block-ref-grammar.ym \
lib/pragma-grammar.ym \
lib/merge-grammar.py \
lib/severity-aliases.table \
lib/severity-aliases.h \
lib/hostname-unix.c

lib/plugin-types.h: lib/cfg-grammar.h

%.h: %.table
$(GPERF) $< --output $@

lib/ libsyslog-ng: lib/libsyslog-ng.la
if IVYKIS_INTERNAL
lib/ivykis/ ivykis: lib/ivykis/src/libivykis.la
Expand Down
6 changes: 3 additions & 3 deletions lib/rewrite/rewrite-set-severity.c
Expand Up @@ -57,17 +57,17 @@ _convert_severity_as_number(GString *severity_text)
static gint
_convert_severity_as_text(GString *severity_text)
{
return syslog_name_lookup_severity_by_name(severity_text->str);
return syslog_name_lookup_severity_by_name_alias(severity_text->str, severity_text->len);
}

static gint
_convert_severity(GString *severity_text)
{
gint severity = _convert_severity_as_number(severity_text);
gint severity = _convert_severity_as_text(severity_text);
if (severity >= 0)
return severity;

severity = _convert_severity_as_text(severity_text);
severity = _convert_severity_as_number(severity_text);
if (severity >= 0)
return severity;

Expand Down
50 changes: 50 additions & 0 deletions lib/severity-aliases.table
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2024 Balazs Scheidler
*
* 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.
*
*/

%{

#pragma GCC diagnostic ignored "-Wswitch-default"

%}
%define lookup-function-name gperf_lookup_severity_alias
%ignore-case
%struct-type
%readonly-tables
%switch=1
struct severity_alias { char *name; int severity; };
%%
emerg, SYSLOG_SEVERITY_CODE(0)
emergency, SYSLOG_SEVERITY_CODE(0)
panic, SYSLOG_SEVERITY_CODE(0)
alert, SYSLOG_SEVERITY_CODE(1)
crit, SYSLOG_SEVERITY_CODE(2)
critical, SYSLOG_SEVERITY_CODE(2)
fatal, SYSLOG_SEVERITY_CODE(2)
err, SYSLOG_SEVERITY_CODE(3)
error, SYSLOG_SEVERITY_CODE(3)
warning, SYSLOG_SEVERITY_CODE(4)
warn, SYSLOG_SEVERITY_CODE(4)
notice, SYSLOG_SEVERITY_CODE(5)
info, SYSLOG_SEVERITY_CODE(6)
log, SYSLOG_SEVERITY_CODE(6)
debug, SYSLOG_SEVERITY_CODE(7)
18 changes: 14 additions & 4 deletions lib/syslog-names.c
Expand Up @@ -23,7 +23,6 @@
*/

#include "syslog-names.h"
#include "syslog-ng.h"
#include <string.h>

struct sl_name sl_severities[] =
Expand Down Expand Up @@ -77,7 +76,7 @@ struct sl_name sl_facilities[] =
{NULL, -1}
};

static inline int
static inline gint
syslog_name_find_name(const char *name, struct sl_name names[])
{
int i;
Expand All @@ -92,13 +91,13 @@ syslog_name_find_name(const char *name, struct sl_name names[])
return -1;
}

int
gint
syslog_name_lookup_id_by_name(const char *name, struct sl_name names[])
{
return syslog_name_find_name(name, names);
}

int
gint
syslog_name_lookup_value_by_name(const char *name, struct sl_name names[])
{
int i;
Expand Down Expand Up @@ -139,3 +138,14 @@ syslog_make_range(guint32 value1, guint32 value2)
}
return ((1 << (value2 + 1)) - 1) & ~((1 << value1) - 1);
}

#include "severity-aliases.h"

gint
syslog_name_lookup_severity_by_name_alias(const gchar *name, gssize name_len)
{
const struct severity_alias *sa = gperf_lookup_severity_alias(name, name_len < 0 ? strlen(name) : name_len);
if (sa)
return sa->severity;
return -1;
}
6 changes: 4 additions & 2 deletions lib/syslog-names.h
Expand Up @@ -54,13 +54,15 @@ const char *syslog_name_lookup_name_by_value(int value, struct sl_name names[]);

guint32 syslog_make_range(guint32 r1, guint32 r2);

static inline guint32
static inline gint
syslog_name_lookup_severity_by_name(const gchar *name)
{
return syslog_name_lookup_value_by_name(name, sl_severities);
}

static inline guint32
gint syslog_name_lookup_severity_by_name_alias(const gchar *name, gssize name_len);

static inline gint
syslog_name_lookup_facility_by_name(const gchar *name)
{
return syslog_name_lookup_value_by_name(name, sl_facilities);
Expand Down
2 changes: 2 additions & 0 deletions news/feature-4763.md
@@ -0,0 +1,2 @@
`set-severity()` support for aliases: widespread aliases to severity values
produced by various applications are added to set-severity().
1 change: 1 addition & 0 deletions news/packaging-4763.md
@@ -0,0 +1 @@
Added `gperf` as a build dependency.
2 changes: 1 addition & 1 deletion packaging/debian/control
Expand Up @@ -10,7 +10,7 @@ Build-Depends: debhelper (>= 10~),
autoconf-archive,
tzdata,
tzdata-legacy <!sng-notzdatalegacy>,
pkg-config, flex, bison (>= 3.4.2),
pkg-config, flex, bison (>= 3.4.2), gperf,
libcriterion-dev <!sng-nocriterion>,
xsltproc, docbook-xsl,
libesmtp-dev,
Expand Down
1 change: 1 addition & 0 deletions packaging/rhel/syslog-ng.spec
Expand Up @@ -58,6 +58,7 @@ BuildRequires: pkgconfig
BuildRequires: libtool
BuildRequires: bison
BuildRequires: flex
BuildRequires: gperf
BuildRequires: libxslt
BuildRequires: glib2-devel
BuildRequires: ivykis-devel
Expand Down
42 changes: 15 additions & 27 deletions scl/pgsql/pgsql.conf
Expand Up @@ -91,37 +91,25 @@ block parser postgresql-csvlog-parser(internal(yes) prefix(".pgsql.") on-type-er
set(string("$(if ('${`prefix`connection_from}' ne '') ${`prefix`connection_from} $HOST_FROM)") value("`prefix`connection_from") internal(yes));
set(string("${`prefix`message}") value("MESSAGE") internal(yes));
};
# Map severity
if (match("DEBUG[1-5]" value('`prefix`severity') type(pcre))) {

# Map severity to match https://www.postgresql.org/docs/current/runtime-config-logging.html#RUNTIME-CONFIG-SEVERITY-LEVELS
# for some reason PgSQL decided to map its internal levels in a
# shifted fashion above and including "warning". "panic" itself is
# shifted by two, whereas the other values are shifted by one.
if (match("DEBUG" value('`prefix`severity') type(string) flags(prefix))) {
rewrite {
set-severity("DEBUG");
};
}
elif (match("WARNING" value('`prefix`severity') type(string))) {
rewrite {
set-severity("NOTICE");
};
}
elif (match("ERROR" value('`prefix`severity') type(string))) {
rewrite {
set-severity("WARNING");
};
}
elif (match("LOG" value('`prefix`severity') type(string))) {
rewrite {
set-severity("INFO");
};
}
elif (match("FATAL" value('`prefix`severity') type(string))) {
rewrite {
set-severity("ERR");
};
}
elif (match("PANIC" value('`prefix`severity') type(string))) {
} else {
rewrite {
set-severity("CRIT");
set-severity("${`prefix`severity}");

# shift panic to alert (needs to be shifted by 2)
set-severity("$(+ $SEVERITY_NUM 1)" condition($SEVERITY_NUM == 0));

# shift alert..warning (warning=>notice, error=>warning, fatal=>error, alert=>crit)
set-severity("$(+ $SEVERITY_NUM 1)" condition($SEVERITY_NUM <= 4));
};
}
else {};
};
};
};
2 changes: 1 addition & 1 deletion tests/copyright/check.sh
Expand Up @@ -301,7 +301,7 @@ extract_holder_license() {

local EXT="`echo "$FILE" | sed -r "s~^.*\.([^.]+)$~\1~"`"
case "$EXT" in
c|h|cpp|hpp|ym|java)
c|h|cpp|hpp|ym|java|table)
extract_holder_license_c
;;
ac|am|cmake|conf|sh|pl|py)
Expand Down
1 change: 1 addition & 0 deletions tests/copyright/policy
Expand Up @@ -107,6 +107,7 @@ lib/multi-line/tests/test_smart_multi_line\.c
libtest/mock-logpipe\.[ch]
lib/generic-number\.[ch]
lib/tests/test_generic_number\.c
lib/severity-aliases\.table
syslog-ng-ctl/commands/log-level.[ch]
modules/afsocket/afsocket-signals.h
syslog-ng-ctl/commands/healthcheck.[ch]
Expand Down

0 comments on commit 8deaa37

Please sign in to comment.