Browse Source

Better logging/descriptive exceptions on invariants.

cl-refactor
Gav Wood 10 years ago
parent
commit
3c78f0f951
  1. 9
      libdevcore/Common.cpp
  2. 16
      libdevcore/Common.h

9
libdevcore/Common.cpp

@ -32,10 +32,13 @@ char const* Version = "0.9.27";
const u256 UndefinedU256 = ~(u256)0; const u256 UndefinedU256 = ~(u256)0;
void HasInvariants::checkInvariants() const void InvariantChecker::checkInvariants() const
{ {
if (!invariants()) if (!m_this->invariants())
BOOST_THROW_EXCEPTION(FailedInvariant()); {
cwarn << "Invariant failed in" << m_function << "at" << m_file << ":" << m_line;
::boost::exception_detail::throw_exception_(FailedInvariant(), m_function, m_file, m_line);
}
} }
struct TimerChannel: public LogChannel { static const char* name(); static const int verbosity = 0; }; struct TimerChannel: public LogChannel { static const char* name(); static const int verbosity = 0; };

16
libdevcore/Common.h

@ -163,10 +163,6 @@ private:
class HasInvariants class HasInvariants
{ {
public: public:
/// Check invariants are met, throw if not.
void checkInvariants() const;
protected:
/// Reimplement to specify the invariants. /// Reimplement to specify the invariants.
virtual bool invariants() const = 0; virtual bool invariants() const = 0;
}; };
@ -175,16 +171,22 @@ protected:
class InvariantChecker class InvariantChecker
{ {
public: public:
InvariantChecker(HasInvariants* _this): m_this(_this) { m_this->checkInvariants(); } InvariantChecker(HasInvariants* _this, char const* _fn, char const* _file, int _line): m_this(_this), m_function(_fn), m_file(_file), m_line(_line) { checkInvariants(); }
~InvariantChecker() { m_this->checkInvariants(); } ~InvariantChecker() { checkInvariants(); }
private: private:
/// Check invariants are met, throw if not.
void checkInvariants() const;
HasInvariants const* m_this; HasInvariants const* m_this;
char const* m_function;
char const* m_file;
int m_line;
}; };
/// Scope guard for invariant check in a class derived from HasInvariants. /// Scope guard for invariant check in a class derived from HasInvariants.
#if ETH_DEBUG #if ETH_DEBUG
#define DEV_INVARIANT_CHECK { ::dev::InvariantChecker __dev_invariantCheck(this); } #define DEV_INVARIANT_CHECK { ::dev::InvariantChecker __dev_invariantCheck(this, BOOST_THROW_EXCEPTION_CURRENT_FUNCTION, __FILE__, __LINE__); }
#else #else
#define DEV_INVARIANT_CHECK (void)0; #define DEV_INVARIANT_CHECK (void)0;
#endif #endif

Loading…
Cancel
Save