2023-01-25 12:01:01 +01:00
|
|
|
/* 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>
|
2023-03-22 16:01:55 +01:00
|
|
|
#include <stdlib.h>
|
2023-01-25 12:01:01 +01:00
|
|
|
|
|
|
|
char *progname = NULL;
|
2023-03-08 16:14:34 +01:00
|
|
|
int is_debug_enabled = 0;
|
2023-01-25 12:01:01 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
progname_set(char *name)
|
|
|
|
{
|
|
|
|
progname = name;
|
|
|
|
}
|
|
|
|
|
2023-03-08 16:14:34 +01:00
|
|
|
void
|
|
|
|
enable_debug(void)
|
|
|
|
{
|
|
|
|
is_debug_enabled = 1;
|
|
|
|
}
|
|
|
|
|
2023-01-25 12:01:01 +01:00
|
|
|
static void
|
2023-03-23 17:38:08 +01:00
|
|
|
vaerr(const char *prefix, const char *func, const char *errstr, va_list ap)
|
2023-01-25 12:01:01 +01:00
|
|
|
{
|
|
|
|
if (progname != NULL)
|
|
|
|
fprintf(stderr, "%s: ", progname);
|
|
|
|
|
2023-03-23 17:38:08 +01:00
|
|
|
if (prefix != NULL)
|
|
|
|
fprintf(stderr, "%s: ", prefix);
|
|
|
|
|
2023-01-25 12:01:01 +01:00
|
|
|
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));
|
2023-02-17 17:36:01 +01:00
|
|
|
else if (last != '\n' && last != '\r')
|
2023-01-25 12:01:01 +01:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-03-23 17:38:08 +01:00
|
|
|
verr(const char *prefix, const char *func, const char *errstr, ...)
|
2023-01-25 12:01:01 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, errstr);
|
2023-03-23 17:38:08 +01:00
|
|
|
vaerr(prefix, func, errstr, ap);
|
2023-01-25 12:01:01 +01:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-03-23 17:38:08 +01:00
|
|
|
vdie(const char *prefix, const char *func, const char *errstr, ...)
|
2023-01-25 12:01:01 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, errstr);
|
2023-03-23 17:38:08 +01:00
|
|
|
vaerr(prefix, func, errstr, ap);
|
2023-01-25 12:01:01 +01:00
|
|
|
va_end(ap);
|
|
|
|
abort();
|
|
|
|
}
|