ovni/src/common.c
Rodrigo Arias 43fa556fa9 Enable debug messages with -d
Allows the user to easily enable debug messages, which are the only way
to debug problems with channels.
2023-04-06 15:33:15 +00:00

70 lines
1.2 KiB
C

/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: MIT */
#include "common.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
char *progname = NULL;
int is_debug_enabled = 0;
void
progname_set(char *name)
{
progname = name;
}
void
enable_debug(void)
{
is_debug_enabled = 1;
}
static void
vaerr(const char *prefix, const char *func, const char *errstr, va_list ap)
{
if (progname != NULL)
fprintf(stderr, "%s: ", progname);
if (prefix != NULL)
fprintf(stderr, "%s: ", prefix);
if (func != NULL)
fprintf(stderr, "%s: ", func);
vfprintf(stderr, errstr, ap);
int len = strlen(errstr);
if (len > 0) {
char last = errstr[len - 1];
if (last == ':')
fprintf(stderr, " %s\n", strerror(errno));
else if (last != '\n' && last != '\r')
fprintf(stderr, "\n");
}
}
void
verr(const char *prefix, const char *func, const char *errstr, ...)
{
va_list ap;
va_start(ap, errstr);
vaerr(prefix, func, errstr, ap);
va_end(ap);
}
void
vdie(const char *prefix, const char *func, const char *errstr, ...)
{
va_list ap;
va_start(ap, errstr);
vaerr(prefix, func, errstr, ap);
va_end(ap);
abort();
}