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
),
54 WHITE
= static_cast<uint32_t>(RED
) | static_cast<uint32_t>(GREEN
) | static_cast<uint32_t>(BLUE
),
63 void SetTextColor(FILE* stream
, TextColor color
= TextColor::WHITE
, TextStyle style
= TextStyle::NORMAL
)
67 HANDLE hConsoleHandle
= nullptr;
70 hConsoleHandle
= GetStdHandle(STD_ERROR_HANDLE
);
72 else if (stream
== stdout
)
74 hConsoleHandle
= GetStdHandle(STD_OUTPUT_HANDLE
);
78 // Not a console stream, do nothing
82 WORD textAttributes
= static_cast<WORD
>(color
);
83 if (style
== TextStyle::INTENSITY
)
85 textAttributes
|= FOREGROUND_INTENSITY
;
87 SetConsoleTextAttribute(hConsoleHandle
, textAttributes
);
92 uint32_t cc
= 30 + ((style
== TextStyle::INTENSITY
) ? 60 : 0) + static_cast<uint32_t>(color
);
93 fprintf(stream
, "\033[0m\033[%d;%dm", static_cast<uint32_t>(style
), cc
);
98 void ResetTextColor(FILE* stream
)
102 SetTextColor(stream
);
107 fprintf(stream
, "\033[0m");
112 static std::mutex g_stderrMutex
;
118 const char* pExpression
,
119 const char* pFileName
,
121 const char* pFunction
,
122 const char* pFmtString
,
125 using namespace ConsoleUtils
;
126 std::lock_guard
<std::mutex
> l(g_stderrMutex
);
128 SetTextColor(stderr
, TextColor::CYAN
, TextStyle::NORMAL
);
130 fprintf(stderr
, "%s(%d): ", pFileName
, lineNum
);
132 SetTextColor(stderr
, TextColor::RED
, TextStyle::INTENSITY
);
134 fprintf(stderr
, "ASSERT: %s\n", pExpression
);
136 SetTextColor(stderr
, TextColor::CYAN
, TextStyle::INTENSITY
);
137 fprintf(stderr
, "\t%s\n", pFunction
);
141 SetTextColor(stderr
, TextColor::YELLOW
, TextStyle::INTENSITY
);
142 fprintf(stderr
, "\t");
144 va_start(args
, pFmtString
);
145 vfprintf(stderr
, pFmtString
, args
);
147 fprintf(stderr
, "\n");
149 ResetTextColor(stderr
);
153 static const int MAX_MESSAGE_LEN
= 2048;
154 char msgBuf
[MAX_MESSAGE_LEN
];
156 sprintf_s(msgBuf
, "%s(%d): ASSERT: %s\n", pFileName
, lineNum
, pExpression
);
157 msgBuf
[MAX_MESSAGE_LEN
- 2] = '\n';
158 msgBuf
[MAX_MESSAGE_LEN
- 1] = 0;
159 OutputDebugStringA(msgBuf
);
161 sprintf_s(msgBuf
, "\t%s\n", pFunction
);
162 msgBuf
[MAX_MESSAGE_LEN
- 2] = '\n';
163 msgBuf
[MAX_MESSAGE_LEN
- 1] = 0;
164 OutputDebugStringA(msgBuf
);
171 va_start(args
, pFmtString
);
172 offset
= _vsnprintf_s(
180 if (offset
< 0) { return true; }
182 OutputDebugStringA("\t");
183 OutputDebugStringA(msgBuf
);
184 OutputDebugStringA("\n");
187 if (enabled
&& KNOB_ENABLE_ASSERT_DIALOGS
)
189 int retval
= sprintf_s(
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)",
204 if (retval
< 0) { return true; }
208 if (!IsDebuggerPresent())
212 MAX_MESSAGE_LEN
- offset
,
213 "\n\n*** NO DEBUGGER DETECTED ***\n\nPressing \"Try Again\" will cause a program crash!");
216 retval
= MessageBoxA(nullptr, msgBuf
, "Assert Failed", MB_CANCELTRYCONTINUE
| MB_ICONEXCLAMATION
| MB_SETFOREGROUND
);
233 return (IsDebuggerPresent() || !chkDebugger
) && enabled
;
241 const char* pFileName
,
243 const char* pFunction
,
244 const char* pFmtString
,
247 using namespace ConsoleUtils
;
248 std::lock_guard
<std::mutex
> l(g_stderrMutex
);
250 SetTextColor(stderr
, TextColor::CYAN
, TextStyle::NORMAL
);
252 fprintf(stderr
, "%s(%d): TRACE in %s:\n", pFileName
, lineNum
, pFunction
);
256 SetTextColor(stderr
, TextColor::PURPLE
, TextStyle::INTENSITY
);
257 fprintf(stderr
, "\t");
259 va_start(args
, pFmtString
);
260 vfprintf(stderr
, pFmtString
, args
);
262 fprintf(stderr
, "\n");
264 ResetTextColor(stderr
);
268 static const int MAX_MESSAGE_LEN
= 2048;
269 char msgBuf
[MAX_MESSAGE_LEN
];
271 sprintf_s(msgBuf
, "%s(%d): TRACE in %s\n", pFileName
, lineNum
, pFunction
);
272 msgBuf
[MAX_MESSAGE_LEN
- 2] = '\n';
273 msgBuf
[MAX_MESSAGE_LEN
- 1] = 0;
274 OutputDebugStringA(msgBuf
);
281 va_start(args
, pFmtString
);
282 offset
= _vsnprintf_s(
290 if (offset
< 0) { return; }
292 OutputDebugStringA("\t");
293 OutputDebugStringA(msgBuf
);
294 OutputDebugStringA("\n");
299 #endif // SWR_ENABLE_ASSERTS