1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_assert.cpp
28 #include "vogl_core.h"
29 #include "vogl_strutils.h"
30 #include "vogl_backtrace.h"
32 #ifdef VOGL_USE_WIN32_API
33 #include "vogl_winhdr.h"
36 #if VOGL_ENABLE_ASSERTIONS_IN_ALL_BUILDS &&(defined(NDEBUG) || defined(VOGL_BUILD_RELEASE))
37 #warning Assertions enabled in release build
40 static bool g_fail_exceptions;
41 static bool g_exit_on_failure = true;
43 void vogl_enable_fail_exceptions(bool enabled)
45 // Make sure off_t is ALWAYS 64-bits.
46 VOGL_ASSUME(sizeof(off_t) == sizeof(uint64_t));
48 g_fail_exceptions = enabled;
51 void vogl_assert(const char *pExp, const char *pFile, unsigned line)
55 vogl::vogl_sprintf_s(buf, sizeof(buf), "%s(%u): Assertion failed: \"%s\"\n", pFile, line, pExp);
57 vogl_output_debug_string(buf);
61 vogl::dynamic_string_array backtrace;
62 if (get_printable_backtrace(backtrace))
64 vogl_output_debug_string("Backtrace:");
65 for (uint i = 0; i < backtrace.size(); i++)
66 vogl_output_debug_string(backtrace[i].get_ptr());
70 if (vogl_is_debugger_present())
74 void vogl_fail(const char *pExp, const char *pFile, unsigned line)
78 vogl::vogl_sprintf_s(buf, sizeof(buf), "%s(%u): Failure: \"%s\"\n", pFile, line, pExp);
80 vogl_output_debug_string(buf);
83 vogl::dynamic_string_array backtrace;
84 if (get_printable_backtrace(backtrace))
86 vogl_output_debug_string("Backtrace:");
87 for (uint i = 0; i < backtrace.size(); i++)
88 vogl_output_debug_string(backtrace[i].get_ptr());
91 if (vogl_is_debugger_present())
94 #ifdef VOGL_USE_WIN32_API
95 if (g_fail_exceptions)
96 RaiseException(VOGL_FAIL_EXCEPTION_CODE, 0, 0, NULL);
99 if (g_exit_on_failure)
103 void trace(const char *pFmt, va_list args)
105 if (vogl_is_debugger_present())
108 vogl::vogl_vsprintf_s(buf, sizeof(buf), pFmt, args);
110 vogl_output_debug_string(buf);
114 void trace(const char *pFmt, ...)
117 va_start(args, pFmt);