Merge the remaining bit of the verbose update (bug #2213)
[asterisk/asterisk.git] / apps / app_verbose.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Verbose application
5  * 
6  * Copyright (c) 2004 Tilghman Lesher.  All rights reserved.
7  *
8  * Tilghman Lesher <app_verbose_v001@the-tilghman.com>
9  *
10  * This code is released by the author with no restrictions on usage.
11  *
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <asterisk/options.h>
19 #include <asterisk/logger.h>
20 #include <asterisk/channel.h>
21 #include <asterisk/pbx.h>
22 #include <asterisk/module.h>
23
24
25 static char *tdesc = "Send verbose output";
26
27 static char *app_verbose = "Verbose";
28
29 static char *verbose_synopsis = "Send arbitrary text to verbose output";
30
31 static char *verbose_descrip =
32 "Verbose([<level>|]<message>)\n"
33 "  level must be an integer value.  If not specified, defaults to 0."
34 "  Always returns 0.\n";
35
36 STANDARD_LOCAL_USER;
37
38 LOCAL_USER_DECL;
39
40 static int verbose_exec(struct ast_channel *chan, void *data)
41 {
42         char *vtext;
43         int vsize;
44
45         if (data) {
46                 vtext = ast_strdupa((char *)data);
47                 if (vtext) {
48                         char *tmp = strsep(&vtext, "|,");
49                         if (vtext) {
50                                 if (sscanf(tmp, "%d", &vsize) != 1) {
51                                         vsize = 0;
52                                         ast_log(LOG_WARNING, "'%s' is not a verboser number\n", vtext);
53                                 }
54                         } else {
55                                 vtext = tmp;
56                                 vsize = 0;
57                         }
58                         if (option_verbose >= vsize) {
59                                 switch (vsize) {
60                                 case 0:
61                                         ast_verbose("%s\n", vtext);
62                                         break;
63                                 case 1:
64                                         ast_verbose(VERBOSE_PREFIX_1 "%s\n", vtext);
65                                         break;
66                                 case 2:
67                                         ast_verbose(VERBOSE_PREFIX_2 "%s\n", vtext);
68                                         break;
69                                 case 3:
70                                         ast_verbose(VERBOSE_PREFIX_3 "%s\n", vtext);
71                                         break;
72                                 default:
73                                         ast_verbose(VERBOSE_PREFIX_4 "%s\n", vtext);
74                                 }
75                         }
76                 } else {
77                         ast_log(LOG_ERROR, "Out of memory\n");
78                 }
79         }
80
81         return 0;
82 }
83
84 int unload_module(void)
85 {
86         STANDARD_HANGUP_LOCALUSERS;
87         return ast_unregister_application(app_verbose);
88 }
89
90 int load_module(void)
91 {
92         return ast_register_application(app_verbose, verbose_exec, verbose_synopsis, verbose_descrip);
93 }
94
95 char *description(void)
96 {
97         return tdesc;
98 }
99
100 int usecount(void)
101 {
102         int res;
103         STANDARD_USECOUNT(res);
104         return res;
105 }
106
107 char *key()
108 {
109         return ASTERISK_GPL_KEY;
110 }