Review at  https://gerrit.osmocom.org/7812

sip: Register log callback function with sofia-sip

sofia-sip allows applications to register a log backend function
which will be called every time the library wants to log something.

We register such a call-back and make it log using the libosmocore logging
framework.

The problem is that sofia-sip has its own log level management, and by
the time the message hits libosmocore, we don't know which log level we
shall use :(

Change-Id: Ib269b6b50f9d79bbd13acc43a626834921f05edb
Related: OS#3105
---
M src/app.h
M src/main.c
M src/sip.c
M src/vty.c
4 files changed, 33 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-sip-connector 
refs/changes/12/7812/1

diff --git a/src/app.h b/src/app.h
index ff48c79..96c8902 100644
--- a/src/app.h
+++ b/src/app.h
@@ -9,6 +9,7 @@
        struct {
                const char *local_addr;
                int local_port;
+               int sofia_log_level;
 
                const char *remote_addr;
                int remote_port;
diff --git a/src/main.c b/src/main.c
index 558eaec..b4ffb63 100644
--- a/src/main.c
+++ b/src/main.c
@@ -126,7 +126,7 @@
 
 
        /* parsing and setup */
-
+       g_app.sip.sofia_log_level = 2;
        handle_options(argc, argv);
        rc = vty_read_config_file(config_file, NULL);
        if (rc < 0) {
diff --git a/src/sip.c b/src/sip.c
index 4f3d034..84d1f6e 100644
--- a/src/sip.c
+++ b/src/sip.c
@@ -27,6 +27,7 @@
 #include <osmocom/core/utils.h>
 
 #include <sofia-sip/sip_status.h>
+#include <sofia-sip/su_log.h>
 
 #include <talloc.h>
 
@@ -383,12 +384,24 @@
                                agent->app->sip.local_port);
 }
 
+/* http://sofia-sip.sourceforge.net/refdocs/debug_logs.html */
+static void sip_logger(void *stream, char const *fmt, va_list ap)
+{
+       /* this is ugly, as unfortunately sofia-sip does not pass the log level 
to
+        * the log handler call-back function, so we have no clue what log 
level the
+        * currently logged message was sent for :(  As a result, we can only 
use one
+        * hard-coded LOGL_NOTICE here */
+       osmo_vlogp(DSIP, LOGL_NOTICE, "", 0, 0, fmt, ap);
+}
+
 void sip_agent_init(struct sip_agent *agent, struct app_config *app)
 {
        agent->app = app;
 
        su_init();
        su_home_init(&agent->home);
+       su_log_redirect(su_log_default, &sip_logger, NULL);
+       su_log_redirect(su_log_global, &sip_logger, NULL);
        agent->root = su_glib_root_create(NULL);
        su_root_threading(agent->root, 0);
 }
diff --git a/src/vty.c b/src/vty.c
index bea1f9f..540e2de 100644
--- a/src/vty.c
+++ b/src/vty.c
@@ -1,5 +1,6 @@
 /*
  * (C) 2016 by Holger Hans Peter Freyther
+ * (C) 2018 by Harald Welte <lafo...@gnumonks.org>
  *
  * All Rights Reserved
  *
@@ -24,6 +25,8 @@
 #include "mncc.h"
 
 #include <talloc.h>
+
+#include <sofia-sip/su_log.h>
 
 extern void *tall_mncc_ctx;
 
@@ -88,6 +91,7 @@
        vty_out(vty, "sip%s", VTY_NEWLINE);
        vty_out(vty, " local %s %d%s", g_app.sip.local_addr, 
g_app.sip.local_port, VTY_NEWLINE);
        vty_out(vty, " remote %s %d%s", g_app.sip.remote_addr, 
g_app.sip.remote_port, VTY_NEWLINE);
+       vty_out(vty, " sofia-sip log-level %d%s", g_app.sip.sofia_log_level, 
VTY_NEWLINE);
        return CMD_SUCCESS;
 }
 
@@ -130,6 +134,19 @@
        talloc_free((char *) g_app.sip.remote_addr);
        g_app.sip.remote_addr = talloc_strdup(tall_mncc_ctx, argv[0]);
        g_app.sip.remote_port = atoi(argv[1]);
+       return CMD_SUCCESS;
+}
+
+DEFUN(cfg_sip_sofia_log_level, cfg_sip_sofia_log_level_cmd,
+       "sofia-sip log-level <0-9>",
+       "sofia-sip library configuration\n"
+       "global log-level for sofia-sip\n"
+       "(0 = nothing, 9 = super-verbose)\n")
+{
+       g_app.sip.sofia_log_level = atoi(argv[0]);
+       su_log_set_level(su_log_default, g_app.sip.sofia_log_level);
+       su_log_set_level(su_log_global, g_app.sip.sofia_log_level);
+
        return CMD_SUCCESS;
 }
 
@@ -302,6 +319,7 @@
        install_node(&sip_node, config_write_sip);
        install_element(SIP_NODE, &cfg_sip_local_addr_cmd);
        install_element(SIP_NODE, &cfg_sip_remote_addr_cmd);
+       install_element(SIP_NODE, &cfg_sip_sofia_log_level_cmd);
 
        install_element(CONFIG_NODE, &cfg_mncc_cmd);
        install_node(&mncc_node, config_write_mncc);

-- 
To view, visit https://gerrit.osmocom.org/7812
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib269b6b50f9d79bbd13acc43a626834921f05edb
Gerrit-PatchSet: 1
Gerrit-Project: osmo-sip-connector
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <lafo...@gnumonks.org>

Reply via email to