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