From 1636ff8c4977e416499006c2571edf41f6e96c67 Mon Sep 17 00:00:00 2001 From: Markus Teich Date: Wed, 13 Jul 2016 12:21:40 +0200 Subject: allow different logging targets --- util.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index 2b44710..1eff0d4 100644 --- a/util.c +++ b/util.c @@ -16,9 +16,10 @@ /** * @file util.c - * @brief \todo + * @brief Implementation of common utility functions. * @author Markus Teich */ +#include #include #include #include @@ -27,30 +28,44 @@ #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 * string ends with a ':', the strerror() from errno.h output will be appended. * The output is always terminated with a newline. * - * @param fmt The format string - * @param ap The inputs to the format string + * @param[in] fmt The format string + * @param[in] ap The inputs to the format string */ -void +static void xvprintf (const char *fmt, va_list ap) { - /**\todo: provide other logging target than stderr */ - fputs ("libbrandt: ", stderr); + fputs ("libbrandt: ", logstream ? logstream : stderr); - vfprintf (stderr, fmt, ap); + vfprintf (logstream ? logstream : stderr, fmt, ap); if (fmt[0] && fmt[strlen (fmt) - 1] == ':') { - fputc (' ', stderr); - perror (NULL); + fputc (' ', logstream ? logstream : stderr); + fputs (strerror (errno), logstream ? logstream : stderr); } 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 * process. * - * @param fmt The format string - * @param ... The inputs to the format string + * @param[in] fmt The format string + * @param[in] ... The inputs to the format string */ void eprintf (const char *fmt, ...) @@ -78,8 +93,8 @@ eprintf (const char *fmt, ...) /** * weprintf prints a warning message * - * @param fmt The format string - * @param ... The inputs to the format string + * @param[in] fmt The format string + * @param[in] ... The inputs to the format string */ void weprintf (const char *fmt, ...) -- cgit v1.2.3