5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 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 bool (*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 bool (*SilcLogCb)(SilcLogType type, char *message, void *context);
77 /****f* silcutil/SilcLogAPI/SilcLogDebugCb
81 * typedef bool (*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 bool (*SilcLogDebugCb)(char *file, char *function, int line,
101 char *message, void *context);
103 /****f* silcutil/SilcLogAPI/SilcLogHexdumpCb
107 * typedef bool (*SilcDebugHexdumpCb)(char *file, char *function, int line,
108 * unsigned char *data,
109 * SilcUInt32 data_len,
110 * char *message, void *context;
114 * The hexdump logging callback function. The default behaviour is to
115 * print a formatted hexdump to stderr, and is commonly what you would
116 * like it to be. `file', `function', and `line' are the corresponding
117 * offsets in the source files. `data' is the begin of the buffer that
118 * should be hexdumped, which is `data_len' bytes long.
120 * The `message' parameter points to a null-terminated buffer containing
121 * the received message, while `context' is the caller-specified context.
122 * The message must not be modified or freed by the callback function.
123 * If the function returns TRUE, SilcLog will assume the message as handled
124 * and won't run its default handler.
127 * silc_debug_hexdump, silc_log_set_debug_callbacks
130 typedef bool (*SilcLogHexdumpCb)(char *file, char *function, int line,
131 unsigned char *data, SilcUInt32 data_len,
132 char *message, void *context);
136 /****d* silcutil/SilcLogAPI/SILC_LOG_INFO
140 * #define SILC_LOG_INFO(...)
144 * This macro is a wrapper to the main logging function.
145 * It supports variable argument list formatting, and *automatically*
146 * appends newline at the end of the string.
150 * This macro requires double parenthesis to ensure that the VA list
151 * formatting would work correctly.
155 * SILC_LOG_INFO(("Today i feel %s", core->mood));
159 #define SILC_LOG_INFO(fmt) silc_log_output(SILC_LOG_INFO, silc_format fmt)
162 /****d* silcutil/SilcLogAPI/SILC_LOG_WARNING
166 * #define SILC_LOG_WARNING(...)
170 * Wrapper to the WARNING logging channel.
171 * Please see the SILC_LOG_INFO macro.
178 #define SILC_LOG_WARNING(fmt) silc_log_output(SILC_LOG_WARNING, silc_format fmt)
181 /****d* silcutil/SilcLogAPI/SILC_LOG_ERROR
185 * #define SILC_LOG_ERROR(...)
189 * Wrapper to the ERROR logging channel.
190 * Please see the SILC_LOG_INFO macro.
197 #define SILC_LOG_ERROR(fmt) silc_log_output(SILC_LOG_ERROR, silc_format fmt)
200 /****d* silcutil/SilcLogAPI/SILC_LOG_FATAL
204 * #define SILC_LOG_FATAL(...)
208 * Wrapper to the FATAL logging channel.
209 * Please see the SILC_LOG_INFO macro.
216 #define SILC_LOG_FATAL(fmt) silc_log_output(SILC_LOG_FATAL, silc_format fmt)
219 /****d* silcutil/SilcLogAPI/SILC_LOG_DEBUG
223 * #define SILC_LOG_DEBUG(...)
227 * This is a special wrapper to the debugging output (usually stderr).
228 * The standard behaviour is the same as SILC_LOG_INFO, with the difference
229 * that this macro also depends on the global define SILC_DEBUG.
231 * Undefining SILC_DEBUG causes these functions to be defined to an empty
232 * value, thus removing all debug logging calls from the compiled
237 #if defined(SILC_DEBUG)
238 #define SILC_LOG_DEBUG(fmt) silc_log_output_debug(__FILE__, \
242 #define SILC_NOT_IMPLEMENTED(string) \
243 SILC_LOG_INFO(("*********** %s: NOT IMPLEMENTED YET", string));
245 #define SILC_LOG_DEBUG(fmt) do { } while(0)
246 #define SILC_NOT_IMPLEMENTED(string) do { } while(0)
247 #endif /* SILC_DEBUG */
250 /****d* silcutil/SilcLogAPI/SILC_LOG_HEXDUMP
254 * #define SILC_LOG_HEXDUMP(...)
258 * This is a special wrapper to the hexdump output function. This macro
259 * behaves slightly differently from other logging wrappers.
260 * The first parameter, is composed by a group of parameters delimited by
262 * The second parameter is a `char *' pointer pointing to the beginning
263 * of the memory section that should be hexdumped, and the third parameter
264 * is the length of this memory section.
265 * Undefining the global SILC_DEBUG define causes these functions to be
266 * defined to an empty value, thus removing all debug logging calls from
267 * the compiled application.
268 * This macro is also affected by the global variable silc_debug_hexdump.
272 * SILC_LOG_HEXDUMP(("Outgoing packet [%d], len %d", pckt->seq, pckt->len),
273 * pckt->data, pckt->datalen);
277 #if defined(SILC_DEBUG)
278 #define SILC_LOG_HEXDUMP(fmt, data, len) silc_log_output_hexdump(__FILE__, \
281 (void *)(data), (len), \
284 #define SILC_LOG_HEXDUMP(fmt, data, len) do { } while(0)
285 #endif /* SILC_DEBUG */
290 /****f* silcutil/SilcLogAPI/silc_log_set_file
294 * bool silc_log_set_file(SilcLogType type, char *filename,
295 * SilcUInt32 maxsize,
296 * SilcSchedule scheduler);
300 * Sets `filename', which can be maximum `maxsize' bytes long, as the new
301 * logging file for the channel `type'. If you specify an illegal filename
302 * a warning message is printed and FALSE is returned. In this case
303 * logging settings are not changed.
305 * You can disable logging for a channel by specifying NULL filename, the
306 * maxsize in this case is not important. The `scheduler' parameter is
307 * needed by the internal logging to allow buffered output and thus to
311 bool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
312 SilcSchedule scheduler);
314 /****f* silcutil/SilcLogAPI/silc_log_get_file
318 * char *silc_log_get_file(SilcLogType type);
322 * Returns the current logging file for the channel `type'.
323 * If there has been an error during the opening of this channel, NULL
324 * is returned, even if the file has been previously set with
325 * silc_log_set_file().
327 * The returned pointer points to internally allocated storage and must
328 * not be freed, modified or stored.
331 char *silc_log_get_file(SilcLogType type);
333 /****f* silcutil/SilcLogAPI/silc_log_set_callback
337 * void silc_log_set_callback(SilcLogType type, SilcLogCb cb,
342 * Set `cb' as the default callback function for the logging channel
343 * `type'. When SilcLog receives a message for this channel, it will
344 * trigger the callback function. If the callback function returns TRUE
345 * SilcLog will assume the input as handled and won't run its default
348 * You can disable/remove a callback by setting it to NULL or calling the
349 * function silc_log_reset_callbacks. If set, the callback function
350 * must be in the form described by SilcLogCb.
353 * silc_log_reset_callbacks
356 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context);
358 /****f* silcutil/SilcLogAPI/silc_log_reset_callbacks
362 * void silc_log_reset_callbacks();
366 * Removes all logging callbacks for normal channels. This function does
367 * NOT remove callbacks for debugging channels (debug and hexdump), you
368 * rather need to call silc_log_set_debug_callbacks() with NULL callbacks.
371 void silc_log_reset_callbacks(void);
373 /****f* silcutil/SilcLogAPI/silc_log_flush_all
377 * void silc_log_flush_all();
381 * Forces flushing for all logging channels. This should be called for
382 * example after receiving special signals.
388 void silc_log_flush_all(void);
390 /****f* silcutil/SilcLogAPI/silc_log_reset_all
394 * void silc_log_reset_all();
398 * Forces all logging channels to close and reopen their streams. Useful
399 * for example after a SIGHUP signal.
401 * Please note that this function could generate some warning messages if
402 * one or more logging channels point to an illegal filename.
405 void silc_log_reset_all(void);
407 /****f* silcutil/SilcLogAPI/silc_log_set_debug_callbacks
411 * void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
412 * void *debug_context,
413 * SilcLogHexdumpCb hexdump_cb,
414 * void *hexdump_context);
418 * Sets `debug_cb' as the the default callback function for the debug
419 * output, that will be called with the `debug_context' parameter.
420 * When SilcLog receives a debug message, it will trigger the callback
421 * function. If the callback function returns TRUE SilcLog will assume
422 * the input as handled and won't run its default handler. The `hexdump_cb'
423 * and `hexdump_context' works the same way, except that they are referred
424 * to SILC_LOG_HEXDUMP requests.
426 * You can disable/remove a callback by setting it to NULL. If set, each
427 * callback function must be either in the form described by SilcLogDebugCb
428 * or SilcLogHexdumpCb.
431 void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
433 SilcLogHexdumpCb hexdump_cb,
434 void *hexdump_context);
436 /****f* silcutil/SilcLogAPI/silc_log_reset_debug_callbacks
440 * void silc_log_reset_debug_callbacks();
444 * Resets debug callbacks set with silc_log_set_debug_callbacks.
447 void silc_log_reset_debug_callbacks(void);
449 /****f* silcutil/SilcLogAPI/silc_log_set_debug_string
453 * void silc_log_set_debug_string(const char *debug_string);
457 * Sets `debug_string' as the regexp string for filtering debugging
458 * output. The string is copied and it can be modified/destroyed after
459 * this function call.
462 void silc_log_set_debug_string(const char *debug_string);
464 /****f* silcutil/SilcLogAPI/silc_log_timestamp
468 * void silc_log_timestamp(bool enable);
472 * Use timestamp in log messages. Set `enable' to TRUE to enable
473 * timestamp and to FALSE to disable it. Default is TRUE.
476 void silc_log_timestamp(bool enable);
478 /****f* silcutil/SilcLogAPI/silc_log_flushdelay
482 * void silc_log_flushdelay(SilcUInt32 flushdelay);
486 * Sets the logfiles flushing delay in seconds. Default is 300 seconds.
489 void silc_log_flushdelay(SilcUInt32 flushdelay);
491 /****f* silcutil/SilcLogAPI/silc_log_quick
495 * void silc_log_quick(bool enable);
499 * SilcLog makes use of libc stream buffering output, which means that it
500 * saves HD activity by buffering the logging messages and writing them
501 * all together every some minutes (default is 5 minutes).
503 * Setting `enable' to TRUE will force SilcLog to write messages to the
504 * filesystem as soon as they are received. This increases the CPU activity
505 * notably on bigger servers, but reduces memory usage.
507 * If you want to change the logging style on-the-fly, make sure to call
508 * silc_log_flush_all() after setting `enable' to TRUE.
513 void silc_log_quick(bool enable);
515 /****v* silcutil/SilcLogAPI/silc_log_debug
519 * void silc_log_debug(bool enable);
523 * If `enable' is set to FALSE, debugging functions won't procude any
524 * output and if set to TRUE prints debug messages to stderr. Default
531 void silc_log_debug(bool enable);
533 /****v* silcutil/SilcLogAPI/silc_log_debug_hexdump
537 * void silc_log_debug_hexdump(bool enable);
541 * If `enable' is set to FALSE, debugging functions won't produce
542 * any output anf if set to TRUE prints hexdump debug message to
543 * stderr. Default is FALSE.
549 void silc_log_debug_hexdump(bool enable);
551 #endif /* !SILCLOG_H */