IOSS  2.0
Ioss_SmartAssert.h
Go to the documentation of this file.
1 // Copyright(C) 1999-2020 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6 #if !defined(IOSS_SMART_ASSERT_H)
7 #define IOSS_SMART_ASSERT_H
8 
9 #if _MSC_VER > 1000
10 #pragma once
11 #endif // _MSC_VER > 1000
12 
13 #if _MSC_VER > 1000
14 
15 // note:
16 // moving this after pragma push will render it useless (VC6)
17 //
18 // identifier truncated to 255 chars in debug information
19 #pragma warning(disable : 4786)
20 
21 #pragma warning(push)
22 // *this used in base-member initialization; it's ok
23 #pragma warning(disable : 4355)
24 #endif
25 
26 #include <iostream>
27 #include <map>
28 #include <sstream>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 enum {
34 
35  // default behavior - just loges this assert
36  // (a message is shown to the user to the console)
37  lvl_warn = 100,
38 
39  // default behavior - asks the user what to do:
40  // Ignore/ Retry/ etc.
41  lvl_debug = 200,
42 
43  // default behavior - throws a smart_assert_error
44  lvl_error = 300,
45 
46  // default behavior - dumps all assert context to console,
47  // and aborts
48  lvl_fatal = 1000
49 };
50 
51 /*
52  contains details about a failed assertion
53 */
55 {
56  using string = std::string;
57 
58 public:
60 
61  // where the assertion failed: file & line
62  void set_file_line(const char *file, int line)
63  {
64  file_ = file;
65  line_ = line;
66  }
67  const string &get_context_file() const { return file_; }
68  int get_context_line() const { return line_; }
69 
70  // get/ set expression
71  void set_expr(const string &str) { expr_ = str; }
72  const string &get_expr() const { return expr_; }
73 
74  typedef std::pair<string, string> val_and_str;
75  using vals_array = std::vector<val_and_str>;
76  // return values array as a vector of pairs:
77  // [Value, corresponding string]
78  const vals_array &get_vals_array() const { return vals_; }
79  // adds one value and its corresponding string
80  void add_val(const string &val, const string &str) { vals_.push_back(val_and_str(val, str)); }
81 
82  // get/set level of assertion
83  void set_level(int nLevel) { level_ = nLevel; }
84  int get_level() const { return level_; }
85 
86  // get/set (user-friendly) message
87  void set_level_msg(const char *strMsg)
88  {
89  if (strMsg != nullptr) {
90  msg_ = strMsg;
91  }
92  else {
93  msg_.erase();
94  }
95  }
96  const string &get_level_msg() const { return msg_; }
97 
98 private:
99  // where the assertion occurred
100  string file_{};
101  int line_;
102 
103  // expression and values
104  string expr_{};
106 
107  // level and message
108  int level_;
109  string msg_{};
110 };
111 
112 namespace SmartAssert {
113 
114  using assert_func = void (*)(const assert_context &);
115 
116  // helpers
117  std::string get_typeof_level(int nLevel);
118  void dump_context_summary(const assert_context &context, std::ostream &out);
119  void dump_context_detail(const assert_context &context, std::ostream &out);
120 
121  // defaults
122  void default_warn_handler(const assert_context &context);
123  void default_debug_handler(const assert_context &context);
124  void default_error_handler(const assert_context &context);
125  void default_fatal_handler(const assert_context &context);
126  void default_logger(const assert_context &context);
127 
128 } // namespace SmartAssert
129 
130 namespace Ioss {
131  namespace Private {
132  void init_assert();
133  void set_default_log_stream(std::ostream &out);
134  void set_default_log_name(const char *str);
135 
136  // allows finding if a value is of type 'const char *'
137  // and is null; if so, we cannot print it to an ostream
138  // directly!!!
139  template <class T> struct is_null_finder
140  {
141  bool is(const T & /*unused*/) const { return false; }
142  };
143 
144  template <> struct is_null_finder<char *>
145  {
146  bool is(char *const &val) { return val == nullptr; }
147  };
148 
149  template <> struct is_null_finder<const char *>
150  {
151  bool is(const char *const &val) { return val == nullptr; }
152  };
153 
154  } // namespace Private
155 } // namespace Ioss
156 
157 struct Assert
158 {
160 
161  // helpers, in order to be able to compile the code
164 
165  explicit Assert(const char *expr)
166  : SMART_ASSERT_A(*this), SMART_ASSERT_B(*this), needs_handling_(true)
167  {
168  context_.set_expr(expr);
169 
170  if ((logger() == nullptr) || handlers().size() < 4) {
171  // used before main!
173  }
174  }
175 
176  Assert(const Assert &other)
177  : SMART_ASSERT_A(*this), SMART_ASSERT_B(*this), context_(other.context_),
178  needs_handling_(true)
179  {
180  other.needs_handling_ = false;
181  }
182 
184  {
185  if (needs_handling_) {
186  handle_assert();
187  }
188  }
189 
190  template <class type> Assert &print_current_val(const type &val, const char *my_msg)
191  {
192  std::ostringstream out;
193 
195  bool bIsNull = f.is(val);
196  if (!bIsNull) {
197  out << val;
198  }
199  else {
200  // null string
201  out << "null";
202  }
203  context_.add_val(out.str(), my_msg);
204  return *this;
205  }
206 
207  Assert &print_context(const char *file, int line)
208  {
209  context_.set_file_line(file, line);
210  return *this;
211  }
212 
213  Assert &msg(const char *strMsg)
214  {
215  context_.set_level_msg(strMsg);
216  return *this;
217  }
218 
219  Assert &level(int nLevel, const char *strMsg = nullptr)
220  {
221  context_.set_level(nLevel);
222  context_.set_level_msg(strMsg);
223  return *this;
224  }
225 
226  Assert &warn(const char *strMsg = nullptr) { return level(lvl_warn, strMsg); }
227 
228  Assert &debug(const char *strMsg = nullptr) { return level(lvl_debug, strMsg); }
229 
230  Assert &error(const char *strMsg = nullptr) { return level(lvl_error, strMsg); }
231 
232  Assert &fatal(const char *strMsg = nullptr) { return level(lvl_fatal, strMsg); }
233 
234  // in this case, we set the default logger, and make it
235  // write everything to this file
236  static void set_log(const char *strFileName)
237  {
240  }
241 
242  // in this case, we set the default logger, and make it
243  // write everything to this log
244  static void set_log(std::ostream &out)
245  {
248  }
249 
250  static void set_log(assert_func log) { logger() = log; }
251 
252  static void set_handler(int nLevel, assert_func handler) { handlers()[nLevel] = handler; }
253 
254 private:
255  // handles the current assertion.
257  {
258  logger()(context_);
260  }
261 
262  /*
263  IMPORTANT NOTE:
264  The only reason logger & handlers are functions, are
265  because you might use SMART_ASSERT before main().
266 
267  In this case, since they're statics, they might not
268  be initialized. However, making them functions
269  will make it work.
270  */
271 
272  // the log
273  static assert_func &logger()
274  {
275  static assert_func inst;
276  return inst;
277  }
278 
279  // the handler
280  typedef std::map<int, assert_func> handlers_collection;
282  {
283  static handlers_collection inst;
284  return inst;
285  }
286 
287  static assert_func get_handler(int nLevel)
288  {
289  handlers_collection::const_iterator found = handlers().find(nLevel);
290  if (found != handlers().end()) {
291  return (*found).second;
292  }
293 
294  // we always assume the debug handler has been set
295  return (*handlers().find(lvl_debug)).second;
296  }
297 
298 private:
300  mutable bool needs_handling_;
301 };
302 
303 namespace SmartAssert {
304  inline Assert make_assert(const char *expr) { return Assert(expr); }
305 } // namespace SmartAssert
306 
307 ////////////////////////////////////////////////////////
308 // macro trickery
309 
310 // note: NEVER define SMART_ASSERT_DEBUG directly
311 // (it will be overridden);
312 //
313 // #define SMART_ASSERT_DEBUG_MODE instead
314 
315 #ifdef SMART_ASSERT_DEBUG_MODE
316 #if SMART_ASSERT_DEBUG_MODE == 1
317 #define SMART_ASSERT_DEBUG
318 #else
319 #undef SMART_ASSERT_DEBUG
320 #endif
321 
322 #else
323 
324 // defaults
325 #ifndef NDEBUG
326 #define SMART_ASSERT_DEBUG
327 #else
328 #undef SMART_ASSERT_DEBUG
329 #endif
330 #endif
331 
332 #ifdef SMART_ASSERT_DEBUG
333 // "debug" mode
334 #define SMART_ASSERT(expr) \
335  if ((expr)) \
336  ; \
337  else \
338  (void)::SmartAssert::make_assert(#expr).print_context(__FILE__, __LINE__).SMART_ASSERT_A /**/
339 
340 #else
341 // "release" mode
342 #define SMART_ASSERT(expr) \
343  if (true) \
344  ; \
345  else \
346  (void)::SmartAssert::make_assert("").SMART_ASSERT_A /**/
347 
348 #endif // ifdef SMART_ASSERT_DEBUG
349 
350 #define SMART_VERIFY(expr) \
351  if ((expr)) \
352  ; \
353  else \
354  (void)::SmartAssert::make_assert(#expr) \
355  .error() \
356  .print_context(__FILE__, __LINE__) \
357  .SMART_ASSERT_A /**/
358 
359 #define SMART_ASSERT_A(x) SMART_ASSERT_OP(x, B)
360 #define SMART_ASSERT_B(x) SMART_ASSERT_OP(x, A)
361 
362 #define SMART_ASSERT_OP(x, next) SMART_ASSERT_A.print_current_val((x), #x).SMART_ASSERT_##next /**/
363 
364 #if _MSC_VER > 1000
365 #pragma warning(pop)
366 #endif
367 
368 #endif
lvl_fatal
@ lvl_fatal
Definition: Ioss_SmartAssert.h:48
Assert::handlers
static handlers_collection & handlers()
Definition: Ioss_SmartAssert.h:281
Assert::needs_handling_
bool needs_handling_
Definition: Ioss_SmartAssert.h:300
SmartAssert::default_logger
void default_logger(const assert_context &context)
Definition: Ioss_SmartAssert.C:108
Assert::Assert
Assert(const Assert &other)
Definition: Ioss_SmartAssert.h:176
Assert::print_current_val
Assert & print_current_val(const type &val, const char *my_msg)
Definition: Ioss_SmartAssert.h:190
Assert::assert_func
SmartAssert::assert_func assert_func
Definition: Ioss_SmartAssert.h:159
assert_context::assert_context
assert_context()
Definition: Ioss_SmartAssert.h:59
Assert::get_handler
static assert_func get_handler(int nLevel)
Definition: Ioss_SmartAssert.h:287
assert_context::get_context_file
const string & get_context_file() const
Definition: Ioss_SmartAssert.h:67
assert_context::expr_
string expr_
Definition: Ioss_SmartAssert.h:104
Assert::print_context
Assert & print_context(const char *file, int line)
Definition: Ioss_SmartAssert.h:207
Assert::SMART_ASSERT_B
Assert & SMART_ASSERT_B
Definition: Ioss_SmartAssert.h:163
Ioss::Private::is_null_finder
Definition: Ioss_SmartAssert.h:140
lvl_error
@ lvl_error
Definition: Ioss_SmartAssert.h:44
SmartAssert
Definition: Ioss_SmartAssert.C:41
assert_context::set_level
void set_level(int nLevel)
Definition: Ioss_SmartAssert.h:83
Ioss
The main namespace for the Ioss library.
Definition: Ioad_DatabaseIO.C:40
Ioss::Private::is_null_finder::is
bool is(const T &) const
Definition: Ioss_SmartAssert.h:141
Assert::debug
Assert & debug(const char *strMsg=nullptr)
Definition: Ioss_SmartAssert.h:228
assert_context::level_
int level_
Definition: Ioss_SmartAssert.h:108
assert_context::set_level_msg
void set_level_msg(const char *strMsg)
Definition: Ioss_SmartAssert.h:87
assert_context::get_level
int get_level() const
Definition: Ioss_SmartAssert.h:84
SmartAssert::dump_context_summary
void dump_context_summary(const assert_context &context, std::ostream &out)
Definition: Ioss_SmartAssert.C:60
Assert::set_log
static void set_log(std::ostream &out)
Definition: Ioss_SmartAssert.h:244
Assert::SMART_ASSERT_A
Assert & SMART_ASSERT_A
Definition: Ioss_SmartAssert.h:162
Assert::context_
assert_context context_
Definition: Ioss_SmartAssert.h:299
SmartAssert::get_typeof_level
std::string get_typeof_level(int nLevel)
Definition: Ioss_SmartAssert.C:44
Ioss::Private::set_default_log_name
void set_default_log_name(const char *str)
Definition: Ioss_SmartAssert.C:215
assert_context::file_
string file_
Definition: Ioss_SmartAssert.h:100
Assert::fatal
Assert & fatal(const char *strMsg=nullptr)
Definition: Ioss_SmartAssert.h:232
lvl_debug
@ lvl_debug
Definition: Ioss_SmartAssert.h:41
SmartAssert::default_warn_handler
void default_warn_handler(const assert_context &context)
Definition: Ioss_SmartAssert.C:120
Assert
Definition: Ioss_SmartAssert.h:158
Assert::logger
static assert_func & logger()
Definition: Ioss_SmartAssert.h:273
assert_context::msg_
string msg_
Definition: Ioss_SmartAssert.h:109
Ioss::Private::is_null_finder< char * >::is
bool is(char *const &val)
Definition: Ioss_SmartAssert.h:146
Assert::set_handler
static void set_handler(int nLevel, assert_func handler)
Definition: Ioss_SmartAssert.h:252
assert_context::set_file_line
void set_file_line(const char *file, int line)
Definition: Ioss_SmartAssert.h:62
assert_context::set_expr
void set_expr(const string &str)
Definition: Ioss_SmartAssert.h:71
Assert::msg
Assert & msg(const char *strMsg)
Definition: Ioss_SmartAssert.h:213
Assert::warn
Assert & warn(const char *strMsg=nullptr)
Definition: Ioss_SmartAssert.h:226
Assert::set_log
static void set_log(assert_func log)
Definition: Ioss_SmartAssert.h:250
Ioss::Private::is_null_finder< const char * >::is
bool is(const char *const &val)
Definition: Ioss_SmartAssert.h:151
assert_context::val_and_str
std::pair< string, string > val_and_str
Definition: Ioss_SmartAssert.h:74
SmartAssert::default_fatal_handler
void default_fatal_handler(const assert_context &context)
Definition: Ioss_SmartAssert.C:187
SmartAssert::default_debug_handler
void default_debug_handler(const assert_context &context)
Definition: Ioss_SmartAssert.C:126
Assert::Assert
Assert(const char *expr)
Definition: Ioss_SmartAssert.h:165
Assert::set_log
static void set_log(const char *strFileName)
Definition: Ioss_SmartAssert.h:236
Ioss::Private::set_default_log_stream
void set_default_log_stream(std::ostream &out)
Definition: Ioss_SmartAssert.C:208
lvl_warn
@ lvl_warn
Definition: Ioss_SmartAssert.h:37
assert_context::get_level_msg
const string & get_level_msg() const
Definition: Ioss_SmartAssert.h:96
assert_context::get_expr
const string & get_expr() const
Definition: Ioss_SmartAssert.h:72
assert_context::get_context_line
int get_context_line() const
Definition: Ioss_SmartAssert.h:68
SmartAssert::make_assert
Assert make_assert(const char *expr)
Definition: Ioss_SmartAssert.h:304
assert_context::add_val
void add_val(const string &val, const string &str)
Definition: Ioss_SmartAssert.h:80
assert_context::vals_array
std::vector< val_and_str > vals_array
Definition: Ioss_SmartAssert.h:75
Assert::~Assert
~Assert()
Definition: Ioss_SmartAssert.h:183
SmartAssert::default_error_handler
void default_error_handler(const assert_context &context)
Definition: Ioss_SmartAssert.C:179
Assert::level
Assert & level(int nLevel, const char *strMsg=nullptr)
Definition: Ioss_SmartAssert.h:219
SmartAssert::dump_context_detail
void dump_context_detail(const assert_context &context, std::ostream &out)
Definition: Ioss_SmartAssert.C:75
assert_context::line_
int line_
Definition: Ioss_SmartAssert.h:101
SmartAssert::assert_func
void(*)(const assert_context &) assert_func
Definition: Ioss_SmartAssert.h:114
assert_context::get_vals_array
const vals_array & get_vals_array() const
Definition: Ioss_SmartAssert.h:78
assert_context
Definition: Ioss_SmartAssert.h:55
Assert::handle_assert
void handle_assert()
Definition: Ioss_SmartAssert.h:256
assert_context::vals_
vals_array vals_
Definition: Ioss_SmartAssert.h:105
Ioss::Private::init_assert
void init_assert()
Definition: Ioss_SmartAssert.C:198
Assert::error
Assert & error(const char *strMsg=nullptr)
Definition: Ioss_SmartAssert.h:230
Assert::handlers_collection
std::map< int, assert_func > handlers_collection
Definition: Ioss_SmartAssert.h:280