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