Improved UTF-8 encoding and decoding, improved toolkit doc,
[silc.git] / lib / silcutil / silclog.h
1 /*
2
3   silclog.h
4
5   Author: Johnny Mnemonic <johnny@themnemonic.org>
6
7   Copyright (C) 1997 - 2002 Pekka Riikonen
8
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.
13
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.
18
19 */
20
21 /****h* silcutil/SILC Logging Interface
22  *
23  * DESCRIPTION
24  *
25  * The SILC logging APIs provide a powerful and easy-to-use interface to
26  * the logging system and debugging output.
27  *
28  ***/
29
30 #ifndef SILCLOG_H
31 #define SILCLOG_H
32
33 /****d* silcutil/SilcLogAPI/SilcLogType
34  *
35  * NAME
36  *
37  *    typedef enum { ... } SilcLogType;
38  *
39  * DESCRIPTION
40  *
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.
46  *
47  * SOURCE
48  */
49 typedef enum {
50   /* Generic info channel file */
51   SILC_LOG_INFO,
52
53   /* This should be used for warnings and non critical failures */
54   SILC_LOG_WARNING,
55
56   /* Generic error and critical failure messages */
57   SILC_LOG_ERROR,
58
59   /* Fatal messages (usually situations that will lead to a program crash */
60   SILC_LOG_FATAL,
61
62   /* Total number logging channels */
63   SILC_LOG_MAX
64 } SilcLogType;
65 /***/
66
67 /****f* silcutil/SilcLogAPI/SilcLogCb
68  *
69  * SYNOPSIS
70  *
71  *    typedef bool (*SilcLogCb)(SilcLogType type, char *message,
72  *                              void *context);
73  *
74  * DESCRIPTION
75  *
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.
82  *
83  * SEE ALSO
84  *    silc_log_set_callback
85  *
86  ***/
87 typedef bool (*SilcLogCb)(SilcLogType type, char *message, void *context);
88
89 /****f* silcutil/SilcLogAPI/SilcLogDebugCb
90  *
91  * SYNOPSIS
92  *
93  *    typedef bool (*SilcLogDebugCb)(char *file, char *function, int line,
94  *                                   char *message, void *context);
95  *
96  * DESCRIPTION
97  *
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.
106  *
107  * SEE ALSO
108  *    silc_debug, silc_log_set_debug_callbacks
109  *
110  ***/
111 typedef bool (*SilcLogDebugCb)(char *file, char *function, int line,
112                                char *message, void *context);
113
114 /****f* silcutil/SilcLogAPI/SilcLogHexdumpCb
115  *
116  * SYNOPSIS
117  *
118  *    typedef bool (*SilcDebugHexdumpCb)(char *file, char *function, int line,
119  *                                       unsigned char *data, SilcUInt32 data_len,
120  *                                       char *message, void *context;
121  *
122  * DESCRIPTION
123  *
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.
134  *
135  * SEE ALSO
136  *    silc_debug_hexdump, silc_log_set_debug_callbacks
137  *
138  ***/
139 typedef bool (*SilcLogHexdumpCb)(char *file, char *function, int line,
140                                  unsigned char *data, SilcUInt32 data_len,
141                                  char *message, void *context);
142
143 /* Global Variables */
144
145 /****v* silcutil/SilcLogAPI/silc_log_quick
146  *
147  * NAME
148  *
149  *    bool silc_log_quick -- enable/disable fast logging output
150  *
151  * DESCRIPTION
152  *
153  *    SilcLog makes use of libc stream buffering output, which means that it
154  *    saves HD activity by buffering the logging messages and writing them
155  *    all together every some minutes (default is 5 minutes).
156  *    Setting this variable to TRUE will force SilcLog to write messages to the
157  *    filesystem as soon as they are received. This increases the CPU activity
158  *    notably on bigger servers, but reduces memory usage.
159  *    If you want to change the logging style on-the-fly, make sure to call
160  *    silc_log_flush_all() after setting this variable to TRUE.
161  *
162  ***/
163 extern DLLAPI bool silc_log_quick;
164
165 /****v* silcutil/SilcLogAPI/silc_log_flushdelay
166  *
167  * NAME
168  *
169  *    long silc_log_flushdelay -- flushing time delay
170  *
171  * DESCRIPTION
172  *
173  *    Sets the logfiles flushing delay in seconds.  As for now, changing this
174  *    value AFTER logfiles initialization won't take effect until previous
175  *    delay time will expire; for example if you change from 300 seconds to
176  *    60 seconds you will have to wait up to 300 seconds for this change to
177  *    take effect.
178  *    This value must be greater than 2 seconds.
179  *
180  ***/
181 extern DLLAPI long silc_log_flushdelay;
182
183 /****v* silcutil/SilcLogAPI/silc_debug
184  *
185  * NAME
186  *
187  *    bool silc_debug -- enable/disable debugging output
188  *
189  * DESCRIPTION
190  *
191  *    If silc_debug is set to FALSE, debugging functions won't procude any
192  *    output.  This is useful when for example you compile in the debugging
193  *    support but at a certain point you want to send the program in the
194  *    background.
195  *
196  * SEE ALSO
197  *    SILC_LOG_DEBUG
198  *
199  ***/
200 extern DLLAPI bool silc_debug;
201
202 /****v* silcutil/SilcLogAPI/silc_debug_hexdump
203  *
204  * NAME
205  *
206  *    bool silc_debug_hexdump -- enable/disable debugging output
207  *
208  * DESCRIPTION
209  *
210  *    If silc_debug_hexdump is set to FALSE, debugging functions won't produce
211  *    any output.  This is useful when for example you compile in the debugging
212  *    support but at a certain point you want to send the program in the
213  *    background.
214  *
215  * SEE ALSO
216  *    SILC_LOG_HEXDUMP
217  *
218  ***/
219 extern DLLAPI bool silc_debug_hexdump;
220
221 /* Macros */
222
223 #ifdef SILC_WIN32
224 #ifndef __FUNCTION__
225 #define __FUNCTION__ ""
226 #endif
227 #endif
228
229 /****d* silcutil/SilcLogAPI/SILC_LOG_INFO
230  *
231  * NAME
232  *
233  *    #define SILC_LOG_INFO(...)
234  *
235  * DESCRIPTION
236  *
237  *    This macro is a wrapper to the main logging function.
238  *    It supports variable argument list formatting, and *automatically*
239  *    appends newline at the end of the string.
240  *
241  * NOTES
242  *
243  *    This macro requires double parenthesis to ensure that the VA list
244  *    formatting would work correctly.
245  *
246  * EXAMPLE
247  *
248  *    SILC_LOG_INFO(("Today i feel %s", core->mood));
249  *
250  * SOURCE
251  */
252 #define SILC_LOG_INFO(fmt) silc_log_output(SILC_LOG_INFO, silc_format fmt)
253 /***/
254
255 /****d* silcutil/SilcLogAPI/SILC_LOG_WARNING
256  *
257  * NAME
258  *
259  *    #define SILC_LOG_WARNING(...)
260  *
261  * DESCRIPTION
262  *
263  *    Wrapper to the WARNING logging channel.
264  *    Please see the SILC_LOG_INFO macro.
265  *
266  * SEE ALSO
267  *    SILC_LOG_INFO
268  *
269  * SOURCE
270  */
271 #define SILC_LOG_WARNING(fmt) silc_log_output(SILC_LOG_WARNING, silc_format fmt)
272 /***/
273
274 /****d* silcutil/SilcLogAPI/SILC_LOG_ERROR
275  *
276  * NAME
277  *
278  *    #define SILC_LOG_ERROR(...)
279  *
280  * DESCRIPTION
281  *
282  *    Wrapper to the ERROR logging channel.
283  *    Please see the SILC_LOG_INFO macro.
284  *
285  * SEE ALSO
286  *    SILC_LOG_INFO
287  *
288  * SOURCE
289  */
290 #define SILC_LOG_ERROR(fmt) silc_log_output(SILC_LOG_ERROR, silc_format fmt)
291 /***/
292
293 /****d* silcutil/SilcLogAPI/SILC_LOG_FATAL
294  *
295  * NAME
296  *
297  *    #define SILC_LOG_FATAL(...)
298  *
299  * DESCRIPTION
300  *
301  *    Wrapper to the FATAL logging channel.
302  *    Please see the SILC_LOG_INFO macro.
303  *
304  * SEE ALSO
305  *    SILC_LOG_INFO
306  *
307  * SOURCE
308  */
309 #define SILC_LOG_FATAL(fmt) silc_log_output(SILC_LOG_FATAL, silc_format fmt)
310 /***/
311
312 /****d* silcutil/SilcLogAPI/SILC_LOG_DEBUG
313  *
314  * NAME
315  *
316  *    #define SILC_LOG_DEBUG(...)
317  *
318  * DESCRIPTION
319  *
320  *    This is a special wrapper to the debugging output (usually stderr).
321  *    The standard behaviour is the same as SILC_LOG_INFO, with the difference
322  *    that this macro also depends on the global define SILC_DEBUG.
323  *    Undefining SILC_DEBUG causes these functions to be defined to an empty
324  *    value, thus removing all debug logging calls from the compiled
325  *    application.
326  *    This macro is also affected by the global variable silc_debug.
327  *
328  * SOURCE
329  */
330 #ifdef SILC_DEBUG
331 #define SILC_LOG_DEBUG(fmt) silc_log_output_debug(__FILE__, \
332                                 __FUNCTION__, \
333                                 __LINE__, \
334                                 silc_format fmt)
335 #define SILC_NOT_IMPLEMENTED(string) \
336   SILC_LOG_DEBUG(("*********** %s: NOT IMPLEMENTED YET", string));
337 #else
338 #define SILC_LOG_DEBUG(fmt)
339 #define SILC_NOT_IMPLEMENTED(string)
340 #endif  /* SILC_DEBUG */
341 /***/
342
343 /****d* silcutil/SilcLogAPI/SILC_LOG_HEXDUMP
344  *
345  * NAME
346  *
347  *    #define SILC_LOG_HEXDUMP(...)
348  *
349  * DESCRIPTION
350  *
351  *    This is a special wrapper to the hexdump output function.  This macro
352  *    behaves slightly differently from other logging wrappers.
353  *    The first parameter, is composed by a group of parameters delimited by
354  *    parenthesis.
355  *    The second parameter is a `char *' pointer pointing to the beginning
356  *    of the memory section that should be hexdumped, and the third parameter
357  *    is the length of this memory section.
358  *    Undefining the global SILC_DEBUG define causes these functions to be
359  *    defined to an empty value, thus removing all debug logging calls from
360  *    the compiled application.
361  *    This macro is also affected by the global variable silc_debug_hexdump.
362  *
363  * EXAMPLE
364  *
365  *    SILC_LOG_HEXDUMP(("Outgoing packet [%d], len %d", pckt->seq, pckt->len),
366  *                     pckt->data, pckt->datalen);
367  *
368  * SOURCE
369  */
370 #ifdef SILC_DEBUG
371 #define SILC_LOG_HEXDUMP(fmt, data, len) silc_log_output_hexdump(__FILE__, \
372                                 __FUNCTION__, \
373                                 __LINE__, \
374                                 (data), (len), \
375                                 silc_format fmt)
376 #else
377 #define SILC_LOG_HEXDUMP(fmt, data, len)
378 #endif  /* SILC_DEBUG */
379 /***/
380
381 /* Prototypes */
382
383 /****f* silcutil/SilcLogAPI/silc_log_output
384  *
385  * SYNOPSIS
386  *
387  *    void silc_log_output(SilcLogType type, char *string);
388  *
389  * DESCRIPTION
390  *
391  *    This is the main function for logging output. Please note that you
392  *    should rather use one of the logging wrapper macros.
393  *    If you really want to use this function, its usage is quite simple.
394  *    The `type' parameter identifies the channel to use, while the `string'
395  *    parameter must be a dynamic allocated (null-terminated) buffer, because
396  *    it will be freed at the end of this function, for internal reasons.
397  *    If there are registered callbacks for the specified type, this function
398  *    will first trigger those callbacks.  The callback functions must NOT
399  *    free or modify the original buffer.
400  *
401  * SEE ALSO
402  *    SILC_LOG_INFO, SILC_LOG_WARNING, SILC_LOG_ERROR, SILC_LOG_FATAL
403  *
404  ***/
405 void silc_log_output(SilcLogType type, char *string);
406
407 /****f* silcutil/SilcLogAPI/silc_log_get_file
408  *
409  * SYNOPSIS
410  *
411  *    char *silc_log_get_file(SilcLogType type);
412  *
413  * DESCRIPTION
414  *
415  *    Returns the current logging file for the channel `type'.
416  *    If there has been an error during the opening of this channel, NULL
417  *    is returned, even if the file has been previously set with
418  *    silc_log_set_file().
419  *    The returned pointer points to internally allocated storage and must
420  *    not be freed, modified or stored.
421  *
422  ***/
423 char *silc_log_get_file(SilcLogType type);
424
425 /****f* silcutil/SilcLogAPI/silc_log_set_file
426  *
427  * SYNOPSIS
428  *
429  *    bool silc_log_set_file(SilcLogType type, char *filename, 
430  *                           SilcUInt32 maxsize,
431  *                           SilcSchedule scheduler);
432  *
433  * DESCRIPTION
434  *
435  *    Sets `filename', which can be maximum `maxsize' bytes long, as the new
436  *    logging file for the channel `type'.  If you specify an illegal filename
437  *    a warning message is printed and FALSE is returned.  In this case
438  *    logging settings are not changed.
439  *    You can disable logging for a channel by specifying NULL filename, the
440  *    maxsize in this case is not important.
441  *    The `scheduler' parameter is needed by the internal logging to allow
442  *    buffered output and thus to save HD activity.
443  *
444  ***/
445 bool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
446                        SilcSchedule scheduler);
447
448 /****f* silcutil/SilcLogAPI/silc_log_set_callback
449  *
450  * SYNOPSIS
451  *
452  *    void silc_log_set_callback(SilcLogType type, SilcLogCb cb,
453  *                               void *context);
454  *
455  * DESCRIPTION
456  *
457  *    Set `cb' as the default callback function for the logging channel
458  *    `type'.  When SilcLog receives a message for this channel, it will
459  *    trigger the callback function.  If the callback function returns TRUE
460  *    SilcLog will assume the input as handled and won't run its default
461  *    handler.
462  *    You can disable/remove a callback by setting it to NULL or calling the
463  *    function silc_log_reset_callbacks.
464  *    If set, the callback function must be in the form described by
465  *    SilcLogCb.
466  *
467  * SEE ALSO
468  *    silc_log_reset_callbacks
469  *
470  ***/
471 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context);
472
473 /****f* silcutil/SilcLogAPI/silc_log_reset_callbacks
474  *
475  * SYNOPSIS
476  *
477  *    void silc_log_reset_callbacks();
478  *
479  * DESCRIPTION
480  *
481  *    Removes all logging callbacks for normal channels.  This function does
482  *    NOT remove callbacks for debugging channels (debug and hexdump), you
483  *    rather need to call silc_log_set_debug_callbacks() with NULL callbacks.
484  *
485  ***/
486 void silc_log_reset_callbacks();
487
488 /****f* silcutil/SilcLogAPI/silc_log_flush_all
489  *
490  * SYNOPSIS
491  *
492  *    void silc_log_flush_all();
493  *
494  * DESCRIPTION
495  *
496  *    Forces flushing for all logging channels.  This should be called for
497  *    example after receiving special signals.
498  *
499  * SEE ALSO
500  *    silc_log_quick
501  *
502  ***/
503 void silc_log_flush_all();
504
505 /****f* silcutil/SilcLogAPI/silc_log_reset_all
506  *
507  * SYNOPSIS
508  *
509  *    void silc_log_reset_all();
510  *
511  * DESCRIPTION
512  *
513  *    Forces all logging channels to close and reopen their streams.  Useful
514  *    for example after a SIGHUP signal.
515  *    Please note that this function could generate some warning messages if
516  *    one or more logging channels point to an illegal filename.
517  *
518  ***/
519 void silc_log_reset_all();
520
521 /****f* silcutil/SilcLogAPI/silc_log_output_debug
522  *
523  * SYNOPSIS
524  *
525  *    void silc_log_output_debug(char *file, char *function,
526  *                               int line, char *string);
527  *
528  * DESCRIPTION
529  *
530  *    This is the main function for debug output.  Please note that you should
531  *    rather use the wrapper macro SILC_LOG_DEBUG.
532  *    If you want to use it anyway, the `file', `function', and `line' are the
533  *    corresponding offsets in the source files, while `string' must be a
534  *    dynamic allocated (null-terminated) buffer.
535  *
536  ***/
537 void silc_log_output_debug(char *file, char *function,
538                            int line, char *string);
539
540 /****f* silcutil/SilcLogAPI/silc_log_output_hexdump
541  *
542  * SYNOPSIS
543  *
544  *    void silc_log_output_hexdump(char *file, char *function,
545  *                                 int line, void *data_in,
546  *                                 SilcUInt32 len, char *string);
547  *
548  * DESCRIPTION
549  *
550  *    This is the main function for hexdump output.  Please note that you
551  *    should rather use the wrapper macro SILC_LOG_HEXDUMP.
552  *    If you want to use it anyway, the `file', `function', and `line' are the
553  *    corresponding offsets in the source files, `data_in' is the beginning
554  *    of the buffer you wish to hexdump, which is `len' bytes long.
555  *    `string' must be a dynamic allocated (null-terminated) buffer.
556  *
557  ***/
558 void silc_log_output_hexdump(char *file, char *function,
559                              int line, void *data_in,
560                              SilcUInt32 len, char *string);
561
562 /****f* silcutil/SilcLogAPI/silc_log_set_debug_callbacks
563  *
564  * SYNOPSIS
565  *
566  *    void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
567  *                                      void *debug_context,
568  *                                      SilcLogHexdumpCb hexdump_cb,
569  *                                      void *hexdump_context);
570  *
571  * DESCRIPTION
572  *
573  *    Sets `debug_cb' as the the default callback function for the debug
574  *    output, that will be called with the `debug_context' parameter.
575  *    When SilcLog receives a debug message, it will trigger the callback
576  *    function.  If the callback function returns TRUE SilcLog will assume
577  *    the input as handled and won't run its default handler.
578  *    `hexdump_cb' and `hexdump_context' works the same way, except that they
579  *    are referred to SILC_LOG_HEXDUMP requests.
580  *    You can disable/remove a callback by setting it to NULL.
581  *    If set, each callback function must be either in the form described by
582  *    SilcLogDebugCb or SilcLogHexdumpCb.
583  *
584  * SEE ALSO
585  *    SilcLogDebugCb,  SilcLogHexdumpCb
586  *
587  ***/
588 void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
589                                   void *debug_context,
590                                   SilcLogHexdumpCb hexdump_cb,
591                                   void *hexdump_context);
592
593 /****f* silcutil/SilcLogAPI/silc_log_set_debug_string
594  *
595  * SYNOPSIS
596  *
597  *    void silc_log_set_debug_string(const char *debug_string);
598  *
599  * DESCRIPTION
600  *
601  *    Sets `debug_string' as the regexp string for filtering debugging
602  *    output.  The string is copied and it can be modified/destroyed after
603  *    this function call.
604  *
605  ***/
606 void silc_log_set_debug_string(const char *debug_string);
607
608 #endif  /* !SILCLOG_H */