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