updates.
[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/SilcLogAPI
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 choosing
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 failures 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 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, uint32 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 it
126  *    like 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, uint32 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 bool silc_log_quick;
164
165 /****v* silcutil/SilcLogAPI/silc_debug
166  *
167  * NAME
168  *
169  *    bool silc_debug -- enable/disable debugging output
170  *
171  * DESCRIPTION
172  *
173  *    If silc_debug is set to FALSE, debugging functions won't procude any
174  *    output.  This is useful when for example you compile in the debugging
175  *    support but at a certain point you want to send the program in the
176  *    background.
177  *
178  * SEE ALSO
179  *    SILC_LOG_DEBUG
180  *
181  ***/
182 extern bool silc_debug;
183
184 /****v* silcutil/SilcLogAPI/silc_debug_hexdump
185  *
186  * NAME
187  *
188  *    bool silc_debug_hexdump -- enable/disable debugging output
189  *
190  * DESCRIPTION
191  *
192  *    If silc_debug_hexdump is set to FALSE, debugging functions won't produce
193  *    any output.  This is useful when for example you compile in the debugging
194  *    support but at a certain point you want to send the program in the
195  *    background.
196  *
197  * SEE ALSO
198  *    SILC_LOG_HEXDUMP
199  *
200  ***/
201 extern bool silc_debug_hexdump;
202
203 /* Macros */
204
205 #ifdef WIN32
206 #define __FUNCTION__ ""
207 #endif
208
209 /****d* silcutil/SilcLogAPI/SILC_LOG_INFO
210  *
211  * NAME
212  *
213  *    #define SILC_LOG_INFO(...)
214  *
215  * DESCRIPTION
216  *
217  *    This macro is a wrapper to the main logging function.
218  *    It supports variable argument list formatting, and *automatically*
219  *    appends newline at the end of the string.
220  *
221  * NOTES
222  *
223  *    This macro requires double parenthesis to ensure that the VA list
224  *    formatting would work correctly.
225  *
226  * EXAMPLE
227  *
228  *    SILC_LOG_INFO(("Today i feel %s", core->mood));
229  *
230  * SOURCE
231  */
232 #define SILC_LOG_INFO(fmt) silc_log_output(SILC_LOG_INFO, silc_format fmt)
233 /***/
234
235 /****d* silcutil/SilcLogAPI/SILC_LOG_WARNING
236  *
237  * NAME
238  *
239  *    #define SILC_LOG_WARNING(...)
240  *
241  * DESCRIPTION
242  *
243  *    Wrapper to the WARNING logging channel.
244  *    Please see the SILC_LOG_INFO macro.
245  *
246  * SEE ALSO
247  *    SILC_LOG_INFO
248  *
249  * SOURCE
250  */
251 #define SILC_LOG_WARNING(fmt) silc_log_output(SILC_LOG_WARNING, silc_format fmt)
252 /***/
253
254 /****d* silcutil/SilcLogAPI/SILC_LOG_ERROR
255  *
256  * NAME
257  *
258  *    #define SILC_LOG_ERROR(...)
259  *
260  * DESCRIPTION
261  *
262  *    Wrapper to the ERROR logging channel.
263  *    Please see the SILC_LOG_INFO macro.
264  *
265  * SEE ALSO
266  *    SILC_LOG_INFO
267  *
268  * SOURCE
269  */
270 #define SILC_LOG_ERROR(fmt) silc_log_output(SILC_LOG_ERROR, silc_format fmt)
271 /***/
272
273 /****d* silcutil/SilcLogAPI/SILC_LOG_FATAL
274  *
275  * NAME
276  *
277  *    #define SILC_LOG_FATAL(...)
278  *
279  * DESCRIPTION
280  *
281  *    Wrapper to the FATAL logging channel.
282  *    Please see the SILC_LOG_INFO macro.
283  *
284  * SEE ALSO
285  *    SILC_LOG_INFO
286  *
287  * SOURCE
288  */
289 #define SILC_LOG_FATAL(fmt) silc_log_output(SILC_LOG_FATAL, silc_format fmt)
290 /***/
291
292 /****d* silcutil/SilcLogAPI/SILC_LOG_DEBUG
293  *
294  * NAME
295  *
296  *    #define SILC_LOG_DEBUG(...)
297  *
298  * DESCRIPTION
299  *
300  *    This is a special wrapper to the debugging output (usually stderr).
301  *    The standard behaviour is the same as SILC_LOG_INFO, but this macro
302  *    also depends on the global debugging macro SILC_DEBUG.
303  *    Undefining the global SILC_DEBUG define causes these functions to be
304  *    defined to an empty value, thus removing all logging calls from the
305  *    compiled program.
306  *
307  * SEE ALSO
308  *    SILC_LOG_INFO
309  *
310  * SOURCE
311  */
312 #ifdef SILC_DEBUG
313 #define SILC_LOG_DEBUG(fmt) silc_log_output_debug(__FILE__, \
314                                 __FUNCTION__, \
315                                 __LINE__, \
316                                 silc_format fmt)
317 #else
318 #define SILC_LOG_DEBUG(fmt)
319 #endif  /* SILC_DEBUG */
320 /***/
321
322 /****d* silcutil/SilcLogAPI/SILC_LOG_HEXDUMP
323  *
324  * NAME
325  *
326  *    #define SILC_LOG_HEXDUMP(...)
327  *
328  * DESCRIPTION
329  *
330  *    This is a special wrapper to the hexdump output function.  This macro
331  *    behaves slightly differently from other logging wrappers.
332  *    The first parameter, is composed by a group of parameters delimited by
333  *    parenthesis.
334  *    The second parameter is a (char *) pointer to the beginning of the
335  *    memory section that should be hexdumped, and the third parameter is
336  *    the length of this memory section.
337  *    This macro is also affected by the global variable silc_debug_hexdump.
338  *    Undefining the global SILC_DEBUG define causes these functions to be
339  *    defined to an empty value, thus removing all logging calls from the
340  *    compiled program.
341  *
342  * EXAMPLE
343  *
344  *    SILC_LOG_HEXDUMP(("Outgoing packet [%d], len %d", pckt->seq, pckt->len),
345  *                     pckt->data, pckt->datalen);
346  *
347  * SOURCE
348  */
349 #ifdef SILC_DEBUG
350 #define SILC_LOG_HEXDUMP(fmt, data, len) silc_log_output_hexdump(__FILE__, \
351                                 __FUNCTION__, \
352                                 __LINE__, \
353                                 (data), (len), \
354                                 silc_format fmt)
355 #else
356 #define SILC_LOG_HEXDUMP(fmt, data, len)
357 #endif  /* SILC_DEBUG */
358 /***/
359
360 /* Prototypes */
361
362 /****f* silcutil/SilcLogAPI/silc_log_output
363  *
364  * SYNOPSIS
365  *
366  *    void silc_log_output(SilcLogType type, char *string);
367  *
368  * DESCRIPTION
369  *
370  *    This is the main function for logging output. Please note that you
371  *    should rather use one of the logging wrapper macros.
372  *    If you really want to use this function, its usage is quite simple.
373  *    The `type' parameter identifies the channel to use, while the `string'
374  *    parameter must be a dynamic allocated (null-terminated) buffer, because
375  *    it will be freed at the end of this function, for internal reasons.
376  *    If there are registered callbacks for the specified type, this function
377  *    will first trigger those callbacks.  The callback functions must NOT
378  *    free or modify the original buffer.
379  *
380  * SEE ALSO
381  *    SILC_LOG_INFO, SILC_LOG_WARNING, SILC_LOG_ERROR, SILC_LOG_FATAL
382  *
383  ***/
384 void silc_log_output(SilcLogType type, char *string);
385
386 /****f* silcutil/SilcLogAPI/silc_log_get_file
387  *
388  * SYNOPSIS
389  *
390  *    char *silc_log_get_file(SilcLogType type);
391  *
392  * DESCRIPTION
393  *
394  *    Returns the current logging file for the channel `type'.
395  *    If there has been an error during the opening of this channel, NULL
396  *    is returned, even if the file has been previously set with
397  *    silc_log_set_file().
398  *    The returned pointer points to internally allocated storage and must
399  *    must not be freed, modified or stored.
400  *
401  ***/
402 char *silc_log_get_file(SilcLogType type);
403
404 /****f* silcutil/SilcLogAPI/silc_log_set_file
405  *
406  * SYNOPSIS
407  *
408  *    bool silc_log_set_file(SilcLogType type, char *filename, uint32 maxsize,
409  *                           SilcSchedule scheduler);
410  *
411  * DESCRIPTION
412  *
413  *    Sets `filename', which can be maximum `maxsize' bytes long, as the new
414  *    logging file for the channel `type'.  If you specify an illegal filename
415  *    a warning message is printed and FALSE is returned.  In this case
416  *    logging settings are not changed.
417  *    You can disable logging for a channel by specifying NULL filename, the
418  *    maxsize in this case is not important.
419  *    The `scheduler' parameter is needed by the internal logging to allow
420  *    buffered output and thus to save HD activity.
421  *
422  ***/
423 bool silc_log_set_file(SilcLogType type, char *filename, uint32 maxsize,
424                        SilcSchedule scheduler);
425
426 /****f* silcutil/SilcLogAPI/silc_log_set_callback
427  *
428  * SYNOPSIS
429  *
430  *    void silc_log_set_callback(SilcLogType type, SilcLogCb cb,
431  *                               void *context);
432  *
433  * DESCRIPTION
434  *
435  *    Set `cb' as the default callback function for the logging channel
436  *    `type'.  When SilcLog receives a message for this channel, it will
437  *    trigger the callback function.  If the callback function returns TRUE
438  *    SilcLog will assume the input as handled and won't run its default
439  *    handler.
440  *    You can disable/remove a callback by setting it to NULL or calling the
441  *    function silc_log_reset_callbacks.
442  *    If set, the callback function must be in the form described by
443  *    SilcLogCb.
444  *
445  * SEE ALSO
446  *    SilcLogCb, silc_log_reset_callbacks
447  *
448  ***/
449 void silc_log_set_callback(SilcLogType type, SilcLogCb cb, void *context);
450
451 /****f* silcutil/SilcLogAPI/silc_log_reset_callbacks
452  *
453  * SYNOPSIS
454  *
455  *    void silc_log_reset_callbacks();
456  *
457  * DESCRIPTION
458  *
459  *    Removes all logging callbacks for normal channels.  This function does
460  *    NOT remove callbacks for debugging channels (debug and hexdump), you
461  *    rather need to call silc_log_set_debug_callbacks() with NULL callbacks.
462  *
463  ***/
464 void silc_log_reset_callbacks();
465
466 /****f* silcutil/SilcLogAPI/silc_log_flush_all
467  *
468  * SYNOPSIS
469  *
470  *    void silc_log_flush_all();
471  *
472  * DESCRIPTION
473  *
474  *    Forces flushing for all logging channels.  This should be called for
475  *    example after receiving special signals.
476  *
477  * SEE ALSO
478  *    silc_log_quick
479  *
480  ***/
481 void silc_log_flush_all();
482
483 /****f* silcutil/SilcLogAPI/silc_log_reset_all
484  *
485  * SYNOPSIS
486  *
487  *    void silc_log_reset_all();
488  *
489  * DESCRIPTION
490  *
491  *    Forces all logging channels to close and reopen their streams.  Useful
492  *    for example after a SIGHUP signal.
493  *    Please note that this function could cause some warning messages if
494  *    some logging channel points to an illegal filename.
495  *
496  ***/
497 void silc_log_reset_all();
498
499 /****f* silcutil/SilcLogAPI/silc_log_output_debug
500  *
501  * SYNOPSIS
502  *
503  *    void silc_log_output_debug(char *file, char *function,
504  *                               int line, char *string);
505  *
506  * DESCRIPTION
507  *
508  *    This is the main function for debug output.  Please note that you should
509  *    rather use the wrapper macro SILC_LOG_DEBUG.
510  *    If you want to use it anyway, the `file', `function', and `line' are the
511  *    corresponding offsets in the source files, while `string' must be a
512  *    dynamic allocated (null-terminated) buffer.
513  *
514  ***/
515 void silc_log_output_debug(char *file, char *function,
516                            int line, char *string);
517
518 /****f* silcutil/SilcLogAPI/silc_log_output_hexdump
519  *
520  * SYNOPSIS
521  *
522  *    void silc_log_output_hexdump(char *file, char *function,
523  *                                 int line, void *data_in,
524  *                                 uint32 len, char *string);
525  *
526  * DESCRIPTION
527  *
528  *    This is the main function for hexdump output.  Please note that you
529  *    should rather use the wrapper macro SILC_LOG_HEXDUMP.
530  *    If you want to use it anyway, the `file', `function', and `line' are the
531  *    corresponding offsets in the source files, `data_in' is the beginning
532  *    of the buffer you wish to hexdump, which is `len' bytes long.
533  *    `string' must be a dynamic allocated (null-terminated) buffer.
534  *
535  ***/
536 void silc_log_output_hexdump(char *file, char *function,
537                              int line, void *data_in,
538                              uint32 len, char *string);
539
540 /****f* silcutil/SilcLogAPI/silc_log_set_debug_callbacks
541  *
542  * SYNOPSIS
543  *
544  *    void silc_log_set_debug_callbacks(SilcLogDebugCb debug_cb,
545  *                                      void *debug_context,
546  *                                      SilcLogHexdumpCb hexdump_cb,
547  *                                      void *hexdump_context);
548  *
549  * DESCRIPTION
550  *
551  *    Sets `debug_cb' as the the default callback function for the debug
552  *    output, that will be called with the `debug_context' parameter.
553  *    When SilcLog receives a debug message, it will trigger the callback
554  *    function.  If the callback function returns TRUE SilcLog will assume
555  *    the input as handled and won't run its default handler.
556  *    `hexdump_cb' and `hexdump_context' works the same way, except that they
557  *    are referred to SILC_LOG_HEXDUMP requests.
558  *    You can disable/remove a callback by setting it to NULL.
559  *    If set, each callback function must be either in the form described by
560  *    SilcLogDebugCb or SilcLogHexdumpCb.
561  *
562  * SEE ALSO
563  *    SilcLogDebugCb,  SilcLogHexdumpCb
564  *
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 /****f* silcutil/SilcLogAPI/silc_log_set_debug_string
572  *
573  * SYNOPSIS
574  *
575  *    void silc_log_set_debug_string(const char *debug_string);
576  *
577  * DESCRIPTION
578  *
579  *    Sets `debug_string' as the regexp string for filtering debugging
580  *    output.  The string is copied and it can be modified/destroyed after
581  *    this function call.
582  *
583  ***/
584 void silc_log_set_debug_string(const char *debug_string);
585
586 #endif  /* !SILCLOG_H */