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
107 * typedef SilcBool (*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 SilcBool (*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 */
288 /****d* silcutil/SilcLogAPI/SILC_ASSERT
292 * #define SILC_ASSERT(experssion)
296 * Assert macro that prints error message to stderr and calls abort()
297 * if the `expression' is is false (ie. compares equal to zero). If
298 * SILC_DEBUG is not defined this macro as no effect.
302 #if defined(SILC_DEBUG)
303 #define SILC_ASSERT(expr) assert((expr));
305 #define SILC_ASSERT(expr) do { } while(0)
306 #endif /* SILC_DEBUG */
311 /****f* silcutil/SilcLogAPI/silc_log_set_file
315 * SilcBool silc_log_set_file(SilcLogType type, char *filename,
316 * SilcUInt32 maxsize,
317 * SilcSchedule scheduler);
321 * Sets `filename', which can be maximum `maxsize' bytes long, as the new
322 * logging file for the channel `type'. If you specify an illegal filename
323 * a warning message is printed and FALSE is returned. In this case
324 * logging settings are not changed.
326 * You can disable logging for a channel by specifying NULL filename, the
327 * maxsize in this case is not important. The `scheduler' parameter is
328 * needed by the internal logging to allow buffered output and thus to
332 SilcBool silc_log_set_file(SilcLogType type, char *filename,
333 SilcUInt32 maxsize, SilcSchedule scheduler);
335 /****f* silcutil/SilcLogAPI/silc_log_get_file
339 * char *silc_log_get_file(SilcLogType type);
343 * Returns the current logging file for the channel `type'.
344 * If there has been an error during the opening of this channel, NULL
345 * is returned, even if the file has been previously set with
346 * silc_log_set_file().
348 * The returned pointer points to internally allocated storage and must
349 * not be freed, modified or stored.
352 char *silc_log_get_file(SilcLogType type);
354 /****f* silcutil/SilcLogAPI/silc_log_set_callback
358 * void silc_log_set_callback(SilcLogType type, SilcLogCb cb,
363 * Set `cb' as the default callback function for the logging channel
364 * `type'. When SilcLog receives a message for this channel, it will
365 * trigger the callback function. If the callback function returns TRUE
366 * SilcLog will assume the input as handled and won't run its default
369 * You can disable/remove a callback by setting it to NULL or calling the
370 * function silc_log_reset_callbacks. If set, the callback function
371 * must be in the form described by SilcLogCb.
374 * silc_log_reset_callbacks
377 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context);
379 /****f* silcutil/SilcLogAPI/silc_log_reset_callbacks
383 * void silc_log_reset_callbacks();
387 * Removes all logging callbacks for normal channels. This function does
388 * NOT remove callbacks for debugging channels (debug and hexdump), you
389 * rather need to call silc_log_set_debug_callbacks() with NULL callbacks.
392 void silc_log_reset_callbacks(void);
394 /****f* silcutil/SilcLogAPI/silc_log_flush_all
398 * void silc_log_flush_all();
402 * Forces flushing for all logging channels. This should be called for
403 * example after receiving special signals.
409 void silc_log_flush_all(void);
411 /****f* silcutil/SilcLogAPI/silc_log_reset_all
415 * void silc_log_reset_all();
419 * Forces all logging channels to close and reopen their streams. Useful
420 * for example after a SIGHUP signal.
422 * Please note that this function could generate some warning messages if
423 * one or more logging channels point to an illegal filename.
426 void silc_log_reset_all(void);
428 /****f* silcutil/SilcLogAPI/silc_log_set_debug_callbacks
432 * void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
433 * void *debug_context,
434 * SilcLogHexdumpCb hexdump_cb,
435 * void *hexdump_context);
439 * Sets `debug_cb' as the the default callback function for the debug
440 * output, that will be called with the `debug_context' parameter.
441 * When SilcLog receives a debug message, it will trigger the callback
442 * function. If the callback function returns TRUE SilcLog will assume
443 * the input as handled and won't run its default handler. The `hexdump_cb'
444 * and `hexdump_context' works the same way, except that they are referred
445 * to SILC_LOG_HEXDUMP requests.
447 * You can disable/remove a callback by setting it to NULL. If set, each
448 * callback function must be either in the form described by SilcLogDebugCb
449 * or SilcLogHexdumpCb.
452 void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
454 SilcLogHexdumpCb hexdump_cb,
455 void *hexdump_context);
457 /****f* silcutil/SilcLogAPI/silc_log_reset_debug_callbacks
461 * void silc_log_reset_debug_callbacks();
465 * Resets debug callbacks set with silc_log_set_debug_callbacks.
468 void silc_log_reset_debug_callbacks(void);
470 /****f* silcutil/SilcLogAPI/silc_log_set_debug_string
474 * void silc_log_set_debug_string(const char *debug_string);
478 * Sets `debug_string' as the regexp string for filtering debugging
479 * output. The string is copied and it can be modified/destroyed after
480 * this function call.
483 void silc_log_set_debug_string(const char *debug_string);
485 /****f* silcutil/SilcLogAPI/silc_log_timestamp
489 * void silc_log_timestamp(SilcBool enable);
493 * Use timestamp in log messages. Set `enable' to TRUE to enable
494 * timestamp and to FALSE to disable it. Default is TRUE.
497 void silc_log_timestamp(SilcBool enable);
499 /****f* silcutil/SilcLogAPI/silc_log_flushdelay
503 * void silc_log_flushdelay(SilcUInt32 flushdelay);
507 * Sets the logfiles flushing delay in seconds. Default is 300 seconds.
510 void silc_log_flushdelay(SilcUInt32 flushdelay);
512 /****f* silcutil/SilcLogAPI/silc_log_quick
516 * void silc_log_quick(SilcBool enable);
520 * SilcLog makes use of libc stream buffering output, which means that it
521 * saves HD activity by buffering the logging messages and writing them
522 * all together every some minutes (default is 5 minutes).
524 * Setting `enable' to TRUE will force SilcLog to write messages to the
525 * filesystem as soon as they are received. This increases the CPU activity
526 * notably on bigger servers, but reduces memory usage.
528 * If you want to change the logging style on-the-fly, make sure to call
529 * silc_log_flush_all() after setting `enable' to TRUE.
534 void silc_log_quick(SilcBool enable);
536 /****v* silcutil/SilcLogAPI/silc_log_debug
540 * void silc_log_debug(SilcBool enable);
544 * If `enable' is set to FALSE, debugging functions won't procude any
545 * output and if set to TRUE prints debug messages to stderr. Default
552 void silc_log_debug(SilcBool enable);
554 /****v* silcutil/SilcLogAPI/silc_log_debug_hexdump
558 * void silc_log_debug_hexdump(SilcBool enable);
562 * If `enable' is set to FALSE, debugging functions won't produce
563 * any output anf if set to TRUE prints hexdump debug message to
564 * stderr. Default is FALSE.
570 void silc_log_debug_hexdump(SilcBool enable);
572 #endif /* !SILCLOG_H */