From 1636ff8c4977e416499006c2571edf41f6e96c67 Mon Sep 17 00:00:00 2001 From: Markus Teich Date: Wed, 13 Jul 2016 12:21:40 +0200 Subject: [PATCH 1/3] allow different logging targets --- util.c | 43 +++++++++++++++++++++++++++++-------------- util.h | 48 +++++++++++++++++++++++++++++++----------------- 2 files changed, 60 insertions(+), 31 deletions(-) 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, ...) diff --git a/util.h b/util.h index d21cc89..1e67197 100644 --- a/util.h +++ b/util.h @@ -16,37 +16,51 @@ /** * @file util.h - * @brief \todo + * @brief Interface for the common utility functions. * @author Markus Teich */ #ifndef _BRANDT_UTIL_H #define _BRANDT_UTIL_H -void eprintf(const char *fmt, ...); -void weprintf(const char *fmt, ...); +void setlog (FILE *stream); +void eprintf (const char *fmt, ...); +void weprintf (const char *fmt, ...); -# undef brandt_assert -# undef brandt_assert_perror -# undef brandt_assert_gpgerr +#undef brandt_assert +#undef brandt_assert_perror +#undef brandt_assert_gpgerr #ifdef NDEBUG -# define brandt_assert(expr) ((expr) ? (void)(0) : \ -eprintf("Assertion failed in file %s line %d function %s: %s", __FILE__, __LINE__, __PRETTY_FUNCTION__, (#expr))) -# define brandt_assert_perror(errnum) (!(errnum) ? (void)(0) : \ -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 brandt_assert(expr) ((expr) ? (void)(0) : \ +eprintf("Assertion failed in file %s line %d function %s: %s", \ + __FILE__, \ + __LINE__, \ + __PRETTY_FUNCTION__, \ + (#expr))) -# define DP(point) ((void)(gcry_log_debugpnt (#point, point, ec_ctx))) -# define DM(mpi) ((void)(gcry_log_debugmpi (#mpi, mpi, ec_ctx))) +#define brandt_assert_perror(errnum) (!(errnum) ? (void)(0) : \ +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 -# define brandt_assert(expr) ((void)(expr)) -# define brandt_assert_perror(errnum) ((void)(errnum)) -# define brandt_assert_gpgerr(errnum) ((void)(errnum)) +#define brandt_assert(expr) ((void)(expr)) +#define brandt_assert_perror(errnum) ((void)(errnum)) +#define brandt_assert_gpgerr(errnum) ((void)(errnum)) #endif From 9fcc42d6ab3bc386c9b42861a69421f534a247f8 Mon Sep 17 00:00:00 2001 From: Markus Teich Date: Wed, 13 Jul 2016 12:22:23 +0200 Subject: [PATCH 2/3] fixup last commit --- util.h | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/util.h b/util.h index 1e67197..faf893a 100644 --- a/util.h +++ b/util.h @@ -33,35 +33,41 @@ void weprintf (const char *fmt, ...); #ifdef NDEBUG -#define brandt_assert(expr) ((expr) ? (void)(0) : \ -eprintf("Assertion failed in file %s line %d function %s: %s", \ - __FILE__, \ - __LINE__, \ - __PRETTY_FUNCTION__, \ - (#expr))) +#define brandt_assert(expr) do { \ + (expr) ? (void)(0) : eprintf ( \ + "Assertion failed in file %s line %d function %s: %s", \ + __FILE__, \ + __LINE__, \ + __PRETTY_FUNCTION__, \ + (# expr)); \ +} while (0) -#define brandt_assert_perror(errnum) (!(errnum) ? (void)(0) : \ -eprintf("Assertion failed in file %s line %d function %s:", \ - __FILE__, \ - __LINE__, \ - __PRETTY_FUNCTION__)) +#define brandt_assert_perror(errnum) do { \ + !(errnum) ? (void)(0) : eprintf ( \ + "Assertion failed in file %s line %d function %s:", \ + __FILE__, \ + __LINE__, \ + __PRETTY_FUNCTION__); \ +} while (0) -#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 brandt_assert_gpgerr(errnum) do { \ + !(errnum) ? (void)(0) : eprintf ( \ + "Assertion failed in file %s line %d function %s: %s", \ + __FILE__, \ + __LINE__, \ + __PRETTY_FUNCTION__, \ + gcry_strerror ((errnum))); \ +} while (0) -#define DP(point) ((void)(gcry_log_debugpnt (#point, point, ec_ctx))) -#define DM(mpi) ((void)(gcry_log_debugmpi (#mpi, mpi, ec_ctx))) +#define DP(point) ((void)(gcry_log_debugpnt (# point, point, ec_ctx))) +#define DM(mpi) ((void)(gcry_log_debugmpi (# mpi, mpi, ec_ctx))) -#else +#else /* ifdef NDEBUG */ #define brandt_assert(expr) ((void)(expr)) #define brandt_assert_perror(errnum) ((void)(errnum)) #define brandt_assert_gpgerr(errnum) ((void)(errnum)) -#endif +#endif /* ifdef NDEBUG */ -#endif +#endif /* ifndef _BRANDT_UTIL_H */ From 8a1d2917f64d425212f0a3d2e8d91ca9f3c20122 Mon Sep 17 00:00:00 2001 From: Markus Teich Date: Wed, 13 Jul 2016 12:22:55 +0200 Subject: [PATCH 3/3] no indent preproc --- uncrustify/conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uncrustify/conf b/uncrustify/conf index b901341..c89e346 100644 --- a/uncrustify/conf +++ b/uncrustify/conf @@ -1373,7 +1373,7 @@ pp_indent = ignore # ignore/add/remove/force pp_indent_at_level = false # false/true # If pp_indent_at_level=false, specifies the number of columns to indent per level. Default=1. -pp_indent_count = 1 # number +pp_indent_count = 0 # number # Add or remove space after # based on pp_level of #if blocks pp_space = ignore # ignore/add/remove/force