Moved the #define of logf back into log.h, so that it won't be
[yaz-moved-to-github.git] / include / yaz / ylog.h
1 /*
2  * Copyright (c) 1995-2004, Index Data.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation, in whole or in part, for any purpose, is hereby granted,
6  * provided that:
7  *
8  * 1. This copyright and permission notice appear in all copies of the
9  * software and its documentation. Notices of copyright or attribution
10  * which appear at the beginning of any file must remain unchanged.
11  *
12  * 2. The name of Index Data or the individual authors may not be used to
13  * endorse or promote products derived from this software without specific
14  * prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19  * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
20  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
22  * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * $Id: ylog.h,v 1.3 2004-11-19 10:07:08 heikki Exp $
27  */
28
29 /**
30  * \file log.h
31  * \brief Header for logging utility
32  */
33
34 #ifndef YLOG_H
35 #define YLOG_H
36
37 #include <stdio.h>
38 #include <yaz/yconfig.h>
39 #include <yaz/xmalloc.h>
40
41 YAZ_BEGIN_CDECL
42
43 #define YLOG_FATAL  0x00000001
44 #define YLOG_DEBUG  0x00000002
45 #define YLOG_WARN   0x00000004
46 #define YLOG_LOG    0x00000008
47 #define YLOG_ERRNO  0x00000010 /* append strerror to message */
48 /*#define YLOG_FILE   0x00000020 */
49 /*#define YLOG_APP    0x00000040 */ 
50    /* Application level events (new-connection) */
51 #define YLOG_MALLOC 0x00000080 /* debugging mallocs */
52 #define YLOG_NOTIME 0x00000100 /* do not output date and time */
53 /* #define YLOG_APP2   0x00000200 */
54    /* Application-level events, such as api calls */
55 /* #define YLOG_APP3   0x00000400 */
56    /* For more application-level events */
57 #define YLOG_FLUSH  0x00000800 /* Flush log after every write (DEBUG does too) */
58 #define YLOG_LOGLVL 0x00001000 /* log when modules query log levels */
59                               /* this has to be a hard-coded bit, not to loop*/
60
61 #define YLOG_ALL   (0xffff&~YLOG_MALLOC&~YLOG_NOTIME)
62
63 #define YLOG_DEFAULT_LEVEL (YLOG_FATAL | YLOG_ERRNO | YLOG_LOG | YLOG_WARN)
64
65 #define YLOG_LAST_BIT YLOG_LOGLVL /* the last bit used for regular log bits */
66                                 /* the rest are for dynamic modules */
67
68
69 /** 
70  * yaz_log_init is a shorthand for initializing the log level and prefixes */
71 YAZ_EXPORT void yaz_log_init(int level, const char *prefix, const char *name);
72
73 /** yaz_log_init_file sets the file name used for yaz_log */
74 YAZ_EXPORT void yaz_log_init_file(const char *fname);
75
76 /** yaz_log_init_level sets the logging level. Use an OR of the bits above */
77 YAZ_EXPORT void yaz_log_init_level(int level);
78
79 /** yaz_log_init_prefix sets the log prefix */
80 YAZ_EXPORT void yaz_log_init_prefix(const char *prefix);
81
82 /** yaz_log_init_prefix2 sets an optional second prefix */
83 YAZ_EXPORT void yaz_log_init_prefix2(const char *prefix);
84
85 /** 
86  * yaz_log_time_format sets the format of the timestamp. See man 3 strftime 
87  * Calling with "old" sets to the old format "11:55:06-02/11"
88  * Calling with NULL or "" sets to the new format "20041102-115719"
89  * If not called at all, the old format is used, for backward compatibility
90  */
91 YAZ_EXPORT void yaz_log_time_format(const char *fmt);
92
93 /** 
94  * yaz_log_init_max_size sets the max size for a log file. 
95  * zero means no limit. Negative means built-in limit (1GB)
96  */
97 YAZ_EXPORT void yaz_log_init_max_size(int mx);
98
99 /**
100  * yaz_log writes an entry in the log. Defaults to stderr if not initialized
101  * to a file with yaz_log_init_file. The level must match the level set via
102  * yaz_log_init_level, optionally defined via yaz_log_mask_str. */
103 YAZ_EXPORT void yaz_log(int level, const char *fmt, ...)
104 #ifdef __GNUC__
105         __attribute__ ((format (printf, 2, 3)))
106 #endif
107         ;
108
109 /** 
110  * yaz_log_mask_str converts a comma-separated list of log levels to a bit 
111  * mask. Starts from default level, and adds bits as specified, unless 'none'
112  * is specified, which clears the list. If a name matches the name of a
113  * YLOG_BIT above, that one is set. Otherwise a new value is picked, and given
114  * to that name, to be found with yaz_log_module_level */
115 YAZ_EXPORT int yaz_log_mask_str(const char *str);
116
117 /** yaz_log_mask_str_x is like yaz_log_mask_str, but with a given start value*/
118 YAZ_EXPORT int yaz_log_mask_str_x(const char *str, int level);
119
120
121 /** 
122  * yaz_log_module_level returns a log level mask corresponding to the module
123  * name. If that had been specified on the -v arguments (that is, passed to
124  * yaz_log_mask_str), then a non-zero mask is returned. If not, we get a 
125  * zero. This can later be used in yaz_log for the level argument
126  */
127 YAZ_EXPORT int yaz_log_module_level(const char *name);
128
129 /** yaz_log_file returns the file handle for yaz_log. */
130 YAZ_EXPORT FILE *yaz_log_file(void);
131
132 YAZ_EXPORT void log_event_start(void (*func)(int level, const char *msg, void *info),
133         void *info);
134 YAZ_EXPORT void log_event_end(void (*func)(int level, const char *msg, void *info),
135         void *info);
136
137 YAZ_EXPORT void yaz_log_reopen(void);
138
139 YAZ_END_CDECL
140
141 #endif