swr: [rasterizer common] add SwrTrace() and macros
authorTim Rowley <timothy.o.rowley@intel.com>
Wed, 23 Nov 2016 01:50:00 +0000 (19:50 -0600)
committerTim Rowley <timothy.o.rowley@intel.com>
Wed, 30 Nov 2016 01:36:46 +0000 (19:36 -0600)
Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
src/gallium/drivers/swr/rasterizer/common/swr_assert.cpp
src/gallium/drivers/swr/rasterizer/common/swr_assert.h

index 72501014de4734da6052cec108c576a1966fa83e..98a5a52957fe2707b29abae31b54712fe5ab6e8d 100644 (file)
@@ -25,6 +25,8 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include <assert.h>
+#include <algorithm>
+#include <mutex>
 
 #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<std::mutex> 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<std::mutex> 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];
index 04d768191b46bf43bd0f1f45fe8f1244ba8310fa..bd6833615a17029cd8a82376ec7875752adc8a55 100644 (file)
@@ -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__)