5 Author: Johnny Mnemonic <johnny@themnemonic.org>
7 Copyright (C) 1997 - 2002 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 /****h* silcutil/SILC Logging Interface
25 * The SILC logging APIs provide a powerful and easy-to-use interface to
26 * the logging system and debugging output.
33 /****d* silcutil/SilcLogAPI/SilcLogType
37 * typedef enum { ... } SilcLogType;
41 * This is the main logging channel id. There are currently four known
42 * logging channels (plus the debugging output channel), and they are
43 * ordered by importance.
44 * See the source code for SILC coding conventions about how to choose
45 * the right output channel.
50 /* Generic info channel file */
53 /* This should be used for warnings and non critical failures */
56 /* Generic error and critical failure messages */
59 /* Fatal messages (usually situations that will lead to a program crash */
62 /* Total number logging channels */
67 /****f* silcutil/SilcLogAPI/SilcLogCb
71 * typedef bool (*SilcLogCb)(SilcLogType type, char *message,
76 * The logging custom callback function. The `type' is the channel ID
77 * that triggered the event, which allows you to use the same callback
78 * function for multiple logging channels.
79 * The `message' parameter points to a null-terminated buffer containing
80 * the received message, while `context' is the caller-specified context.
81 * The message must not be modified or freed by the callback function.
84 * silc_log_set_callback
87 typedef bool (*SilcLogCb)(SilcLogType type, char *message, void *context);
89 /****f* silcutil/SilcLogAPI/SilcLogDebugCb
93 * typedef bool (*SilcLogDebugCb)(char *file, char *function, int line,
94 * char *message, void *context);
98 * The debug logging callback function. The default behaviour is to
99 * output messages to stderr. `file', `function', and `line' are the
100 * corresponding offsets in the source files. `message' points to a
101 * null-terminated buffer containing the debugging message, and `context'
102 * is the caller-specified context.
103 * The message must not be modified or freed by the callback function.
104 * If the function returns TRUE, SilcLog will assume the message as handled
105 * and won't run its default handler.
108 * silc_debug, silc_log_set_debug_callbacks
111 typedef bool (*SilcLogDebugCb)(char *file, char *function, int line,
112 char *message, void *context);
114 /****f* silcutil/SilcLogAPI/SilcLogHexdumpCb
118 * typedef bool (*SilcDebugHexdumpCb)(char *file, char *function, int line,
119 * unsigned char *data, SilcUInt32 data_len,
120 * char *message, void *context;
124 * The hexdump logging callback function. The default behaviour is to
125 * print a formatted hexdump to stderr, and is commonly what you would
126 * like it to be. `file', `function', and `line' are the corresponding
127 * offsets in the source files. `data' is the begin of the buffer that
128 * should be hexdumped, which is `data_len' bytes long.
129 * The `message' parameter points to a null-terminated buffer containing
130 * the received message, while `context' is the caller-specified context.
131 * The message must not be modified or freed by the callback function.
132 * If the function returns TRUE, SilcLog will assume the message as handled
133 * and won't run its default handler.
136 * silc_debug_hexdump, silc_log_set_debug_callbacks
139 typedef bool (*SilcLogHexdumpCb)(char *file, char *function, int line,
140 unsigned char *data, SilcUInt32 data_len,
141 char *message, void *context);
143 /* Global Variables */
145 /****v* silcutil/SilcLogAPI/silc_log_timestamp
149 * bool silc_log_timestamp -- enable/disable fast logging timestamp
153 * Causes SilcLog to add a timestamp as returned by silc_get_time().
154 * This may be useful for example if you run your application under a
155 * daemon helper like watchdog that adds its own timestamp. Defaults to
159 extern DLLAPI bool silc_log_timestamp;
161 /****v* silcutil/SilcLogAPI/silc_log_quick
165 * bool silc_log_quick -- enable/disable fast logging output
169 * SilcLog makes use of libc stream buffering output, which means that it
170 * saves HD activity by buffering the logging messages and writing them
171 * all together every some minutes (default is 5 minutes).
172 * Setting this variable to TRUE will force SilcLog to write messages to the
173 * filesystem as soon as they are received. This increases the CPU activity
174 * notably on bigger servers, but reduces memory usage.
175 * If you want to change the logging style on-the-fly, make sure to call
176 * silc_log_flush_all() after setting this variable to TRUE.
179 extern DLLAPI bool silc_log_quick;
181 /****v* silcutil/SilcLogAPI/silc_log_flushdelay
185 * long silc_log_flushdelay -- flushing time delay
189 * Sets the logfiles flushing delay in seconds. As for now, changing this
190 * value AFTER logfiles initialization won't take effect until previous
191 * delay time will expire; for example if you change from 300 seconds to
192 * 60 seconds you will have to wait up to 300 seconds for this change to
194 * This value must be greater than 2 seconds.
197 extern DLLAPI long silc_log_flushdelay;
199 /****v* silcutil/SilcLogAPI/silc_debug
203 * bool silc_debug -- enable/disable debugging output
207 * If silc_debug is set to FALSE, debugging functions won't procude any
208 * output. This is useful when for example you compile in the debugging
209 * support but at a certain point you want to send the program in the
216 extern DLLAPI bool silc_debug;
218 /****v* silcutil/SilcLogAPI/silc_debug_hexdump
222 * bool silc_debug_hexdump -- enable/disable debugging output
226 * If silc_debug_hexdump is set to FALSE, debugging functions won't produce
227 * any output. This is useful when for example you compile in the debugging
228 * support but at a certain point you want to send the program in the
235 extern DLLAPI bool silc_debug_hexdump;
241 #define __FUNCTION__ ""
245 /****d* silcutil/SilcLogAPI/SILC_ENABLE_DEBUG
249 * #define SILC_ENABLE_DEBUG
253 * Use this macro to enable the debugging in your application. If
254 * SILC was compiled with debugging enabled, this macro enables it.
255 * Use this macro in your application's main header, or in place where
256 * you need to enable the debugging.
260 * You still can control the debugging with silc_debug variable, on
261 * whether to actually print the debugging or not. This macro is
262 * used to enable debugging, not to say it is printed or not.
266 #define SILC_ENABLE_DEBUG \
268 #define SILC_DEBUG 1 \
272 /****d* silcutil/SilcLogAPI/SILC_LOG_INFO
276 * #define SILC_LOG_INFO(...)
280 * This macro is a wrapper to the main logging function.
281 * It supports variable argument list formatting, and *automatically*
282 * appends newline at the end of the string.
286 * This macro requires double parenthesis to ensure that the VA list
287 * formatting would work correctly.
291 * SILC_LOG_INFO(("Today i feel %s", core->mood));
295 #define SILC_LOG_INFO(fmt) silc_log_output(SILC_LOG_INFO, silc_format fmt)
298 /****d* silcutil/SilcLogAPI/SILC_LOG_WARNING
302 * #define SILC_LOG_WARNING(...)
306 * Wrapper to the WARNING logging channel.
307 * Please see the SILC_LOG_INFO macro.
314 #define SILC_LOG_WARNING(fmt) silc_log_output(SILC_LOG_WARNING, silc_format fmt)
317 /****d* silcutil/SilcLogAPI/SILC_LOG_ERROR
321 * #define SILC_LOG_ERROR(...)
325 * Wrapper to the ERROR logging channel.
326 * Please see the SILC_LOG_INFO macro.
333 #define SILC_LOG_ERROR(fmt) silc_log_output(SILC_LOG_ERROR, silc_format fmt)
336 /****d* silcutil/SilcLogAPI/SILC_LOG_FATAL
340 * #define SILC_LOG_FATAL(...)
344 * Wrapper to the FATAL logging channel.
345 * Please see the SILC_LOG_INFO macro.
352 #define SILC_LOG_FATAL(fmt) silc_log_output(SILC_LOG_FATAL, silc_format fmt)
355 /****d* silcutil/SilcLogAPI/SILC_LOG_DEBUG
359 * #define SILC_LOG_DEBUG(...)
363 * This is a special wrapper to the debugging output (usually stderr).
364 * The standard behaviour is the same as SILC_LOG_INFO, with the difference
365 * that this macro also depends on the global define SILC_DEBUG.
366 * Undefining SILC_DEBUG causes these functions to be defined to an empty
367 * value, thus removing all debug logging calls from the compiled
369 * This macro is also affected by the global variable silc_debug.
373 #if defined(SILC_DEBUG)
374 #define SILC_LOG_DEBUG(fmt) silc_log_output_debug(__FILE__, \
378 #define SILC_NOT_IMPLEMENTED(string) \
379 SILC_LOG_INFO(("*********** %s: NOT IMPLEMENTED YET", string));
381 #define SILC_LOG_DEBUG(fmt)
382 #define SILC_NOT_IMPLEMENTED(string)
383 #endif /* SILC_DEBUG */
386 /****d* silcutil/SilcLogAPI/SILC_LOG_HEXDUMP
390 * #define SILC_LOG_HEXDUMP(...)
394 * This is a special wrapper to the hexdump output function. This macro
395 * behaves slightly differently from other logging wrappers.
396 * The first parameter, is composed by a group of parameters delimited by
398 * The second parameter is a `char *' pointer pointing to the beginning
399 * of the memory section that should be hexdumped, and the third parameter
400 * is the length of this memory section.
401 * Undefining the global SILC_DEBUG define causes these functions to be
402 * defined to an empty value, thus removing all debug logging calls from
403 * the compiled application.
404 * This macro is also affected by the global variable silc_debug_hexdump.
408 * SILC_LOG_HEXDUMP(("Outgoing packet [%d], len %d", pckt->seq, pckt->len),
409 * pckt->data, pckt->datalen);
413 #if defined(SILC_DEBUG)
414 #define SILC_LOG_HEXDUMP(fmt, data, len) silc_log_output_hexdump(__FILE__, \
420 #define SILC_LOG_HEXDUMP(fmt, data, len)
421 #endif /* SILC_DEBUG */
426 /****f* silcutil/SilcLogAPI/silc_log_output
430 * void silc_log_output(SilcLogType type, char *string);
434 * This is the main function for logging output. Please note that you
435 * should rather use one of the logging wrapper macros.
436 * If you really want to use this function, its usage is quite simple.
437 * The `type' parameter identifies the channel to use, while the `string'
438 * parameter must be a dynamic allocated (null-terminated) buffer, because
439 * it will be freed at the end of this function, for internal reasons.
440 * If there are registered callbacks for the specified type, this function
441 * will first trigger those callbacks. The callback functions must NOT
442 * free or modify the original buffer.
445 * SILC_LOG_INFO, SILC_LOG_WARNING, SILC_LOG_ERROR, SILC_LOG_FATAL
448 void silc_log_output(SilcLogType type, char *string);
450 /****f* silcutil/SilcLogAPI/silc_log_get_file
454 * char *silc_log_get_file(SilcLogType type);
458 * Returns the current logging file for the channel `type'.
459 * If there has been an error during the opening of this channel, NULL
460 * is returned, even if the file has been previously set with
461 * silc_log_set_file().
462 * The returned pointer points to internally allocated storage and must
463 * not be freed, modified or stored.
466 char *silc_log_get_file(SilcLogType type);
468 /****f* silcutil/SilcLogAPI/silc_log_set_file
472 * bool silc_log_set_file(SilcLogType type, char *filename,
473 * SilcUInt32 maxsize,
474 * SilcSchedule scheduler);
478 * Sets `filename', which can be maximum `maxsize' bytes long, as the new
479 * logging file for the channel `type'. If you specify an illegal filename
480 * a warning message is printed and FALSE is returned. In this case
481 * logging settings are not changed.
482 * You can disable logging for a channel by specifying NULL filename, the
483 * maxsize in this case is not important.
484 * The `scheduler' parameter is needed by the internal logging to allow
485 * buffered output and thus to save HD activity.
488 bool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
489 SilcSchedule scheduler);
491 /****f* silcutil/SilcLogAPI/silc_log_set_callback
495 * void silc_log_set_callback(SilcLogType type, SilcLogCb cb,
500 * Set `cb' as the default callback function for the logging channel
501 * `type'. When SilcLog receives a message for this channel, it will
502 * trigger the callback function. If the callback function returns TRUE
503 * SilcLog will assume the input as handled and won't run its default
505 * You can disable/remove a callback by setting it to NULL or calling the
506 * function silc_log_reset_callbacks.
507 * If set, the callback function must be in the form described by
511 * silc_log_reset_callbacks
514 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context);
516 /****f* silcutil/SilcLogAPI/silc_log_reset_callbacks
520 * void silc_log_reset_callbacks();
524 * Removes all logging callbacks for normal channels. This function does
525 * NOT remove callbacks for debugging channels (debug and hexdump), you
526 * rather need to call silc_log_set_debug_callbacks() with NULL callbacks.
529 void silc_log_reset_callbacks();
531 /****f* silcutil/SilcLogAPI/silc_log_flush_all
535 * void silc_log_flush_all();
539 * Forces flushing for all logging channels. This should be called for
540 * example after receiving special signals.
546 void silc_log_flush_all();
548 /****f* silcutil/SilcLogAPI/silc_log_reset_all
552 * void silc_log_reset_all();
556 * Forces all logging channels to close and reopen their streams. Useful
557 * for example after a SIGHUP signal.
558 * Please note that this function could generate some warning messages if
559 * one or more logging channels point to an illegal filename.
562 void silc_log_reset_all();
564 /****f* silcutil/SilcLogAPI/silc_log_output_debug
568 * void silc_log_output_debug(char *file, char *function,
569 * int line, char *string);
573 * This is the main function for debug output. Please note that you should
574 * rather use the wrapper macro SILC_LOG_DEBUG.
575 * If you want to use it anyway, the `file', `function', and `line' are the
576 * corresponding offsets in the source files, while `string' must be a
577 * dynamic allocated (null-terminated) buffer.
580 void silc_log_output_debug(char *file, char *function,
581 int line, char *string);
583 /****f* silcutil/SilcLogAPI/silc_log_output_hexdump
587 * void silc_log_output_hexdump(char *file, char *function,
588 * int line, void *data_in,
589 * SilcUInt32 len, char *string);
593 * This is the main function for hexdump output. Please note that you
594 * should rather use the wrapper macro SILC_LOG_HEXDUMP.
595 * If you want to use it anyway, the `file', `function', and `line' are the
596 * corresponding offsets in the source files, `data_in' is the beginning
597 * of the buffer you wish to hexdump, which is `len' bytes long.
598 * `string' must be a dynamic allocated (null-terminated) buffer.
601 void silc_log_output_hexdump(char *file, char *function,
602 int line, void *data_in,
603 SilcUInt32 len, char *string);
605 /****f* silcutil/SilcLogAPI/silc_log_set_debug_callbacks
609 * void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
610 * void *debug_context,
611 * SilcLogHexdumpCb hexdump_cb,
612 * void *hexdump_context);
616 * Sets `debug_cb' as the the default callback function for the debug
617 * output, that will be called with the `debug_context' parameter.
618 * When SilcLog receives a debug message, it will trigger the callback
619 * function. If the callback function returns TRUE SilcLog will assume
620 * the input as handled and won't run its default handler.
621 * `hexdump_cb' and `hexdump_context' works the same way, except that they
622 * are referred to SILC_LOG_HEXDUMP requests.
623 * You can disable/remove a callback by setting it to NULL.
624 * If set, each callback function must be either in the form described by
625 * SilcLogDebugCb or SilcLogHexdumpCb.
628 * SilcLogDebugCb, SilcLogHexdumpCb
631 void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
633 SilcLogHexdumpCb hexdump_cb,
634 void *hexdump_context);
636 /****f* silcutil/SilcLogAPI/silc_log_set_debug_string
640 * void silc_log_set_debug_string(const char *debug_string);
644 * Sets `debug_string' as the regexp string for filtering debugging
645 * output. The string is copied and it can be modified/destroyed after
646 * this function call.
649 void silc_log_set_debug_string(const char *debug_string);
651 #endif /* !SILCLOG_H */