5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2006 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silcutil/SILC Logging Interface
24 * The SILC logging APIs provide a powerful and easy-to-use interface to
25 * the logging system and debugging output.
32 /****d* silcutil/SilcLogAPI/SilcLogType
36 * typedef enum { ... } SilcLogType;
40 * The log type. This can be given to various silc_log_* routines.
45 SILC_LOG_INFO = 1, /* Generic info */
46 SILC_LOG_WARNING = 2, /* Warnings and non-critical failures */
47 SILC_LOG_ERROR = 3, /* Generic error and critical failure */
48 SILC_LOG_FATAL = 4, /* Fatal error */
52 #include "silclog_i.h"
54 /****f* silcutil/SilcLogAPI/SilcLogCb
58 * typedef SilcBool (*SilcLogCb)(SilcLogType type, char *message,
63 * The logging custom callback function. The `type' is the channel ID
64 * that triggered the event, which allows you to use the same callback
65 * function for multiple logging channels.
67 * The `message' parameter points to a null-terminated buffer containing
68 * the received message, while `context' is the caller-specified context.
69 * The message must not be modified or freed by the callback function.
70 * If the function returns TRUE, SilcLog will assume the message was
71 * handled and won't run its default handler.
74 * silc_log_set_callback
77 typedef SilcBool (*SilcLogCb)(SilcLogType type, char *message, void *context);
79 /****f* silcutil/SilcLogAPI/SilcLogDebugCb
83 * typedef SilcBool (*SilcLogDebugCb)(char *file, char *function, int line,
84 * char *message, void *context);
88 * The debug logging callback function. The default behaviour is to
89 * output messages to stderr. `file', `function', and `line' are the
90 * corresponding offsets in the source files. `message' points to a
91 * null-terminated buffer containing the debugging message, and `context'
92 * is the caller-specified context.
94 * The message must not be modified or freed by the callback function.
95 * If the function returns TRUE, SilcLog will assume the message as handled
96 * and won't run its default handler.
99 * silc_debug, silc_log_set_debug_callbacks
102 typedef SilcBool (*SilcLogDebugCb)(char *file, char *function, int line,
103 char *message, void *context);
105 /****f* silcutil/SilcLogAPI/SilcLogHexdumpCb
110 * (*SilcDebugHexdumpCb)(char *file, char *function, int line,
111 * unsigned char *data,
112 * SilcUInt32 data_len,
113 * char *message, void *context;
117 * The hexdump logging callback function. The default behaviour is to
118 * print a formatted hexdump to stderr, and is commonly what you would
119 * like it to be. `file', `function', and `line' are the corresponding
120 * offsets in the source files. `data' is the begin of the buffer that
121 * should be hexdumped, which is `data_len' bytes long.
123 * The `message' parameter points to a null-terminated buffer containing
124 * the received message, while `context' is the caller-specified context.
125 * The message must not be modified or freed by the callback function.
126 * If the function returns TRUE, SilcLog will assume the message as handled
127 * and won't run its default handler.
130 * silc_debug_hexdump, silc_log_set_debug_callbacks
133 typedef SilcBool (*SilcLogHexdumpCb)(char *file, char *function, int line,
134 unsigned char *data, SilcUInt32 data_len,
135 char *message, void *context);
139 /****d* silcutil/SilcLogAPI/SILC_LOG_INFO
143 * #define SILC_LOG_INFO(...)
147 * This macro is a wrapper to the main logging function.
148 * It supports variable argument list formatting, and *automatically*
149 * appends newline at the end of the string.
153 * This macro requires double parenthesis to ensure that the VA list
154 * formatting would work correctly.
158 * SILC_LOG_INFO(("Today i feel %s", core->mood));
162 #define SILC_LOG_INFO(fmt) silc_log_output(SILC_LOG_INFO, silc_format fmt)
165 /****d* silcutil/SilcLogAPI/SILC_LOG_WARNING
169 * #define SILC_LOG_WARNING(...)
173 * Wrapper to the WARNING logging channel.
174 * Please see the SILC_LOG_INFO macro.
181 #define SILC_LOG_WARNING(fmt) silc_log_output(SILC_LOG_WARNING, silc_format fmt)
184 /****d* silcutil/SilcLogAPI/SILC_LOG_ERROR
188 * #define SILC_LOG_ERROR(...)
192 * Wrapper to the ERROR logging channel.
193 * Please see the SILC_LOG_INFO macro.
200 #define SILC_LOG_ERROR(fmt) silc_log_output(SILC_LOG_ERROR, silc_format fmt)
203 /****d* silcutil/SilcLogAPI/SILC_LOG_FATAL
207 * #define SILC_LOG_FATAL(...)
211 * Wrapper to the FATAL logging channel.
212 * Please see the SILC_LOG_INFO macro.
219 #define SILC_LOG_FATAL(fmt) silc_log_output(SILC_LOG_FATAL, silc_format fmt)
222 /****d* silcutil/SilcLogAPI/SILC_LOG_DEBUG
226 * #define SILC_LOG_DEBUG(...)
230 * This is a special wrapper to the debugging output (usually stderr).
231 * The standard behaviour is the same as SILC_LOG_INFO, with the difference
232 * that this macro also depends on the global define SILC_DEBUG.
234 * Undefining SILC_DEBUG causes these functions to be defined to an empty
235 * value, thus removing all debug logging calls from the compiled
240 #if defined(SILC_DEBUG)
241 #define SILC_LOG_DEBUG(fmt) silc_log_output_debug(__FILE__, \
245 #define SILC_NOT_IMPLEMENTED(string) \
246 SILC_LOG_INFO(("*********** %s: NOT IMPLEMENTED YET", string));
248 #define SILC_LOG_DEBUG(fmt) do { } while(0)
249 #define SILC_NOT_IMPLEMENTED(string) do { } while(0)
250 #endif /* SILC_DEBUG */
253 /****d* silcutil/SilcLogAPI/SILC_LOG_HEXDUMP
257 * #define SILC_LOG_HEXDUMP(...)
261 * This is a special wrapper to the hexdump output function. This macro
262 * behaves slightly differently from other logging wrappers.
263 * The first parameter, is composed by a group of parameters delimited by
265 * The second parameter is a `char *' pointer pointing to the beginning
266 * of the memory section that should be hexdumped, and the third parameter
267 * is the length of this memory section.
268 * Undefining the global SILC_DEBUG define causes these functions to be
269 * defined to an empty value, thus removing all debug logging calls from
270 * the compiled application.
271 * This macro is also affected by the global variable silc_debug_hexdump.
275 * SILC_LOG_HEXDUMP(("Outgoing packet [%d], len %d", pckt->seq, pckt->len),
276 * pckt->data, pckt->datalen);
280 #if defined(SILC_DEBUG)
281 #define SILC_LOG_HEXDUMP(fmt, data, len) silc_log_output_hexdump(__FILE__, \
284 (void *)(data), (len), \
287 #define SILC_LOG_HEXDUMP(fmt, data, len) do { } while(0)
288 #endif /* SILC_DEBUG */
291 /****d* silcutil/SilcLogAPI/SILC_ASSERT
295 * #define SILC_ASSERT(experssion)
299 * Assert macro that prints error message to stderr and calls abort()
300 * if the `expression' is false (ie. compares equal to zero). If
301 * SILC_DEBUG is not defined this macro has no effect.
305 #if defined(SILC_DEBUG)
306 #define SILC_ASSERT(expr) assert((expr));
308 #define SILC_ASSERT(expr) do { } while(0)
309 #endif /* SILC_DEBUG */
314 /****f* silcutil/SilcLogAPI/silc_log_set_file
318 * SilcBool silc_log_set_file(SilcLogType type, char *filename,
319 * SilcUInt32 maxsize,
320 * SilcSchedule scheduler);
324 * Sets `filename', which can be maximum `maxsize' bytes long, as the new
325 * logging file for the channel `type'. If you specify an illegal filename
326 * a warning message is printed and FALSE is returned. In this case
327 * logging settings are not changed.
329 * You can disable logging for a channel by specifying NULL filename, the
330 * maxsize in this case is not important. The `scheduler' parameter is
331 * needed by the internal logging to allow buffered output and thus to
335 SilcBool silc_log_set_file(SilcLogType type, char *filename,
336 SilcUInt32 maxsize, SilcSchedule scheduler);
338 /****f* silcutil/SilcLogAPI/silc_log_get_file
342 * char *silc_log_get_file(SilcLogType type);
346 * Returns the current logging file for the channel `type'.
347 * If there has been an error during the opening of this channel, NULL
348 * is returned, even if the file has been previously set with
349 * silc_log_set_file().
351 * The returned pointer points to internally allocated storage and must
352 * not be freed, modified or stored.
355 char *silc_log_get_file(SilcLogType type);
357 /****f* silcutil/SilcLogAPI/silc_log_set_callback
361 * void silc_log_set_callback(SilcLogType type, SilcLogCb cb,
366 * Set `cb' as the default callback function for the logging channel
367 * `type'. When SilcLog receives a message for this channel, it will
368 * trigger the callback function. If the callback function returns TRUE
369 * SilcLog will assume the input as handled and won't run its default
372 * You can disable/remove a callback by setting it to NULL or calling the
373 * function silc_log_reset_callbacks. If set, the callback function
374 * must be in the form described by SilcLogCb.
377 * silc_log_reset_callbacks
380 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context);
382 /****f* silcutil/SilcLogAPI/silc_log_reset_callbacks
386 * void silc_log_reset_callbacks();
390 * Removes all logging callbacks for normal channels. This function does
391 * NOT remove callbacks for debugging channels (debug and hexdump), you
392 * rather need to call silc_log_set_debug_callbacks() with NULL callbacks.
395 void silc_log_reset_callbacks(void);
397 /****f* silcutil/SilcLogAPI/silc_log_flush_all
401 * void silc_log_flush_all();
405 * Forces flushing for all logging channels. This should be called for
406 * example after receiving special signals.
412 void silc_log_flush_all(void);
414 /****f* silcutil/SilcLogAPI/silc_log_reset_all
418 * void silc_log_reset_all();
422 * Forces all logging channels to close and reopen their streams. Useful
423 * for example after a SIGHUP signal.
425 * Please note that this function could generate some warning messages if
426 * one or more logging channels point to an illegal filename.
429 void silc_log_reset_all(void);
431 /****f* silcutil/SilcLogAPI/silc_log_set_debug_callbacks
435 * void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
436 * void *debug_context,
437 * SilcLogHexdumpCb hexdump_cb,
438 * void *hexdump_context);
442 * Sets `debug_cb' as the the default callback function for the debug
443 * output, that will be called with the `debug_context' parameter.
444 * When SilcLog receives a debug message, it will trigger the callback
445 * function. If the callback function returns TRUE SilcLog will assume
446 * the input as handled and won't run its default handler. The `hexdump_cb'
447 * and `hexdump_context' works the same way, except that they are referred
448 * to SILC_LOG_HEXDUMP requests.
450 * You can disable/remove a callback by setting it to NULL. If set, each
451 * callback function must be either in the form described by SilcLogDebugCb
452 * or SilcLogHexdumpCb.
455 void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
457 SilcLogHexdumpCb hexdump_cb,
458 void *hexdump_context);
460 /****f* silcutil/SilcLogAPI/silc_log_reset_debug_callbacks
464 * void silc_log_reset_debug_callbacks();
468 * Resets debug callbacks set with silc_log_set_debug_callbacks.
471 void silc_log_reset_debug_callbacks(void);
473 /****f* silcutil/SilcLogAPI/silc_log_set_debug_string
477 * void silc_log_set_debug_string(const char *debug_string);
481 * Sets `debug_string' as the regexp string for filtering debugging
482 * output. The string is copied and it can be modified/destroyed after
483 * this function call.
486 void silc_log_set_debug_string(const char *debug_string);
488 /****f* silcutil/SilcLogAPI/silc_log_timestamp
492 * void silc_log_timestamp(SilcBool enable);
496 * Use timestamp in log messages. Set `enable' to TRUE to enable
497 * timestamp and to FALSE to disable it. Default is TRUE.
500 void silc_log_timestamp(SilcBool enable);
502 /****f* silcutil/SilcLogAPI/silc_log_flushdelay
506 * void silc_log_flushdelay(SilcUInt32 flushdelay);
510 * Sets the logfiles flushing delay in seconds. Default is 300 seconds.
513 void silc_log_flushdelay(SilcUInt32 flushdelay);
515 /****f* silcutil/SilcLogAPI/silc_log_quick
519 * void silc_log_quick(SilcBool enable);
523 * SilcLog makes use of libc stream buffering output, which means that it
524 * saves HD activity by buffering the logging messages and writing them
525 * all together every some minutes (default is 5 minutes).
527 * Setting `enable' to TRUE will force SilcLog to write messages to the
528 * filesystem as soon as they are received. This increases the CPU activity
529 * notably on bigger servers, but reduces memory usage.
531 * If you want to change the logging style on-the-fly, make sure to call
532 * silc_log_flush_all() after setting `enable' to TRUE.
537 void silc_log_quick(SilcBool enable);
539 /****v* silcutil/SilcLogAPI/silc_log_debug
543 * void silc_log_debug(SilcBool enable);
547 * If `enable' is set to FALSE, debugging functions won't procude any
548 * output and if set to TRUE prints debug messages to stderr. Default
555 void silc_log_debug(SilcBool enable);
557 /****v* silcutil/SilcLogAPI/silc_log_debug_hexdump
561 * void silc_log_debug_hexdump(SilcBool enable);
565 * If `enable' is set to FALSE, debugging functions won't produce
566 * any output anf if set to TRUE prints hexdump debug message to
567 * stderr. Default is FALSE.
573 void silc_log_debug_hexdump(SilcBool enable);
575 #endif /* !SILCLOG_H */