git migration: Refactor the ASTERISK_FILE_VERSION macro
[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 ASTERISK_REGISTER_FILE()
34
35 #include "asterisk/module.h"
36 #include "asterisk/app.h"
37 #include "asterisk/channel.h"
38
39 static char *app_verbose = "Verbose";
40 static char *app_log = "Log";
41
42 /*** DOCUMENTATION
43         <application name="Verbose" language="en_US">
44                 <synopsis>
45                         Send arbitrary text to verbose output.
46                 </synopsis>
47                 <syntax>
48                         <parameter name="level">
49                                 <para>Must be an integer value.  If not specified, defaults to 0.</para>
50                         </parameter>
51                         <parameter name="message" required="true">
52                                 <para>Output text message.</para>
53                         </parameter>
54                 </syntax>
55                 <description>
56                         <para>Sends an arbitrary text message to verbose output.</para>
57                 </description>
58         </application>
59         <application name="Log" language="en_US">
60                 <synopsis>
61                         Send arbitrary text to a selected log level.
62                 </synopsis>
63                 <syntax>
64                         <parameter name="level" required="true">
65                                 <para>Level must be one of <literal>ERROR</literal>, <literal>WARNING</literal>, <literal>NOTICE</literal>,
66                                 <literal>DEBUG</literal>, <literal>VERBOSE</literal> or <literal>DTMF</literal>.</para>
67                         </parameter>
68                         <parameter name="message" required="true">
69                                 <para>Output text message.</para>
70                         </parameter>
71                 </syntax>
72                 <description>
73                         <para>Sends an arbitrary text message to a selected log level.</para>
74                 </description>
75         </application>
76  ***/
77
78
79 static int verbose_exec(struct ast_channel *chan, const char *data)
80 {
81         unsigned int vsize;
82         char *parse;
83         AST_DECLARE_APP_ARGS(args,
84                 AST_APP_ARG(level);
85                 AST_APP_ARG(msg);
86         );
87
88         if (ast_strlen_zero(data)) {
89                 return 0;
90         }
91
92         parse = ast_strdupa(data);
93         AST_STANDARD_APP_ARGS(args, parse);
94         if (args.argc == 1) {
95                 args.msg = args.level;
96                 args.level = "0";
97         }
98
99         if (sscanf(args.level, "%30u", &vsize) != 1) {
100                 vsize = 0;
101                 ast_log(LOG_WARNING, "'%s' is not a verboser number\n", args.level);
102         } else if (4 < vsize) {
103                 vsize = 4;
104         }
105
106         ast_verb(vsize, "%s\n", args.msg);
107
108         return 0;
109 }
110
111 static int log_exec(struct ast_channel *chan, const char *data)
112 {
113         char *parse;
114         int lnum = -1;
115         char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
116         AST_DECLARE_APP_ARGS(args,
117                 AST_APP_ARG(level);
118                 AST_APP_ARG(msg);
119         );
120
121         if (ast_strlen_zero(data))
122                 return 0;
123
124         parse = ast_strdupa(data);
125         AST_STANDARD_APP_ARGS(args, parse);
126
127         if (!strcasecmp(args.level, "ERROR")) {
128                 lnum = __LOG_ERROR;
129         } else if (!strcasecmp(args.level, "WARNING")) {
130                 lnum = __LOG_WARNING;
131         } else if (!strcasecmp(args.level, "NOTICE")) {
132                 lnum = __LOG_NOTICE;
133         } else if (!strcasecmp(args.level, "DEBUG")) {
134                 lnum = __LOG_DEBUG;
135         } else if (!strcasecmp(args.level, "VERBOSE")) {
136                 lnum = __LOG_VERBOSE;
137         } else if (!strcasecmp(args.level, "DTMF")) {
138                 lnum = __LOG_DTMF;
139         } else {
140                 ast_log(LOG_ERROR, "Unknown log level: '%s'\n", args.level);
141         }
142
143         if (lnum > -1) {
144                 snprintf(context, sizeof(context), "@ %s", ast_channel_context(chan));
145                 snprintf(extension, sizeof(extension), "Ext. %s", ast_channel_exten(chan));
146
147                 ast_log(lnum, extension, ast_channel_priority(chan), context, "%s\n", args.msg);
148         }
149
150         return 0;
151 }
152
153 static int unload_module(void)
154 {
155         int res;
156
157         res = ast_unregister_application(app_verbose);
158         res |= ast_unregister_application(app_log);
159
160         return res;
161 }
162
163 static int load_module(void)
164 {
165         int res;
166
167         res = ast_register_application_xml(app_log, log_exec);
168         res |= ast_register_application_xml(app_verbose, verbose_exec);
169
170         return res;
171 }
172
173 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send verbose output");