allow different logging targets

This commit is contained in:
Markus Teich 2016-07-13 12:21:40 +02:00
parent b700ddb437
commit 1636ff8c49
2 changed files with 60 additions and 31 deletions

43
util.c
View File

@ -16,9 +16,10 @@
/** /**
* @file util.c * @file util.c
* @brief \todo * @brief Implementation of common utility functions.
* @author Markus Teich * @author Markus Teich
*/ */
#include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -27,30 +28,44 @@
#include "util.h" #include "util.h"
static FILE *logstream = NULL;
/**
* setlog sets another output for logging.
*
* @param[in] stream The new logging target.
*/
void
setlog (FILE *stream)
{
logstream = stream;
}
/** /**
* xvprintf prints a formatstring with prefix "libbrandt: ". If the format * xvprintf prints a formatstring with prefix "libbrandt: ". If the format
* string ends with a ':', the strerror() from errno.h output will be appended. * string ends with a ':', the strerror() from errno.h output will be appended.
* The output is always terminated with a newline. * The output is always terminated with a newline.
* *
* @param fmt The format string * @param[in] fmt The format string
* @param ap The inputs to the format string * @param[in] ap The inputs to the format string
*/ */
void static void
xvprintf (const char *fmt, va_list ap) xvprintf (const char *fmt, va_list ap)
{ {
/**\todo: provide other logging target than stderr */ fputs ("libbrandt: ", logstream ? logstream : stderr);
fputs ("libbrandt: ", stderr);
vfprintf (stderr, fmt, ap); vfprintf (logstream ? logstream : stderr, fmt, ap);
if (fmt[0] && fmt[strlen (fmt) - 1] == ':') if (fmt[0] && fmt[strlen (fmt) - 1] == ':')
{ {
fputc (' ', stderr); fputc (' ', logstream ? logstream : stderr);
perror (NULL); fputs (strerror (errno), logstream ? logstream : stderr);
} }
else else
{ {
fputc ('\n', stderr); fputc ('\n', logstream ? logstream : stderr);
} }
} }
@ -59,8 +74,8 @@ xvprintf (const char *fmt, va_list ap)
* eprintf prints an error message and then calls abort() to terminate the * eprintf prints an error message and then calls abort() to terminate the
* process. * process.
* *
* @param fmt The format string * @param[in] fmt The format string
* @param ... The inputs to the format string * @param[in] ... The inputs to the format string
*/ */
void void
eprintf (const char *fmt, ...) eprintf (const char *fmt, ...)
@ -78,8 +93,8 @@ eprintf (const char *fmt, ...)
/** /**
* weprintf prints a warning message * weprintf prints a warning message
* *
* @param fmt The format string * @param[in] fmt The format string
* @param ... The inputs to the format string * @param[in] ... The inputs to the format string
*/ */
void void
weprintf (const char *fmt, ...) weprintf (const char *fmt, ...)

48
util.h
View File

@ -16,37 +16,51 @@
/** /**
* @file util.h * @file util.h
* @brief \todo * @brief Interface for the common utility functions.
* @author Markus Teich * @author Markus Teich
*/ */
#ifndef _BRANDT_UTIL_H #ifndef _BRANDT_UTIL_H
#define _BRANDT_UTIL_H #define _BRANDT_UTIL_H
void eprintf(const char *fmt, ...); void setlog (FILE *stream);
void weprintf(const char *fmt, ...); void eprintf (const char *fmt, ...);
void weprintf (const char *fmt, ...);
# undef brandt_assert #undef brandt_assert
# undef brandt_assert_perror #undef brandt_assert_perror
# undef brandt_assert_gpgerr #undef brandt_assert_gpgerr
#ifdef NDEBUG #ifdef NDEBUG
# define brandt_assert(expr) ((expr) ? (void)(0) : \ #define brandt_assert(expr) ((expr) ? (void)(0) : \
eprintf("Assertion failed in file %s line %d function %s: %s", __FILE__, __LINE__, __PRETTY_FUNCTION__, (#expr))) eprintf("Assertion failed in file %s line %d function %s: %s", \
# define brandt_assert_perror(errnum) (!(errnum) ? (void)(0) : \ __FILE__, \
eprintf("Assertion failed in file %s line %d function %s:", __FILE__, __LINE__, __PRETTY_FUNCTION__)) __LINE__, \
# define brandt_assert_gpgerr(errnum) (!(errnum) ? (void)(0) : \ __PRETTY_FUNCTION__, \
eprintf("Assertion failed in file %s line %d function %s: %s", __FILE__, __LINE__, __PRETTY_FUNCTION__, gcry_strerror((errnum)))) (#expr)))
# define DP(point) ((void)(gcry_log_debugpnt (#point, point, ec_ctx))) #define brandt_assert_perror(errnum) (!(errnum) ? (void)(0) : \
# define DM(mpi) ((void)(gcry_log_debugmpi (#mpi, mpi, ec_ctx))) eprintf("Assertion failed in file %s line %d function %s:", \
__FILE__, \
__LINE__, \
__PRETTY_FUNCTION__))
#define brandt_assert_gpgerr(errnum) (!(errnum) ? (void)(0) : \
eprintf("Assertion failed in file %s line %d function %s: %s", \
__FILE__, \
__LINE__, \
__PRETTY_FUNCTION__, \
gcry_strerror((errnum))))
#define DP(point) ((void)(gcry_log_debugpnt (#point, point, ec_ctx)))
#define DM(mpi) ((void)(gcry_log_debugmpi (#mpi, mpi, ec_ctx)))
#else #else
# define brandt_assert(expr) ((void)(expr)) #define brandt_assert(expr) ((void)(expr))
# define brandt_assert_perror(errnum) ((void)(errnum)) #define brandt_assert_perror(errnum) ((void)(errnum))
# define brandt_assert_gpgerr(errnum) ((void)(errnum)) #define brandt_assert_gpgerr(errnum) ((void)(errnum))
#endif #endif