SheafSystem  0.0.0.0
error_message.cc
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Implementation for class error_message.
19 
20 #include "SheafSystem/error_message.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/std_iostream.h"
24 #include "SheafSystem/std_sstream.h"
25 #include "SheafSystem/std_stdexcept.h"
26 
27 using namespace std;
28 
29 // ===========================================================
30 // ERROR_MESSAGE FACET
31 // ===========================================================
32 
36 {
37 
38  // Preconditions:
39 
40 
41  // Body:
42 
43  // Source is empty.
44  // Text is empty
45 
46  _level = INFORMATION;
47 
48  // Postconditions:
49 
50  // ensure(invariant());
51  ensure(source() == "");
52  ensure(text() == "");
53  ensure(level() == INFORMATION);
54 
55  // Exit:
56 
57  return;
58 }
59 
60 
64 {
65 
66  // Preconditions:
67 
68 
69  // Body:
70 
71  *this = xother;
72 
73  // Postconditions:
74 
75  // ensure(invariant());
76  ensure(text() == xother.text());
77  ensure(level() == xother.level());
78 
79  // Exit:
80 
81  return;
82 }
83 
86 operator=(const error_message& xother)
87 {
88  // Preconditions:
89 
90  // Body:
91 
92  if(this != &xother)
93  {
94  _source = xother.source();
95  _text = xother.text();
96  _level = xother.level();
97  }
98 
99  // Postconditions:
100 
101  //ensure(invariant());
102  ensure(source() == xother.source());
103  ensure(text() == xother.text());
104  ensure(level() == xother.level());
105 
106  // Exit:
107 
108  return *this;
109 }
110 
114 {
115 
116  // Preconditions:
117 
118 
119  // Body:
120 
121  // No action required
122 
123  // Postconditions:
124 
125  // ensure(invariant());
126 
127  // Exit:
128 
129  return;
130 }
131 
134 error_message(level_type xlevel, const std::string& xsource, const std::string& xtext)
135 {
136 
137  // Preconditions:
138 
139  require(xlevel != LEVEL_TYPE_UB);
140  require(!xsource.empty());
141  require(!xtext.empty());
142 
143  // Body:
144 
145  _level = xlevel;
146  _source = xsource;
147  _text = xtext;
148 
149  // Postconditions:
150 
151  // ensure(invariant());
152  ensure(level() == xlevel);
153  ensure(source() == xsource);
154  ensure(text() == xtext);
155 
156  // Exit:
157 
158  return;
159 }
160 
163 error_message(level_type xlevel, const std::string& xfile, int xline, const std::string& xtext)
164 {
165 
166  // Preconditions:
167 
168  require(xlevel != LEVEL_TYPE_UB);
169  require(!xfile.empty());
170  // require(!xtext.empty());
171 
172  // Body:
173 
174  _level = xlevel;
175  _source = xfile;
176  stringstream lstrm;
177  lstrm << xline;
178  string lline;
179  lstrm >> lline;
180  _source += " at line ";
181  _source += lline;
182  _text = xtext;
183 
184  // Postconditions:
185 
186  // ensure(invariant());
187  ensure(level() == xlevel);
188  ensure(text() == xtext);
189 
190  // Exit:
191 
192  return;
193 }
194 
195 
197 std::string
199 source() const
200 {
201  return _source;
202 }
203 
205 std::string
207 text() const
208 {
209  return _text;
210 }
211 
215 level() const
216 {
217  return _level;
218 }
219 
221 void
223 post(bool xforce_exit) const
224 {
225  // Preconditions:
226 
227 
228  // Body:
229 
232 
233  cerr << level_name(_level) << ": "
234  << "In " << _source << ": "
235  << _text
236  << endl;
237 
238  if(xforce_exit)
239  {
240  //exit(_level);
241  throw std::logic_error(level_name(_level));
242  }
243 
244  // Postconditions:
245 
246 
247  // Exit:
248 
249  return;
250 }
251 
253 const std::string&
256 {
257  // Preconditions:
258 
259  require((0 <= xlevel ) && (xlevel < LEVEL_TYPE_UB));
260 
261  // Body:
262 
263  static const string names[LEVEL_TYPE_UB] =
264  {
265  "UNSPECIFIED",
266  "INFORMATION",
267  "WARNING",
268  "SEVERE ERROR",
269  "FATAL ERROR"
270  };
271 
272  const string& result = names[xlevel];
273 
274  // Postconditions:
275 
276  ensure(!result.empty());
277 
278  // Exit:
279 
280  return result;
281 }
282 
283 // ===========================================================
284 // NON-MEMBER FUNCTIONS
285 // ===========================================================
286 
288 std::ostream&
289 sheaf::operator<<(std::ostream& xos, const error_message& xmsg)
290 {
291  // Preconditions:
292 
293 
294  // Body:
295 
296  if(xmsg.level() != error_message::UNSPECIFIED)
297  {
298  xos << error_message::level_name(xmsg.level()) << ": ";
299  }
300 
301  xos << "In " << xmsg.source() << ": " << xmsg.text();
302 
303  // Postconditions:
304 
305 
306  // Exit:
307 
308  return xos;
309 }
void post(bool xforce_exit=false) const
Registers this message with the message handling subsystem.
virtual ~error_message()
Destructor.
std::string text() const
The text of this message.
STL namespace.
level_type
The set of predefined error levels.
Definition: error_message.h:69
std::string source() const
The source of this message.
error_message()
Default constructor.
level_type level() const
The level of this message.
SHEAF_DLL_SPEC std::ostream & operator<<(std::ostream &os, const dof_descriptor_array &p)
Insert dof_descriptor_array& p into ostream& os.
error_message & operator=(const error_message &xother)
Assignment operator.
static const std::string & level_name(level_type xlevel)
The name of level xlevel.
A message to report error conditions.
Definition: error_message.h:37