Merge the dialplan_aesthetics branch. Most of this patch simply converts applications
[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 #include "asterisk/app.h"
42
43 static char *app_verbose = "Verbose";
44 static char *verbose_synopsis = "Send arbitrary text to verbose output";
45 static char *verbose_descrip =
46 "Verbose([<level>,]<message>)\n"
47 "  level must be an integer value.  If not specified, defaults to 0.\n";
48
49 static char *app_log = "Log";
50 static char *log_synopsis = "Send arbitrary text to a selected log level";
51 static char *log_descrip =
52 "Log(<level>,<message>)\n"
53 "  level must be one of ERROR, WARNING, NOTICE, DEBUG, VERBOSE, DTMF\n";
54
55
56 static int verbose_exec(struct ast_channel *chan, void *data)
57 {
58         int vsize;
59         char *parse;
60         AST_DECLARE_APP_ARGS(args,
61                 AST_APP_ARG(level);
62                 AST_APP_ARG(msg);
63         );
64
65         if (ast_strlen_zero(data)) {
66                 return 0;
67         }
68
69         parse = ast_strdupa(data);
70         AST_STANDARD_APP_ARGS(args, parse);
71         if (args.argc == 1) {
72                 args.msg = args.level;
73                 args.level = "0";
74         }
75
76         if (sscanf(args.level, "%d", &vsize) != 1) {
77                 vsize = 0;
78                 ast_log(LOG_WARNING, "'%s' is not a verboser number\n", args.level);
79         }
80         if (option_verbose >= vsize) {
81                 switch (vsize) {
82                 case 0:
83                         ast_verbose("%s\n", args.msg);
84                         break;
85                 case 1:
86                         ast_verbose(VERBOSE_PREFIX_1 "%s\n", args.msg);
87                         break;
88                 case 2:
89                         ast_verbose(VERBOSE_PREFIX_2 "%s\n", args.msg);
90                         break;
91                 case 3:
92                         ast_verbose(VERBOSE_PREFIX_3 "%s\n", args.msg);
93                         break;
94                 default:
95                         ast_verbose(VERBOSE_PREFIX_4 "%s\n", args.msg);
96                 }
97         }
98
99         return 0;
100 }
101
102 static int log_exec(struct ast_channel *chan, void *data)
103 {
104         char *parse;
105         int lnum = -1;
106         char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
107         AST_DECLARE_APP_ARGS(args,
108                 AST_APP_ARG(level);
109                 AST_APP_ARG(msg);
110         );
111
112         if (ast_strlen_zero(data))
113                 return 0;
114
115         parse = ast_strdupa(data);
116         AST_STANDARD_APP_ARGS(args, parse);
117
118         if (!strcasecmp(args.level, "ERROR")) {
119                 lnum = __LOG_ERROR;
120         } else if (!strcasecmp(args.level, "WARNING")) {
121                 lnum = __LOG_WARNING;
122         } else if (!strcasecmp(args.level, "NOTICE")) {
123                 lnum = __LOG_NOTICE;
124         } else if (!strcasecmp(args.level, "DEBUG")) {
125                 lnum = __LOG_DEBUG;
126         } else if (!strcasecmp(args.level, "VERBOSE")) {
127                 lnum = __LOG_VERBOSE;
128         } else if (!strcasecmp(args.level, "DTMF")) {
129                 lnum = __LOG_DTMF;
130         } else if (!strcasecmp(args.level, "EVENT")) {
131                 lnum = __LOG_EVENT;
132         } else {
133                 ast_log(LOG_ERROR, "Unknown log level: '%s'\n", args.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", args.msg);
141         }
142
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");