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