Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
test_logger.c File Reference

Test module for the logging subsystem. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/cli.h"
Include dependency graph for test_logger.c:

Go to the source code of this file.

Data Structures

struct  test
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static char * handle_cli_dynamic_level_test (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_performance_test (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_queue_test (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static int load_module (void)
 
static void output_tests (struct test *tests, size_t num_tests, int fd)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Logger Test Module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_cli_entry cli_logger []
 

Detailed Description

Test module for the logging subsystem.

Author
Kevin P. Fleming <[email protected]> 

Definition in file test_logger.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 275 of file test_logger.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 275 of file test_logger.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 275 of file test_logger.c.

◆ handle_cli_dynamic_level_test()

static char* handle_cli_dynamic_level_test ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 65 of file test_logger.c.

References ARRAY_LEN, ast_cli(), ast_log_dynamic_level, ast_logger_register_level(), ast_logger_unregister_level(), ast_strlen_zero, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, test::name, NULL, output_tests(), test, test::u_failure, test::u_success, ast_cli_entry::usage, test::x_failure, and test::x_success.

66 {
67  unsigned int level;
68  unsigned int x;
69  unsigned int test;
70  struct test tests[] = {
71  { .name = "Simple register/message/unregister",
72  },
73  { .name = "Register multiple levels",
74  },
75  };
76 
77  switch (cmd) {
78  case CLI_INIT:
79  e->command = "logger test dynamic";
80  e->usage = ""
81  "Usage: logger test dynamic\n"
82  "";
83  return NULL;
84  case CLI_GENERATE:
85  return NULL;
86  }
87 
88  for (test = 0; test < ARRAY_LEN(tests); test++) {
89  ast_cli(a->fd, "Test %u: %s.\n", test + 1, tests[test].name);
90  switch (test) {
91  case 0:
92  if ((level = ast_logger_register_level("test")) != -1) {
93  ast_cli(a->fd, "Test: got level %u\n", level);
94  ast_log_dynamic_level(level, "Logger Dynamic Test: Test 1\n");
96  tests[test].x_success++;
97  } else {
98  ast_cli(a->fd, "Test: Failed, could not register level 'test'.\n");
99  tests[test].u_failure++;
100  }
101  break;
102  case 1:
103  {
104  char level_name[18][8];
105 
106  for (x = 0; x < ARRAY_LEN(level_name); x++) {
107  sprintf(level_name[x], "level%02u", x);
108  if ((level = ast_logger_register_level(level_name[x])) == -1) {
109  if (x < 16) {
110  tests[test].u_failure++;
111  } else {
112  tests[test].x_failure++;
113  }
114  level_name[x][0] = '\0';
115  } else {
116  ast_cli(a->fd, "Test: registered '%s', got level %u\n", level_name[x], level);
117  if (x < 16) {
118  tests[test].x_success++;
119  } else {
120  tests[test].u_success++;
121  }
122  }
123  }
124 
125  for (x = 0; x < ARRAY_LEN(level_name); x++) {
126  if (!ast_strlen_zero(level_name[x])) {
127  ast_logger_unregister_level(level_name[x]);
128  }
129  }
130  }
131  }
132  }
133 
134  output_tests(tests, ARRAY_LEN(tests), a->fd);
135 
136  return CLI_SUCCESS;
137 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
unsigned int x_failure
Definition: test_logger.c:46
Definition: cli.h:152
#define ast_log_dynamic_level(level,...)
Send a log message to a dynamically registered log level.
Definition: logger.h:439
unsigned int u_success
Definition: test_logger.c:47
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
const char * name
Definition: test_logger.c:44
#define ast_strlen_zero(foo)
Definition: strings.h:52
void ast_logger_unregister_level(const char *name)
Unregister a previously registered logger level.
Definition: logger.c:2536
const int fd
Definition: cli.h:159
int ast_logger_register_level(const char *name)
Register a new logger level.
Definition: logger.c:2503
unsigned int x_success
Definition: test_logger.c:45
static void output_tests(struct test *tests, size_t num_tests, int fd)
Definition: test_logger.c:51
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
Definition: test.c:104
unsigned int u_failure
Definition: test_logger.c:48
static struct ast_test * test
Definition: localtime.c:115

◆ handle_cli_performance_test()

static char* handle_cli_performance_test ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 139 of file test_logger.c.

References ARRAY_LEN, ast_cli(), ast_log_dynamic_level, ast_logger_register_level(), ast_logger_unregister_level(), ast_tvdiff_ms(), ast_tvnow(), CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, test::name, NULL, output_tests(), test, test::u_failure, ast_cli_entry::usage, and test::x_success.

140 {
141  unsigned int level;
142  unsigned int test;
143  struct test tests[] = {
144  { .name = "Log 10,000 messages",
145  },
146  };
147 
148  switch (cmd) {
149  case CLI_INIT:
150  e->command = "logger test performance";
151  e->usage = ""
152  "Usage: logger test performance\n"
153  "";
154  return NULL;
155  case CLI_GENERATE:
156  return NULL;
157  }
158 
159  for (test = 0; test < ARRAY_LEN(tests); test++) {
160  ast_cli(a->fd, "Test %u: %s.\n", test + 1, tests[test].name);
161  switch (test) {
162  case 0:
163  if ((level = ast_logger_register_level("perftest")) != -1) {
164  unsigned int x;
165  struct timeval start, end;
166  int elapsed;
167 
168  ast_cli(a->fd, "Test: got level %u\n", level);
169  start = ast_tvnow();
170  for (x = 0; x < 10000; x++) {
171  ast_log_dynamic_level(level, "Performance test log message\n");
172  }
173  end = ast_tvnow();
174  elapsed = ast_tvdiff_ms(end, start);
175  ast_cli(a->fd, "Test: 10,000 messages in %f seconds.\n", (float) elapsed / 1000);
176  ast_logger_unregister_level("perftest");
177  tests[test].x_success++;
178  } else {
179  ast_cli(a->fd, "Test: Failed, could not register level 'perftest'.\n");
180  tests[test].u_failure++;
181  }
182  break;
183  }
184  }
185 
186  output_tests(tests, ARRAY_LEN(tests), a->fd);
187 
188  return CLI_SUCCESS;
189 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
Definition: cli.h:152
#define ast_log_dynamic_level(level,...)
Send a log message to a dynamically registered log level.
Definition: logger.h:439
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
#define NULL
Definition: resample.c:96
char * end
Definition: eagi_proxy.c:73
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
const char * name
Definition: test_logger.c:44
void ast_logger_unregister_level(const char *name)
Unregister a previously registered logger level.
Definition: logger.c:2536
const int fd
Definition: cli.h:159
int ast_logger_register_level(const char *name)
Register a new logger level.
Definition: logger.c:2503
unsigned int x_success
Definition: test_logger.c:45
static void output_tests(struct test *tests, size_t num_tests, int fd)
Definition: test_logger.c:51
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
Definition: test.c:104
unsigned int u_failure
Definition: test_logger.c:48
static struct ast_test * test
Definition: localtime.c:115

◆ handle_cli_queue_test()

static char* handle_cli_queue_test ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 191 of file test_logger.c.

References ast_cli(), ast_log_dynamic_level, ast_logger_create_channel(), ast_logger_get_queue_limit(), ast_logger_register_level(), ast_logger_remove_channel(), ast_logger_set_queue_limit(), AST_LOGGER_SUCCESS, ast_logger_unregister_level(), ast_tvdiff_ms(), ast_tvnow(), CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, error(), ast_cli_args::fd, NULL, and ast_cli_entry::usage.

192 {
193  int level;
194  int current_queue_limit;
195  unsigned int x;
196  struct timeval start, end;
197  int elapsed;
198  char tmppath[] = "/tmp/asterisk_logger_queue.XXXXXX";
199  int fd;
200 
201  switch (cmd) {
202  case CLI_INIT:
203  e->command = "logger test queue";
204  e->usage = ""
205  "Usage: logger test queue\n"
206  "";
207  return NULL;
208  case CLI_GENERATE:
209  return NULL;
210  }
211 
212  fd = mkstemp(tmppath);
213  if (fd < 0) {
214  ast_cli(a->fd, "Test: Failed, could not create temporary log file '%s'.\n", tmppath);
215  return CLI_SUCCESS;
216  }
217 
218  level = ast_logger_register_level("queuetest");
219  if (level < 0) {
220  ast_cli(a->fd, "Test: Failed, could not register level 'queuetest'.\n");
221  return CLI_SUCCESS;
222  }
223  ast_cli(a->fd, "Test: got level %d for 'queuetest'.\n", level);
224 
225  if (ast_logger_create_channel(tmppath, "queuetest") != AST_LOGGER_SUCCESS) {
226  ast_cli(a->fd, "Test: Unable to create logger channel '%s'\n", tmppath);
227  goto error;
228  }
229 
230  current_queue_limit = ast_logger_get_queue_limit();
231  ast_cli(a->fd, "Test: Current queue limit: %d. Setting to 100 for test.\n", current_queue_limit);
233 
234  ast_cli(a->fd, "Test: You should see SOME 'exceeded' and 'resumed' messages after the test "
235  "is completed. How many is dependent on system resources.\n");
236 
237  start = ast_tvnow();
238  for (x = 0; x < 10000; x++) {
239  ast_log_dynamic_level(level, "Performance test log message %2d\n", x);
240  }
241  end = ast_tvnow();
242  elapsed = ast_tvdiff_ms(end, start);
243  ast_cli(a->fd, "Test: 10,000 messages in %f seconds.\n", (float) elapsed / 1000);
244  ast_cli(a->fd, "Test: Completed. Resetting queue limit to %d.\n", current_queue_limit);
245  ast_logger_set_queue_limit(current_queue_limit);
246 
247 error:
248 
249  ast_logger_remove_channel(tmppath);
250  ast_logger_unregister_level("queuetest");
251  close(fd);
252  unlink(tmppath);
253 
254  return CLI_SUCCESS;
255 }
Definition: cli.h:152
#define ast_log_dynamic_level(level,...)
Send a log message to a dynamically registered log level.
Definition: logger.h:439
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
#define NULL
Definition: resample.c:96
char * end
Definition: eagi_proxy.c:73
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
void ast_logger_unregister_level(const char *name)
Unregister a previously registered logger level.
Definition: logger.c:2536
const int fd
Definition: cli.h:159
void ast_logger_set_queue_limit(int queue_limit)
Set the maximum number of messages allowed in the processing queue.
Definition: logger.c:2582
int ast_logger_register_level(const char *name)
Register a new logger level.
Definition: logger.c:2503
int ast_logger_create_channel(const char *log_channel, const char *components)
Create a log channel.
Definition: logger.c:1419
int ast_logger_get_queue_limit(void)
Get the maximum number of messages allowed in the processing queue.
Definition: logger.c:2587
int ast_logger_remove_channel(const char *log_channel)
Delete the specified log channel.
Definition: logger.c:1485
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
int error(const char *format,...)
Definition: utils/frame.c:999

◆ load_module()

static int load_module ( void  )
static

Definition at line 269 of file test_logger.c.

References ARRAY_LEN, ast_cli_register_multiple, and AST_MODULE_LOAD_SUCCESS.

270 {
273 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static struct ast_cli_entry cli_logger[]
Definition: test_logger.c:257

◆ output_tests()

static void output_tests ( struct test tests,
size_t  num_tests,
int  fd 
)
static

Definition at line 51 of file test_logger.c.

References ast_cli(), test::name, test::u_failure, test::u_success, test::x_failure, and test::x_success.

Referenced by handle_cli_dynamic_level_test(), and handle_cli_performance_test().

52 {
53  unsigned int x;
54 
55  for (x = 0; x < num_tests; x++) {
56  ast_cli(fd, "Test %u: %s\n", x + 1, tests[x].name);
57  ast_cli(fd, "\tExpected Successes: %u\n", tests[x].x_success);
58  ast_cli(fd, "\tExpected Failures: %u\n", tests[x].x_failure);
59  ast_cli(fd, "\tUnexpected Successes: %u\n", tests[x].u_success);
60  ast_cli(fd, "\tUnexpected Failures: %u\n", tests[x].u_failure);
61  ast_cli(fd, "Test %u Result: %s\n", x + 1, (tests[x].u_success + tests[x].u_failure) ? "FAIL" : "PASS");
62  }
63 }
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
static const char name[]
Definition: cdr_mysql.c:74

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 263 of file test_logger.c.

References ARRAY_LEN, and ast_cli_unregister_multiple().

264 {
266  return 0;
267 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
static struct ast_cli_entry cli_logger[]
Definition: test_logger.c:257

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Logger Test Module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 275 of file test_logger.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 275 of file test_logger.c.

◆ cli_logger

struct ast_cli_entry cli_logger[]
static
Initial value:
= {
{ .handler = handle_cli_dynamic_level_test , .summary = "Test the dynamic logger level implementation" ,},
{ .handler = handle_cli_performance_test , .summary = "Test the logger performance" ,},
{ .handler = handle_cli_queue_test , .summary = "Test the logger queue" ,},
}
static char * handle_cli_queue_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: test_logger.c:191
static char * handle_cli_dynamic_level_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: test_logger.c:65
static char * handle_cli_performance_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: test_logger.c:139

Definition at line 257 of file test_logger.c.