update doxygen docs to specify authors
[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 <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/options.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
41
42
43 static char *tdesc = "Send verbose output";
44
45 static char *app_verbose = "Verbose";
46 static char *verbose_synopsis = "Send arbitrary text to verbose output";
47 static char *verbose_descrip =
48 "Verbose([<level>|]<message>)\n"
49 "  level must be an integer value.  If not specified, defaults to 0.\n";
50
51 static char *app_log = "Log";
52 static char *log_synopsis = "Send arbitrary text to a selected log level";
53 static char *log_descrip =
54 "Log(<level>|<message>)\n"
55 "  level must be one of ERROR, WARNING, NOTICE, DEBUG, VERBOSE, DTMF\n";
56
57 STANDARD_LOCAL_USER;
58
59 LOCAL_USER_DECL;
60
61 static int verbose_exec(struct ast_channel *chan, void *data)
62 {
63         char *vtext;
64         int vsize;
65         struct localuser *u;
66
67         LOCAL_USER_ADD(u);
68
69         if (data) {
70                 vtext = ast_strdupa((char *)data);
71                 if (vtext) {
72                         char *tmp = strsep(&vtext, "|,");
73                         if (vtext) {
74                                 if (sscanf(tmp, "%d", &vsize) != 1) {
75                                         vsize = 0;
76                                         ast_log(LOG_WARNING, "'%s' is not a verboser number\n", vtext);
77                                 }
78                         } else {
79                                 vtext = tmp;
80                                 vsize = 0;
81                         }
82                         if (option_verbose >= vsize) {
83                                 switch (vsize) {
84                                 case 0:
85                                         ast_verbose("%s\n", vtext);
86                                         break;
87                                 case 1:
88                                         ast_verbose(VERBOSE_PREFIX_1 "%s\n", vtext);
89                                         break;
90                                 case 2:
91                                         ast_verbose(VERBOSE_PREFIX_2 "%s\n", vtext);
92                                         break;
93                                 case 3:
94                                         ast_verbose(VERBOSE_PREFIX_3 "%s\n", vtext);
95                                         break;
96                                 default:
97                                         ast_verbose(VERBOSE_PREFIX_4 "%s\n", vtext);
98                                 }
99                         }
100                 } else {
101                         ast_log(LOG_ERROR, "Out of memory\n");
102                 }
103         }
104
105         LOCAL_USER_REMOVE(u);
106
107         return 0;
108 }
109
110 static int log_exec(struct ast_channel *chan, void *data)
111 {
112         char *level, *ltext;
113         struct localuser *u;
114         int lnum = -1;
115         char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
116
117         LOCAL_USER_ADD(u);
118         if (ast_strlen_zero(data)) {
119                 LOCAL_USER_REMOVE(u);
120                 return 0;
121         }
122
123         ltext = ast_strdupa(data);
124         if (!ltext) {
125                 ast_log(LOG_ERROR, "Out of memory\n");
126                 LOCAL_USER_REMOVE(u);
127                 return 0;
128         }
129
130         level = strsep(&ltext, "|");
131
132         if (!strcasecmp(level, "ERROR")) {
133                 lnum = __LOG_ERROR;
134         } else if (!strcasecmp(level, "WARNING")) {
135                 lnum = __LOG_WARNING;
136         } else if (!strcasecmp(level, "NOTICE")) {
137                 lnum = __LOG_NOTICE;
138         } else if (!strcasecmp(level, "DEBUG")) {
139                 lnum = __LOG_DEBUG;
140         } else if (!strcasecmp(level, "VERBOSE")) {
141                 lnum = __LOG_VERBOSE;
142         } else if (!strcasecmp(level, "DTMF")) {
143                 lnum = __LOG_DTMF;
144         } else if (!strcasecmp(level, "EVENT")) {
145                 lnum = __LOG_EVENT;
146         } else {
147                 ast_log(LOG_ERROR, "Unknown log level: '%s'\n", level);
148         }
149
150         if (lnum > -1) {
151                 snprintf(context, sizeof(context), "@ %s", chan->context);
152                 snprintf(extension, sizeof(extension), "Ext. %s", chan->exten);
153
154                 ast_log(lnum, extension, chan->priority, context, "%s\n", ltext);
155         }
156         LOCAL_USER_REMOVE(u);
157         return 0;
158 }
159
160 int unload_module(void)
161 {
162         int res;
163
164         res = ast_unregister_application(app_verbose);
165         res |= ast_unregister_application(app_log);
166
167         STANDARD_HANGUP_LOCALUSERS;
168
169         return res;     
170 }
171
172 int load_module(void)
173 {
174         int res;
175
176         res = ast_register_application(app_log, log_exec, log_synopsis, log_descrip);
177         res |= ast_register_application(app_verbose, verbose_exec, verbose_synopsis, verbose_descrip);
178
179         return res;
180 }
181
182 char *description(void)
183 {
184         return tdesc;
185 }
186
187 int usecount(void)
188 {
189         int res;
190         STANDARD_USECOUNT(res);
191         return res;
192 }
193
194 char *key()
195 {
196         return ASTERISK_GPL_KEY;
197 }