]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_assert.cpp
Initial vogl checkin
[vogl] / src / voglcore / vogl_assert.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_assert.cpp
28 #include "vogl_core.h"
29 #include "vogl_strutils.h"
30 #include "vogl_backtrace.h"
31
32 #ifdef VOGL_USE_WIN32_API
33 #include "vogl_winhdr.h"
34 #endif
35
36 #if VOGL_ENABLE_ASSERTIONS_IN_ALL_BUILDS &&(defined(NDEBUG) || defined(VOGL_BUILD_RELEASE))
37 #warning Assertions enabled in release build
38 #endif
39
40 static bool g_fail_exceptions;
41 static bool g_exit_on_failure = true;
42
43 void vogl_enable_fail_exceptions(bool enabled)
44 {
45     // Make sure off_t is ALWAYS 64-bits.
46     VOGL_ASSUME(sizeof(off_t) == sizeof(uint64_t));
47
48     g_fail_exceptions = enabled;
49 }
50
51 void vogl_assert(const char *pExp, const char *pFile, unsigned line)
52 {
53     char buf[512];
54
55     vogl::vogl_sprintf_s(buf, sizeof(buf), "%s(%u): Assertion failed: \"%s\"\n", pFile, line, pExp);
56
57     vogl_output_debug_string(buf);
58     fputs(buf, stderr);
59
60 #if 0
61         vogl::dynamic_string_array backtrace;
62         if (get_printable_backtrace(backtrace))
63         {
64                 vogl_output_debug_string("Backtrace:");
65                 for (uint i = 0; i < backtrace.size(); i++)
66                         vogl_output_debug_string(backtrace[i].get_ptr());
67         }
68 #endif
69
70     if (vogl_is_debugger_present())
71         vogl_debug_break();
72 }
73
74 void vogl_fail(const char *pExp, const char *pFile, unsigned line)
75 {
76     char buf[512];
77
78     vogl::vogl_sprintf_s(buf, sizeof(buf), "%s(%u): Failure: \"%s\"\n", pFile, line, pExp);
79
80     vogl_output_debug_string(buf);
81     fputs(buf, stderr);
82
83     vogl::dynamic_string_array backtrace;
84     if (get_printable_backtrace(backtrace))
85     {
86         vogl_output_debug_string("Backtrace:");
87         for (uint i = 0; i < backtrace.size(); i++)
88             vogl_output_debug_string(backtrace[i].get_ptr());
89     }
90
91     if (vogl_is_debugger_present())
92         vogl_debug_break();
93
94 #ifdef VOGL_USE_WIN32_API
95     if (g_fail_exceptions)
96         RaiseException(VOGL_FAIL_EXCEPTION_CODE, 0, 0, NULL);
97     else
98 #endif
99         if (g_exit_on_failure)
100         exit(EXIT_FAILURE);
101 }
102
103 void trace(const char *pFmt, va_list args)
104 {
105     if (vogl_is_debugger_present())
106     {
107         char buf[512];
108         vogl::vogl_vsprintf_s(buf, sizeof(buf), pFmt, args);
109
110         vogl_output_debug_string(buf);
111     }
112 };
113
114 void trace(const char *pFmt, ...)
115 {
116     va_list args;
117     va_start(args, pFmt);
118     trace(pFmt, args);
119     va_end(args);
120 };