Applications no longer need to call ast_module_user_add and ast_module_user_remove...
[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
60         if (data) {
61                 char *tmp;
62                 vtext = ast_strdupa(data);
63                 tmp = strsep(&vtext, "|");
64                 if (vtext) {
65                         if (sscanf(tmp, "%d", &vsize) != 1) {
66                                 vsize = 0;
67                                 ast_log(LOG_WARNING, "'%s' is not a verboser number\n", vtext);
68                         }
69                 } else {
70                         vtext = tmp;
71                         vsize = 0;
72                 }
73                 if (option_verbose >= vsize) {
74                         switch (vsize) {
75                         case 0:
76                                 ast_verbose("%s\n", vtext);
77                                 break;
78                         case 1:
79                                 ast_verbose(VERBOSE_PREFIX_1 "%s\n", vtext);
80                                 break;
81                         case 2:
82                                 ast_verbose(VERBOSE_PREFIX_2 "%s\n", vtext);
83                                 break;
84                         case 3:
85                                 ast_verbose(VERBOSE_PREFIX_3 "%s\n", vtext);
86                                 break;
87                         default:
88                                 ast_verbose(VERBOSE_PREFIX_4 "%s\n", vtext);
89                         }
90                 }
91         }
92
93         return 0;
94 }
95
96 static int log_exec(struct ast_channel *chan, void *data)
97 {
98         char *level, *ltext;
99         int lnum = -1;
100         char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
101
102         if (ast_strlen_zero(data))
103                 return 0;
104
105         ltext = ast_strdupa(data);
106
107         level = strsep(&ltext, "|");
108
109         if (!strcasecmp(level, "ERROR")) {
110                 lnum = __LOG_ERROR;
111         } else if (!strcasecmp(level, "WARNING")) {
112                 lnum = __LOG_WARNING;
113         } else if (!strcasecmp(level, "NOTICE")) {
114                 lnum = __LOG_NOTICE;
115         } else if (!strcasecmp(level, "DEBUG")) {
116                 lnum = __LOG_DEBUG;
117         } else if (!strcasecmp(level, "VERBOSE")) {
118                 lnum = __LOG_VERBOSE;
119         } else if (!strcasecmp(level, "DTMF")) {
120                 lnum = __LOG_DTMF;
121         } else if (!strcasecmp(level, "EVENT")) {
122                 lnum = __LOG_EVENT;
123         } else {
124                 ast_log(LOG_ERROR, "Unknown log level: '%s'\n", level);
125         }
126
127         if (lnum > -1) {
128                 snprintf(context, sizeof(context), "@ %s", chan->context);
129                 snprintf(extension, sizeof(extension), "Ext. %s", chan->exten);
130
131                 ast_log(lnum, extension, chan->priority, context, "%s\n", ltext);
132         }
133
134         return 0;
135 }
136
137 static int unload_module(void)
138 {
139         int res;
140
141         res = ast_unregister_application(app_verbose);
142         res |= ast_unregister_application(app_log);
143
144         return res;     
145 }
146
147 static int load_module(void)
148 {
149         int res;
150
151         res = ast_register_application(app_log, log_exec, log_synopsis, log_descrip);
152         res |= ast_register_application(app_verbose, verbose_exec, verbose_synopsis, verbose_descrip);
153
154         return res;
155 }
156
157 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send verbose output");