From 3133918bcd903c4ae173df40f78fe6d07ef05ae8 Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Wed, 21 Oct 2015 10:16:02 +0200 Subject: [PATCH] log_systemd: added support to replace core syslog with logging to systemd --- modules/log_systemd/README | 41 ++++++++++++++----- modules/log_systemd/doc/log_systemd_admin.xml | 28 ++++++++++++- modules/log_systemd/log_systemd_mod.c | 32 +++++++++++++++ 3 files changed, 88 insertions(+), 13 deletions(-) diff --git a/modules/log_systemd/README b/modules/log_systemd/README index 739f405e855..36b2b5cd41a 100644 --- a/modules/log_systemd/README +++ b/modules/log_systemd/README @@ -23,13 +23,15 @@ Daniel-Constantin Mierla 2.1. Kamailio Modules 2.2. External Libraries or Applications - 3. Functions + 3. Core Logging + 4. Functions - 3.1. sd_journal_print(level, text) + 4.1. sd_journal_print(level, text) List of Examples - 1.1. sd_journal_print usage + 1.1. Core Logging Replacement with Systemd + 1.2. sd_journal_print usage Chapter 1. Admin Guide @@ -41,14 +43,15 @@ Chapter 1. Admin Guide 2.1. Kamailio Modules 2.2. External Libraries or Applications - 3. Functions + 3. Core Logging + 4. Functions - 3.1. sd_journal_print(level, text) + 4.1. sd_journal_print(level, text) 1. Overview - This module provides logging to systemd journal from configuration - file. + It provides logging to systemd journal from the moment of loading this + module. 2. Dependencies @@ -66,11 +69,27 @@ Chapter 1. Admin Guide running Kamailio with this module loaded: * libsystemd -3. Functions +3. Core Logging - 3.1. sd_journal_print(level, text) + This module can replace the syslog logging with sending the log + messages to systemd journal. The logging to systemd is started when + this module is loaded, before that the default syslog system is used. -3.1. sd_journal_print(level, text) + It requires that core parameters log_engine_type to be set to + 'systemd'. It is not enabled if log_stderror=yes. + + Example 1.1. Core Logging Replacement with Systemd +... +log_engine_type="systemd" +... +loadmodule "log_systemd.so" +... + +4. Functions + + 4.1. sd_journal_print(level, text) + +4.1. sd_journal_print(level, text) Print the text in the systemd journal at the provided level parameter. @@ -81,7 +100,7 @@ Chapter 1. Admin Guide This function can be used from ANY_ROUTE. - Example 1.1. sd_journal_print usage + Example 1.2. sd_journal_print usage ... sd_journal_print("LOG_INFO", "R-URI is $ru\n"); ... diff --git a/modules/log_systemd/doc/log_systemd_admin.xml b/modules/log_systemd/doc/log_systemd_admin.xml index 11d3295131a..26f7dfc0088 100644 --- a/modules/log_systemd/doc/log_systemd_admin.xml +++ b/modules/log_systemd/doc/log_systemd_admin.xml @@ -16,8 +16,8 @@
Overview - This module provides logging to systemd journal from configuration - file. + It provides logging to systemd journal from the moment + of loading this module.
@@ -52,6 +52,30 @@ +
+ Core Logging + + This module can replace the syslog logging with sending the log messages + to systemd journal. The logging to systemd is + started when this module is loaded, before that the default + syslog system is used. + + + It requires that core parameters log_engine_type to be set to 'systemd'. + It is not enabled if log_stderror=yes. + + + <function>Core Logging Replacement with Systemd</function> + +... +log_engine_type="systemd" +... +loadmodule "log_systemd.so" +... + + +
+
Functions
diff --git a/modules/log_systemd/log_systemd_mod.c b/modules/log_systemd/log_systemd_mod.c index f84d057a4cf..7ac2d695b48 100644 --- a/modules/log_systemd/log_systemd_mod.c +++ b/modules/log_systemd/log_systemd_mod.c @@ -33,6 +33,9 @@ MODULE_VERSION +static int _lc_log_systemd = 0; +void _lc_core_log_systemd(int lpriority, const char *format, ...); + static int mod_init(void); static int child_init(int); static void mod_destroy(void); @@ -65,7 +68,18 @@ struct module_exports exports = { child_init /* per child init function */ }; +int mod_register(char *path, int *dlflags, void *p1, void *p2) +{ + if(_km_log_engine_type==0) + return 0; + + if(strcasecmp(_km_log_engine_type, "systemd")!=0) + return 0; + km_log_func_set(&_lc_core_log_systemd); + _lc_log_systemd = 1; + return 0; +} /** * init module function @@ -131,3 +145,21 @@ static int w_sd_journal_print(struct sip_msg* msg, char* lev, char* txt) return 1; } + +#define LC_LOG_MSG_MAX_SIZE 16384 +void _lc_core_log_systemd(int lpriority, const char *format, ...) +{ + va_list arglist; + char obuf[LC_LOG_MSG_MAX_SIZE]; + int n; + int priority; + + /* Return on MASKed log priorities */ + priority = LOG_PRI(lpriority); + + va_start(arglist, format); + n = 0; + n += vsnprintf(obuf+n, LC_LOG_MSG_MAX_SIZE - n, format, arglist); + va_end(arglist); + sd_journal_print(priority, "%.*s", n, obuf); +}