Merged revisions 24669 via svnmerge from
[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 LOCAL_USER_DECL;
58
59 static int verbose_exec(struct ast_channel *chan, void *data)
60 {
61         char *vtext;
62         int vsize;
63         struct localuser *u;
64
65         LOCAL_USER_ADD(u);
66
67         if (data) {
68                 if ((vtext = ast_strdupa(data))) {
69                         char *tmp = strsep(&vtext, "|");
70                         if (vtext) {
71                                 if (sscanf(tmp, "%d", &vsize) != 1) {
72                                         vsize = 0;
73                                         ast_log(LOG_WARNING, "'%s' is not a verboser number\n", vtext);
74                                 }
75                         } else {
76                                 vtext = tmp;
77                                 vsize = 0;
78                         }
79                         if (option_verbose >= vsize) {
80                                 switch (vsize) {
81                                 case 0:
82                                         ast_verbose("%s\n", vtext);
83                                         break;
84                                 case 1:
85                                         ast_verbose(VERBOSE_PREFIX_1 "%s\n", vtext);
86                                         break;
87                                 case 2:
88                                         ast_verbose(VERBOSE_PREFIX_2 "%s\n", vtext);
89                                         break;
90                                 case 3:
91                                         ast_verbose(VERBOSE_PREFIX_3 "%s\n", vtext);
92                                         break;
93                                 default:
94                                         ast_verbose(VERBOSE_PREFIX_4 "%s\n", vtext);
95                                 }
96                         }
97                 }
98         }
99
100         LOCAL_USER_REMOVE(u);
101
102         return 0;
103 }
104
105 static int log_exec(struct ast_channel *chan, void *data)
106 {
107         char *level, *ltext;
108         struct localuser *u;
109         int lnum = -1;
110         char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
111
112         LOCAL_USER_ADD(u);
113         if (ast_strlen_zero(data)) {
114                 LOCAL_USER_REMOVE(u);
115                 return 0;
116         }
117
118         if (!(ltext = ast_strdupa(data))) {
119                 LOCAL_USER_REMOVE(u);
120                 return 0;
121         }
122
123         level = strsep(&ltext, "|");
124
125         if (!strcasecmp(level, "ERROR")) {
126                 lnum = __LOG_ERROR;
127         } else if (!strcasecmp(level, "WARNING")) {
128                 lnum = __LOG_WARNING;
129         } else if (!strcasecmp(level, "NOTICE")) {
130                 lnum = __LOG_NOTICE;
131         } else if (!strcasecmp(level, "DEBUG")) {
132                 lnum = __LOG_DEBUG;
133         } else if (!strcasecmp(level, "VERBOSE")) {
134                 lnum = __LOG_VERBOSE;
135         } else if (!strcasecmp(level, "DTMF")) {
136                 lnum = __LOG_DTMF;
137         } else if (!strcasecmp(level, "EVENT")) {
138                 lnum = __LOG_EVENT;
139         } else {
140                 ast_log(LOG_ERROR, "Unknown log level: '%s'\n", level);
141         }
142
143         if (lnum > -1) {
144                 snprintf(context, sizeof(context), "@ %s", chan->context);
145                 snprintf(extension, sizeof(extension), "Ext. %s", chan->exten);
146
147                 ast_log(lnum, extension, chan->priority, context, "%s\n", ltext);
148         }
149         LOCAL_USER_REMOVE(u);
150         return 0;
151 }
152
153 static int unload_module(void *mod)
154 {
155         int res;
156
157         res = ast_unregister_application(app_verbose);
158         res |= ast_unregister_application(app_log);
159
160         STANDARD_HANGUP_LOCALUSERS;
161
162         return res;     
163 }
164
165 static int load_module(void *mod)
166 {
167         int res;
168
169         res = ast_register_application(app_log, log_exec, log_synopsis, log_descrip);
170         res |= ast_register_application(app_verbose, verbose_exec, verbose_synopsis, verbose_descrip);
171
172         return res;
173 }
174
175 static const char *description(void)
176 {
177         return tdesc;
178 }
179
180 static const char *key(void)
181 {
182         return ASTERISK_GPL_KEY;
183 }
184
185 STD_MOD1;