2020-03-23 21:57:07 +02:00
|
|
|
/*! \file qpms_error.h
|
|
|
|
*
|
|
|
|
* \brief QPMS miscellanous internal error handling functions and macros.
|
|
|
|
*
|
|
|
|
*/
|
2019-03-05 21:27:01 +02:00
|
|
|
#ifndef QPMS_ERROR_H
|
|
|
|
#define QPMS_ERROR_H
|
|
|
|
|
2019-09-02 12:03:27 +03:00
|
|
|
#include "optim.h"
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Print error message and abort();
|
2020-01-28 18:10:52 +02:00
|
|
|
QPMS_NORETURN void qpms_pr_error(const char *fmt, ...);
|
2019-03-05 21:27:01 +02:00
|
|
|
//void qpms_error(const char *fmt, ...);
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Print an error message, indicating source, function name and line number, and abort().
|
|
|
|
/**
|
|
|
|
* Usually not used directly, but rather via some of the macros
|
|
|
|
* that fill the first arguments automatically.
|
|
|
|
*
|
|
|
|
* \see QPMS_PR_ERROR
|
|
|
|
*/
|
2020-01-28 18:10:52 +02:00
|
|
|
QPMS_NORETURN void qpms_pr_error_at_flf(const char *filename, unsigned int linenum,
|
2019-03-05 21:27:01 +02:00
|
|
|
const char *func,
|
|
|
|
const char *fmt, ...);
|
|
|
|
|
2019-03-20 20:46:47 +02:00
|
|
|
/// Print a warning message to stderr and flush the buffer. Don't call this directly, use QPMS_WARN().
|
|
|
|
void qpms_warn_at_flf(const char *filename, unsigned int linenum,
|
|
|
|
const char *func,
|
|
|
|
const char *fmt, ...);
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Classification of debugging messages.
|
|
|
|
/**
|
|
|
|
* \see qpms_dbgmsg_enabled, qpms_dbgmsg_enable(), qpms_dbgmsg_disable()
|
|
|
|
*/
|
2019-03-27 09:03:24 +02:00
|
|
|
typedef enum {
|
|
|
|
QPMS_DBGMSG_MISC = 1,
|
2020-03-23 21:57:07 +02:00
|
|
|
QPMS_DBGMSG_THREADS = 2, ///< Multithreading-related debug messages.
|
|
|
|
QPMS_DBGMSG_INTEGRATION = 4 ///< Quadrature-related debug messages.
|
2019-03-27 09:03:24 +02:00
|
|
|
} qpms_dbgmsg_flags;
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Print a debugging message to stderr and flush the buffer. Don't call this directly, use QPMS_DEBUG().
|
2019-03-27 09:03:24 +02:00
|
|
|
void qpms_debug_at_flf(const char *filename, unsigned int linenum,
|
|
|
|
const char *func,
|
|
|
|
qpms_dbgmsg_flags type,
|
|
|
|
const char *fmt, ...);
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Global variable determining which types of debug messages shall be printed with QPMS_DEBUG().
|
|
|
|
/**
|
|
|
|
* Use qpms_dbgmsg_enable() and qpms_dbgmsg_disable() to manipulate
|
|
|
|
* this variable.
|
|
|
|
*
|
|
|
|
* \see QPMS_DEBUG()
|
|
|
|
*/
|
2019-03-27 09:03:24 +02:00
|
|
|
extern qpms_dbgmsg_flags qpms_dbgmsg_enabled;
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Enable debugging messages of given \a types.
|
|
|
|
/** \see qpms_dbgmsg_disable() */
|
2019-03-27 09:03:24 +02:00
|
|
|
qpms_dbgmsg_flags qpms_dbgmsg_disable(qpms_dbgmsg_flags types);
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Disable debugging messages of given \a types.
|
|
|
|
/** \see qpms_dbgmsg_enable() */
|
|
|
|
qpms_dbgmsg_flags qpms_dbgmsg_enable(qpms_dbgmsg_flags types);
|
2019-03-27 09:03:24 +02:00
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Print a warning to stderr and flush the buffer.
|
|
|
|
/**
|
|
|
|
* The arguments are the same as in standard printf().
|
|
|
|
*/
|
2019-03-20 20:46:47 +02:00
|
|
|
#define QPMS_WARN(msg, ...) qpms_warn_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__)
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Print a debugging message to stderr and flush the buffer.
|
|
|
|
/**
|
|
|
|
* The arguments after \a type are the same as in standard printf().
|
|
|
|
*
|
|
|
|
* The debugging message is printed only if the corresponding \a type flag
|
|
|
|
* is set in qpms_dbgmsg_enabled.
|
|
|
|
*
|
|
|
|
* \see qpms_dbgmsg_enabled
|
|
|
|
*/
|
|
|
|
#define QPMS_DEBUG(type /**< Debugging message type flag, see qpms_dbgmsg_flags */ , msg, ...) qpms_debug_at_flf(__FILE__,__LINE__,__func__,type,msg, ##__VA_ARGS__)
|
2019-03-26 11:54:07 +02:00
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Wrapper macro of standard malloc(), crashing on failure.
|
|
|
|
/**
|
|
|
|
* Normally corresponds to a `pointer = malloc(size)`
|
|
|
|
* statement; however, if NULL is returned, this prints an
|
|
|
|
* error message and abort()s the program.
|
|
|
|
*
|
|
|
|
* The arguments are expanded several times.
|
|
|
|
*
|
|
|
|
* Note that this macro expands to a code block, to be kept in mind when using
|
|
|
|
* together with if/else etc.
|
|
|
|
*
|
|
|
|
* Assigned memory block is to be deallocated with standard free().
|
|
|
|
*
|
|
|
|
* \see QPMS_CRASHING_CALLOC.
|
|
|
|
*/
|
2019-07-15 13:58:55 +03:00
|
|
|
#define QPMS_CRASHING_MALLOC(pointer, size) {\
|
|
|
|
(pointer) = malloc(size);\
|
2019-09-02 12:03:27 +03:00
|
|
|
if(QPMS_UNLIKELY(!(pointer) && (size)))\
|
2020-03-23 21:57:07 +02:00
|
|
|
qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
|
2019-07-15 13:58:55 +03:00
|
|
|
"Allocation of %zd bytes for " #pointer " failed.",\
|
|
|
|
(size_t) (size));\
|
|
|
|
}
|
2019-03-09 12:55:46 +02:00
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Allocate and copy.
|
|
|
|
/**
|
|
|
|
* Behaves as QPMS_CRASHING_MALLOC(dest, size), but
|
|
|
|
* additionaly copies a chunk of memory from \a src to \a dest.
|
|
|
|
*
|
|
|
|
* \see QPMS_CRASHING_MALLOC()
|
|
|
|
*/
|
2020-01-08 15:18:25 +02:00
|
|
|
#define QPMS_CRASHING_MALLOCPY(dest, src, size) {\
|
|
|
|
(dest) = malloc(size);\
|
|
|
|
if(QPMS_UNLIKELY(!(dest) && (size)))\
|
2020-03-23 21:57:07 +02:00
|
|
|
qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
|
2020-01-08 15:18:25 +02:00
|
|
|
"Allocation of %zd bytes for " #dest " failed.",\
|
|
|
|
(size_t) (size));\
|
|
|
|
memcpy((dest), (src), (size));\
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Wrapper macro of standard calloc(), crashing on failure.
|
|
|
|
/**
|
|
|
|
* Normally corresponds to a `pointer = calloc(nmemb, size)`
|
|
|
|
* statement; however, if NULL is returned, this prints an
|
|
|
|
* error message and abort()s the program.
|
|
|
|
*
|
|
|
|
* The arguments are expanded several times.
|
|
|
|
*
|
|
|
|
* Note that this macro expands to a code block, to be kept in mind when using
|
|
|
|
* together with if/else etc.
|
|
|
|
*
|
|
|
|
* Assigned memory block is to be deallocated with standard free().
|
|
|
|
*
|
|
|
|
* \see QPMS_CRASHING_MALLOC, QPMS_CRASHING_REALLOC.
|
|
|
|
*/
|
2019-07-15 13:58:55 +03:00
|
|
|
#define QPMS_CRASHING_CALLOC(pointer, nmemb, size) {\
|
|
|
|
(pointer) = calloc((nmemb), (size));\
|
2019-09-02 12:03:27 +03:00
|
|
|
if(QPMS_UNLIKELY(!(pointer) && (nmemb) && (size)))\
|
2020-03-23 21:57:07 +02:00
|
|
|
qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
|
2019-07-15 13:58:55 +03:00
|
|
|
"Allocation of %zd bytes for " #pointer " failed.",\
|
|
|
|
(size_t)((nmemb)*(size)));\
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Wrapper macro of standard realloc(), crashing on failure.
|
|
|
|
/**
|
|
|
|
* Normally corresponds to a `pointer = realloc(pointer, size)`
|
|
|
|
* statement; however, if NULL is returned, this prints an
|
|
|
|
* error message and abort()s the program.
|
|
|
|
*
|
|
|
|
* The arguments are expanded several times.
|
|
|
|
*
|
|
|
|
* Note that this macro expands to a code block, to be kept in mind when using
|
|
|
|
* together with if/else etc.
|
|
|
|
*
|
|
|
|
* Assigned memory block is to be deallocated with standard free().
|
|
|
|
*
|
|
|
|
* \see QPMS_CRASHING_MALLOC.
|
|
|
|
*/
|
2019-07-15 13:58:55 +03:00
|
|
|
#define QPMS_CRASHING_REALLOC(pointer, size) {\
|
|
|
|
(pointer) = realloc((pointer), size);\
|
2019-09-02 12:03:27 +03:00
|
|
|
if(QPMS_UNLIKELY(!(pointer) && (size)))\
|
2020-03-23 21:57:07 +02:00
|
|
|
qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
|
2019-07-15 13:58:55 +03:00
|
|
|
"Rellocation of %zd bytes for " #pointer " failed.",\
|
|
|
|
(size_t) (size));\
|
|
|
|
}
|
2019-03-09 20:47:05 +02:00
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Prints an "unexpected error" message and aborts the program.
|
|
|
|
/** Usually only put to presumably unreachable places in the code and similar. */
|
2019-03-09 12:55:46 +02:00
|
|
|
#define QPMS_WTF qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Unexpected error.")
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Aborts the program with "invalid enumerator" error message.
|
2020-03-23 11:55:26 +02:00
|
|
|
#define QPMS_INVALID_ENUM(x) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Invalid enum value (" #x " == %d)", (int) (x))
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Prints an "untested function/feature" warning once when reached in the code.
|
2019-06-23 20:30:14 +03:00
|
|
|
#define QPMS_UNTESTED {\
|
2019-08-23 09:40:45 +03:00
|
|
|
static _Bool already_bitched = 0; \
|
2019-09-02 12:03:27 +03:00
|
|
|
if (QPMS_UNLIKELY(!already_bitched)) {\
|
2019-06-24 15:12:06 +03:00
|
|
|
qpms_warn_at_flf(__FILE__,__LINE__,__func__,"Warning: untested function/feature!");\
|
2019-08-23 09:40:45 +03:00
|
|
|
already_bitched = 1;\
|
2019-06-23 20:30:14 +03:00
|
|
|
}\
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Prints a given error message and aborts the program.
|
|
|
|
/** The arguments are as in standard printf(). */
|
2019-03-20 20:46:47 +02:00
|
|
|
#define QPMS_PR_ERROR(msg, ...) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__)
|
|
|
|
|
2020-03-23 11:55:26 +02:00
|
|
|
/// Raises an error if \a x is not zero.
|
2019-08-23 11:26:22 +03:00
|
|
|
#define QPMS_ENSURE_SUCCESS(x) { \
|
2020-03-23 11:55:26 +02:00
|
|
|
int errorcode = (x); /* evaluate x only once */ \
|
2019-09-02 12:03:27 +03:00
|
|
|
if(QPMS_UNLIKELY(errorcode)) \
|
2019-08-23 11:26:22 +03:00
|
|
|
qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Unexpected error (%d)", errorcode); \
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Raises an error if \a x is not zero, with custom error message.
|
2020-01-23 16:16:41 +02:00
|
|
|
#define QPMS_ENSURE_SUCCESS_M(x, msg, ...) { \
|
|
|
|
int errorcode = (x); \
|
|
|
|
if(QPMS_UNLIKELY(errorcode)) \
|
|
|
|
qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__); \
|
|
|
|
}
|
|
|
|
|
2020-03-23 11:55:26 +02:00
|
|
|
/// Raises an error if \a x is not zero or one of the values listed in arguments.
|
|
|
|
#define QPMS_ENSURE_SUCCESS_OR(x, ...) { \
|
|
|
|
int errorcode = (x); /* evaluate x only once */ \
|
|
|
|
static const int allowed_errorcodes[] = {0, ##__VA_ARGS__};\
|
|
|
|
static const int n_allowed = sizeof(allowed_errorcodes) / sizeof(int); \
|
|
|
|
int i; \
|
|
|
|
for(i = 0; i < n_allowed; ++i) \
|
|
|
|
if (errorcode == allowed_errorcodes[i]) break; \
|
|
|
|
if (QPMS_UNLIKELY(i >= n_allowed)) \
|
|
|
|
qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Unexpected error (%d)", errorcode); \
|
|
|
|
}
|
2019-03-09 12:55:46 +02:00
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Raises an error if \a x is not true, with custom error message.
|
2019-09-02 12:03:27 +03:00
|
|
|
#define QPMS_ENSURE(x, msg, ...) {if(QPMS_UNLIKELY(!(x))) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__); }
|
2019-03-10 17:14:50 +02:00
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Raises an error if \a x is false.
|
|
|
|
/**
|
|
|
|
* Currently, this is always expanded, ignoring the possible NDEBUG macro.
|
|
|
|
* In places where the evaluation could have significant performance impact,
|
|
|
|
* consider using QPMS_PARANOID_ASSERT() instead.
|
|
|
|
*/
|
2019-07-15 13:58:55 +03:00
|
|
|
#define QPMS_ASSERT(x) {\
|
2019-09-02 12:03:27 +03:00
|
|
|
if(QPMS_UNLIKELY(!(x)))\
|
2019-07-15 13:58:55 +03:00
|
|
|
qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
|
|
|
|
"Unexpected error. This is most certainly a bug.");\
|
|
|
|
}
|
2019-06-23 22:04:00 +03:00
|
|
|
|
2020-01-23 16:16:41 +02:00
|
|
|
#ifdef QPMS_EVALUATE_PARANOID_ASSERTS
|
2020-03-23 21:57:07 +02:00
|
|
|
/** \brief Raises an error if \a x is false.
|
|
|
|
*
|
|
|
|
* Expanded only if QPMS_EVALUATE_PARANOID_ASSERTS macro is defined.
|
|
|
|
*/
|
2020-01-23 16:16:41 +02:00
|
|
|
#define QPMS_PARANOID_ASSERT(x) {\
|
|
|
|
if(QPMS_UNLIKELY(!(x)))\
|
|
|
|
qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\
|
|
|
|
"Unexpected error. This is most certainly a bug.");\
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define QPMS_PARANOID_ASSERT(x) {;}
|
|
|
|
#endif
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Raises a "not implemented" error with additional custom message.
|
|
|
|
/** Serves also as a label/placeholder of not implemented parts of the code. */
|
2019-03-18 17:54:32 +02:00
|
|
|
#define QPMS_NOT_IMPLEMENTED(msg, ...) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__, \
|
|
|
|
"Not implemented:" msg, ##__VA_ARGS__)
|
|
|
|
|
2020-03-23 21:57:07 +02:00
|
|
|
/// Prints an "incomplete implementation" warning once with a custom message.
|
|
|
|
/** Serves mainly as a label/placeholder of incomplete parts of the code. */
|
2019-07-15 09:16:51 +03:00
|
|
|
#define QPMS_INCOMPLETE_IMPLEMENTATION(msg, ...) {\
|
2019-08-23 09:40:45 +03:00
|
|
|
static _Bool already_bitched = 0; \
|
2019-09-02 12:03:27 +03:00
|
|
|
if (QPMS_UNLIKELY(!already_bitched)) {\
|
2019-07-15 09:16:51 +03:00
|
|
|
qpms_warn_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__);\
|
2019-08-23 09:40:45 +03:00
|
|
|
already_bitched = 1;\
|
2019-07-15 09:16:51 +03:00
|
|
|
}\
|
|
|
|
}
|
|
|
|
|
2019-03-05 21:27:01 +02:00
|
|
|
#endif
|