update util: logging and assertions

This commit is contained in:
Markus Teich 2016-06-12 00:55:09 +02:00
parent 6416f0fbfe
commit ec1ce068ed
2 changed files with 70 additions and 20 deletions

47
util.c Normal file
View File

@ -0,0 +1,47 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
static void xvprintf(const char *, va_list);
void
eprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
xvprintf(fmt, ap);
va_end(ap);
abort();
}
void
weprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
xvprintf(fmt, ap);
va_end(ap);
}
void
xvprintf(const char *fmt, va_list ap)
{
/**TODO: provide other logging target than stderr */
fputs("libbrandt: ", stderr);
vfprintf(stderr, fmt, ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
}

43
util.h
View File

@ -1,25 +1,28 @@
/* This file is part of libgotr.
* (C) 2014-2015 Markus Teich, Jannik Theiß
*
* libgotr is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 3, or (at your
* option) any later version.
*
* libgotr is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libgotr; see the file LICENSE. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef _BRANDT_UTIL_H
#define _BRANDT_UTIL_H
void brandt_eprintf(const char *format, ...);
void eprintf(const char *fmt, ...);
void weprintf(const char *fmt, ...);
# undef brandt_assert
# undef brandt_assert_perror
# undef brandt_assert_gpgerr
#ifdef NDEBUG
# define brandt_assert(expr) ((void)(0))
# define brandt_assert_perror(errnum) ((void)(0))
# define brandt_assert_gpgerr(errnum) ((void)(0))
#else
# 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))))
#endif
#endif