From f7ab0e4b7ea9d9334af38d6ba5680d326c5fd1eb Mon Sep 17 00:00:00 2001 From: Tim Rowley Date: Tue, 22 Nov 2016 19:50:00 -0600 Subject: [PATCH] swr: [rasterizer common] add SwrTrace() and macros Reviewed-by: Bruce Cherniak --- .../swr/rasterizer/common/swr_assert.cpp | 96 ++++++++++++++++--- .../swr/rasterizer/common/swr_assert.h | 14 +++ 2 files changed, 95 insertions(+), 15 deletions(-) diff --git a/src/gallium/drivers/swr/rasterizer/common/swr_assert.cpp b/src/gallium/drivers/swr/rasterizer/common/swr_assert.cpp index 72501014de4..98a5a52957f 100644 --- a/src/gallium/drivers/swr/rasterizer/common/swr_assert.cpp +++ b/src/gallium/drivers/swr/rasterizer/common/swr_assert.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #if SWR_ENABLE_ASSERTS || SWR_ENABLE_REL_ASSERTS @@ -111,30 +113,24 @@ void ResetTextColor(FILE* stream) #endif } -bool SwrAssert( - bool chkDebugger, - bool& enabled, - const char* pExpression, +static std::mutex g_stderrMutex; + +void SwrTrace( const char* pFileName, uint32_t lineNum, - const char* pFunction, - const char* pFmtString /* = nullptr */, + const char* function, + const char* pFmtString, ...) { - SetTextColor(stderr, TEXT_CYAN, TEXT_NORMAL); + std::lock_guard l(g_stderrMutex); - fprintf(stderr, "%s(%d): ", pFileName, lineNum); - - SetTextColor(stderr, TEXT_RED, TEXT_INTENSITY); - - fprintf(stderr, "ASSERT: %s\n", pExpression); + SetTextColor(stderr, TEXT_CYAN, TEXT_NORMAL); - SetTextColor(stderr, TEXT_CYAN, TEXT_INTENSITY); - fprintf(stderr, "\t%s\n", pFunction); + fprintf(stderr, "%s(%d): TRACE in %s:\n", pFileName, lineNum, function); if (pFmtString) { - SetTextColor(stderr, TEXT_YELLOW, TEXT_INTENSITY); + SetTextColor(stderr, TEXT_PURPLE, TEXT_INTENSITY); fprintf(stderr, "\t"); va_list args; va_start(args, pFmtString); @@ -145,6 +141,76 @@ bool SwrAssert( ResetTextColor(stderr); fflush(stderr); +#if defined(_WIN32) + static const int MAX_MESSAGE_LEN = 2048; + char msgBuf[MAX_MESSAGE_LEN]; + + sprintf_s(msgBuf, "%s(%d): TRACE in %s\n", pFileName, lineNum, function); + msgBuf[MAX_MESSAGE_LEN - 2] = '\n'; + msgBuf[MAX_MESSAGE_LEN - 1] = 0; + OutputDebugStringA(msgBuf); + + int offset = 0; + + if (pFmtString) + { + va_list args; + va_start(args, pFmtString); + offset = _vsnprintf_s( + msgBuf, + sizeof(msgBuf), + sizeof(msgBuf), + pFmtString, + args); + va_end(args); + + if (offset < 0) { return; } + + OutputDebugStringA("\t"); + OutputDebugStringA(msgBuf); + OutputDebugStringA("\n"); + } +#endif // _WIN32 +} + +bool SwrAssert( + bool chkDebugger, + bool& enabled, + const char* pExpression, + const char* pFileName, + uint32_t lineNum, + const char* pFunction, + const char* pFmtString /* = nullptr */, + ...) +{ + { + std::lock_guard l(g_stderrMutex); + + SetTextColor(stderr, TEXT_CYAN, TEXT_NORMAL); + + fprintf(stderr, "%s(%d): ", pFileName, lineNum); + + SetTextColor(stderr, TEXT_RED, TEXT_INTENSITY); + + fprintf(stderr, "ASSERT: %s\n", pExpression); + + SetTextColor(stderr, TEXT_CYAN, TEXT_INTENSITY); + fprintf(stderr, "\t%s\n", pFunction); + + if (pFmtString) + { + SetTextColor(stderr, TEXT_YELLOW, TEXT_INTENSITY); + fprintf(stderr, "\t"); + va_list args; + va_start(args, pFmtString); + vfprintf(stderr, pFmtString, args); + va_end(args); + fprintf(stderr, "\n"); + } + ResetTextColor(stderr); + fflush(stderr); + } + #if defined(_WIN32) static const int MAX_MESSAGE_LEN = 2048; char msgBuf[MAX_MESSAGE_LEN]; diff --git a/src/gallium/drivers/swr/rasterizer/common/swr_assert.h b/src/gallium/drivers/swr/rasterizer/common/swr_assert.h index 04d768191b4..bd6833615a1 100644 --- a/src/gallium/drivers/swr/rasterizer/common/swr_assert.h +++ b/src/gallium/drivers/swr/rasterizer/common/swr_assert.h @@ -104,6 +104,13 @@ bool SwrAssert( const char* pFmtString = nullptr, ...); +void SwrTrace( + const char* pFileName, + uint32_t lineNum, + const char* function, + const char* pFmtString, + ...); + #define _SWR_ASSERT(chkDebugger, e, ...) {\ bool expFailed = !(e);\ if (expFailed) {\ @@ -113,9 +120,13 @@ bool SwrAssert( }\ } +#define _SWR_TRACE(_fmtstr, ...) \ + SwrTrace(__FILE__, __LINE__, __FUNCTION__, _fmtstr, ##__VA_ARGS__); + #if SWR_ENABLE_ASSERTS #define SWR_ASSERT(e, ...) _SWR_ASSERT(true, e, ##__VA_ARGS__) #define SWR_ASSUME_ASSERT(e, ...) SWR_ASSERT(e, ##__VA_ARGS__) +#define SWR_TRACE(_fmtstr, ...) _SWR_TRACE(_fmtstr, ##__VA_ARGS__) #if defined(assert) #undef assert @@ -127,6 +138,7 @@ bool SwrAssert( #if SWR_ENABLE_REL_ASSERTS #define SWR_REL_ASSERT(e, ...) _SWR_ASSERT(false, e, ##__VA_ARGS__) #define SWR_REL_ASSUME_ASSERT(e, ...) SWR_REL_ASSERT(e, ##__VA_ARGS__) +#define SWR_REL_TRACE(_fmtstr, ...) _SWR_TRACE(_fmtstr, ##__VA_ARGS__) #endif #endif // C++ @@ -136,11 +148,13 @@ bool SwrAssert( #if !SWR_ENABLE_ASSERTS #define SWR_ASSERT(e, ...) (void)(0) #define SWR_ASSUME_ASSERT(e, ...) SWR_ASSUME(e, ##__VA_ARGS__) +#define SWR_TRACE(_fmtstr, ...) (void)(0) #endif #if !SWR_ENABLE_REL_ASSERTS #define SWR_REL_ASSERT(e, ...) (void)(0) #define SWR_REL_ASSUME_ASSERT(e, ...) SWR_ASSUME(e, ##__VA_ARGS__) +#define SWR_REL_TRACE(_fmtstr, ...) (void)(0) #endif #define SWR_NOT_IMPL SWR_ASSERT(0, "%s not implemented", __FUNCTION__) -- 2.30.2