Path: blob/21.2-virgl/src/gallium/drivers/swr/rasterizer/common/swr_assert.cpp
4574 views
/****************************************************************************1* Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21****************************************************************************/2223#include "common/os.h"24#include <stdarg.h>25#include <stdio.h>26#include <assert.h>27#include <algorithm>28#include <mutex>2930#if SWR_ENABLE_ASSERTS || SWR_ENABLE_REL_ASSERTS3132#if defined(_MSC_VER)33#pragma comment(lib, "user32.lib")34#endif // _WIN323536namespace ConsoleUtils37{38enum class TextColor39{40BLACK = 0,41#if defined(_WIN32)42RED = 4,43GREEN = 2,44BLUE = 1,45#else46RED = 1,47GREEN = 2,48BLUE = 4,49#endif // _WIN3250PURPLE = static_cast<uint32_t>(RED) | static_cast<uint32_t>(BLUE),51CYAN = static_cast<uint32_t>(GREEN) | static_cast<uint32_t>(BLUE),52YELLOW = static_cast<uint32_t>(RED) | static_cast<uint32_t>(GREEN),53WHITE =54static_cast<uint32_t>(RED) | static_cast<uint32_t>(GREEN) | static_cast<uint32_t>(BLUE),55};5657enum class TextStyle58{59NORMAL = 0,60INTENSITY = 1,61};6263void SetTextColor(FILE* stream,64TextColor color = TextColor::WHITE,65TextStyle style = TextStyle::NORMAL)66{67#if defined(_WIN32)6869HANDLE hConsoleHandle = nullptr;70if (stream == stderr)71{72hConsoleHandle = GetStdHandle(STD_ERROR_HANDLE);73}74else if (stream == stdout)75{76hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);77}78else79{80// Not a console stream, do nothing81return;82}8384WORD textAttributes = static_cast<WORD>(color);85if (style == TextStyle::INTENSITY)86{87textAttributes |= FOREGROUND_INTENSITY;88}89SetConsoleTextAttribute(hConsoleHandle, textAttributes);9091#else // !_WIN329293// Print ANSI codes94uint32_t cc =9530 + ((style == TextStyle::INTENSITY) ? 60 : 0) + static_cast<uint32_t>(color);96fprintf(stream, "\033[0m\033[%d;%dm", static_cast<uint32_t>(style), cc);9798#endif99}100101void ResetTextColor(FILE* stream)102{103#if defined(_WIN32)104105SetTextColor(stream);106107#else // !_WIN32108109// Print ANSI codes110fprintf(stream, "\033[0m");111112#endif113}114115static std::mutex g_stderrMutex;116} // namespace ConsoleUtils117118bool SwrAssert(bool chkDebugger,119bool& enabled,120const char* pExpression,121const char* pFileName,122uint32_t lineNum,123const char* pFunction,124const char* pFmtString,125...)126{127using namespace ConsoleUtils;128std::lock_guard<std::mutex> l(g_stderrMutex);129130SetTextColor(stderr, TextColor::CYAN, TextStyle::NORMAL);131132fprintf(stderr, "%s(%d): ", pFileName, lineNum);133134SetTextColor(stderr, TextColor::RED, TextStyle::INTENSITY);135136fprintf(stderr, "ASSERT: %s\n", pExpression);137138SetTextColor(stderr, TextColor::CYAN, TextStyle::INTENSITY);139fprintf(stderr, "\t%s\n", pFunction);140141if (pFmtString)142{143SetTextColor(stderr, TextColor::YELLOW, TextStyle::INTENSITY);144fprintf(stderr, "\t");145va_list args;146va_start(args, pFmtString);147vfprintf(stderr, pFmtString, args);148va_end(args);149fprintf(stderr, "\n");150}151ResetTextColor(stderr);152fflush(stderr);153154#if defined(_WIN32)155static const int MAX_MESSAGE_LEN = 2048;156char msgBuf[MAX_MESSAGE_LEN];157158sprintf_s(msgBuf, "%s(%d): ASSERT: %s\n", pFileName, lineNum, pExpression);159msgBuf[MAX_MESSAGE_LEN - 2] = '\n';160msgBuf[MAX_MESSAGE_LEN - 1] = 0;161OutputDebugStringA(msgBuf);162163sprintf_s(msgBuf, "\t%s\n", pFunction);164msgBuf[MAX_MESSAGE_LEN - 2] = '\n';165msgBuf[MAX_MESSAGE_LEN - 1] = 0;166OutputDebugStringA(msgBuf);167168int offset = 0;169170if (pFmtString)171{172va_list args;173va_start(args, pFmtString);174offset = _vsnprintf_s(msgBuf, sizeof(msgBuf), sizeof(msgBuf), pFmtString, args);175va_end(args);176177if (offset < 0)178{179return true;180}181182OutputDebugStringA("\t");183OutputDebugStringA(msgBuf);184OutputDebugStringA("\n");185}186187if (enabled && KNOB_ENABLE_ASSERT_DIALOGS)188{189int retval = sprintf_s(&msgBuf[offset],190MAX_MESSAGE_LEN - offset,191"\n\n"192"File: %s\n"193"Line: %d\n"194"\n"195"Expression: %s\n\n"196"Cancel: Disable this assert for the remainder of the process\n"197"Try Again: Break into the debugger\n"198"Continue: Continue execution (but leave assert enabled)",199pFileName,200lineNum,201pExpression);202203if (retval < 0)204{205return true;206}207208offset += retval;209210if (!IsDebuggerPresent())211{212sprintf_s(&msgBuf[offset],213MAX_MESSAGE_LEN - offset,214"\n\n*** NO DEBUGGER DETECTED ***\n\nPressing \"Try Again\" will cause a "215"program crash!");216}217218retval = MessageBoxA(nullptr,219msgBuf,220"Assert Failed",221MB_CANCELTRYCONTINUE | MB_ICONEXCLAMATION | MB_SETFOREGROUND);222223switch (retval)224{225case IDCANCEL:226enabled = false;227return false;228229case IDTRYAGAIN:230return true;231232case IDCONTINUE:233return false;234}235}236else237{238return (IsDebuggerPresent() || !chkDebugger) && enabled;239}240#endif // _WIN32241242return enabled;243}244245void SwrTrace(246const char* pFileName, uint32_t lineNum, const char* pFunction, const char* pFmtString, ...)247{248using namespace ConsoleUtils;249std::lock_guard<std::mutex> l(g_stderrMutex);250251SetTextColor(stderr, TextColor::CYAN, TextStyle::NORMAL);252253fprintf(stderr, "%s(%d): TRACE in %s:\n", pFileName, lineNum, pFunction);254255if (pFmtString)256{257SetTextColor(stderr, TextColor::PURPLE, TextStyle::INTENSITY);258fprintf(stderr, "\t");259va_list args;260va_start(args, pFmtString);261vfprintf(stderr, pFmtString, args);262va_end(args);263fprintf(stderr, "\n");264}265ResetTextColor(stderr);266fflush(stderr);267268#if defined(_WIN32)269static const int MAX_MESSAGE_LEN = 2048;270char msgBuf[MAX_MESSAGE_LEN];271272sprintf_s(msgBuf, "%s(%d): TRACE in %s\n", pFileName, lineNum, pFunction);273msgBuf[MAX_MESSAGE_LEN - 2] = '\n';274msgBuf[MAX_MESSAGE_LEN - 1] = 0;275OutputDebugStringA(msgBuf);276277int offset = 0;278279if (pFmtString)280{281va_list args;282va_start(args, pFmtString);283offset = _vsnprintf_s(msgBuf, sizeof(msgBuf), sizeof(msgBuf), pFmtString, args);284va_end(args);285286if (offset < 0)287{288return;289}290291OutputDebugStringA("\t");292OutputDebugStringA(msgBuf);293OutputDebugStringA("\n");294}295#endif // _WIN32296}297298#endif // SWR_ENABLE_ASSERTS299300301