diff --git a/CRT.c b/CRT.c index 30236a3b..3a665496 100644 --- a/CRT.c +++ b/CRT.c @@ -13,6 +13,7 @@ in the source distribution for its full text. #include #include #include +#include #include #include #include @@ -844,6 +845,16 @@ static void dumpStderr(void) { stderrRedirectNewFd = -1; } +void CRT_debug_impl(const char* file, size_t lineno, const char* func, const char* fmt, ...) { + va_list args; + + fprintf(stderr, "[%s:%zu (%s)]: ", file, lineno, func); + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fprintf(stderr, "\n"); +} + #else /* !NDEBUG */ static void redirectStderr(void) { diff --git a/CRT.h b/CRT.h index 0b6302fc..eae4722d 100644 --- a/CRT.h +++ b/CRT.h @@ -150,6 +150,13 @@ typedef enum ColorElements_ { void CRT_fatalError(const char* note) ATTR_NORETURN; +#ifdef NDEBUG +# define CRT_debug(...) +#else +void CRT_debug_impl(const char* file, size_t lineno, const char* func, const char* fmt, ...) ATTR_FORMAT(printf, 4, 5); +# define CRT_debug(...) CRT_debug_impl(__FILE__, __LINE__, __func__, __VA_ARGS__) +#endif + void CRT_handleSIGSEGV(int signal) ATTR_NORETURN; #define KEY_WHEELUP KEY_F(30)