-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex.cpp
170 lines (143 loc) · 3.95 KB
/
Ex.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
#include <Windows.h>
#include "Ex\Ex.h"
#include "Ex\ConsoleLogger.h"
#include <iostream>
bool Ex::Exception::s_variableData = true;
bool Ex::False()
{
return false;
}
/*
* The Exception contructor.
*/
Ex::Exception::Exception( Exception* pExceptionToNest )
: m_pNestedException( pExceptionToNest ? pExceptionToNest->Clone() : ExceptionPtr() ),
m_stack( ),
m_stream( )
{
}
/*
* The Exception copy contructor.
*/
Ex::Exception::Exception( const Exception& exceptionToCopy )
: m_pNestedException( exceptionToCopy.m_pNestedException ),
m_stack( exceptionToCopy.m_stack ),
m_stream( exceptionToCopy.m_stream.str() )
{
}
/*
* AsString()
*
* Returns a string representaion of the Exception.
*/
std::string Ex::Exception::AsString() const
{
std::stringstream stream;
stream << GetName() << " : " << m_stream.str() << m_stack;
if ( m_pNestedException.get() != 0 )
{
stream << std::endl << "Caused by: ";
m_pNestedException->StreamInto( stream );
}
return stream.str();
}
/*
* Log()
*
* Logs the Exception to the default logger with a message.
*/
void Ex::Exception::Log( const std::string& message ) const
{
Logger::GetDefaultLogger().Log( message + "\n" + AsString() );
}
/*
* SafeLog()
*
* Logs the Exception to the event log with a message. This version
* of logging does not throw exceptions and is intended to be used in
* destructors.
*
*/
void Ex::Exception::SafeLog( const std::string& message ) const
{
try
{
Log( message );
}
catch( Exception& )
{
// don't do anything in this case (we are in a destructor).
}
catch ( exception& )
{
// don't do anything in this case (we are in a destructor).
}
}
/*
* StreamInto()
*
* Streams a string representaion of the Exception into stream.
*/
void Ex::Exception::StreamInto( std::stringstream& stream ) const
{
stream << AsString();
}
/*
* LocationAsString
*/
std::string Ex::Exception::LocationAsString( const char* fileName, const char* functionName, int lineNumber )
{
std::string fileNameString;
if ( !GetVariableData() )
{
lineNumber = 0;
char *p = strrchr( fileName, '\\' ); // get last backslash
if ( p )
{
fileNameString = p + 1;
}
else
{
fileNameString = fileName;
}
strlwr( const_cast< char* > ( fileNameString.c_str() ) );
}
else
{
fileNameString = fileName;
}
std::stringstream fileAndLine;
fileAndLine << functionName << " (" << fileNameString << ":" << lineNumber << ")";
return fileAndLine.str();
}
/*
* AddStackFrame
*/
void Ex::Exception::AddStackFrame( const char* fileName, const char* functionName, int line )
{
m_stack = std::string( "\n at " ) + LocationAsString( fileName, functionName, line ) + m_stack;
}
/*
* STL exception wrapper.
*/
Ex::StlException::StlException( const std::string& what, Exception* pExecptionToNest )
: NamedException( pExecptionToNest )
{
m_stream << "STL Exception \"" << what << "\"";
}
/*
* Assertion programmer error exception.
*/
Ex::Assert::Assert( const char* predicate, const char* fileName, const char* functionName, int line, Exception* pExecptionToNest )
: ProgrammerException( pExecptionToNest )
{
m_stream << "Assert( \"" << predicate << "\" ) failed in file " << LocationAsString( fileName, functionName, line );
}
/*
* Precondition programmer error exception.
*/
Ex::Precondition::Precondition( const char* predicate, const char* fileName, const char* functionName, int line, Exception* pExecptionToNest )
: ProgrammerException( pExecptionToNest )
{
m_stream << "Precondition( \"" << predicate << "\" ) failed in file " << LocationAsString( fileName, functionName, line );
}