2016-06-12 01:08:06 +02:00
|
|
|
/* This file is part of libbrandt.
|
|
|
|
* Copyright (C) 2016 GNUnet e.V.
|
|
|
|
*
|
|
|
|
* libbrandt is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libbrandt 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* libbrandt. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-06-12 00:55:09 +02:00
|
|
|
|
2016-06-12 01:08:06 +02:00
|
|
|
/**
|
|
|
|
* @file util.c
|
2016-07-13 12:21:40 +02:00
|
|
|
* @brief Implementation of common utility functions.
|
2016-06-22 23:18:46 +02:00
|
|
|
* @author Markus Teich
|
2016-06-12 01:08:06 +02:00
|
|
|
*/
|
2016-07-13 14:01:24 +02:00
|
|
|
|
|
|
|
#include "brandt_config.h"
|
|
|
|
|
2016-07-13 12:21:40 +02:00
|
|
|
#include <errno.h>
|
2016-06-12 00:55:09 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
2016-07-13 12:21:40 +02:00
|
|
|
static FILE *logstream = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setlog sets another output for logging.
|
|
|
|
*
|
|
|
|
* @param[in] stream The new logging target.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
setlog (FILE *stream)
|
|
|
|
{
|
|
|
|
logstream = stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-12 01:08:06 +02:00
|
|
|
/**
|
2016-06-19 23:21:01 +02:00
|
|
|
* 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.
|
2016-06-12 01:08:06 +02:00
|
|
|
*
|
2016-07-13 12:21:40 +02:00
|
|
|
* @param[in] fmt The format string
|
|
|
|
* @param[in] ap The inputs to the format string
|
2016-06-12 01:08:06 +02:00
|
|
|
*/
|
2016-07-13 12:21:40 +02:00
|
|
|
static void
|
2016-06-19 23:21:01 +02:00
|
|
|
xvprintf (const char *fmt, va_list ap)
|
2016-06-12 00:55:09 +02:00
|
|
|
{
|
2016-07-13 12:21:40 +02:00
|
|
|
fputs ("libbrandt: ", logstream ? logstream : stderr);
|
2016-06-12 00:55:09 +02:00
|
|
|
|
2016-07-13 12:21:40 +02:00
|
|
|
vfprintf (logstream ? logstream : stderr, fmt, ap);
|
2016-06-12 00:55:09 +02:00
|
|
|
|
2016-06-19 23:21:01 +02:00
|
|
|
if (fmt[0] && fmt[strlen (fmt) - 1] == ':')
|
|
|
|
{
|
2016-07-13 12:21:40 +02:00
|
|
|
fputc (' ', logstream ? logstream : stderr);
|
|
|
|
fputs (strerror (errno), logstream ? logstream : stderr);
|
2016-06-19 23:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-13 12:21:40 +02:00
|
|
|
fputc ('\n', logstream ? logstream : stderr);
|
2016-06-19 23:21:01 +02:00
|
|
|
}
|
2016-06-12 00:55:09 +02:00
|
|
|
}
|
|
|
|
|
2016-06-19 23:21:01 +02:00
|
|
|
|
2016-06-12 01:08:06 +02:00
|
|
|
/**
|
2016-06-19 23:21:01 +02:00
|
|
|
* eprintf prints an error message and then calls abort() to terminate the
|
|
|
|
* process.
|
2016-06-12 01:08:06 +02:00
|
|
|
*
|
2016-07-13 12:21:40 +02:00
|
|
|
* @param[in] fmt The format string
|
|
|
|
* @param[in] ... The inputs to the format string
|
2016-06-12 01:08:06 +02:00
|
|
|
*/
|
2016-06-12 00:55:09 +02:00
|
|
|
void
|
2016-06-19 23:21:01 +02:00
|
|
|
eprintf (const char *fmt, ...)
|
2016-06-12 00:55:09 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2016-06-12 01:08:06 +02:00
|
|
|
va_start (ap, fmt);
|
|
|
|
xvprintf (fmt, ap);
|
|
|
|
va_end (ap);
|
2016-06-19 23:21:01 +02:00
|
|
|
|
|
|
|
abort ();
|
2016-06-12 00:55:09 +02:00
|
|
|
}
|
|
|
|
|
2016-06-19 23:21:01 +02:00
|
|
|
|
2016-06-12 01:08:06 +02:00
|
|
|
/**
|
2016-06-19 23:21:01 +02:00
|
|
|
* weprintf prints a warning message
|
2016-06-12 01:08:06 +02:00
|
|
|
*
|
2016-07-13 12:21:40 +02:00
|
|
|
* @param[in] fmt The format string
|
|
|
|
* @param[in] ... The inputs to the format string
|
2016-06-12 01:08:06 +02:00
|
|
|
*/
|
2016-06-12 00:55:09 +02:00
|
|
|
void
|
2016-06-19 23:21:01 +02:00
|
|
|
weprintf (const char *fmt, ...)
|
2016-06-12 00:55:09 +02:00
|
|
|
{
|
2016-06-19 23:21:01 +02:00
|
|
|
va_list ap;
|
2016-06-12 00:55:09 +02:00
|
|
|
|
2016-06-19 23:21:01 +02:00
|
|
|
va_start (ap, fmt);
|
|
|
|
xvprintf (fmt, ap);
|
|
|
|
va_end (ap);
|
2016-06-12 00:55:09 +02:00
|
|
|
}
|