Add ability for modules to dynamically register logger levels
[asterisk/asterisk.git] / tests / test_logger.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * Kevin P. Fleming <kpfleming@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Test module for the logging subsystem
22  *
23  * \author\verbatim Kevin P. Fleming <kpfleming@digium.com> \endverbatim
24  *
25  * \ingroup tests
26  */
27
28 /*** MODULEINFO
29         <defaultenabled>no</defaultenabled>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/file.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/lock.h"
41 #include "asterisk/app.h"
42 #include "asterisk/cli.h"
43
44 struct test {
45         const char *name;
46         unsigned int x_success;
47         unsigned int x_failure;
48         unsigned int u_success;
49         unsigned int u_failure;
50 };
51
52 static void output_tests(struct test *tests, size_t num_tests, int fd)
53 {
54         unsigned int x;
55
56         for (x = 0; x < num_tests; x++) {
57                 ast_cli(fd, "Test %d: %s\n", x + 1, tests[x].name);
58                 ast_cli(fd, "\tExpected Successes: %d\n", tests[x].x_success);
59                 ast_cli(fd, "\tExpected Failures: %d\n", tests[x].x_failure);
60                 ast_cli(fd, "\tUnexpected Successes: %d\n", tests[x].u_success);
61                 ast_cli(fd, "\tUnexpected Failures: %d\n", tests[x].u_failure);
62                 ast_cli(fd, "Test %d Result: %s\n", x + 1, (tests[x].u_success + tests[x].u_failure) ? "FAIL" : "PASS");
63         }
64 }
65
66 static char *handle_cli_dynamic_level_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
67 {
68         unsigned int level;
69         unsigned int x;
70         unsigned int test;
71         struct test tests[] = {
72                 { .name = "Simple register/message/unregister",
73                 },
74                 { .name = "Register multiple levels",
75                 },
76         };
77
78         switch (cmd) {
79         case CLI_INIT:
80                 e->command = "logger test dynamic";
81                 e->usage = ""
82                         "Usage: logger test dynamic\n"
83                         "";
84                 return NULL;
85         case CLI_GENERATE:
86                 return NULL;
87         }
88
89         for (test = 0; test < ARRAY_LEN(tests); test++) {
90                 ast_cli(a->fd, "Test %d: %s.\n", test + 1, tests[test].name);
91                 switch (test) {
92                 case 0:
93                         if ((level = ast_logger_register_level("test")) != -1) {
94                                 ast_cli(a->fd, "Test: got level %d\n", level);
95                                 ast_log_dynamic_level(level, "Logger Dynamic Test: Test 1\n");
96                                 ast_logger_unregister_level("test");
97                                 tests[test].x_success++;
98                         } else {
99                                 ast_cli(a->fd, "Test: Failed, could not register level 'test'.\n");
100                                 tests[test].u_failure++;
101                         }
102                         break;
103                 case 1:
104                 {
105                         char level_name[18][8];
106
107                         for (x = 0; x < ARRAY_LEN(level_name); x++) {
108                                 sprintf(level_name[x], "level%02d", x);
109                                 if ((level = ast_logger_register_level(level_name[x])) == -1) {
110                                         if (x < 16) {
111                                                 tests[test].u_failure++;
112                                         } else {
113                                                 tests[test].x_failure++;
114                                         }
115                                         level_name[x][0] = '\0';
116                                 } else {
117                                         ast_cli(a->fd, "Test: registered '%s', got level %d\n", level_name[x], level);
118                                         if (x < 16) {
119                                                 tests[test].x_success++;
120                                         } else {
121                                                 tests[test].u_success++;
122                                         }
123                                 }
124                         }
125
126                         for (x = 0; x < ARRAY_LEN(level_name); x++) {
127                                 if (!ast_strlen_zero(level_name[x])) {
128                                         ast_logger_unregister_level(level_name[x]);
129                                 }
130                         }
131                 }
132                 }
133         }
134
135         output_tests(tests, ARRAY_LEN(tests), a->fd);
136
137         return CLI_SUCCESS;
138 }
139
140 static char *handle_cli_performance_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
141 {
142         unsigned int level;
143         unsigned int test;
144         struct test tests[] = {
145                 { .name = "Log 10,000 messages",
146                 },
147         };
148
149         switch (cmd) {
150         case CLI_INIT:
151                 e->command = "logger test performance";
152                 e->usage = ""
153                         "Usage: logger test performance\n"
154                         "";
155                 return NULL;
156         case CLI_GENERATE:
157                 return NULL;
158         }
159
160         for (test = 0; test < ARRAY_LEN(tests); test++) {
161                 ast_cli(a->fd, "Test %d: %s.\n", test + 1, tests[test].name);
162                 switch (test) {
163                 case 0:
164                         if ((level = ast_logger_register_level("perftest")) != -1) {
165                                 unsigned int x;
166                                 struct timeval start, end;
167                                 int elapsed;
168
169                                 ast_cli(a->fd, "Test: got level %d\n", level);
170                                 start = ast_tvnow();
171                                 for (x = 0; x < 10000; x++) {
172                                         ast_log_dynamic_level(level, "Performance test log message\n");
173                                 }
174                                 end = ast_tvnow();
175                                 elapsed = ast_tvdiff_ms(end, start);
176                                 ast_cli(a->fd, "Test: 10,000 messages in %f seconds.\n", (float) elapsed / 1000);
177                                 ast_logger_unregister_level("perftest");
178                                 tests[test].x_success++;
179                         } else {
180                                 ast_cli(a->fd, "Test: Failed, could not register level 'perftest'.\n");
181                                 tests[test].u_failure++;
182                         }
183                         break;
184                 }
185         }
186
187         output_tests(tests, ARRAY_LEN(tests), a->fd);
188
189         return CLI_SUCCESS;
190 }
191
192 static struct ast_cli_entry cli_logger[] = {
193         AST_CLI_DEFINE(handle_cli_dynamic_level_test, "Test the dynamic logger level implementation"),
194         AST_CLI_DEFINE(handle_cli_performance_test, "Test the logger performance"),
195 };
196
197 static int unload_module(void)
198 {
199         ast_cli_unregister_multiple(cli_logger, ARRAY_LEN(cli_logger));
200         return 0;
201 }
202
203 static int load_module(void)
204 {
205         ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
206         return AST_MODULE_LOAD_SUCCESS;
207 }
208
209 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logger Test Module");