c94ba290eb564bed57646db7b4886edc980a648f
[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 <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
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 static char *app_verbose = "Verbose";
43 static char *verbose_synopsis = "Send arbitrary text to verbose output";
44 static char *verbose_descrip =
45 "Verbose([<level>|]<message>)\n"
46 "  level must be an integer value.  If not specified, defaults to 0.\n";
47
48 static char *app_log = "Log";
49 static char *log_synopsis = "Send arbitrary text to a selected log level";
50 static char *log_descrip =
51 "Log(<level>|<message>)\n"
52 "  level must be one of ERROR, WARNING, NOTICE, DEBUG, VERBOSE, DTMF\n";
53
54
55 static int verbose_exec(struct ast_channel *chan, void *data)
56 {
57         char *vtext;
58         int vsize;
59         struct ast_module_user *u;
60
61         u = ast_module_user_add(chan);
62
63         if (data) {
64                 char *tmp;
65                 vtext = ast_strdupa(data);
66                 tmp = strsep(&vtext, "|");
67                 if (vtext) {
68                         if (sscanf(tmp, "%d", &vsize) != 1) {
69                                 vsize = 0;
70                                 ast_log(LOG_WARNING, "'%s' is not a verboser number\n", vtext);
71                         }
72                 } else {
73                         vtext = tmp;
74                         vsize = 0;
75                 }
76                 if (option_verbose >= vsize) {
77                         switch (vsize) {
78                         case 0:
79                                 ast_verbose("%s\n", vtext);
80                                 break;
81                         case 1:
82                                 ast_verbose(VERBOSE_PREFIX_1 "%s\n", vtext);
83                                 break;
84                         case 2:
85                                 ast_verbose(VERBOSE_PREFIX_2 "%s\n", vtext);
86                                 break;
87                         case 3:
88                                 ast_verbose(VERBOSE_PREFIX_3 "%s\n", vtext);
89                                 break;
90                         default:
91                                 ast_verbose(VERBOSE_PREFIX_4 "%s\n", vtext);
92                         }
93                 }
94         }
95
96         ast_module_user_remove(u);
97
98         return 0;
99 }
100
101 static int log_exec(struct ast_channel *chan, void *data)
102 {
103         char *level, *ltext;
104         struct ast_module_user *u;
105         int lnum = -1;
106         char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
107
108         u = ast_module_user_add(chan);
109         if (ast_strlen_zero(data)) {
110                 ast_module_user_remove(u);
111                 return 0;
112         }
113
114         ltext = ast_strdupa(data);
115
116         level = strsep(&ltext, "|");
117
118         if (!strcasecmp(level, "ERROR")) {
119                 lnum = __LOG_ERROR;
120         } else if (!strcasecmp(level, "WARNING")) {
121                 lnum = __LOG_WARNING;
122         } else if (!strcasecmp(level, "NOTICE")) {
123                 lnum = __LOG_NOTICE;
124         } else if (!strcasecmp(level, "DEBUG")) {
125                 lnum = __LOG_DEBUG;
126         } else if (!strcasecmp(level, "VERBOSE")) {
127                 lnum = __LOG_VERBOSE;
128         } else if (!strcasecmp(level, "DTMF")) {
129                 lnum = __LOG_DTMF;
130         } else if (!strcasecmp(level, "EVENT")) {
131                 lnum = __LOG_EVENT;
132         } else {
133                 ast_log(LOG_ERROR, "Unknown log level: '%s'\n", level);
134         }
135
136         if (lnum > -1) {
137                 snprintf(context, sizeof(context), "@ %s", chan->context);
138                 snprintf(extension, sizeof(extension), "Ext. %s", chan->exten);
139
140                 ast_log(lnum, extension, chan->priority, context, "%s\n", ltext);
141         }
142         ast_module_user_remove(u);
143         return 0;
144 }
145
146 static int unload_module(void)
147 {
148         int res;
149
150         res = ast_unregister_application(app_verbose);
151         res |= ast_unregister_application(app_log);
152
153         return res;     
154 }
155
156 static int load_module(void)
157 {
158         int res;
159
160         res = ast_register_application(app_log, log_exec, log_synopsis, log_descrip);
161         res |= ast_register_application(app_verbose, verbose_exec, verbose_synopsis, verbose_descrip);
162
163         return res;
164 }
165
166 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send verbose output");