VICI  0.11.815
Visual Chart Interpreter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
vx.h
Go to the documentation of this file.
1 /*
2  * vx.h
3  *
4  * Copyright 2001 - 2016 Brenton Ross
5  *
6  * This file is part of VICI.
7  *
8  * VICI is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * VICI is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with VICI. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef VICI_VX_H
28 #define VICI_VX_H
29 
30 #include <stdexcept>
31 #include <string>
32 #include <sstream>
33 #include <vector>
34 
35 // these included here since its normal to include the reason for the failure
36 // by calling strerror(errno)
37 #include <errno.h>
38 #include <string.h>
39 
40 // ------------------------------------------------------------------
41 
42 namespace VICI
43 {
44 
45 // ------------------------------------------------------------------
46 
48 enum Severity {
53  Code,
56  Info,
58 };
59 
60 // ------------------------------------------------------------------
61 
63 
78 template< typename N >
79 class vxt : public std::runtime_error
80 {
81  // Tests - cfitest - ExceptionTestCase
82 protected:
84  std::string buff;
85  std::string file;
86  int line;
87  int ptr;
88 
89 public:
91 
94  explicit vxt( Severity s )
95  : runtime_error("vici error"), mSeverity(s), line(0)
96  {
97  buff = sevToString(s) + ": ";
98  ptr = 0;
99  }
100 
102 
107  vxt( Severity s, const std::string &f, int ln )
108  : runtime_error("vici error"), mSeverity(s), file(f), line(ln)
109  {
110  buff = sevToString(s) + ": ";
111  ptr = buff.length();
112  if ( ! file.empty() )
113  {
114  std::ostringstream ss;
115  ss << " [" << file << ":" << line << "]";
116  buff.append( ss.str() );
117  file.clear();
118  }
119  }
120 
121 
122  //
123  // Use this one like this:
124  // vx x; throw x << "Message" << value << etc;
125  // or
126  // throw vx( VICI::Alert ) << "message " << strerror( errno );
127  //
128 
130  template < class T >
131  vxt & operator << ( T x )
132  {
133  std::ostringstream ss;
134  ss << x;
135  if ( ptr )
136  {
137  buff.insert( ptr, ss.str() );
138  ptr += ss.str().length();
139  }
140  else
141  {
142  buff.append( ss.str() );
143  }
144 
145  return *this;
146  }
147 
150 
152  virtual const char *what() const throw()
153  {
154  return buff.c_str();
155  }
156 
158  static const std::string & sevToString( VICI::Severity s)
159  {
160  static std::vector< std::string >snames;
161  if ( snames.size() == 0)
162  {
163  snames.push_back( "Emergency" );
164  snames.push_back( "Alert" );
165  snames.push_back( "Critical" );
166  snames.push_back( "Error" );
167  snames.push_back( "Code" );
168  snames.push_back( "Warning" );
169  snames.push_back( "Notice" );
170  snames.push_back( "Info" );
171  snames.push_back( "Debug" );
172  }
173  return snames[(int)s];
174  }
175 
178 
181  //void log( Severity sev )
182  //{ doLog( sev ); }
183 
186  //void log()
187  //{ doLog( mSeverity ); }
188 
191 
194  //void log( const std::string &msg )
195  //{ doLog( mSeverity, msg ); }
196 };
197 
198 // ----------------------------------------------------
199 
200 class vici_class; // matches the namespace
201 
202 }; // namespace
203 
206 
208 // defined method so that file and line number are passed correctly.
209 // typically used like: throw VX(Error) << "message: " << strerror(errno);
210 #define VX(x) vx(x, __FILE__, __LINE__ )
211 
212 #endif // VX_H
213 
214 // ----------------------------- end --------------------------------
215 
An exception object with severity levels.
Definition: vx.h:79
vxt(Severity s, const std::string &f, int ln)
constructor
Definition: vx.h:107
std::string buff
message built into this
Definition: vx.h:84
Severity
Severity levels for log messages.
Definition: vx.h:48
std::string file
source file from which it was thrown
Definition: vx.h:85
A programming error has been detected.
Definition: vx.h:53
virtual const char * what() const
get the message from the exception
Definition: vx.h:152
vxt & operator<<(T x)
Provide stream syntax for the exception object.
Definition: vx.h:131
static const std::string & sevToString(VICI::Severity s)
Convert a severity into a string.
Definition: vx.h:158
The program cannot continue.
Definition: vx.h:52
A configuration error has been detected.
Definition: vx.h:50
Severity severity()
get the severity of the exception
Definition: vx.h:149
The program cannot continue and may have corrupted its data.
Definition: vx.h:51
Debugging messages.
Definition: vx.h:57
A fault has been detected which may compromise the computer.
Definition: vx.h:49
Something is odd.
Definition: vx.h:55
vxt(Severity s)
constructor
Definition: vx.h:94
Severity mSeverity
severity level of the exception
Definition: vx.h:83
There is a problem but the program can continue.
Definition: vx.h:54
Information messages.
Definition: vx.h:56
int line
line number in the source file
Definition: vx.h:86
int ptr
insertion point for message text
Definition: vx.h:87