OpenWalnut  1.5.0dev
WException.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WEXCEPTION_H
26 #define WEXCEPTION_H
27 
28 #include <exception>
29 #include <list>
30 #include <string>
31 
32 #include "WTerminalColor.h"
33 
34 
35 /**
36  * Basic exception handler.
37  */
38 class WException: public std::exception
39 {
40 /**
41  * Only UnitTests are allowed to be a friend of this class.
42  */
43 friend class WExceptionTest;
44 
45 public:
46  /**
47  * Default constructor.
48  * \param msg Exception description.
49  */
50  explicit WException( const std::string& msg = std::string() );
51 
52  /**
53  * Copy a std::exception and encapsulate it.
54  *
55  * \param e the exception.
56  */
57  explicit WException( const std::exception& e );
58 
59  /**
60  * Destructor.
61  */
62  virtual ~WException() throw();
63 
64  /**
65  * Returns the message string set on throw
66  * \return Message text
67  */
68  virtual const char* what() const throw();
69 
70  /**
71  * Prints the trace of the call chain which caused this exception.
72  * \return Calltrace as string
73  * \note Isn't this useless? Should be removed.
74  */
75  std::string getTrace( ) const;
76 
77  /**
78  * Returns a call stacktrace.
79  * \return The backtrace at the moment of "throw".
80  */
81  std::string getBacktrace() const;
82 
83  /**
84  * Function disables backtraces. Please note that the backtrace can not be reactivated to avoid people from dis/enabling them
85  * at will.
86  */
87  static void disableBacktrace();
88 
89 protected:
90  /**
91  * Message given during throw.
92  */
93  std::string m_msg;
94 
95  /**
96  * Stack trace for identifying the source where this exception came from.
97  * \note Isn't this useless? Should be removed.
98  */
99  std::list< std::string > m_trace;
100 
101  /**
102  * True if the backtrace should NOT be printed.
103  */
104  static bool noBacktrace;
105 private:
106  /**
107  * Color used for the "trace:" label.
108  */
110 
111  /**
112  * Color used for function name.
113  */
115 
116  /**
117  * Color used for symbols.
118  */
120 
121  /**
122  * Color used for exception headline.
123  */
125 };
126 
127 #endif // WEXCEPTION_H
128 
Test WException.
Basic exception handler.
Definition: WException.h:39
static bool noBacktrace
True if the backtrace should NOT be printed.
Definition: WException.h:104
WTerminalColor m_headlineColor
Color used for exception headline.
Definition: WException.h:124
std::list< std::string > m_trace
Stack trace for identifying the source where this exception came from.
Definition: WException.h:99
static void disableBacktrace()
Function disables backtraces.
Definition: WException.cpp:200
WException(const std::string &msg=std::string())
Default constructor.
Definition: WException.cpp:50
std::string m_msg
Message given during throw.
Definition: WException.h:93
std::string getBacktrace() const
Returns a call stacktrace.
Definition: WException.cpp:107
WTerminalColor m_functionColor
Color used for function name.
Definition: WException.h:114
virtual ~WException()
Destructor.
Definition: WException.cpp:85
virtual const char * what() const
Returns the message string set on throw.
Definition: WException.cpp:90
WTerminalColor m_symbolColor
Color used for symbols.
Definition: WException.h:119
WTerminalColor m_labelColor
Color used for the "trace:" label.
Definition: WException.h:109
std::string getTrace() const
Prints the trace of the call chain which caused this exception.
Definition: WException.cpp:96
Helper class to provide a convenient way to colorize output on the console.