SASSY  0.0
Software Architecture Support System
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
except.h
Go to the documentation of this file.
1 
2 /* RDF C++ API
3  *
4  * except.h
5  *
6  * Copyright 2001 - 2017 Brenton Ross
7  *
8  * -----------------------------------------------------------------------------
9  * LICENSE
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this library. If not, see <http://www.gnu.org/licenses/>.
23  *
24  * -----------------------------------------------------------------------------
25  */
26 
27 
28 
34 #ifndef RDFXX_VX_H
35 #define RDFXX_VX_H
36 
37 #include <stdexcept>
38 #include <string>
39 #include <sstream>
40 #include <vector>
41 
42 // these included here since its normal to include the reason for the failure
43 // by calling strerror(errno)
44 #include <errno.h>
45 #include <string.h>
46 
47 // ------------------------------------------------------------------
48 
49 namespace rdf
50 {
51 
52 // ------------------------------------------------------------------
53 
55 enum Severity {
60  Code,
63  Info,
65 };
66 
67 // ------------------------------------------------------------------
68 
70 
85 template< typename N >
86 class vxt : public std::runtime_error
87 {
88 protected:
90  std::string buff;
91  std::string file;
92  int line;
93  int ptr;
94 
95 public:
97 
100  explicit vxt( Severity s )
101  : runtime_error("vici error"), mSeverity(s), line(0)
102  {
103  buff = sevToString(s) + ": ";
104  ptr = 0;
105  }
106 
108 
113  vxt( Severity s, const std::string &f, int ln )
114  : runtime_error("vici error"), mSeverity(s), file(f), line(ln)
115  {
116  buff = sevToString(s) + ": ";
117  ptr = buff.length();
118  if ( ! file.empty() )
119  {
120  std::ostringstream ss;
121  ss << " [" << file << ":" << line << "]";
122  buff.append( ss.str() );
123  file.clear();
124  }
125  }
126 
127 
128  //
129  // Use this one like this:
130  // vx x; throw x << "Message" << value << etc;
131  // or
132  // throw vx( VICI::Alert ) << "message " << strerror( errno );
133  //
134 
136  template < class T >
137  vxt & operator << ( T x )
138  {
139  std::ostringstream ss;
140  ss << x;
141  if ( ptr )
142  {
143  buff.insert( ptr, ss.str() );
144  ptr += ss.str().length();
145  }
146  else
147  {
148  buff.append( ss.str() );
149  }
150 
151  return *this;
152  }
153 
156 
158  virtual const char *what() const throw()
159  {
160  return buff.c_str();
161  }
162 
164  static const std::string & sevToString( Severity s)
165  {
166  static std::vector< std::string >snames;
167  if ( snames.size() == 0)
168  {
169  snames.push_back( "Emergency" );
170  snames.push_back( "Alert" );
171  snames.push_back( "Critical" );
172  snames.push_back( "Error" );
173  snames.push_back( "Code" );
174  snames.push_back( "Warning" );
175  snames.push_back( "Notice" );
176  snames.push_back( "Info" );
177  snames.push_back( "Debug" );
178  }
179  return snames[(int)s];
180  }
181 
182 };
183 
184 // ----------------------------------------------------
185 
186 class rdf_class; // matches the namespace
187 
188 } // namespace rdf
189 
192 
194 // defined method so that file and line number are passed correctly.
195 // typically used like: throw VX(Error) << "message: " << strerror(errno);
196 #define VX(x) vx(x, __FILE__, __LINE__ )
197 
198 #endif // VX_H
199 
200 // ----------------------------- end --------------------------------
201 
The program cannot continue.
Definition: except.h:59
Severity severity()
get the severity of the exception
Definition: except.h:155
virtual const char * what() const
get the message from the exception
Definition: except.h:158
std::string file
source file from which it was thrown
Definition: except.h:91
static const std::string & sevToString(Severity s)
Convert a severity into a string.
Definition: except.h:164
The program cannot continue and may have corrupted its data.
Definition: except.h:58
Information messages.
Definition: except.h:63
int line
line number in the source file
Definition: except.h:92
vxt(Severity s, const std::string &f, int ln)
constructor
Definition: except.h:113
A fault has been detected which may compromise the computer.
Definition: except.h:56
An exception object with severity levels.
Definition: except.h:86
vxt & operator<<(T x)
Provide stream syntax for the exception object.
Definition: except.h:137
vxt(Severity s)
constructor
Definition: except.h:100
Severity mSeverity
severity level of the exception
Definition: except.h:89
A programming error has been detected.
Definition: except.h:60
A configuration error has been detected.
Definition: except.h:57
Severity
Severity levels for log messages.
Definition: except.h:55
std::string buff
message built into this
Definition: except.h:90
There is a problem but the program can continue.
Definition: except.h:61
Something is odd.
Definition: except.h:62
int ptr
insertion point for message text
Definition: except.h:93
Debugging messages.
Definition: except.h:64