Merge "AST-2018-005: Fix tdata leaks when calling pjsip_endpt_send_response(2)"
[asterisk/asterisk.git] / apps / app_verbose.c
index 33f6660..5b78e18 100644 (file)
  * \ingroup applications
  */
 
-#include "asterisk.h"
-
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
+#include "asterisk.h"
 
-#include "asterisk/options.h"
-#include "asterisk/logger.h"
-#include "asterisk/channel.h"
-#include "asterisk/pbx.h"
 #include "asterisk/module.h"
-
-
-static char *tdesc = "Send verbose output";
+#include "asterisk/app.h"
+#include "asterisk/channel.h"
 
 static char *app_verbose = "Verbose";
-static char *verbose_synopsis = "Send arbitrary text to verbose output";
-static char *verbose_descrip =
-"Verbose([<level>|]<message>)\n"
-"  level must be an integer value.  If not specified, defaults to 0.\n";
-
 static char *app_log = "Log";
-static char *log_synopsis = "Send arbitrary text to a selected log level";
-static char *log_descrip =
-"Log(<level>|<message>)\n"
-"  level must be one of ERROR, WARNING, NOTICE, DEBUG, VERBOSE, DTMF\n";
-
-LOCAL_USER_DECL;
 
-static int verbose_exec(struct ast_channel *chan, void *data)
+/*** DOCUMENTATION
+       <application name="Verbose" language="en_US">
+               <synopsis>
+                       Send arbitrary text to verbose output.
+               </synopsis>
+               <syntax>
+                       <parameter name="level">
+                               <para>Must be an integer value.  If not specified, defaults to 0.</para>
+                       </parameter>
+                       <parameter name="message" required="true">
+                               <para>Output text message.</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Sends an arbitrary text message to verbose output.</para>
+               </description>
+       </application>
+       <application name="Log" language="en_US">
+               <synopsis>
+                       Send arbitrary text to a selected log level.
+               </synopsis>
+               <syntax>
+                       <parameter name="level" required="true">
+                               <para>Level must be one of <literal>ERROR</literal>, <literal>WARNING</literal>, <literal>NOTICE</literal>,
+                               <literal>DEBUG</literal>, <literal>VERBOSE</literal> or <literal>DTMF</literal>.</para>
+                       </parameter>
+                       <parameter name="message" required="true">
+                               <para>Output text message.</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Sends an arbitrary text message to a selected log level.</para>
+               </description>
+       </application>
+ ***/
+
+
+static int verbose_exec(struct ast_channel *chan, const char *data)
 {
-       char *vtext;
-       int vsize;
-       struct localuser *u;
-
-       LOCAL_USER_ADD(u);
-
-       if (data) {
-               char *tmp;
-               vtext = ast_strdupa(data);
-               tmp = strsep(&vtext, "|");
-               if (vtext) {
-                       if (sscanf(tmp, "%d", &vsize) != 1) {
-                               vsize = 0;
-                               ast_log(LOG_WARNING, "'%s' is not a verboser number\n", vtext);
-                       }
-               } else {
-                       vtext = tmp;
-                       vsize = 0;
-               }
-               if (option_verbose >= vsize) {
-                       switch (vsize) {
-                       case 0:
-                               ast_verbose("%s\n", vtext);
-                               break;
-                       case 1:
-                               ast_verbose(VERBOSE_PREFIX_1 "%s\n", vtext);
-                               break;
-                       case 2:
-                               ast_verbose(VERBOSE_PREFIX_2 "%s\n", vtext);
-                               break;
-                       case 3:
-                               ast_verbose(VERBOSE_PREFIX_3 "%s\n", vtext);
-                               break;
-                       default:
-                               ast_verbose(VERBOSE_PREFIX_4 "%s\n", vtext);
-                       }
-               }
+       unsigned int vsize;
+       char *parse;
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(level);
+               AST_APP_ARG(msg);
+       );
+
+       if (ast_strlen_zero(data)) {
+               return 0;
+       }
+
+       parse = ast_strdupa(data);
+       AST_STANDARD_APP_ARGS(args, parse);
+       if (args.argc == 1) {
+               args.msg = args.level;
+               args.level = "0";
        }
 
-       LOCAL_USER_REMOVE(u);
+       if (sscanf(args.level, "%30u", &vsize) != 1) {
+               vsize = 0;
+               ast_log(LOG_WARNING, "'%s' is not a verboser number\n", args.level);
+       } else if (4 < vsize) {
+               vsize = 4;
+       }
+
+       ast_verb(vsize, "%s\n", args.msg);
 
        return 0;
 }
 
-static int log_exec(struct ast_channel *chan, void *data)
+static int log_exec(struct ast_channel *chan, const char *data)
 {
-       char *level, *ltext;
-       struct localuser *u;
+       char *parse;
        int lnum = -1;
        char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(level);
+               AST_APP_ARG(msg);
+       );
 
-       LOCAL_USER_ADD(u);
-       if (ast_strlen_zero(data)) {
-               LOCAL_USER_REMOVE(u);
+       if (ast_strlen_zero(data))
                return 0;
-       }
 
-       ltext = ast_strdupa(data);
+       parse = ast_strdupa(data);
+       AST_STANDARD_APP_ARGS(args, parse);
 
-       level = strsep(&ltext, "|");
-
-       if (!strcasecmp(level, "ERROR")) {
+       if (!strcasecmp(args.level, "ERROR")) {
                lnum = __LOG_ERROR;
-       } else if (!strcasecmp(level, "WARNING")) {
+       } else if (!strcasecmp(args.level, "WARNING")) {
                lnum = __LOG_WARNING;
-       } else if (!strcasecmp(level, "NOTICE")) {
+       } else if (!strcasecmp(args.level, "NOTICE")) {
                lnum = __LOG_NOTICE;
-       } else if (!strcasecmp(level, "DEBUG")) {
+       } else if (!strcasecmp(args.level, "DEBUG")) {
                lnum = __LOG_DEBUG;
-       } else if (!strcasecmp(level, "VERBOSE")) {
+       } else if (!strcasecmp(args.level, "VERBOSE")) {
                lnum = __LOG_VERBOSE;
-       } else if (!strcasecmp(level, "DTMF")) {
+       } else if (!strcasecmp(args.level, "DTMF")) {
                lnum = __LOG_DTMF;
-       } else if (!strcasecmp(level, "EVENT")) {
-               lnum = __LOG_EVENT;
        } else {
-               ast_log(LOG_ERROR, "Unknown log level: '%s'\n", level);
+               ast_log(LOG_ERROR, "Unknown log level: '%s'\n", args.level);
        }
 
        if (lnum > -1) {
-               snprintf(context, sizeof(context), "@ %s", chan->context);
-               snprintf(extension, sizeof(extension), "Ext. %s", chan->exten);
+               snprintf(context, sizeof(context), "@ %s", ast_channel_context(chan));
+               snprintf(extension, sizeof(extension), "Ext. %s", ast_channel_exten(chan));
 
-               ast_log(lnum, extension, chan->priority, context, "%s\n", ltext);
+               ast_log(lnum, extension, ast_channel_priority(chan), context, "%s\n", args.msg);
        }
-       LOCAL_USER_REMOVE(u);
+
        return 0;
 }
 
-static int unload_module(void *mod)
+static int unload_module(void)
 {
        int res;
 
        res = ast_unregister_application(app_verbose);
        res |= ast_unregister_application(app_log);
 
-       STANDARD_HANGUP_LOCALUSERS;
-
-       return res;     
+       return res;
 }
 
-static int load_module(void *mod)
+static int load_module(void)
 {
        int res;
 
-       res = ast_register_application(app_log, log_exec, log_synopsis, log_descrip);
-       res |= ast_register_application(app_verbose, verbose_exec, verbose_synopsis, verbose_descrip);
+       res = ast_register_application_xml(app_log, log_exec);
+       res |= ast_register_application_xml(app_verbose, verbose_exec);
 
        return res;
 }
 
-static const char *description(void)
-{
-       return tdesc;
-}
-
-static const char *key(void)
-{
-       return ASTERISK_GPL_KEY;
-}
-
-STD_MOD1;
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send verbose output");