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