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"
29 #if SWR_ENABLE_ASSERTS || SWR_ENABLE_REL_ASSERTS
32 #pragma comment(lib, "user32.lib")
41 TEXT_PURPLE
= TEXT_RED
| TEXT_BLUE
,
42 TEXT_CYAN
= TEXT_GREEN
| TEXT_BLUE
,
43 TEXT_YELLOW
= TEXT_RED
| TEXT_GREEN
,
44 TEXT_WHITE
= TEXT_RED
| TEXT_GREEN
| TEXT_BLUE
,
53 void SetTextColor(FILE* stream
, TextColor color
= TEXT_WHITE
, TextStyle style
= TEXT_NORMAL
)
57 HANDLE hConsoleHandle
= nullptr;
60 hConsoleHandle
= GetStdHandle(STD_ERROR_HANDLE
);
62 else if (stream
== stdout
)
64 hConsoleHandle
= GetStdHandle(STD_OUTPUT_HANDLE
);
68 // Not a console stream, do nothing
72 WORD textAttributes
= 0;
75 textAttributes
|= FOREGROUND_RED
;
77 if (color
& TEXT_GREEN
)
79 textAttributes
|= FOREGROUND_GREEN
;
81 if (color
& TEXT_BLUE
)
83 textAttributes
|= FOREGROUND_BLUE
;
85 if (style
& TEXT_INTENSITY
)
87 textAttributes
|= FOREGROUND_INTENSITY
;
89 SetConsoleTextAttribute(hConsoleHandle
, textAttributes
);
94 uint32_t cc
= 30 + (style
? 60 : 0) + color
;
95 fprintf(stream
, "\033[0m\033[%d;%dm", style
, cc
);
100 void ResetTextColor(FILE* stream
)
104 SetTextColor(stream
);
109 fprintf(stream
, "\033[0m");
117 const char* pExpression
,
118 const char* pFileName
,
120 const char* pFunction
,
121 const char* pFmtString
/* = nullptr */,
124 if (!enabled
) return false;
126 SetTextColor(stderr
, TEXT_CYAN
, TEXT_NORMAL
);
128 fprintf(stderr
, "%s(%d): ", pFileName
, lineNum
);
130 SetTextColor(stderr
, TEXT_RED
, TEXT_INTENSITY
);
132 fprintf(stderr
, "ASSERT: %s\n", pExpression
);
134 SetTextColor(stderr
, TEXT_CYAN
, TEXT_INTENSITY
);
135 fprintf(stderr
, "\t%s\n", pFunction
);
139 SetTextColor(stderr
, TEXT_YELLOW
, TEXT_INTENSITY
);
140 fprintf(stderr
, "\t");
142 va_start(args
, pFmtString
);
143 vfprintf(stderr
, pFmtString
, args
);
145 fprintf(stderr
, "\n");
147 ResetTextColor(stderr
);
151 static const int MAX_MESSAGE_LEN
= 2048;
152 char msgBuf
[MAX_MESSAGE_LEN
];
154 sprintf_s(msgBuf
, "%s(%d): ASSERT: %s\n", pFileName
, lineNum
, pExpression
);
155 msgBuf
[MAX_MESSAGE_LEN
- 2] = '\n';
156 msgBuf
[MAX_MESSAGE_LEN
- 1] = 0;
157 OutputDebugStringA(msgBuf
);
159 sprintf_s(msgBuf
, "\t%s\n", pFunction
);
160 msgBuf
[MAX_MESSAGE_LEN
- 2] = '\n';
161 msgBuf
[MAX_MESSAGE_LEN
- 1] = 0;
162 OutputDebugStringA(msgBuf
);
169 va_start(args
, pFmtString
);
170 offset
= _vsnprintf_s(
178 if (offset
< 0) { return true; }
180 OutputDebugStringA("\t");
181 OutputDebugStringA(msgBuf
);
182 OutputDebugStringA("\n");
185 if (KNOB_ENABLE_ASSERT_DIALOGS
)
187 int retval
= sprintf_s(
189 MAX_MESSAGE_LEN
- offset
,
195 "Cancel: Disable this assert for the remainder of the process\n"
196 "Try Again: Break into the debugger\n"
197 "Continue: Continue execution (but leave assert enabled)",
202 if (retval
< 0) { return true; }
206 if (!IsDebuggerPresent())
210 MAX_MESSAGE_LEN
- offset
,
211 "\n\n*** NO DEBUGGER DETECTED ***\n\nPressing \"Try Again\" will cause a program crash!");
214 retval
= MessageBoxA(nullptr, msgBuf
, "Assert Failed", MB_CANCELTRYCONTINUE
| MB_ICONEXCLAMATION
);
231 return IsDebuggerPresent() || !chkDebugger
;
238 #endif // SWR_ENABLE_ASSERTS