SASSY  0.0
Software Architecture Support System
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sx.h
Go to the documentation of this file.
1 /*
2  * sx.h
3  *
4  * Copyright 2001 - 2017 Brenton Ross
5  *
6  * This file is part of SASSY.
7  *
8  * SASSY 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  * SASSY 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 SASSY. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef SASSY_SX_H
28 #define SASSY_SX_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 SASSY
43 {
44 
45 // ------------------------------------------------------------------
46 
48 enum Severity {
53  Code,
56  Info,
58 };
59 
60 // ------------------------------------------------------------------
61 
63 
78 template< typename N >
79 class sxt : public std::runtime_error
80 {
81 protected:
83  std::string buff;
84  std::string file;
85  int line;
86  int ptr;
87 
88 public:
90 
93  explicit sxt( Severity s )
94  : runtime_error("sassy error"), mSeverity(s), line(0)
95  {
96  buff = sevToString(s) + ": ";
97  ptr = 0;
98  }
99 
101 
106  sxt( Severity s, const std::string &f, int ln )
107  : runtime_error("sassy error"), mSeverity(s), file(f), line(ln)
108  {
109  buff = sevToString(s) + ": ";
110  ptr = buff.length();
111  if ( ! file.empty() )
112  {
113  std::ostringstream ss;
114  ss << " [" << file << ":" << line << "]";
115  buff.append( ss.str() );
116  file.clear();
117  }
118  }
119 
120 
121  //
122  // Use this one like this:
123  // sx x; throw x << "Message" << value << etc;
124  // or
125  // throw sx( SASSY::Alert ) << "message " << strerror( errno );
126  //
127 
129  template < class T >
130  sxt & operator << ( T x )
131  {
132  std::ostringstream ss;
133  ss << x;
134  if ( ptr )
135  {
136  buff.insert( ptr, ss.str() );
137  ptr += ss.str().length();
138  }
139  else
140  {
141  buff.append( ss.str() );
142  }
143 
144  return *this;
145  }
146 
149 
151  virtual const char *what() const throw()
152  {
153  return buff.c_str();
154  }
155 
157  static const std::string & sevToString( SASSY::Severity s)
158  {
159  static std::vector< std::string >snames;
160  if ( snames.size() == 0)
161  {
162  snames.push_back( "Emergency" );
163  snames.push_back( "Alert" );
164  snames.push_back( "Critical" );
165  snames.push_back( "Error" );
166  snames.push_back( "Code" );
167  snames.push_back( "Warning" );
168  snames.push_back( "Notice" );
169  snames.push_back( "Info" );
170  snames.push_back( "Debug" );
171  }
172  return snames[(int)s];
173  }
174 
177 
180  //void log( Severity sev )
181  //{ doLog( sev ); }
182 
185  //void log()
186  //{ doLog( mSeverity ); }
187 
190 
193  //void log( const std::string &msg )
194  //{ doLog( mSeverity, msg ); }
195 };
196 
197 // ----------------------------------------------------
198 
199 class sassy_class; // matches the namespace
200 
201 }; // namespace
202 
205 
207 // defined method so that file and line number are passed correctly.
208 // typically used like: throw SX(Error) << "message: " << strerror(errno);
209 #define SX(x) sx(x, __FILE__, __LINE__ )
210 
211 #endif // SX_H
212 
213 // ----------------------------- end --------------------------------
214 
Information messages.
Definition: sx.h:56
sxt(Severity s)
constructor
Definition: sx.h:93
sxt & operator<<(T x)
Provide stream syntax for the exception object.
Definition: sx.h:130
int ptr
insertion point for message text
Definition: sx.h:86
A programming error has been detected.
Definition: sx.h:53
static const std::string & sevToString(SASSY::Severity s)
Convert a severity into a string.
Definition: sx.h:157
There is a problem but the program can continue.
Definition: sx.h:54
An exception object with severity levels.
Definition: sx.h:79
Debugging messages.
Definition: sx.h:57
Something is odd.
Definition: sx.h:55
Severity severity()
get the severity of the exception
Definition: sx.h:148
std::string file
source file from which it was thrown
Definition: sx.h:84
Severity
Severity levels for log messages.
Definition: sx.h:48
Severity mSeverity
severity level of the exception
Definition: sx.h:82
A configuration error has been detected.
Definition: sx.h:50
std::string buff
message built into this
Definition: sx.h:83
The program cannot continue and may have corrupted its data.
Definition: sx.h:51
sxt(Severity s, const std::string &f, int ln)
constructor
Definition: sx.h:106
A fault has been detected which may compromise the computer.
Definition: sx.h:49
int line
line number in the source file
Definition: sx.h:85
The program cannot continue.
Definition: sx.h:52
virtual const char * what() const
get the message from the exception
Definition: sx.h:151