30 #if !defined(SMART_ASSERT_H)
31 #define SMART_ASSERT_H
39 #include <glog/logging.h>
45 lvl_info = google::GLOG_INFO,
49 lvl_warn = google::GLOG_WARNING,
53 lvl_debug = google::GLOG_INFO,
56 lvl_error = google::GLOG_ERROR,
60 lvl_fatal = google::GLOG_FATAL
72 typedef std::string string;
78 void setFileLine(
const char * file,
int line )
83 const string & getContextFile()
const
87 int getContextLine()
const
93 void setExpression(
const string & str )
97 const string & expression()
const
102 typedef std::pair< string, string> val_and_str;
103 typedef std::vector< val_and_str> vals_array;
106 const vals_array & get_vals_array()
const
111 void add_val(
const string &
val,
const string & str )
113 M_vals.push_back( val_and_str( val, str ) );
117 void setLevel(
int nLevel )
121 int get_level()
const
127 void setLevelMsg(
const char * strMsg )
135 const string & get_level_msg()
const
155 namespace SmartAssert
158 typedef void ( *assert_function_type )(
const AssertContext & context );
161 std::string getTypeofLevel(
int nLevel );
162 void dumpContextSummary(
const AssertContext & context, std::ostream & out );
163 void dumpContextDetail(
const AssertContext & context, std::ostream & out );
177 void setDefaultLogStream( std::ostream & out );
178 void setDefaultLogName(
const char * str );
186 bool is(
const T & )
const
193 struct isNullFinder< char*>
195 bool is(
char *
const &
val )
202 struct isNullFinder< const char*>
204 bool is(
const char *
const &
val )
216 typedef SmartAssert::assert_function_type assert_function_type;
219 Assert & SMART_ASSERT_A;
220 Assert & SMART_ASSERT_B;
222 Assert(
const char * expr )
223 : SMART_ASSERT_A( *this ),
224 SMART_ASSERT_B( *this ),
225 M_needs_handling( true )
227 M_context.setExpression( expr );
229 if ( ( logger() == 0 ) || handlers().size() < 4 )
232 Private::initAssert();
236 Assert(
const Assert & other )
237 : SMART_ASSERT_A( *this ),
238 SMART_ASSERT_B( *this ),
239 M_context( other.M_context ),
240 M_needs_handling( true )
242 other.M_needs_handling =
false;
247 if ( M_needs_handling )
251 template<
class type>
252 Assert & printCurrentValue(
const type &
val,
const char * msg );
254 Assert & printContext(
const char * file,
int line )
256 M_context.setFileLine( file, line );
260 Assert & msg(
const char * strMsg )
262 M_context.setLevelMsg( strMsg );
266 Assert & level(
int nLevel,
const char * strMsg = 0 )
268 M_context.setLevel( nLevel );
269 M_context.setLevelMsg( strMsg );
273 Assert & info(
const char * strMsg = 0 )
275 return level( lvl_info, strMsg );
277 Assert & warn(
const char * strMsg = 0 )
279 return level( lvl_warn, strMsg );
282 Assert & debug(
const char * strMsg = 0 )
284 return level( lvl_debug, strMsg );
287 Assert & error(
const char * strMsg = 0 )
289 return level( lvl_error, strMsg );
292 Assert & fatal(
const char * strMsg = 0 )
294 return level( lvl_fatal, strMsg );
299 static void setLog(
const char * strFileName )
301 Private::setDefaultLogName( strFileName );
302 logger() = &SmartAssert::defaultLogger;
307 static void setLog( std::ostream & out )
309 Private::setDefaultLogStream( out );
310 logger() = &SmartAssert::defaultLogger;
313 static void setLog( assert_function_type log )
318 static void setHandler(
int nLevel, assert_function_type handler )
320 handlers()[ nLevel] = handler;
327 logger()( M_context );
328 get_handler( M_context.get_level() )( M_context );
342 static assert_function_type & logger()
344 static assert_function_type inst;
349 typedef std::map< int, assert_function_type> handlers_collection;
350 static handlers_collection & handlers()
352 static handlers_collection inst;
356 static assert_function_type get_handler(
int nLevel )
358 handlers_collection::const_iterator found = handlers().find( nLevel );
360 if ( found != handlers().end() )
361 return found->second;
365 return handlers().find( lvl_debug )->second;
369 AssertContext M_context;
370 mutable bool M_needs_handling;
376 Assert::printCurrentValue(
const type & _val,
const char * _msg )
378 std::ostringstream out;
380 Private::isNullFinder< type> f;
381 bool bIsNull = f.is( _val );
390 M_context.add_val( out.str(), _msg );
395 namespace SmartAssert
397 inline ::Feel::Assert make_assert(
const char * expr )
399 return ::Feel::Assert( expr );
405 #ifdef FEELPP_SMART_ASSERT_DEBUG_MODE
407 #if FEELPP_SMART_ASSERT_DEBUG_MODE == 1
408 #define FEELPP_SMART_ASSERT_DEBUG
410 #undef FEELPP_SMART_ASSERT_DEBUG
417 #define FEELPP_SMART_ASSERT_DEBUG
419 #undef FEELPP_SMART_ASSERT_DEBUG
425 #ifdef FEELPP_SMART_ASSERT_DEBUG
427 #define FEELPP_SMART_ASSERT( expr) \
429 else ::Feel::SmartAssert::make_assert( #expr).printContext( __FILE__, __LINE__).SMART_ASSERT_A \
434 #define FEELPP_SMART_ASSERT( expr) \
436 else ::Feel::SmartAssert::make_assert( "").SMART_ASSERT_A \
439 #endif // ifdef FEELPP_SMART_ASSERT_DEBUG
442 #define FEELPP_ASSERT( expr) FEELPP_SMART_ASSERT(expr)
445 #define FEELPP_SMART_VERIFY( expr) \
447 else ::Feel::SmartAssert::make_assert( #expr).error().printContext( __FILE__, __LINE__).SMART_ASSERT_A \
449 #define FEELPP_VERIFY( expr) FEELPP_SMART_VERIFY(expr)
452 #define SMART_ASSERT_A(x) FEELPP_SMART_ASSERT_OP(x, B)
453 #define SMART_ASSERT_B(x) FEELPP_SMART_ASSERT_OP(x, A)
455 #define FEELPP_SMART_ASSERT_OP(x, next) \
456 SMART_ASSERT_A.printCurrentValue((x), #x).SMART_ASSERT_ ## next \
Expr< Val< typename mpl::if_< boost::is_arithmetic< ExprT1 >, mpl::identity< Cst< ExprT1 > >, mpl::identity< ExprT1 > >::type::type > > val(ExprT1 const &__e1)
precompute expression tensor
Definition: val.hpp:304
contains details about a failed assertion
Definition: smartassert.hpp:70