1 /* This file is part of the YAZ toolkit. 2 * Copyright (C) Index Data. 3 * All rights reserved. 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * * Neither the name of Index Data nor the names of its contributors 13 * may be used to endorse or promote products derived from this 14 * software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /** 29 * \file log.h 30 * \brief Logging utility 31 */ 32 33 #ifndef YAZ_LOG_H 34 #define YAZ_LOG_H 35 36 #include <stdio.h> 37 #include <yaz/yconfig.h> 38 39 YAZ_BEGIN_CDECL 40 41 /** \brief log level: fatal */ 42 #define YLOG_FATAL 0x00000001 43 /** \brief log level: debugging */ 44 #define YLOG_DEBUG 0x00000002 45 /** \brief log level: warning */ 46 #define YLOG_WARN 0x00000004 47 /** \brief log level: log (regular) */ 48 #define YLOG_LOG 0x00000008 49 /** \brief log level: append system error message */ 50 #define YLOG_ERRNO 0x00000010 51 /** \brief log level: append thread Id */ 52 #define YLOG_TID 0x00000020 53 /** \brief log level: application */ 54 #define YLOG_APP 0x00000040 55 /** \brief log level: malloc debug */ 56 #define YLOG_MALLOC 0x00000080 57 /** \brief log level: do not output date and time */ 58 #define YLOG_NOTIME 0x00000100 59 /** \brief log level: application 2 */ 60 #define YLOG_APP2 0x00000200 61 /** \brief log level: application 3 */ 62 #define YLOG_APP3 0x00000400 63 /** \brief log level: flush */ 64 #define YLOG_FLUSH 0x00000800 65 /** \brief dynamic log level start */ 66 #define YLOG_LOGLVL 0x00001000 /* log when modules query log levels */ 67 /* this has to be a hard-coded bit, not to loop*/ 68 69 #define YLOG_ALL (0xffff&~YLOG_MALLOC&~YLOG_NOTIME) 70 71 /** \brief default log level */ 72 #define YLOG_DEFAULT_LEVEL \ 73 (YLOG_FATAL | YLOG_ERRNO | YLOG_LOG | YLOG_WARN | YLOG_FLUSH) 74 /* not having flush here confuses Solaris users, who won't see any logs until 75 * (and if) the program exits normally */ 76 77 /** \brief last bit for regular log bits . Rest are dynamic */ 78 #define YLOG_LAST_BIT YLOG_LOGLVL 79 80 /** \brief sets level, prefix and filename for logging 81 \param level log level 82 \param prefix log message prefix 83 \param fname filename 84 85 If fname is NULL, the filename logging is not changed. 86 */ 87 YAZ_EXPORT void yaz_log_init(int level, const char *prefix, const char *fname); 88 89 /** \brief sets log file 90 \param fname filename 91 92 A filename of NULL makes the log to be completely disabled. 93 A filename which is the empty string ("") makes the system 94 log to stderr (which is also the default). Otherwise the 95 filename given is used. 96 */ 97 YAZ_EXPORT void yaz_log_init_file(const char *fname); 98 99 /** \brief sets log level 100 \param level (combination of YLOG_..) 101 */ 102 YAZ_EXPORT void yaz_log_init_level(int level); 103 104 /** \brief sets log message prefix 105 \param prefix log message prefix 106 */ 107 YAZ_EXPORT void yaz_log_init_prefix(const char *prefix); 108 109 /** \brief sets second log message prefix 110 \param prefix log message prefix 111 */ 112 YAZ_EXPORT void yaz_log_init_prefix2(const char *prefix); 113 114 /** \brief sets time format for log mesages 115 \param fmt format (strftime) 116 117 Sets the format of the timestamp. See man 3 strftime. 118 Calling with "old" sets to the old format "11:55:06-02/11" 119 Calling with NULL or "" sets to the new format "20041102-115719" 120 If not called at all, the old format is used, for backward compatibility 121 */ 122 YAZ_EXPORT void yaz_log_time_format(const char *fmt); 123 124 /** \brief sets limit in bytes for size for log file 125 \param mx size in bytes 126 127 Sets the max size for a log file. Zero means no limit. 128 Negative means built-in limit (1GB) 129 */ 130 YAZ_EXPORT void yaz_log_init_max_size(int mx); 131 132 /** \brief Writes log message 133 \param level log level mask 134 \param fmt format string ala printf 135 136 Writes an entry in the log. Defaults to stderr if not initialized or 137 to a file with yaz_log_init_file(). The level must match the level set 138 via yaz_log_init_level(), optionally defined via yaz_log_mask_str(). 139 */ 140 YAZ_EXPORT void yaz_log(int level, const char *fmt, ...) 141 #ifdef __GNUC__ 142 __attribute__ ((format (printf, 2, 3))) 143 #endif 144 ; 145 146 /** \brief converts log level string to log level (integer) 147 \param str log level string 148 \return log level mask 149 150 yaz_log_mask_str() converts a comma-separated list of log levels to a 151 bit mask. Starts from default level, and adds bits as specified, 152 unless 'none' is specified, which clears the list. If a name matches 153 the name of a YLOG_BIT above, that one is set. Otherwise a new value is 154 picked, and given to that name, to be found with yaz_log_module_level() 155 */ 156 YAZ_EXPORT int yaz_log_mask_str(const char *str); 157 158 /** \brief converts log level string to log level with "start" level 159 \param str log level string 160 \param level initialing log level 161 \return log level mask 162 163 yaz_log_mask_str_x() is like yaz_log_mask_str(), but with a given start 164 value. 165 */ 166 YAZ_EXPORT int yaz_log_mask_str_x(const char *str, int level); 167 168 /** \brief returns level for module 169 \param name module name 170 \returns log level for module 171 172 yaz_log_module_level() returns a log level mask corresponding to the 173 module name. If that had been specified on the -v arguments (that is 174 passed to yaz_log_mask_str()), then a non-zero mask is returned. If 175 not, we get a zero. This can later be used in yaz_log for the level 176 argument 177 */ 178 YAZ_EXPORT int yaz_log_module_level(const char *name); 179 180 /** \brief returns FILE handle for log or NULL if no file is in use 181 \retval FILE FILE handle in use (possibly stderr) 182 \retval NULL log is currently not written to a file 183 */ 184 YAZ_EXPORT FILE *yaz_log_file(void); 185 186 /** \brief sets custom log handler 187 \param func custom log handler 188 \param info custom pointer to be passed to func handler 189 190 Allows log output to be captured to something else.. The 191 func parameter takes a log level, a message + custom pointer 192 */ 193 YAZ_EXPORT void yaz_log_set_handler(void (*func)(int, const char *, 194 void *), void *info); 195 196 /** \brief reopen current log file (unless disabled or stderr) 197 */ 198 YAZ_EXPORT void yaz_log_reopen(void); 199 200 /** \brief Truncate the log file */ 201 YAZ_EXPORT void yaz_log_trunc(void); 202 203 /** \brief installs hook to be called before each log msg 204 \param func function to be called 205 \param info user data to be passed to function 206 */ 207 YAZ_EXPORT void log_event_start(void (*func)(int level, const char *msg, 208 void *info), void *info); 209 210 /** \brief installs hook to be called after each log msg 211 \param func function to be called 212 \param info user data to be passed to function 213 */ 214 YAZ_EXPORT void log_event_end(void (*func)(int level, const char *msg, 215 void *info), void *info); 216 217 /** \brief Makes Libxml2 and Libxslt log errors through yaz_log 218 \param prefix prefix to use for log messages (may be 0) 219 \param log_level log level to use for Libxml2/Libxslt messages 220 */ 221 YAZ_EXPORT void yaz_log_xml_errors(const char *prefix, int log_level); 222 223 /** \brief Lock for YAZ log writes 224 */ 225 YAZ_EXPORT void yaz_log_lock(void); 226 227 /** \brief Unlock for YAZ log writes 228 */ 229 YAZ_EXPORT void yaz_log_unlock(void); 230 YAZ_END_CDECL 231 232 #endif 233 /* 234 * Local variables: 235 * c-basic-offset: 4 236 * c-file-style: "Stroustrup" 237 * indent-tabs-mode: nil 238 * End: 239 * vim: shiftwidth=4 tabstop=8 expandtab 240 */ 241 242