a whole pile of Zaptel/DAHDI compatibility work, with lots more to come... this tree...
[asterisk-bristuff.git] / apps / app_verbose.c
blobf9bcfd1161998480ac27c828b49f7865c229dca8
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2004 - 2005 Tilghman Lesher. All rights reserved.
6 * Tilghman Lesher <app_verbose_v001@the-tilghman.com>
8 * This code is released by the author with no restrictions on usage.
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.
18 /*! \file
20 * \brief Verbose logging application
22 * \author Tilghman Lesher <app_verbose_v001@the-tilghman.com>
24 * \ingroup applications
27 #include "asterisk.h"
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
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"
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";
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";
55 static int verbose_exec(struct ast_channel *chan, void *data)
57 char *vtext;
58 int vsize;
59 struct ast_module_user *u;
61 u = ast_module_user_add(chan);
63 if (data) {
64 char *tmp;
65 vtext = ast_strdupa(data);
66 tmp = strsep(&vtext, "|");
67 if (vtext) {
68 if (sscanf(tmp, "%d", &vsize) != 1) {
69 vsize = 0;
70 ast_log(LOG_WARNING, "'%s' is not a verboser number\n", vtext);
72 } else {
73 vtext = tmp;
74 vsize = 0;
76 if (option_verbose >= vsize) {
77 switch (vsize) {
78 case 0:
79 ast_verbose("%s\n", vtext);
80 break;
81 case 1:
82 ast_verbose(VERBOSE_PREFIX_1 "%s\n", vtext);
83 break;
84 case 2:
85 ast_verbose(VERBOSE_PREFIX_2 "%s\n", vtext);
86 break;
87 case 3:
88 ast_verbose(VERBOSE_PREFIX_3 "%s\n", vtext);
89 break;
90 default:
91 ast_verbose(VERBOSE_PREFIX_4 "%s\n", vtext);
96 ast_module_user_remove(u);
98 return 0;
101 static int log_exec(struct ast_channel *chan, void *data)
103 char *level, *ltext;
104 struct ast_module_user *u;
105 int lnum = -1;
106 char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
108 u = ast_module_user_add(chan);
109 if (ast_strlen_zero(data)) {
110 ast_module_user_remove(u);
111 return 0;
114 ltext = ast_strdupa(data);
116 level = strsep(&ltext, "|");
118 if (!strcasecmp(level, "ERROR")) {
119 lnum = __LOG_ERROR;
120 } else if (!strcasecmp(level, "WARNING")) {
121 lnum = __LOG_WARNING;
122 } else if (!strcasecmp(level, "NOTICE")) {
123 lnum = __LOG_NOTICE;
124 } else if (!strcasecmp(level, "DEBUG")) {
125 lnum = __LOG_DEBUG;
126 } else if (!strcasecmp(level, "VERBOSE")) {
127 lnum = __LOG_VERBOSE;
128 } else if (!strcasecmp(level, "DTMF")) {
129 lnum = __LOG_DTMF;
130 } else if (!strcasecmp(level, "EVENT")) {
131 lnum = __LOG_EVENT;
132 } else {
133 ast_log(LOG_ERROR, "Unknown log level: '%s'\n", level);
136 if (lnum > -1) {
137 snprintf(context, sizeof(context), "@ %s", chan->context);
138 snprintf(extension, sizeof(extension), "Ext. %s", chan->exten);
140 ast_log(lnum, extension, chan->priority, context, "%s\n", ltext);
142 ast_module_user_remove(u);
143 return 0;
146 static int unload_module(void)
148 int res;
150 res = ast_unregister_application(app_verbose);
151 res |= ast_unregister_application(app_log);
153 ast_module_user_hangup_all();
155 return res;
158 static int load_module(void)
160 int res;
162 res = ast_register_application(app_log, log_exec, log_synopsis, log_descrip);
163 res |= ast_register_application(app_verbose, verbose_exec, verbose_synopsis, verbose_descrip);
165 return res;
168 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send verbose output");