diff --git a/library/Error.cpp b/library/Error.cpp index ba1482918..3cd0eb31d 100644 --- a/library/Error.cpp +++ b/library/Error.cpp @@ -8,13 +8,13 @@ inline std::string safe_str(const char *s) return s ? s : "(NULL)"; } -NullPointer::NullPointer(const char *varname) - :All("NULL pointer: " + safe_str(varname)), +NullPointer::NullPointer(const char *varname, const char *func) + :All("In " + safe_str(func) + ": NULL pointer: " + safe_str(varname)), varname(varname) {} -InvalidArgument::InvalidArgument(const char *expr) - :All("Invalid argument; expected: " + safe_str(expr)), +InvalidArgument::InvalidArgument(const char *expr, const char *func) + :All("In " + safe_str(func) + ": Invalid argument; expected: " + safe_str(expr)), expr(expr) {} diff --git a/library/include/Error.h b/library/include/Error.h index d3d208b1f..29564d69d 100644 --- a/library/include/Error.h +++ b/library/include/Error.h @@ -24,11 +24,13 @@ distribution. #pragma once +#include +#include +#include + #include "Export.h" +#include "MiscUtils.h" #include "Pragma.h" -#include -#include -#include namespace DFHack { @@ -68,20 +70,20 @@ namespace DFHack class DFHACK_EXPORT NullPointer : public All { public: const char *const varname; - NullPointer(const char *varname = NULL); + NullPointer(const char *varname = NULL, const char *func = NULL); }; #define CHECK_NULL_POINTER(var) \ - { if (var == NULL) throw DFHack::Error::NullPointer(#var); } + { if (var == NULL) throw DFHack::Error::NullPointer(#var, DFHACK_FUNCTION_SIG); } class DFHACK_EXPORT InvalidArgument : public All { public: const char *const expr; - InvalidArgument(const char *expr = NULL); + InvalidArgument(const char *expr = NULL, const char *func = NULL); }; #define CHECK_INVALID_ARGUMENT(expr) \ - { if (!(expr)) throw DFHack::Error::InvalidArgument(#expr); } + { if (!(expr)) throw DFHack::Error::InvalidArgument(#expr, DFHACK_FUNCTION_SIG); } class DFHACK_EXPORT VTableMissing : public All { public: diff --git a/library/include/MiscUtils.h b/library/include/MiscUtils.h index 917b67489..378b7a728 100644 --- a/library/include/MiscUtils.h +++ b/library/include/MiscUtils.h @@ -36,6 +36,14 @@ using std::ostream; using std::stringstream; using std::endl; +#if defined(_MSC_VER) + #define DFHACK_FUNCTION_SIG __FUNCSIG__ +#elif defined(__GNUC__) + #define DFHACK_FUNCTION_SIG __PRETTY_FUNCTION__ +#else + #define DFHACK_FUNCTION_SIG __func__ +#endif + template void print_bits ( T val, ostream& out ) {