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