+static char *pjsip_enable_logger_host(int fd, const char *arg)
+{
+ if (ast_sockaddr_resolve_first_af(&log_addr, arg, 0, AST_AF_UNSPEC)) {
+ return CLI_SHOWUSAGE;
+ }
+
+ ast_cli(fd, "PJSIP Logging Enabled for host: %s\n", ast_sockaddr_stringify_addr(&log_addr));
+ logging_mode = LOGGING_MODE_ENABLED;
+
+ return CLI_SUCCESS;
+}
+
+static char *pjsip_set_logger(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ const char *what;
+
+ if (cmd == CLI_INIT) {
+ e->command = "pjsip set logger {on|off|host}";
+ e->usage =
+ "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"
+ " host.\n";
+ return NULL;
+ } else if (cmd == CLI_GENERATE) {
+ return NULL;
+ }
+
+ what = a->argv[e->args - 1]; /* Guaranteed to exist */
+
+ if (a->argc == e->args) { /* on/off */
+ if (!strcasecmp(what, "on")) {
+ logging_mode = LOGGING_MODE_ENABLED;
+ ast_cli(a->fd, "PJSIP Logging enabled\n");
+ ast_sockaddr_setnull(&log_addr);
+ return CLI_SUCCESS;
+ } else if (!strcasecmp(what, "off")) {
+ logging_mode = LOGGING_MODE_DISABLED;
+ ast_cli(a->fd, "PJSIP Logging disabled\n");
+ return CLI_SUCCESS;
+ }
+ } else if (a->argc == e->args + 1) {
+ if (!strcasecmp(what, "host")) {
+ return pjsip_enable_logger_host(a->fd, a->argv[e->args]);
+ }
+ }
+
+ return CLI_SHOWUSAGE;
+}
+
+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
+};
+