forked from HeliumProject/Foundation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exception.cpp
189 lines (147 loc) · 4.63 KB
/
Exception.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "FoundationPch.h"
#include "Exception.h"
#include "Platform/Assert.h"
#include "Platform/Encoding.h"
#include "Platform/Console.h"
#include "Foundation/Log.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <fstream>
#include <sys/stat.h>
using namespace Helium;
BreakpointSignature::Delegate Helium::g_BreakpointOccurred;
ExceptionSignature::Delegate Helium::g_ExceptionOccurred;
TerminateSignature::Event Helium::g_Terminating;
bool g_EnableExceptionFilter = false;
#if HELIUM_OS_WIN
static LONG __stdcall ProcessFilteredException( LPEXCEPTION_POINTERS info )
{
return Helium::ProcessException(info, true, true);
}
#endif
void Helium::EnableExceptionFilter(bool enable)
{
#if HELIUM_OS_WIN
g_EnableExceptionFilter = enable;
// handles an exception occuring in the process not handled by a user exception handler
SetUnhandledExceptionFilter( g_EnableExceptionFilter ? &ProcessFilteredException : NULL );
#endif
}
void Helium::ProcessException(const Helium::Exception& exception, bool print, bool fatal)
{
#if HELIUM_OS_WIN
SetUnhandledExceptionFilter( NULL );
#endif
ExceptionArgs args( ExceptionTypes::CPP, fatal );
const char* cppClass = "Unknown";
#ifdef _CPPRTTI
try
{
cppClass = typeid(exception).name();
}
catch (...)
{
}
#else
cppClass = "Unknown, no RTTI";
#endif
bool converted = Helium::ConvertString( exception.What(), args.m_Message );
HELIUM_ASSERT( converted );
converted = Helium::ConvertString( cppClass, args.m_CPPClass );
HELIUM_ASSERT( converted );
args.m_State = Log::GetOutlineState();
if (print)
{
Helium::Print(Helium::ConsoleColors::Red, stderr, TXT( "An exception has occurred\nType: C++ Exception\n Class: %s\n Message: %s\n" ), args.m_CPPClass.c_str(), args.m_Message.c_str() );
}
g_ExceptionOccurred.Invoke( args );
if ( fatal )
{
g_Terminating.Raise( TerminateArgs () );
EnableExceptionFilter( false );
}
else if ( g_EnableExceptionFilter )
{
#if HELIUM_OS_WIN
SetUnhandledExceptionFilter( &ProcessFilteredException );
#endif
}
}
void Helium::ProcessException(const std::exception& exception, bool print, bool fatal)
{
#if HELIUM_OS_WIN
SetUnhandledExceptionFilter( NULL );
#endif
ExceptionArgs args( ExceptionTypes::CPP, fatal );
const char* cppClass = "Unknown";
#ifdef _CPPRTTI
try
{
cppClass = typeid(exception).name();
}
catch (...)
{
}
#else
cppClass = "Unknown, no RTTI";
#endif
bool converted = Helium::ConvertString( exception.what(), args.m_Message );
HELIUM_ASSERT( converted );
converted = Helium::ConvertString( cppClass, args.m_CPPClass );
HELIUM_ASSERT( converted );
args.m_State = Log::GetOutlineState();
if (print)
{
Helium::Print(Helium::ConsoleColors::Red, stderr, TXT( "An exception has occurred\nType: C++ Exception\n Class: %s\n Message: %s\n" ), args.m_CPPClass.c_str(), args.m_Message.c_str() );
}
g_ExceptionOccurred.Invoke( args );
if ( fatal )
{
g_Terminating.Raise( TerminateArgs () );
EnableExceptionFilter( false );
}
else if ( g_EnableExceptionFilter )
{
#if HELIUM_OS_WIN
SetUnhandledExceptionFilter( &ProcessFilteredException );
#endif
}
}
#if HELIUM_OS_WIN
uint32_t Helium::ProcessException(LPEXCEPTION_POINTERS info, bool print, bool fatal)
{
SetUnhandledExceptionFilter( NULL );
// handle breakpoint exceptions outside the debugger
if ( !::IsDebuggerPresent()
&& info->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT
&& g_BreakpointOccurred.Valid() )
{
BreakpointArgs args (info, fatal);
g_BreakpointOccurred.Invoke( args );
return args.m_Result;
}
else
{
ExceptionArgs args ( ExceptionTypes::Structured, fatal );
args.m_State = Log::GetOutlineState();
Helium::GetExceptionDetails( info, args );
if ( print )
{
Helium::Print( Helium::ConsoleColors::Red, stderr, TXT( "%s" ), GetExceptionInfo( info ).c_str() );
}
g_ExceptionOccurred.Invoke( args );
if ( fatal )
{
g_Terminating.Raise( TerminateArgs () );
EnableExceptionFilter( false );
}
}
if ( g_EnableExceptionFilter )
{
SetUnhandledExceptionFilter( &ProcessFilteredException );
}
return EXCEPTION_CONTINUE_SEARCH;
}
#endif