Files
s390-tools/zdump/zg_error.c
T
Alexander Egorenkov 81013f0c70 zdump/zg: Convert error and abort macros to functions which can be mocked
This change allows mocking of error/abort macros in unit tests.
Being able to do this in unit tests, enables us to test error conditions w/o
terminating the unit test runner.

The new error functions do not have "noreturn" attribute because
this would make mocking of them in unit tests impossible. We must not
compile these functions as noreturn because we need to return from them
in unit tests and returning from a noreturn function is an undefined
behavior in the C++ standard!

For more details:
- ISO/IEC 14882:2017, Chapter 10.6.8 "Noreturn attribute""
- https://en.cppreference.com/w/cpp/language/attributes/noreturn.

Signed-off-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2021-12-09 16:19:25 +01:00

71 lines
1.2 KiB
C

/*
* Copyright IBM Corp. 2001, 2017, 2021
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <stdarg.h>
#include "lib/util_libc.h"
#include "zg.h"
static inline void _zg_err(const char *fmt, va_list ap)
{
fprintf(stderr, "%s: ", "zgetdump");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
}
static inline void _zg_err_errno(const char *fmt, va_list ap)
{
fflush(stdout);
fprintf(stderr, "%s: ", "zgetdump");
vfprintf(stderr, fmt, ap);
fprintf(stderr, " (%s)", strerror(errno));
fprintf(stderr, "\n");
}
void zg_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_zg_err(fmt, ap);
va_end(ap);
}
void zg_err_exit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_zg_err(fmt, ap);
va_end(ap);
zg_exit(1);
}
void zg_err_exit_errno(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_zg_err_errno(fmt, ap);
va_end(ap);
zg_exit(1);
}
void zg_abort(const char *fmt, ...)
{
char *newfmt;
va_list ap;
newfmt = util_strcat_realloc(util_strdup("Internal Error: "), fmt);
va_start(ap, fmt);
_zg_err(newfmt, ap);
va_end(ap);
free(newfmt);
abort();
}