Version 5.14.8
[yaz-moved-to-github.git] / test / test_log.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <yaz/options.h>
13 #include <yaz/log.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17
18 void hook_func(int level, const char *msg, void *vp)
19 {
20     fprintf(stderr, "[HOOK level=%d msg=%s]\n", level, msg);
21 }
22
23 int main(int argc, char **argv)
24 {
25     char *arg;
26     int i, ret;
27     int level = YLOG_LOG;
28     int number = 1;
29     unsigned int wait_between_log = 0;
30
31     while ((ret = options("f:p:v:l:m:n:r:w:Hh", argv, argc, &arg)) != -2)
32     {
33         switch (ret)
34         {
35         case 'r':
36             yaz_log_init_max_size(atoi(arg));
37             break;
38         case 'f':
39             yaz_log_time_format(arg);
40             break;
41         case 'p':
42             yaz_log_init_prefix(arg);
43             break;
44         case 'v':
45             yaz_log_init_level(yaz_log_mask_str(arg));
46             break;
47         case 'l':
48             if (!strcmp(arg, "@"))
49                 yaz_log_init_file(0);
50             else
51                 yaz_log_init_file(arg);
52             break;
53         case 'n':
54             number = atoi(arg);
55             break;
56         case 'm':
57             level = yaz_log_module_level(arg);
58             break;
59         case 'w':
60             wait_between_log = atoi(arg);
61             break;
62         case 'H':
63             yaz_log_set_handler(hook_func, 0);
64             break;
65         case 0:
66             for (i = 0; i<number; i++)
67             {
68                 yaz_log(level, "%d %s", i, arg);
69 #if HAVE_UNISTD_H
70                 if (wait_between_log)
71                     sleep(wait_between_log);
72 #endif
73             }
74             break;
75         case 'h':
76         default:
77             fprintf(stderr, "tstlog [-f logformat] [-v level] [-l file] "
78                     "[-p prefix] [-m module] [-w sec] [-r max] [-n num] [-H] msg ..\n");
79             exit(1);
80         }
81     }
82     exit(0);
83 }
84 /*
85  * Local variables:
86  * c-basic-offset: 4
87  * c-file-style: "Stroustrup"
88  * indent-tabs-mode: nil
89  * End:
90  * vim: shiftwidth=4 tabstop=8 expandtab
91  */
92