| 1 |
// Copyright(C) 1999-2020, 2023 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 |
|
| 7 |
#include "Ioss_SmartAssert.h" |
| 8 |
#include <cstdlib> |
| 9 |
#include <fstream> |
| 10 |
#include <stdexcept> |
| 11 |
|
| 12 |
namespace { |
| 13 |
// in case we're logging using the default logger... |
| 14 |
struct stream_holder |
| 15 |
{ |
| 16 |
stream_holder() = default; |
| 17 |
~stream_holder() |
| 18 |
{ |
| 19 |
if (owns_) { |
| 20 |
delete out_; |
| 21 |
} |
| 22 |
out_ = nullptr; |
| 23 |
} |
| 24 |
std::ostream *out_{nullptr}; |
| 25 |
bool owns_{false}; |
| 26 |
}; |
| 27 |
// information about the stream we write to, in case |
| 28 |
// we're using the default logger |
| 29 |
stream_holder default_logger_info; |
| 30 |
} // anonymous namespace |
| 31 |
|
| 32 |
namespace Ioss { |
| 33 |
namespace SmartAssert { |
| 34 |
|
| 35 |
// returns a message corresponding to the type of level |
| 36 |
std::string get_typeof_level(int nLevel) |
| 37 |
{ |
| 38 |
switch (nLevel) { |
| 39 |
case lvl_warn: return "Warning"; |
| 40 |
case lvl_debug: return "Assertion failed"; |
| 41 |
case lvl_error: return "Assertion failed (Error)"; |
| 42 |
case lvl_fatal: return "Assertion failed (FATAL)"; |
| 43 |
default: { |
| 44 |
std::ostringstream out; |
| 45 |
out << "Assertion failed (level=" << nLevel << ")"; |
| 46 |
return out.str(); |
| 47 |
} |
| 48 |
}; |
| 49 |
} |
| 50 |
|
| 51 |
// helpers, for dumping the assertion context |
| 52 |
void dump_context_summary(const assert_context &context, std::ostream &out) |
| 53 |
{ |
| 54 |
out << "\n" |
| 55 |
<< get_typeof_level(context.get_level()) << " in " << context.get_context_file() << ":" |
| 56 |
<< context.get_context_line() << '\n'; |
| 57 |
if (!context.get_level_msg().empty()) { |
| 58 |
// we have a user-friendly message |
| 59 |
out << context.get_level_msg(); |
| 60 |
} |
| 61 |
else { |
| 62 |
out << "\nExpression: " << context.get_expr(); |
| 63 |
} |
| 64 |
out << '\n'; |
| 65 |
} |
| 66 |
|
| 67 |
void dump_context_detail(const assert_context &context, std::ostream &out) |
| 68 |
{ |
| 69 |
out << "\n" |
| 70 |
<< get_typeof_level(context.get_level()) << " in " << context.get_context_file() << ":" |
| 71 |
<< context.get_context_line() << '\n'; |
| 72 |
if (!context.get_level_msg().empty()) { |
| 73 |
out << "User-friendly msg: '" << context.get_level_msg() << "'\n"; |
| 74 |
} |
| 75 |
out << "\nExpression: '" << context.get_expr() << "'\n"; |
| 76 |
|
| 77 |
using ac_vals_array = assert_context::vals_array; |
| 78 |
const ac_vals_array &aVals = context.get_vals_array(); |
| 79 |
if (!aVals.empty()) { |
| 80 |
bool bFirstTime = true; |
| 81 |
auto first = aVals.cbegin(); |
| 82 |
auto last = aVals.cend(); |
| 83 |
while (first != last) { |
| 84 |
if (bFirstTime) { |
| 85 |
out << "Values: "; |
| 86 |
bFirstTime = false; |
| 87 |
} |
| 88 |
else { |
| 89 |
out << " "; |
| 90 |
} |
| 91 |
out << first->second << "='" << first->first << "'\n"; |
| 92 |
++first; |
| 93 |
} |
| 94 |
} |
| 95 |
out << '\n'; |
| 96 |
} |
| 97 |
|
| 98 |
/////////////////////////////////////////////////////// |
| 99 |
// logger |
| 100 |
|
| 101 |
void default_logger(const assert_context &context) |
| 102 |
{ |
| 103 |
if (default_logger_info.out_ == nullptr) { |
| 104 |
return; |
| 105 |
} |
| 106 |
dump_context_detail(context, *(default_logger_info.out_)); |
| 107 |
} |
| 108 |
|
| 109 |
/////////////////////////////////////////////////////// |
| 110 |
// handlers |
| 111 |
|
| 112 |
// warn : just dump summary to console |
| 113 |
void default_warn_handler(const assert_context &context) |
| 114 |
{ |
| 115 |
dump_context_summary(context, std::cout); |
| 116 |
} |
| 117 |
|
| 118 |
// debug: ask user what to do |
| 119 |
void default_debug_handler(const assert_context &context) |
| 120 |
{ |
| 121 |
#if 1 |
| 122 |
dump_context_detail(context, std::cerr); |
| 123 |
abort(); |
| 124 |
#else |
| 125 |
static bool ignore_all = false; |
| 126 |
if (ignore_all) |
| 127 |
// ignore All asserts |
| 128 |
return; |
| 129 |
typedef std::pair<std::string, int> file_and_line; |
| 130 |
static std::set<file_and_line> ignorer; |
| 131 |
if (ignorer.find(file_and_line(context.get_context_file(), context.get_context_line())) != |
| 132 |
ignorer.end()) |
| 133 |
// this is Ignored Forever |
| 134 |
return; |
| 135 |
|
| 136 |
dump_context_summary(context, std::cerr); |
| 137 |
std::cerr << "\nPress (I)gnore/ Ignore (F)orever/ Ignore (A)ll/ (D)ebug/ A(b)ort: "; |
| 138 |
std::cerr.flush(); |
| 139 |
char ch = 0; |
| 140 |
|
| 141 |
bool bContinue = true; |
| 142 |
while (bContinue && std::cin.get(ch)) { |
| 143 |
bContinue = false; |
| 144 |
switch (ch) { |
| 145 |
case 'i': |
| 146 |
case 'I': |
| 147 |
// ignore |
| 148 |
break; |
| 149 |
|
| 150 |
case 'f': |
| 151 |
case 'F': |
| 152 |
// ignore forever |
| 153 |
ignorer.insert(file_and_line(context.get_context_file(), context.get_context_line())); |
| 154 |
break; |
| 155 |
|
| 156 |
case 'a': |
| 157 |
case 'A': |
| 158 |
// ignore all |
| 159 |
ignore_all = true; |
| 160 |
break; |
| 161 |
|
| 162 |
case 'b': |
| 163 |
case 'B': abort(); break; |
| 164 |
|
| 165 |
default: bContinue = true; break; |
| 166 |
} |
| 167 |
} |
| 168 |
#endif |
| 169 |
} |
| 170 |
|
| 171 |
// error : throw a runtime exception |
| 172 |
void default_error_handler(const assert_context &context) |
| 173 |
{ |
| 174 |
std::ostringstream out; |
| 175 |
dump_context_summary(context, out); |
| 176 |
throw std::runtime_error(out.str()); |
| 177 |
} |
| 178 |
|
| 179 |
// fatal : dump error and abort |
| 180 |
void default_fatal_handler(const assert_context &context) |
| 181 |
{ |
| 182 |
dump_context_detail(context, std::cerr); |
| 183 |
abort(); |
| 184 |
} |
| 185 |
|
| 186 |
} // namespace SmartAssert |
| 187 |
|
| 188 |
namespace Private { |
| 189 |
|
| 190 |
void init_assert() |
| 191 |
{ |
| 192 |
Assert::set_log(&::Ioss::SmartAssert::default_logger); |
| 193 |
Assert::set_handler(lvl_warn, &::Ioss::SmartAssert::default_warn_handler); |
| 194 |
Assert::set_handler(lvl_debug, &::Ioss::SmartAssert::default_debug_handler); |
| 195 |
Assert::set_handler(lvl_error, &::Ioss::SmartAssert::default_error_handler); |
| 196 |
Assert::set_handler(lvl_fatal, &::Ioss::SmartAssert::default_fatal_handler); |
| 197 |
} |
| 198 |
|
| 199 |
// sets the default logger to write to this stream |
| 200 |
void set_default_log_stream(std::ostream &out) |
| 201 |
{ |
| 202 |
default_logger_info.out_ = &out; |
| 203 |
default_logger_info.owns_ = false; |
| 204 |
} |
| 205 |
|
| 206 |
// sets the default logger to write to this file |
| 207 |
void set_default_log_name(const char *str) |
| 208 |
{ |
| 209 |
default_logger_info.owns_ = false; |
| 210 |
default_logger_info.out_ = new std::ofstream(str); |
| 211 |
default_logger_info.owns_ = true; |
| 212 |
} |
| 213 |
|
| 214 |
} // namespace Private |
| 215 |
} // namespace Ioss |