Merge "ari: Implement 'debug all' and request/response logging"

This commit is contained in:
George Joseph
2017-01-26 17:06:40 -06:00
committed by Gerrit Code Review
28 changed files with 527 additions and 1157 deletions

View File

@@ -41,6 +41,9 @@
#define CHANNEL_ALL "__AST_CHANNEL_ALL_TOPIC"
#define ENDPOINT_ALL "__AST_ENDPOINT_ALL_TOPIC"
/*! Global debug flag. No need for locking */
int global_debug;
static int unsubscribe(struct stasis_app *app, const char *kind, const char *id, int terminate);
struct stasis_app {
@@ -840,15 +843,64 @@ static void bridge_default_handler(void *data, struct stasis_subscription *sub,
}
}
void app_set_debug(struct stasis_app *app, int debug)
void stasis_app_set_debug(struct stasis_app *app, int debug)
{
if (!app) {
return;
}
{
SCOPED_AO2LOCK(lock, app);
app->debug = debug;
app->debug = debug;
}
void stasis_app_set_debug_by_name(const char *app_name, int debug)
{
struct stasis_app *app = stasis_app_get_by_name(app_name);
if (!app) {
return;
}
app->debug = debug;
ao2_cleanup(app);
}
int stasis_app_get_debug(struct stasis_app *app)
{
return (app ? app->debug : 0) || global_debug;
}
int stasis_app_get_debug_by_name(const char *app_name)
{
RAII_VAR(struct stasis_app *, app, stasis_app_get_by_name(app_name), ao2_cleanup);
return (app ? app->debug : 0) || global_debug;
}
void stasis_app_set_global_debug(int debug)
{
global_debug = debug;
if (!global_debug) {
struct ao2_container *app_names = stasis_app_get_all();
struct ao2_iterator it_app_names;
char *app_name;
struct stasis_app *app;
if (!app_names || !ao2_container_count(app_names)) {
ao2_cleanup(app_names);
return;
}
it_app_names = ao2_iterator_init(app_names, 0);
while ((app_name = ao2_iterator_next(&it_app_names))) {
if ((app = stasis_app_get_by_name(app_name))) {
stasis_app_set_debug(app, 0);
}
ao2_cleanup(app_name);
ao2_cleanup(app);
}
ao2_iterator_cleanup(&it_app_names);
ao2_cleanup(app_names);
}
}
@@ -949,7 +1001,6 @@ struct stasis_topic *ast_app_get_topic(struct stasis_app *app)
void app_send(struct stasis_app *app, struct ast_json *message)
{
stasis_app_cb handler;
int debug;
char eid[20];
RAII_VAR(void *, data, NULL, ao2_cleanup);
@@ -962,7 +1013,6 @@ void app_send(struct stasis_app *app, struct ast_json *message)
/* Copy off mutable state with lock held */
{
SCOPED_AO2LOCK(lock, app);
debug = app->debug;
handler = app->handler;
if (app->data) {
ao2_ref(app->data, +1);
@@ -971,13 +1021,6 @@ void app_send(struct stasis_app *app, struct ast_json *message)
/* Name is immutable; no need to copy */
}
if (debug) {
char *dump = ast_json_dump_string_format(message, AST_JSON_PRETTY);
ast_verb(0, "Dispatching message to Stasis app '%s':\n%s\n",
app->name, dump);
ast_json_free(dump);
}
if (!handler) {
ast_verb(3,
"Inactive Stasis app '%s' missed message\n", app->name);
@@ -1050,7 +1093,7 @@ void app_update(struct stasis_app *app, stasis_app_cb handler, void *data)
app->data = data;
}
const char *app_name(const struct stasis_app *app)
const char *stasis_app_name(const struct stasis_app *app)
{
return app->name;
}
@@ -1067,7 +1110,7 @@ static int forwards_filter_by_type(void *obj, void *arg, int flags)
return 0;
}
void app_to_cli(const struct stasis_app *app, struct ast_cli_args *a)
void stasis_app_to_cli(const struct stasis_app *app, struct ast_cli_args *a)
{
struct ao2_iterator *channels;
struct ao2_iterator *endpoints;

View File

@@ -108,15 +108,6 @@ int app_is_finished(struct stasis_app *app);
*/
void app_update(struct stasis_app *app, stasis_app_cb handler, void *data);
/*!
* \brief Return an application's name.
*
* \param app Application.
* \return Name of the application.
* \return \c NULL is \a app is \c NULL.
*/
const char *app_name(const struct stasis_app *app);
/*!
* \brief Send a message to an application.
*
@@ -137,16 +128,6 @@ struct app_forwards;
*/
struct ast_json *app_to_json(const struct stasis_app *app);
struct ast_cli_args;
/*!
* \brief Dump properties of a \c stasis_app to the CLI
*
* \param app The application
* \param a The CLI arguments
*/
void app_to_cli(const struct stasis_app *app, struct ast_cli_args *a);
/*!
* \brief Subscribes an application to a channel.
*
@@ -300,12 +281,4 @@ char *app_get_replace_channel_app(struct ast_channel *chan);
*/
int app_send_end_msg(struct stasis_app *app, struct ast_channel *chan);
/*!
* \brief Enable/disable debugging on an application
*
* \param app The app to debug
* \param debug If non-zero, enable debugging. If zero, disable.
*/
void app_set_debug(struct stasis_app *app, int debug);
#endif /* _ASTERISK_RES_STASIS_APP_H */

View File

@@ -1,214 +0,0 @@
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2016, Digium, Inc.
*
* Matt Jordan <mjordan@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Stasis CLI commands.
*
* \author Matt Jordan <mjordan@digium.com>
*/
#include "asterisk.h"
#include "asterisk/cli.h"
#include "asterisk/astobj2.h"
#include "cli.h"
#include "app.h"
static char *ari_show_apps(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
struct ao2_container *apps;
struct ao2_iterator it_apps;
char *app;
switch (cmd) {
case CLI_INIT:
e->command = "ari show apps";
e->usage =
"Usage: ari show apps\n"
" Lists all registered applications.\n"
;
return NULL;
case CLI_GENERATE:
return NULL;
default:
break;
}
if (a->argc != 3) {
return CLI_SHOWUSAGE;
}
apps = stasis_app_get_all();
if (!apps) {
ast_cli(a->fd, "Unable to retrieve registered applications!\n");
return CLI_FAILURE;
}
ast_cli(a->fd, "Application Name \n");
ast_cli(a->fd, "=========================\n");
it_apps = ao2_iterator_init(apps, 0);
while ((app = ao2_iterator_next(&it_apps))) {
ast_cli(a->fd, "%-25.25s\n", app);
ao2_ref(app, -1);
}
ao2_iterator_destroy(&it_apps);
ao2_ref(apps, -1);
return CLI_SUCCESS;
}
struct app_complete {
/*! Nth app to search for */
int state;
/*! Which app currently on */
int which;
};
static int complete_ari_app_search(void *obj, void *arg, void *data, int flags)
{
struct app_complete *search = data;
if (++search->which > search->state) {
return CMP_MATCH;
}
return 0;
}
static char *complete_ari_app(struct ast_cli_args *a)
{
RAII_VAR(struct ao2_container *, apps, stasis_app_get_all(), ao2_cleanup);
RAII_VAR(char *, app, NULL, ao2_cleanup);
struct app_complete search = {
.state = a->n,
};
if (!apps) {
ast_cli(a->fd, "Error getting ARI applications\n");
return CLI_FAILURE;
}
app = ao2_callback_data(apps,
ast_strlen_zero(a->word) ? 0 : OBJ_PARTIAL_KEY,
complete_ari_app_search, (char*)a->word, &search);
return app ? ast_strdup(app) : NULL;
}
static char *complete_ari_show_app(struct ast_cli_args *a)
{
if (a->pos == 3) {
return complete_ari_app(a);
}
return NULL;
}
static char *ari_show_app(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
void *app;
switch (cmd) {
case CLI_INIT:
e->command = "ari show app";
e->usage =
"Usage: ari show app <application>\n"
" Provide detailed information about a registered application.\n"
;
return NULL;
case CLI_GENERATE:
return complete_ari_show_app(a);
default:
break;
}
if (a->argc != 4) {
return CLI_SHOWUSAGE;
}
app = stasis_app_get_by_name(a->argv[3]);
if (!app) {
return CLI_FAILURE;
}
app_to_cli(app, a);
ao2_ref(app, -1);
return CLI_SUCCESS;
}
static char *ari_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
void *app;
int debug;
switch (cmd) {
case CLI_INIT:
e->command = "ari set debug";
e->usage =
"Usage: ari set debug <application> <on|off>\n"
" Enable or disable debugging on a specific application.\n"
;
return NULL;
case CLI_GENERATE:
return complete_ari_show_app(a);
default:
break;
}
if (a->argc != 5) {
return CLI_SHOWUSAGE;
}
app = stasis_app_get_by_name(a->argv[3]);
if (!app) {
return CLI_FAILURE;
}
debug = !strcmp(a->argv[4], "on");
app_set_debug(app, debug);
ast_cli(a->fd, "Debugging on '%s' %s\n",
app_name(app),
debug ? "enabled" : "disabled");
ao2_ref(app, -1);
return CLI_SUCCESS;
}
static struct ast_cli_entry cli_ari[] = {
AST_CLI_DEFINE(ari_show_apps, "List registered ARI applications"),
AST_CLI_DEFINE(ari_show_app, "Display details of a registered ARI application"),
AST_CLI_DEFINE(ari_set_debug, "Enable/disable debugging of an ARI application"),
};
int cli_init(void)
{
return ast_cli_register_multiple(cli_ari, ARRAY_LEN(cli_ari));
}
void cli_cleanup(void)
{
ast_cli_unregister_multiple(cli_ari, ARRAY_LEN(cli_ari));
}

View File

@@ -1,43 +0,0 @@
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2016, Digium, Inc.
*
* Matt Jordan <mjordan@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
#ifndef _ASTERISK_RES_STASIS_CLI_H
#define _ASTERISK_RES_STASIS_CLI_H
/*! \file
*
* \brief Internal API for Stasis application CLI commands
*
* \author Matt Jordan <mjordan@digium.com>
* \since 13.13.0
*/
/*!
* \brief Initialize the CLI commands
*
* \retval 0 on success
* \retval non-zero on error
*/
int cli_init(void);
/*!
* \brief Cleanup the CLI commands
*/
void cli_cleanup(void);
#endif /* _ASTERISK_RES_STASIS_CLI_H */

View File

@@ -155,13 +155,13 @@ static int bridge_stasis_push_peek(struct ast_bridge *self, struct ast_bridge_ch
}
to_be_replaced = ast_channel_snapshot_get_latest(ast_channel_uniqueid(swap->chan));
ast_debug(3, "Copying stasis app name %s from %s to %s\n", app_name(control_app(swap_control)),
ast_debug(3, "Copying stasis app name %s from %s to %s\n", stasis_app_name(control_app(swap_control)),
ast_channel_name(swap->chan), ast_channel_name(bridge_channel->chan));
ast_channel_lock(bridge_channel->chan);
/* copy the app name from the swap channel */
app_set_replace_channel_app(bridge_channel->chan, app_name(control_app(swap_control)));
app_set_replace_channel_app(bridge_channel->chan, stasis_app_name(control_app(swap_control)));
/* set the replace channel snapshot */
app_set_replace_channel_snapshot(bridge_channel->chan, to_be_replaced);