27
27
#include <stdarg.h>
28
28
#include <syslog.h>
30
32
#ifndef VANESSA_LOGGER_FLIM
31
33
#define VANESSA_LOGGER_FLIM
33
35
typedef void vanessa_logger_t;
37
typedef void (*vanessa_logger_log_function_va_t)
38
(int priority, const char *fmt, va_list ap);
41
* NB: There was a fundamental design error in the original
42
* implementation of logging to functions such that it cannot work
43
* wit vanessa_loggers internal framework. Thus
44
* vanessa_logger_log_function_t has been replaced by
45
* vanessa_logger_log_function_va_t. Please do not use
46
* vanessa_logger_log_function_t.
48
* I apologies that this breaks backwards compatibility, but
49
* there is no way to make the original protoype work.
51
* Perhaps there was something in the air on the train in Northern Germany
52
* when I wrote this code. Perhaps I was just tired. But in any case
55
* Horms, December 2002
57
* typedef int (*vanessa_logger_log_function_t)
58
* (int priority, const char *fmt, ...);
61
typedef unsigned int vanessa_logger_flag_t;
64
/**********************************************************************
65
* Flags for filehandle or filename loggers
66
**********************************************************************/
68
#define VANESSA_LOGGER_F_NONE 0x0 /* Default behaviour */
69
#define VANESSA_LOGGER_F_NO_IDENT_PID 0x1 /* Don't show ident and pid */
70
#define VANESSA_LOGGER_F_TIMESTAMP 0x2 /* Log the date and time */
71
#define VANESSA_LOGGER_F_CONS 0x4 /* Log to the console if there
72
is an error while writing
73
to the filehandle or filename */
74
#define VANESSA_LOGGER_F_PERROR 0x8 /* Print to stderr as well */
36
76
/**********************************************************************
37
77
* vanessa_logger_openlog_syslog
211
269
* on for instance receiving a SIGHUP
212
270
**********************************************************************/
214
int vanessa_logger_reopen(vanessa_logger_t *vl);
273
vanessa_logger_reopen(vanessa_logger_t * vl);
276
/**********************************************************************
277
* vanessa_logger_strherror_r
278
* Returns a string describing the error code present in errnum
279
* according to the errors for h_errno which is set by gethostbyname(3)
280
* gethostbyaddr(3) and others. Analagous to strerror_r(3).
281
* pre: errnum: Error to show as a string
282
* buf: buffer to write error string to
283
* n: length of buf in bytes
284
* post: on success error string is written to buf
285
* on invalud input errno is set to -EINVAL
286
* if buf is too short then errno is set to -ERANGE
287
* return: 0 on success
289
**********************************************************************/
292
vanessa_logger_strherror_r(int errnum, char *buf, size_t n);
295
/**********************************************************************
296
* vanessa_logger_strherror
297
* Returns a string describing the error code present in errnum
298
* according to the errors for h_errno which is set by gethostbyname(3)
299
* gethostbyaddr(3) and others. Analagous to strerror_r(3).
300
* pre: errnum: Error to show as a string
301
* post: none on success
302
* on invalid input errno is set to -EINVAL
303
* if buf is too short then errno is set to -ERANGE
304
* return: error string for errnum on success
305
* error string for newly set errno on error
306
**********************************************************************/
309
vanessa_logger_strherror(int errnum);
312
/**********************************************************************
313
* vanessa_logger_set_flag
314
* Set flags for logger
315
* Should only be used on filehandle or filename loggers,
316
* ignored otherewise.
317
* pre: vl: logger to set flags of
318
* flag: value to set flags to
319
* See "Flags for filehandle or filename loggers"
320
* in vanessa_logger.h for valid flags
323
**********************************************************************/
326
vanessa_logger_set_flag(vanessa_logger_t * vl, vanessa_logger_flag_t flag);
329
/**********************************************************************
330
* vanessa_logger_get_flag
331
* Set flags for logger
332
* Should only be used on filehandle or filename loggers,
333
* will return 0 for other types of loggers.
334
* See "Flags for filehandle or filename loggers"
335
* in vanessa_logger.h for valid flags
336
* pre: vl: logger to get flags of
338
* return: flags for logger
339
**********************************************************************/
341
vanessa_logger_flag_t
342
vanessa_logger_get_flag(vanessa_logger_t * vl);
345
/**********************************************************************
346
* vanessa_logger_str_dump
347
* Sanitise a buffer into ASCII
348
* pre: vl: Vanessa logger to log errors to. May be NULL.
349
* buffer: buffer to sanitise
350
* size: number of bytes in buffer to sanitise
351
* flag: If VANESSA_LOGGER_STR_DUMP_HEX then a hexidecimal dump
352
* will be done. Else an octal dump will be done.
353
* post: a new buffer is alocated. For each byte in buffer
354
* that is a printable ASCII character it is added to
355
* the new buffer. All other characters are represented
356
* in the new buffer as octal, in the form \xxx.
357
* return: the new buffer, this should be freed by the caller
359
**********************************************************************/
362
#define VANESSA_LOGGER_STR_DUMP_OCT 0x0
363
#define VANESSA_LOGGER_STR_DUMP_HEX 0x1
366
vanessa_logger_str_dump(vanessa_logger_t * vl, const unsigned char *buffer,
367
const size_t buffer_length, vanessa_logger_flag_t flag);
370
/**********************************************************************
371
* The code below sets an internal logger and provides convenience
372
* macros to use this logger. You may either use this, or keep
373
* track of the logger yourself and use the vanessa_logger functions
374
* above directly. The latter approach allows for more than
375
* one logger to be in use at in a single programme.
376
**********************************************************************/
379
extern vanessa_logger_t *__vanessa_logger_vl;
382
/**********************************************************************
383
* vanessa_logger_vl_set
384
* set the logger function to use with convenience macros
385
* No logging will take place using conveineince macros if logger is
386
* set to NULL (default). That is you _must_ call this function to
387
* enable logging using convenience macros.
388
* pre: logger: pointer to a vanessa_logger
389
* post: logger for ip_vs_nl is set to logger
391
**********************************************************************/
393
#define vanessa_logger_set(_vl) __vanessa_logger_vl=(_vl)
396
/**********************************************************************
397
* vanessa_logger_vl_unset
398
* set logger to use with convenience macros to NULL
399
* That is no logging will take place when convenience macros are called
401
* post: logger is NULL
403
**********************************************************************/
405
#define vanessa_logger_unset() vanessa_logger_set(NULL)
408
/**********************************************************************
409
* vanessa_logger_vl_get
410
* retreive the logger function used by convenience macros
413
* return: logger used by convenience macros
414
**********************************************************************/
416
#define vanessa_logger_get() (__vanessa_logger_vl)
419
/**********************************************************************
420
* VANESSA_LOGGER_DEBUG et al
421
* Convenience macros for using internal logger set using
422
* vanessa_logger_set()
423
**********************************************************************/
426
* Hooray for format string problems!
428
* Each of the logging macros has two versions. The UNSAFE version will
429
* accept a format string. You should _NOT_ use the UNSAFE versions if the
430
* first argument, the format string, is derived from user input. The safe
431
* versions (versions that do not have the "_UNSAFE" suffix) do not accept
432
* a format string and only accept one argument, the string to log. These
433
* should be safe to use with user derived input.
436
#define VANESSA_LOGGER_LOG_UNSAFE(priority, fmt, ...) \
437
vanessa_logger_log(__vanessa_logger_vl, priority, fmt, __VA_ARGS__);
439
#define VANESSA_LOGGER_LOG(priority, str) \
440
vanessa_logger_log(__vanessa_logger_vl, priority, "%s", str)
442
#define VANESSA_LOGGER_DEBUG_UNSAFE(fmt, ...) \
443
_vanessa_logger_log_prefix(__vanessa_logger_vl, LOG_DEBUG, \
444
__FUNCTION__, fmt, __VA_ARGS__);
446
#define VANESSA_LOGGER_DEBUG(str) \
447
_vanessa_logger_log_prefix(__vanessa_logger_vl, LOG_DEBUG, \
448
__FUNCTION__, "%s", str);
450
#define VANESSA_LOGGER_DEBUG_ERRNO(str) \
451
_vanessa_logger_log_prefix(__vanessa_logger_vl, LOG_DEBUG, \
452
__FUNCTION__, "%s: %s", str, strerror(errno));
454
#define VANESSA_LOGGER_DEBUG_HERRNO(str) \
455
_vanessa_logger_log_prefix(__vanessa_logger_vl, LOG_DEBUG, \
456
__FUNCTION__, "%s: %s", str, \
457
vanessa_logger_strherror(h_errno));
459
#define VANESSA_LOGGER_DEBUG_RAW_UNSAFE(fmt, ...) \
460
vanessa_logger_log(__vanessa_logger_vl, LOG_DEBUG, \
463
#define VANESSA_LOGGER_DEBUG_RAW(str) \
464
vanessa_logger_log(__vanessa_logger_vl, LOG_DEBUG, "%s", str);
466
#define VANESSA_LOGGER_INFO_UNSAFE(fmt, ...) \
467
vanessa_logger_log(__vanessa_logger_vl, LOG_INFO, fmt, __VA_ARGS__);
469
#define VANESSA_LOGGER_INFO(str) \
470
vanessa_logger_log(__vanessa_logger_vl, LOG_INFO, "%s", str);
472
#define VANESSA_LOGGER_ERR_UNSAFE(fmt, ...) \
473
vanessa_logger_log(__vanessa_logger_vl, LOG_ERR, fmt, __VA_ARGS__);
475
#define VANESSA_LOGGER_ERR_RAW_UNSAFE(fmt, ...) \
476
vanessa_logger_log(__vanessa_logger_vl, LOG_ERR, fmt, __VA_ARGS__);
478
#define VANESSA_LOGGER_ERR(str) \
479
vanessa_logger_log(__vanessa_logger_vl, LOG_ERR, "%s", str);
481
#define VANESSA_LOGGER_RAW_ERR(str) \
482
vanessa_logger_log(__vanessa_logger_vl, LOG_ERR, "%s", str);
484
#define VANESSA_LOGGER_DUMP(buffer, buffer_length, flag) \
485
vanessa_logger_str_dump(__vanessa_logger_vl, (buffer), \
486
(buffer_length), (flag))