529c346ac32f9034452f6773e9cce6dd77da4927
[yaz-moved-to-github.git] / test / tstlog.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <yaz/options.h>
9 #include <yaz/log.h>
10 #if HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif
13
14 void hook_func(int level, const char *msg, void *vp)
15 {
16     fprintf(stderr, "[HOOK level=%d msg=%s]\n", level, msg);
17 }
18
19 int main(int argc, char **argv)
20 {
21     char *arg;
22     int i, ret;
23     int level = YLOG_LOG;
24     int number = 1;
25     unsigned int wait_between_log = 0;
26
27     while ((ret = options("f:p:v:l:m:n:r:w:Hh", argv, argc, &arg)) != -2)
28     {
29         switch (ret)
30         {
31         case 'r':
32             yaz_log_init_max_size(atoi(arg));
33             break;
34         case 'f':
35             yaz_log_time_format(arg);
36             break;
37         case 'p':
38             yaz_log_init_prefix(arg);
39             break;
40         case 'v':
41             yaz_log_init_level(yaz_log_mask_str(arg));
42             break;
43         case 'l':
44             if (!strcmp(arg, "@"))
45                 yaz_log_init_file(0);
46             else
47                 yaz_log_init_file(arg);
48             break;
49         case 'n':
50             number = atoi(arg);
51             break;
52         case 'm':        
53             level = yaz_log_module_level(arg);
54             break;
55         case 'w':
56             wait_between_log = atoi(arg);
57             break;
58         case 'H':
59             yaz_log_set_handler(hook_func, 0);
60             break;
61         case 0:
62             for (i = 0; i<number; i++)
63             {
64                 yaz_log(level, "%d %s", i, arg);
65 #if HAVE_UNISTD_H
66                 if (wait_between_log)
67                     sleep(wait_between_log);
68 #endif
69             }
70             break;
71         case 'h':
72         default:
73             fprintf(stderr, "tstlog [-f logformat] [-v level] [-l file] "
74                     "[-p prefix] [-m module] [-w sec] [-r 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