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.
72 * silc_log_set_callback
75 typedef SilcBool (*SilcLogCb)(SilcLogType type, char *message, void *context);
77 /****f* silcutil/SilcLogAPI/SilcLogDebugCb
81 * typedef SilcBool (*SilcLogDebugCb)(char *file, char *function, int line,
82 * char *message, void *context);
86 * The debug logging callback function. The default behaviour is to
87 * output messages to stderr. `file', `function', and `line' are the
88 * corresponding offsets in the source files. `message' points to a
89 * null-terminated buffer containing the debugging message, and `context'
90 * is the caller-specified context.
92 * The message must not be modified or freed by the callback function.
93 * If the function returns TRUE, SilcLog will assume the message as handled
94 * and won't run its default handler.
97 * silc_debug, silc_log_set_debug_callbacks
100 typedef SilcBool (*SilcLogDebugCb)(char *file, char *function, int line,
101 char *message, void *context);
103 /****f* silcutil/SilcLogAPI/SilcLogHexdumpCb
108 * (*SilcDebugHexdumpCb)(char *file, char *function, int line,
109 * unsigned char *data,
110 * SilcUInt32 data_len,
111 * char *message, void *context;
115 * The hexdump logging callback function. The default behaviour is to
116 * print a formatted hexdump to stderr, and is commonly what you would
117 * like it to be. `file', `function', and `line' are the corresponding
118 * offsets in the source files. `data' is the begin of the buffer that
119 * should be hexdumped, which is `data_len' bytes long.
121 * The `message' parameter points to a null-terminated buffer containing
122 * the received message, while `context' is the caller-specified context.
123 * The message must not be modified or freed by the callback function.
124 * If the function returns TRUE, SilcLog will assume the message as handled
125 * and won't run its default handler.
128 * silc_debug_hexdump, silc_log_set_debug_callbacks
131 typedef SilcBool (*SilcLogHexdumpCb)(char *file, char *function, int line,
132 unsigned char *data, SilcUInt32 data_len,
133 char *message, void *context);
137 /****d* silcutil/SilcLogAPI/SILC_LOG_INFO
141 * #define SILC_LOG_INFO(...)
145 * This macro is a wrapper to the main logging function.
146 * It supports variable argument list formatting, and *automatically*
147 * appends newline at the end of the string.
151 * This macro requires double parenthesis to ensure that the VA list
152 * formatting would work correctly.
156 * SILC_LOG_INFO(("Today i feel %s", core->mood));
160 #define SILC_LOG_INFO(fmt) silc_log_output(SILC_LOG_INFO, silc_format fmt)
163 /****d* silcutil/SilcLogAPI/SILC_LOG_WARNING
167 * #define SILC_LOG_WARNING(...)
171 * Wrapper to the WARNING logging channel.
172 * Please see the SILC_LOG_INFO macro.
179 #define SILC_LOG_WARNING(fmt) silc_log_output(SILC_LOG_WARNING, silc_format fmt)
182 /****d* silcutil/SilcLogAPI/SILC_LOG_ERROR
186 * #define SILC_LOG_ERROR(...)
190 * Wrapper to the ERROR logging channel.
191 * Please see the SILC_LOG_INFO macro.
198 #define SILC_LOG_ERROR(fmt) silc_log_output(SILC_LOG_ERROR, silc_format fmt)
201 /****d* silcutil/SilcLogAPI/SILC_LOG_FATAL
205 * #define SILC_LOG_FATAL(...)
209 * Wrapper to the FATAL logging channel.
210 * Please see the SILC_LOG_INFO macro.
217 #define SILC_LOG_FATAL(fmt) silc_log_output(SILC_LOG_FATAL, silc_format fmt)
220 /****d* silcutil/SilcLogAPI/SILC_LOG_DEBUG
224 * #define SILC_LOG_DEBUG(...)
228 * This is a special wrapper to the debugging output (usually stderr).
229 * The standard behaviour is the same as SILC_LOG_INFO, with the difference
230 * that this macro also depends on the global define SILC_DEBUG.
232 * Undefining SILC_DEBUG causes these functions to be defined to an empty
233 * value, thus removing all debug logging calls from the compiled
238 #if defined(SILC_DEBUG)
239 #define SILC_LOG_DEBUG(fmt) silc_log_output_debug(__FILE__, \
243 #define SILC_NOT_IMPLEMENTED(string) \
244 SILC_LOG_INFO(("*********** %s: NOT IMPLEMENTED YET", string));
246 #define SILC_LOG_DEBUG(fmt) do { } while(0)
247 #define SILC_NOT_IMPLEMENTED(string) do { } while(0)
248 #endif /* SILC_DEBUG */
251 /****d* silcutil/SilcLogAPI/SILC_LOG_HEXDUMP
255 * #define SILC_LOG_HEXDUMP(...)
259 * This is a special wrapper to the hexdump output function. This macro
260 * behaves slightly differently from other logging wrappers.
261 * The first parameter, is composed by a group of parameters delimited by
263 * The second parameter is a `char *' pointer pointing to the beginning
264 * of the memory section that should be hexdumped, and the third parameter
265 * is the length of this memory section.
266 * Undefining the global SILC_DEBUG define causes these functions to be
267 * defined to an empty value, thus removing all debug logging calls from
268 * the compiled application.
269 * This macro is also affected by the global variable silc_debug_hexdump.
273 * SILC_LOG_HEXDUMP(("Outgoing packet [%d], len %d", pckt->seq, pckt->len),
274 * pckt->data, pckt->datalen);
278 #if defined(SILC_DEBUG)
279 #define SILC_LOG_HEXDUMP(fmt, data, len) silc_log_output_hexdump(__FILE__, \
282 (void *)(data), (len), \
285 #define SILC_LOG_HEXDUMP(fmt, data, len) do { } while(0)
286 #endif /* SILC_DEBUG */
289 /****d* silcutil/SilcLogAPI/SILC_ASSERT
293 * #define SILC_ASSERT(experssion)
297 * Assert macro that prints error message to stderr and calls abort()
298 * if the `expression' is false (ie. compares equal to zero). If
299 * SILC_DEBUG is not defined this macro has no effect.
303 #if defined(SILC_DEBUG)
304 #define SILC_ASSERT(expr) assert((expr));
306 #define SILC_ASSERT(expr) do { } while(0)
307 #endif /* SILC_DEBUG */
312 /****f* silcutil/SilcLogAPI/silc_log_set_file
316 * SilcBool silc_log_set_file(SilcLogType type, char *filename,
317 * SilcUInt32 maxsize,
318 * SilcSchedule scheduler);
322 * Sets `filename', which can be maximum `maxsize' bytes long, as the new
323 * logging file for the channel `type'. If you specify an illegal filename
324 * a warning message is printed and FALSE is returned. In this case
325 * logging settings are not changed.
327 * You can disable logging for a channel by specifying NULL filename, the
328 * maxsize in this case is not important. The `scheduler' parameter is
329 * needed by the internal logging to allow buffered output and thus to
333 SilcBool silc_log_set_file(SilcLogType type, char *filename,
334 SilcUInt32 maxsize, SilcSchedule scheduler);
336 /****f* silcutil/SilcLogAPI/silc_log_get_file
340 * char *silc_log_get_file(SilcLogType type);
344 * Returns the current logging file for the channel `type'.
345 * If there has been an error during the opening of this channel, NULL
346 * is returned, even if the file has been previously set with
347 * silc_log_set_file().
349 * The returned pointer points to internally allocated storage and must
350 * not be freed, modified or stored.
353 char *silc_log_get_file(SilcLogType type);
355 /****f* silcutil/SilcLogAPI/silc_log_set_callback
359 * void silc_log_set_callback(SilcLogType type, SilcLogCb cb,
364 * Set `cb' as the default callback function for the logging channel
365 * `type'. When SilcLog receives a message for this channel, it will
366 * trigger the callback function. If the callback function returns TRUE
367 * SilcLog will assume the input as handled and won't run its default
370 * You can disable/remove a callback by setting it to NULL or calling the
371 * function silc_log_reset_callbacks. If set, the callback function
372 * must be in the form described by SilcLogCb.
375 * silc_log_reset_callbacks
378 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context);
380 /****f* silcutil/SilcLogAPI/silc_log_reset_callbacks
384 * void silc_log_reset_callbacks();
388 * Removes all logging callbacks for normal channels. This function does
389 * NOT remove callbacks for debugging channels (debug and hexdump), you
390 * rather need to call silc_log_set_debug_callbacks() with NULL callbacks.
393 void silc_log_reset_callbacks(void);
395 /****f* silcutil/SilcLogAPI/silc_log_flush_all
399 * void silc_log_flush_all();
403 * Forces flushing for all logging channels. This should be called for
404 * example after receiving special signals.
410 void silc_log_flush_all(void);
412 /****f* silcutil/SilcLogAPI/silc_log_reset_all
416 * void silc_log_reset_all();
420 * Forces all logging channels to close and reopen their streams. Useful
421 * for example after a SIGHUP signal.
423 * Please note that this function could generate some warning messages if
424 * one or more logging channels point to an illegal filename.
427 void silc_log_reset_all(void);
429 /****f* silcutil/SilcLogAPI/silc_log_set_debug_callbacks
433 * void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
434 * void *debug_context,
435 * SilcLogHexdumpCb hexdump_cb,
436 * void *hexdump_context);
440 * Sets `debug_cb' as the the default callback function for the debug
441 * output, that will be called with the `debug_context' parameter.
442 * When SilcLog receives a debug message, it will trigger the callback
443 * function. If the callback function returns TRUE SilcLog will assume
444 * the input as handled and won't run its default handler. The `hexdump_cb'
445 * and `hexdump_context' works the same way, except that they are referred
446 * to SILC_LOG_HEXDUMP requests.
448 * You can disable/remove a callback by setting it to NULL. If set, each
449 * callback function must be either in the form described by SilcLogDebugCb
450 * or SilcLogHexdumpCb.
453 void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
455 SilcLogHexdumpCb hexdump_cb,
456 void *hexdump_context);
458 /****f* silcutil/SilcLogAPI/silc_log_reset_debug_callbacks
462 * void silc_log_reset_debug_callbacks();
466 * Resets debug callbacks set with silc_log_set_debug_callbacks.
469 void silc_log_reset_debug_callbacks(void);
471 /****f* silcutil/SilcLogAPI/silc_log_set_debug_string
475 * void silc_log_set_debug_string(const char *debug_string);
479 * Sets `debug_string' as the regexp string for filtering debugging
480 * output. The string is copied and it can be modified/destroyed after
481 * this function call.
484 void silc_log_set_debug_string(const char *debug_string);
486 /****f* silcutil/SilcLogAPI/silc_log_timestamp
490 * void silc_log_timestamp(SilcBool enable);
494 * Use timestamp in log messages. Set `enable' to TRUE to enable
495 * timestamp and to FALSE to disable it. Default is TRUE.
498 void silc_log_timestamp(SilcBool enable);
500 /****f* silcutil/SilcLogAPI/silc_log_flushdelay
504 * void silc_log_flushdelay(SilcUInt32 flushdelay);
508 * Sets the logfiles flushing delay in seconds. Default is 300 seconds.
511 void silc_log_flushdelay(SilcUInt32 flushdelay);
513 /****f* silcutil/SilcLogAPI/silc_log_quick
517 * void silc_log_quick(SilcBool enable);
521 * SilcLog makes use of libc stream buffering output, which means that it
522 * saves HD activity by buffering the logging messages and writing them
523 * all together every some minutes (default is 5 minutes).
525 * Setting `enable' to TRUE will force SilcLog to write messages to the
526 * filesystem as soon as they are received. This increases the CPU activity
527 * notably on bigger servers, but reduces memory usage.
529 * If you want to change the logging style on-the-fly, make sure to call
530 * silc_log_flush_all() after setting `enable' to TRUE.
535 void silc_log_quick(SilcBool enable);
537 /****v* silcutil/SilcLogAPI/silc_log_debug
541 * void silc_log_debug(SilcBool enable);
545 * If `enable' is set to FALSE, debugging functions won't procude any
546 * output and if set to TRUE prints debug messages to stderr. Default
553 void silc_log_debug(SilcBool enable);
555 /****v* silcutil/SilcLogAPI/silc_log_debug_hexdump
559 * void silc_log_debug_hexdump(SilcBool enable);
563 * If `enable' is set to FALSE, debugging functions won't produce
564 * any output anf if set to TRUE prints hexdump debug message to
565 * stderr. Default is FALSE.
571 void silc_log_debug_hexdump(SilcBool enable);
573 #endif /* !SILCLOG_H */