1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 ****************************************************************************/
24 #include "common/os.h"
31 #if SWR_ENABLE_ASSERTS || SWR_ENABLE_REL_ASSERTS
34 #pragma comment(lib, "user32.lib")
37 namespace ConsoleUtils
51 PURPLE
= static_cast<uint32_t>(RED
) | static_cast<uint32_t>(BLUE
),
52 CYAN
= static_cast<uint32_t>(GREEN
) | static_cast<uint32_t>(BLUE
),
53 YELLOW
= static_cast<uint32_t>(RED
) | static_cast<uint32_t>(GREEN
),
55 static_cast<uint32_t>(RED
) | static_cast<uint32_t>(GREEN
) | static_cast<uint32_t>(BLUE
),
64 void SetTextColor(FILE* stream
,
65 TextColor color
= TextColor::WHITE
,
66 TextStyle style
= TextStyle::NORMAL
)
70 HANDLE hConsoleHandle
= nullptr;
73 hConsoleHandle
= GetStdHandle(STD_ERROR_HANDLE
);
75 else if (stream
== stdout
)
77 hConsoleHandle
= GetStdHandle(STD_OUTPUT_HANDLE
);
81 // Not a console stream, do nothing
85 WORD textAttributes
= static_cast<WORD
>(color
);
86 if (style
== TextStyle::INTENSITY
)
88 textAttributes
|= FOREGROUND_INTENSITY
;
90 SetConsoleTextAttribute(hConsoleHandle
, textAttributes
);
96 30 + ((style
== TextStyle::INTENSITY
) ? 60 : 0) + static_cast<uint32_t>(color
);
97 fprintf(stream
, "\033[0m\033[%d;%dm", static_cast<uint32_t>(style
), cc
);
102 void ResetTextColor(FILE* stream
)
106 SetTextColor(stream
);
111 fprintf(stream
, "\033[0m");
116 static std::mutex g_stderrMutex
;
117 } // namespace ConsoleUtils
119 bool SwrAssert(bool chkDebugger
,
121 const char* pExpression
,
122 const char* pFileName
,
124 const char* pFunction
,
125 const char* pFmtString
,
128 using namespace ConsoleUtils
;
129 std::lock_guard
<std::mutex
> l(g_stderrMutex
);
131 SetTextColor(stderr
, TextColor::CYAN
, TextStyle::NORMAL
);
133 fprintf(stderr
, "%s(%d): ", pFileName
, lineNum
);
135 SetTextColor(stderr
, TextColor::RED
, TextStyle::INTENSITY
);
137 fprintf(stderr
, "ASSERT: %s\n", pExpression
);
139 SetTextColor(stderr
, TextColor::CYAN
, TextStyle::INTENSITY
);
140 fprintf(stderr
, "\t%s\n", pFunction
);
144 SetTextColor(stderr
, TextColor::YELLOW
, TextStyle::INTENSITY
);
145 fprintf(stderr
, "\t");
147 va_start(args
, pFmtString
);
148 vfprintf(stderr
, pFmtString
, args
);
150 fprintf(stderr
, "\n");
152 ResetTextColor(stderr
);
156 static const int MAX_MESSAGE_LEN
= 2048;
157 char msgBuf
[MAX_MESSAGE_LEN
];
159 sprintf_s(msgBuf
, "%s(%d): ASSERT: %s\n", pFileName
, lineNum
, pExpression
);
160 msgBuf
[MAX_MESSAGE_LEN
- 2] = '\n';
161 msgBuf
[MAX_MESSAGE_LEN
- 1] = 0;
162 OutputDebugStringA(msgBuf
);
164 sprintf_s(msgBuf
, "\t%s\n", pFunction
);
165 msgBuf
[MAX_MESSAGE_LEN
- 2] = '\n';
166 msgBuf
[MAX_MESSAGE_LEN
- 1] = 0;
167 OutputDebugStringA(msgBuf
);
174 va_start(args
, pFmtString
);
175 offset
= _vsnprintf_s(msgBuf
, sizeof(msgBuf
), sizeof(msgBuf
), pFmtString
, args
);
183 OutputDebugStringA("\t");
184 OutputDebugStringA(msgBuf
);
185 OutputDebugStringA("\n");
188 if (enabled
&& KNOB_ENABLE_ASSERT_DIALOGS
)
190 int retval
= sprintf_s(&msgBuf
[offset
],
191 MAX_MESSAGE_LEN
- offset
,
197 "Cancel: Disable this assert for the remainder of the process\n"
198 "Try Again: Break into the debugger\n"
199 "Continue: Continue execution (but leave assert enabled)",
211 if (!IsDebuggerPresent())
213 sprintf_s(&msgBuf
[offset
],
214 MAX_MESSAGE_LEN
- offset
,
215 "\n\n*** NO DEBUGGER DETECTED ***\n\nPressing \"Try Again\" will cause a "
219 retval
= MessageBoxA(nullptr,
222 MB_CANCELTRYCONTINUE
| MB_ICONEXCLAMATION
| MB_SETFOREGROUND
);
239 return (IsDebuggerPresent() || !chkDebugger
) && enabled
;
247 const char* pFileName
, uint32_t lineNum
, const char* pFunction
, const char* pFmtString
, ...)
249 using namespace ConsoleUtils
;
250 std::lock_guard
<std::mutex
> l(g_stderrMutex
);
252 SetTextColor(stderr
, TextColor::CYAN
, TextStyle::NORMAL
);
254 fprintf(stderr
, "%s(%d): TRACE in %s:\n", pFileName
, lineNum
, pFunction
);
258 SetTextColor(stderr
, TextColor::PURPLE
, TextStyle::INTENSITY
);
259 fprintf(stderr
, "\t");
261 va_start(args
, pFmtString
);
262 vfprintf(stderr
, pFmtString
, args
);
264 fprintf(stderr
, "\n");
266 ResetTextColor(stderr
);
270 static const int MAX_MESSAGE_LEN
= 2048;
271 char msgBuf
[MAX_MESSAGE_LEN
];
273 sprintf_s(msgBuf
, "%s(%d): TRACE in %s\n", pFileName
, lineNum
, pFunction
);
274 msgBuf
[MAX_MESSAGE_LEN
- 2] = '\n';
275 msgBuf
[MAX_MESSAGE_LEN
- 1] = 0;
276 OutputDebugStringA(msgBuf
);
283 va_start(args
, pFmtString
);
284 offset
= _vsnprintf_s(msgBuf
, sizeof(msgBuf
), sizeof(msgBuf
), pFmtString
, args
);
292 OutputDebugStringA("\t");
293 OutputDebugStringA(msgBuf
);
294 OutputDebugStringA("\n");
299 #endif // SWR_ENABLE_ASSERTS