remove almost all of the checks of the result from ast_strdupa() or alloca().
[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 <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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
42
43 static char *tdesc = "Send verbose output";
44
45 static char *app_verbose = "Verbose";
46 static char *verbose_synopsis = "Send arbitrary text to verbose output";
47 static char *verbose_descrip =
48 "Verbose([<level>|]<message>)\n"
49 "  level must be an integer value.  If not specified, defaults to 0.\n";
50
51 static char *app_log = "Log";
52 static char *log_synopsis = "Send arbitrary text to a selected log level";
53 static char *log_descrip =
54 "Log(<level>|<message>)\n"
55 "  level must be one of ERROR, WARNING, NOTICE, DEBUG, VERBOSE, DTMF\n";
56
57 LOCAL_USER_DECL;
58
59 static int verbose_exec(struct ast_channel *chan, void *data)
60 {
61         char *vtext;
62         int vsize;
63         struct localuser *u;
64
65         LOCAL_USER_ADD(u);
66
67         if (data) {
68                 char *tmp;
69                 vtext = ast_strdupa(data);
70                 tmp = strsep(&vtext, "|");
71                 if (vtext) {
72                         if (sscanf(tmp, "%d", &vsize) != 1) {
73                                 vsize = 0;
74                                 ast_log(LOG_WARNING, "'%s' is not a verboser number\n", vtext);
75                         }
76                 } else {
77                         vtext = tmp;
78                         vsize = 0;
79                 }
80                 if (option_verbose >= vsize) {
81                         switch (vsize) {
82                         case 0:
83                                 ast_verbose("%s\n", vtext);
84                                 break;
85                         case 1:
86                                 ast_verbose(VERBOSE_PREFIX_1 "%s\n", vtext);
87                                 break;
88                         case 2:
89                                 ast_verbose(VERBOSE_PREFIX_2 "%s\n", vtext);
90                                 break;
91                         case 3:
92                                 ast_verbose(VERBOSE_PREFIX_3 "%s\n", vtext);
93                                 break;
94                         default:
95                                 ast_verbose(VERBOSE_PREFIX_4 "%s\n", vtext);
96                         }
97                 }
98         }
99
100         LOCAL_USER_REMOVE(u);
101
102         return 0;
103 }
104
105 static int log_exec(struct ast_channel *chan, void *data)
106 {
107         char *level, *ltext;
108         struct localuser *u;
109         int lnum = -1;
110         char extension[AST_MAX_EXTENSION + 5], context[AST_MAX_EXTENSION + 2];
111
112         LOCAL_USER_ADD(u);
113         if (ast_strlen_zero(data)) {
114                 LOCAL_USER_REMOVE(u);
115                 return 0;
116         }
117
118         ltext = ast_strdupa(data);
119
120         level = strsep(&ltext, "|");
121
122         if (!strcasecmp(level, "ERROR")) {
123                 lnum = __LOG_ERROR;
124         } else if (!strcasecmp(level, "WARNING")) {
125                 lnum = __LOG_WARNING;
126         } else if (!strcasecmp(level, "NOTICE")) {
127                 lnum = __LOG_NOTICE;
128         } else if (!strcasecmp(level, "DEBUG")) {
129                 lnum = __LOG_DEBUG;
130         } else if (!strcasecmp(level, "VERBOSE")) {
131                 lnum = __LOG_VERBOSE;
132         } else if (!strcasecmp(level, "DTMF")) {
133                 lnum = __LOG_DTMF;
134         } else if (!strcasecmp(level, "EVENT")) {
135                 lnum = __LOG_EVENT;
136         } else {
137                 ast_log(LOG_ERROR, "Unknown log level: '%s'\n", level);
138         }
139
140         if (lnum > -1) {
141                 snprintf(context, sizeof(context), "@ %s", chan->context);
142                 snprintf(extension, sizeof(extension), "Ext. %s", chan->exten);
143
144                 ast_log(lnum, extension, chan->priority, context, "%s\n", ltext);
145         }
146         LOCAL_USER_REMOVE(u);
147         return 0;
148 }
149
150 static int unload_module(void *mod)
151 {
152         int res;
153
154         res = ast_unregister_application(app_verbose);
155         res |= ast_unregister_application(app_log);
156
157         STANDARD_HANGUP_LOCALUSERS;
158
159         return res;     
160 }
161
162 static int load_module(void *mod)
163 {
164         int res;
165
166         res = ast_register_application(app_log, log_exec, log_synopsis, log_descrip);
167         res |= ast_register_application(app_verbose, verbose_exec, verbose_synopsis, verbose_descrip);
168
169         return res;
170 }
171
172 static const char *description(void)
173 {
174         return tdesc;
175 }
176
177 static const char *key(void)
178 {
179         return ASTERISK_GPL_KEY;
180 }
181
182 STD_MOD1;