Merge "AST-2018-005: Fix tdata leaks when calling pjsip_endpt_send_response(2)"
[asterisk/asterisk.git] / apps / app_verbose.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2004 - 2005 Tilghman Lesher.  All rights reserved.
5  *
6  * Tilghman Lesher <app_verbose_v001@the-tilghman.com>
7  *
8  * This code is released by the author with no restrictions on usage.
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  */
17
18 /*! \file
19  *
20  * \brief Verbose logging application
21  *
22  * \author Tilghman Lesher <app_verbose_v001@the-tilghman.com>
23  *
24  * \ingroup applications
25  */
26
27 /*** MODULEINFO
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 #include "asterisk/module.h"
34 #include "asterisk/app.h"
35 #include "asterisk/channel.h"
36
37 static char *app_verbose = "Verbose";
38 static char *app_log = "Log";
39
40 /*** DOCUMENTATION
41         <application name="Verbose" language="en_US">
42                 <synopsis>
43                         Send arbitrary text to verbose output.
44                 </synopsis>
45                 <syntax>
46                         <parameter name="level">
47                                 <para>Must be an integer value.  If not specified, defaults to 0.</para>
48                         </parameter>
49                         <parameter name="message" required="true">
50                                 <para>Output text message.</para>
51                         </parameter>
52                 </syntax>
53                 <description>
54                         <para>Sends an arbitrary text message to verbose output.</para>
55                 </description>
56         </application>
57         <application name="Log" language="en_US">
58                 <synopsis>
59                         Send arbitrary text to a selected log level.
60                 </synopsis>
61                 <syntax>
62                         <parameter name="level" required="true">
63                                 <para>Level must be one of <literal>ERROR</literal>, <literal>WARNING</literal>, <literal>NOTICE</literal>,
64                                 <literal>DEBUG</literal>, <literal>VERBOSE</literal> or <literal>DTMF</literal>.</para>
65                         </parameter>
66                         <parameter name="message" required="true">
67                                 <para>Output text message.</para>
68                         </parameter>
69                 </syntax>
70                 <description>
71                         <para>Sends an arbitrary text message to a selected log level.</para>
72                 </description>
73         </application>
74  ***/
75
76
77 static int verbose_exec(struct ast_channel *chan, const char *data)
78 {
79         unsigned int vsize;
80         char *parse;
81         AST_DECLARE_APP_ARGS(args,
82                 AST_APP_ARG(level);
83                 AST_APP_ARG(msg);
84         );
85
86         if (ast_strlen_zero(data)) {
87                 return 0;
88         }
89
90         parse = ast_strdupa(data);
91         AST_STANDARD_APP_ARGS(args, parse);
92         if (args.argc == 1) {
93                 args.msg = args.level;
94                 args.level = "0";
95         }
96
97         if (sscanf(args.level, "%30u", &vsize) != 1) {
98                 vsize = 0;
99                 ast_log(LOG_WARNING, "'%s' is not a verboser number\n", args.level);
100         } else if (4 < vsize) {
101                 vsize = 4;
102         }
103
104         ast_verb(vsize, "%s\n", args.msg);
105
106         return 0;
107 }
108
109 static int log_exec(struct ast_channel *chan, const char *data)
110 {
111         char *parse;
112         int lnum = -1;
113         char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
114         AST_DECLARE_APP_ARGS(args,
115                 AST_APP_ARG(level);
116                 AST_APP_ARG(msg);
117         );
118
119         if (ast_strlen_zero(data))
120                 return 0;
121
122         parse = ast_strdupa(data);
123         AST_STANDARD_APP_ARGS(args, parse);
124
125         if (!strcasecmp(args.level, "ERROR")) {
126                 lnum = __LOG_ERROR;
127         } else if (!strcasecmp(args.level, "WARNING")) {
128                 lnum = __LOG_WARNING;
129         } else if (!strcasecmp(args.level, "NOTICE")) {
130                 lnum = __LOG_NOTICE;
131         } else if (!strcasecmp(args.level, "DEBUG")) {
132                 lnum = __LOG_DEBUG;
133         } else if (!strcasecmp(args.level, "VERBOSE")) {
134                 lnum = __LOG_VERBOSE;
135         } else if (!strcasecmp(args.level, "DTMF")) {
136                 lnum = __LOG_DTMF;
137         } else {
138                 ast_log(LOG_ERROR, "Unknown log level: '%s'\n", args.level);
139         }
140
141         if (lnum > -1) {
142                 snprintf(context, sizeof(context), "@ %s", ast_channel_context(chan));
143                 snprintf(extension, sizeof(extension), "Ext. %s", ast_channel_exten(chan));
144
145                 ast_log(lnum, extension, ast_channel_priority(chan), context, "%s\n", args.msg);
146         }
147
148         return 0;
149 }
150
151 static int unload_module(void)
152 {
153         int res;
154
155         res = ast_unregister_application(app_verbose);
156         res |= ast_unregister_application(app_log);
157
158         return res;
159 }
160
161 static int load_module(void)
162 {
163         int res;
164
165         res = ast_register_application_xml(app_log, log_exec);
166         res |= ast_register_application_xml(app_verbose, verbose_exec);
167
168         return res;
169 }
170
171 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send verbose output");