Moved back to a single log.h, with the new #define YAZ_USE_NEW_LOG
[yaz-moved-to-github.git] / include / yaz / log.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: log.h,v 1.28 2004-12-13 14:21:55 heikki Exp $
27  */
28
29 /**
30  * \file log.h
31  * \brief The old, deprecated header for log.c
32  */
33
34 #ifndef LOG_H
35 #define LOG_H
36
37 #include <stdio.h>
38 #include <yaz/yconfig.h>
39
40 YAZ_BEGIN_CDECL
41
42 #ifndef YAZ_USE_NEW_LOG
43
44
45 /* The old LOG_ bit names are here for compatibility only. They may 
46  * conflict with bits defined in syslog.h, or other places. 'LOG'
47  * really is not such a good name. YLOG must be more unique
48  */
49 #define LOG_FATAL  YLOG_FATAL
50 #define LOG_DEBUG  YLOG_DEBUG
51 #define LOG_WARN   YLOG_WARN
52 #define LOG_LOG    YLOG_LOG /* Deprecated, use the modern dynamic log levels*/
53 #define LOG_ERRNO  YLOG_ERRNO 
54 #define LOG_FILE   0x00000020 /* Deprecated - not in ylog.h at all*/
55 #define LOG_APP    0x00000040 /* Deprecated - not in ylog.h at all*/
56 #define LOG_MALLOC YLOG_MALLOC /* deprecated */
57 #define LOG_NOTIME YLOG_NOTIME /* do not output date and time */
58 #define LOG_APP2   0x00000200 /* Deprecated - not in ylog.h at all*/
59 #define LOG_APP3   0x00000400 /* Deprecated - not in ylog.h at all*/
60 #define LOG_FLUSH  YLOG_FLUSH 
61  /*     LOG_LOGLVL is a new one in ylog.h. So new that no log.h users should
62   *     use it */
63
64 #define LOG_ALL    YLOG_ALL
65
66 #define LOG_DEFAULT_LEVEL YLOG_DEFAULT_LEVEL
67
68
69 /* logf is deprecated, as it conflicts with a math function */
70 #define logf yaz_log
71
72
73
74 #endif /* ndef YAZ_USE_NEW_LOG */
75
76 #define YLOG_FATAL  0x00000001
77 #define YLOG_DEBUG  0x00000002
78 #define YLOG_WARN   0x00000004
79 #define YLOG_LOG    0x00000008
80 #define YLOG_ERRNO  0x00000010 /* append strerror to message */
81 /*#define YLOG_FILE   0x00000020 */
82 /*#define YLOG_APP    0x00000040 */ 
83    /* Application level events (new-connection) */
84 #define YLOG_MALLOC 0x00000080 /* debugging mallocs */
85 #define YLOG_NOTIME 0x00000100 /* do not output date and time */
86 /* #define YLOG_APP2   0x00000200 */
87    /* Application-level events, such as api calls */
88 /* #define YLOG_APP3   0x00000400 */
89    /* For more application-level events */
90 #define YLOG_FLUSH  0x00000800 /* Flush log after every write (DEBUG does too) */
91 #define YLOG_LOGLVL 0x00001000 /* log when modules query log levels */
92                               /* this has to be a hard-coded bit, not to loop*/
93
94 #define YLOG_ALL   (0xffff&~YLOG_MALLOC&~YLOG_NOTIME)
95
96 #define YLOG_DEFAULT_LEVEL (YLOG_FATAL | YLOG_ERRNO | YLOG_LOG | YLOG_WARN)
97
98 #define YLOG_LAST_BIT YLOG_LOGLVL /* the last bit used for regular log bits */
99                                 /* the rest are for dynamic modules */
100
101
102 /** 
103  * yaz_log_init is a shorthand for initializing the log level and prefixes */
104 YAZ_EXPORT void yaz_log_init(int level, const char *prefix, const char *name);
105
106 /** yaz_log_init_file sets the file name used for yaz_log */
107 YAZ_EXPORT void yaz_log_init_file(const char *fname);
108
109 /** yaz_log_init_level sets the logging level. Use an OR of the bits above */
110 YAZ_EXPORT void yaz_log_init_level(int level);
111
112 /** yaz_log_init_prefix sets the log prefix */
113 YAZ_EXPORT void yaz_log_init_prefix(const char *prefix);
114
115 /** yaz_log_init_prefix2 sets an optional second prefix */
116 YAZ_EXPORT void yaz_log_init_prefix2(const char *prefix);
117
118 /** 
119  * yaz_log_time_format sets the format of the timestamp. See man 3 strftime 
120  * Calling with "old" sets to the old format "11:55:06-02/11"
121  * Calling with NULL or "" sets to the new format "20041102-115719"
122  * If not called at all, the old format is used, for backward compatibility
123  */
124 YAZ_EXPORT void yaz_log_time_format(const char *fmt);
125
126 /** 
127  * yaz_log_init_max_size sets the max size for a log file. 
128  * zero means no limit. Negative means built-in limit (1GB)
129  */
130 YAZ_EXPORT void yaz_log_init_max_size(int mx);
131
132 /**
133  * yaz_log writes an entry in the log. Defaults to stderr if not initialized
134  * to a file with yaz_log_init_file. The level must match the level set via
135  * yaz_log_init_level, optionally defined via yaz_log_mask_str. */
136 YAZ_EXPORT void yaz_log(int level, const char *fmt, ...)
137 #ifdef __GNUC__
138         __attribute__ ((format (printf, 2, 3)))
139 #endif
140         ;
141
142 /** 
143  * yaz_log_mask_str converts a comma-separated list of log levels to a bit 
144  * mask. Starts from default level, and adds bits as specified, unless 'none'
145  * is specified, which clears the list. If a name matches the name of a
146  * YLOG_BIT above, that one is set. Otherwise a new value is picked, and given
147  * to that name, to be found with yaz_log_module_level */
148 YAZ_EXPORT int yaz_log_mask_str(const char *str);
149
150 /** yaz_log_mask_str_x is like yaz_log_mask_str, but with a given start value*/
151 YAZ_EXPORT int yaz_log_mask_str_x(const char *str, int level);
152
153
154 /** 
155  * yaz_log_module_level returns a log level mask corresponding to the module
156  * name. If that had been specified on the -v arguments (that is, passed to
157  * yaz_log_mask_str), then a non-zero mask is returned. If not, we get a 
158  * zero. This can later be used in yaz_log for the level argument
159  */
160 YAZ_EXPORT int yaz_log_module_level(const char *name);
161
162 /** yaz_log_file returns the file handle for yaz_log. */
163 YAZ_EXPORT FILE *yaz_log_file(void);
164
165 YAZ_EXPORT void log_event_start(void (*func)(int level, const char *msg, void *info),
166         void *info);
167 YAZ_EXPORT void log_event_end(void (*func)(int level, const char *msg, void *info),
168         void *info);
169
170 YAZ_EXPORT void yaz_log_reopen(void);
171
172 YAZ_END_CDECL
173
174 #endif