13 #if !defined(SMART_ASSERT_H) 14 #define SMART_ASSERT_H 18 #endif // _MSC_VER > 1000 26 #pragma warning ( disable : 4786) 28 #pragma warning ( push ) 30 #pragma warning ( disable : 4355) 49 #ifdef MATLAB_MEX_FILE 51 #define SA_Abort(expout) mexErrMsgTxt(expout) 53 #define SA_Abort(expout) do { std::cerr << expout <<"\n" << std::flush; abort(); } while(0) 56 #if defined(__BORLANDC__) 57 #define __SMART_ASSERT_LOCATION__ __FUNC__ 58 #elif defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(CSWIG) 59 #define __SMART_ASSERT_LOCATION__ __FUNCSIG__ 60 #elif defined(__GNUC__) 61 #define __SMART_ASSERT_LOCATION__ __PRETTY_FUNCTION__ 63 #define __SMART_ASSERT_LOCATION__ __FUNCTION__ 129 void add_val(
const string & val,
const string & str) {
193 bool is(
const T &)
const {
200 bool is(
char *
const & val) {
207 bool is(
const char *
const & val) {
224 : SMART_ASSERT_A( *this),
225 SMART_ASSERT_B( *this),
226 needs_handling_( true) {
227 context_.set_expr( expr);
229 if ( ( logger() == 0) || handlers().size() < 5) {
236 : SMART_ASSERT_A( *this),
237 SMART_ASSERT_B( *this),
238 context_( other.context_),
239 needs_handling_( true) {
244 if ( needs_handling_)
248 template<
class type>
250 std::ostringstream out;
253 bool bIsNull = f.
is( val);
259 context_.add_val( out.str(), msg);
264 context_.set_file_line( file, line);
268 context_.set_file_line_func_cond( file, line, func, cond);
273 context_.set_level_msg( strMsg);
278 context_.set_level( nLevel);
279 context_.set_level_msg( strMsg);
284 return level(
lvl_log, strMsg);
305 static void set_log(
const char * strFileName) {
322 handlers()[ nLevel] = handler;
329 get_handler( context_.get_level() )( context_);
344 static assert_func inst;
351 static handlers_collection inst;
356 handlers_collection::const_iterator found = handlers().find( nLevel);
357 if ( found != handlers().end() )
358 return found->second;
361 return handlers().find(
lvl_debug)->second;
385 #ifdef SMART_ASSERT_DEBUG_MODE 386 #if SMART_ASSERT_DEBUG_MODE == 1 387 #define SMART_ASSERT_DEBUG 389 #undef SMART_ASSERT_DEBUG 396 #define SMART_ASSERT_DEBUG 398 #undef SMART_ASSERT_DEBUG 402 #ifdef SMART_ASSERT_DEBUG 404 #define SMART_ASSERT( expr) \ 406 else ::smart_assert::make_assert( #expr).print_context( __FILE__, __LINE__,__SMART_ASSERT_LOCATION__, ::smart_assert::lvl_condition_assert).SMART_ASSERT_A \ 411 #define SMART_ASSERT( expr) \ 413 else ::smart_assert::make_assert("").SMART_ASSERT_A \ 416 #endif // ifdef SMART_ASSERT_DEBUG 419 #define SMART_VERIFY( expr) \ 421 else ::smart_assert::make_assert( #expr).error().print_context( __FILE__, __LINE__,__SMART_ASSERT_LOCATION__, ::smart_assert::lvl_condition_assert).SMART_ASSERT_A \ 424 #define SMART_EXCEPTION( expr) \ 426 else ::smart_assert::make_assert( #expr).error().print_context( __FILE__, __LINE__,__SMART_ASSERT_LOCATION__, ::smart_assert::lvl_condition_exception).SMART_ASSERT_A \ 428 #define SMART_PRINT \ 430 else ::smart_assert::make_assert("").log().print_context( __FILE__, __LINE__,__SMART_ASSERT_LOCATION__, ::smart_assert::lvl_condition_exception).SMART_ASSERT_A \ 432 #define SMART_ASSERT_A(x) SMART_ASSERT_OP(x, B) 433 #define SMART_ASSERT_B(x) SMART_ASSERT_OP(x, A) 435 #define SMART_ASSERT_OP(x, next) \ 436 SMART_ASSERT_A.print_current_val((x), #x).SMART_ASSERT_##next \ 441 #pragma warning ( pop ) 446 #if (defined _MSC_VER) || (defined __BORLANDC__) 448 #elif defined(__GNUC__) 452 # error Please supply instruction to break into code 459 struct stream_holder {
460 stream_holder() : out_( 0), owns_(
false) {}
471 stream_holder default_logger_info;
474 struct assert_initializer {
475 assert_initializer() {
494 std::ostringstream out;
495 out <<
"(level=" << nLevel <<
")";
524 if ( !aVals.empty() ) {
525 bool bFirstTime =
true;
526 vals_array::const_iterator first = aVals.begin(), last = aVals.end();
527 while ( first != last) {
535 out << first->second <<
"='" << first->first <<
"'\n";
552 if ( !aVals.empty() ) {
554 out <<
"(" << aVals[0].second <<
") = " <<
"(" << aVals[0].first <<
")";
558 for (
int i = 0; i < aVals.size()-1; i += 1 )
559 out << aVals[i].second <<
", ";
560 out << aVals.back().second <<
") = (";
561 for (
int i = 0; i < aVals.size()-1; i += 1 )
562 out << aVals[i].first <<
", ";
563 out << aVals.back().first <<
")";
574 if ( default_logger_info.out_ == 0)
588 if ( default_logger_info.out_ == 0)
602 static bool ignore_all =
false;
606 typedef std::pair< std::string, int> file_and_line;
607 static std::set< file_and_line> ignorer;
613 std::cerr <<
"\nPress (I)gnore/ Igore (F)orever/ Ignore (A)ll/ (D)ebug/ A(b)ort: ";
617 bool bContinue =
true;
618 while ( bContinue && std::cin.
get( ch)) {
654 std::ostringstream out;
656 throw std::runtime_error( out.str());
681 default_logger_info.out_ = &out;
682 default_logger_info.owns_ =
false;
687 default_logger_info.owns_ =
false;
688 default_logger_info.out_ =
new std::ofstream( str);
689 default_logger_info.owns_ =
true;
void set_default_log_stream(std::ostream &out)
const string & get_level_msg() const
const string & get_context_file() const
int get_condition() const
void default_debug_handler(const assert_context &context)
static void set_handler(int nLevel, assert_func handler)
static assert_func & logger()
void set_expr(const string &str)
Assert & fatal(const char *strMsg=0)
const vals_array & get_vals_array() const
void default_fatal_handler(const assert_context &context)
std::pair< string, string > val_and_str
const string & get_context_func() const
#define __UTL_FATAL_STRING
void(* assert_func)(const assert_context &context)
void break_into_debugger()
void default_error_handler(const assert_context &context)
const string & get_expr() const
void default_logger(const assert_context &context)
Assert & print_context(const char *file, int line)
static handlers_collection & handlers()
smart_assert::assert_func assert_func
static assert_func get_handler(int nLevel)
static void set_log(std::ostream &out)
void dump_context_summary(const assert_context &context, std::ostream &out)
void default_warn_handler(const assert_context &context)
void add_val(const string &val, const string &str)
Assert & level(int nLevel, const char *strMsg=0)
void set_file_line_func_cond(const char *file, int line, const char *func, int cond)
static void set_log(assert_func log)
static void set_log(const char *strFileName)
void set_level_msg(const char *strMsg)
Assert make_assert(const char *expr)
Assert & print_current_val(const type &val, const char *msg)
Assert & debug(const char *strMsg=0)
std::string get_typeof_level(int nLevel)
#define __UTL_DEBUG_STRING
Assert & error(const char *strMsg=0)
std::vector< val_and_str > vals_array
bool is(char *const &val)
Assert & log(const char *strMsg=0)
Assert & print_context(const char *file, int line, const char *func, int cond)
bool is(const char *const &val)
#define __UTL_WARNING_STRING
int get_context_line() const
void dump_context_detail(const assert_context &context, std::ostream &out)
void set_file_line(const char *file, int line)
void default_log_handler(const assert_context &context)
Assert(const Assert &other)
Assert & warn(const char *strMsg=0)
#define __UTL_ERROR_STRING
void set_condition(int cond)
std::map< int, assert_func > handlers_collection
Assert & msg(const char *strMsg)
void set_default_log_name(const char *str)
void set_level(int nLevel)
#define __UTL_EXPSTR(str)
void dump_context_log_detail(const assert_context &context, std::ostream &out)