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