OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 #include <sstream>
32 
33 #include "WTerminalColor.h"
34 #include "WExportCommon.h"
35 
36 /**
37  * Basic exception handler.
38  */
39 class OWCOMMON_EXPORT WException: public std::exception
40 {
41 /**
42  * Only UnitTests are allowed to be a friend of this class.
43  */
44 friend class WExceptionTest;
45 
46 public:
47 
48  /**
49  * Default constructor.
50  * \param msg Exception description.
51  */
52  explicit WException( const std::string& msg = std::string() );
53 
54  /**
55  * Copy a std::exception and encapsulate it.
56  *
57  * \param e the exception.
58  */
59  explicit WException( const std::exception& e );
60 
61  /**
62  * Destructor.
63  */
64  virtual ~WException() throw();
65 
66  /**
67  * Returns the message string set on throw
68  * \return Message text
69  */
70  virtual const char* what() const throw();
71 
72  /**
73  * Prints the trace of the call chain which caused this exception.
74  * \return Calltrace as string
75  * \note Isn't this useless? Should be removed.
76  */
77  std::string getTrace( ) const;
78 
79  /**
80  * Returns a call stacktrace.
81  * \return The backtrace at the moment of "throw".
82  */
83  std::string getBacktrace() const;
84 
85  /**
86  * Function disables backtraces. Please note that the backtrace can not be reactivated to avoid people from dis/enabling them
87  * at will.
88  */
89  static void disableBacktrace();
90 
91 protected:
92  /**
93  * Message given during throw.
94  */
95  std::string m_msg;
96 
97  /**
98  * Stack trace for identifying the source where this exception came from.
99  * \note Isn't this useless? Should be removed.
100  */
101  std::list< std::string > m_trace;
102 
103  /**
104  * True if the backtrace should NOT be printed.
105  */
106  static bool noBacktrace;
107 private:
108 
109  /**
110  * Color used for the "trace:" label.
111  */
112  WTerminalColor m_labelColor;
113 
114  /**
115  * Color used for function name.
116  */
117  WTerminalColor m_functionColor;
118 
119  /**
120  * Color used for symbols.
121  */
122  WTerminalColor m_symbolColor;
123 
124  /**
125  * Color used for exception headline.
126  */
127  WTerminalColor m_headlineColor;
128 };
129 
130 #endif // WEXCEPTION_H
131