res_pjsip: Updates and adds more PJSIP CLI commands.
[asterisk/asterisk.git] / res / res_pjsip_logger.c
index 90cb06f..a69ca42 100644 (file)
@@ -25,6 +25,8 @@
 
 #include "asterisk.h"
 
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include <pjsip.h>
 
 #include "asterisk/res_pjsip.h"
@@ -157,7 +159,7 @@ static char *pjsip_set_logger(struct ast_cli_entry *e, int cmd, struct ast_cli_a
        if (cmd == CLI_INIT) {
                e->command = "pjsip set logger {on|off|host}";
                e->usage =
-                       "Usage: pjsip set logger {on|off}\n"
+                       "Usage: pjsip set logger {on|off|host <name>}\n"
                        "       Enables or disabling logging of SIP packets\n"
                        "       read on ports bound to PJSIP transports either\n"
                        "       globally or enables logging for an individual\n"
@@ -193,10 +195,50 @@ static struct ast_cli_entry cli_pjsip[] = {
        AST_CLI_DEFINE(pjsip_set_logger, "Enable/Disable PJSIP Logger Output")
 };
 
+static void check_debug(void)
+{
+       RAII_VAR(char *, debug, ast_sip_get_debug(), ast_free);
+
+       if (ast_false(debug)) {
+               logging_mode = LOGGING_MODE_DISABLED;
+               return;
+       }
+
+       logging_mode = LOGGING_MODE_ENABLED;
+
+       if (ast_true(debug)) {
+               ast_sockaddr_setnull(&log_addr);
+               return;
+       }
+
+       /* assume host */
+       if (ast_sockaddr_resolve_first_af(&log_addr, debug, 0, AST_AF_UNSPEC)) {
+               ast_log(LOG_WARNING, "Could not resolve host %s for debug "
+                       "logging\n", debug);
+       }
+}
+
+static void global_reloaded(const char *object_type)
+{
+       check_debug();
+}
+
+static const struct ast_sorcery_observer global_observer = {
+       .loaded = global_reloaded
+};
+
 static int load_module(void)
 {
+       if (ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &global_observer)) {
+               ast_log(LOG_WARNING, "Unable to add global observer\n");
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
+       check_debug();
+
        ast_sip_register_service(&logging_module);
        ast_cli_register_multiple(cli_pjsip, ARRAY_LEN(cli_pjsip));
+
        return AST_MODULE_LOAD_SUCCESS;
 }
 
@@ -204,6 +246,10 @@ static int unload_module(void)
 {
        ast_cli_unregister_multiple(cli_pjsip, ARRAY_LEN(cli_pjsip));
        ast_sip_unregister_service(&logging_module);
+
+       ast_sorcery_observer_remove(
+               ast_sip_get_sorcery(), "global", &global_observer);
+
        return 0;
 }