mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
Make assertions stringable
This commit is contained in:
parent
b035e4f92f
commit
4fca3ad77a
18
stdafx.h
18
stdafx.h
@ -70,6 +70,12 @@ using namespace std;
|
||||
|
||||
namespace mongo {
|
||||
|
||||
class Stringable {
|
||||
public:
|
||||
virtual ~Stringable() throw() {}
|
||||
virtual string toString() const = 0;
|
||||
};
|
||||
|
||||
/* these are manipulated outside of mutexes, so be careful */
|
||||
struct Assertion {
|
||||
Assertion() {
|
||||
@ -107,9 +113,12 @@ namespace mongo {
|
||||
/* last assert of diff types: regular, wassert, msgassert, uassert: */
|
||||
extern Assertion lastAssert[4];
|
||||
|
||||
class DBException : public exception {
|
||||
class DBException : public exception, public Stringable {
|
||||
public:
|
||||
virtual const char* what() const throw() = 0;
|
||||
virtual string toString() const {
|
||||
return what();
|
||||
}
|
||||
};
|
||||
|
||||
class AssertionException : public DBException {
|
||||
@ -123,9 +132,6 @@ namespace mongo {
|
||||
virtual bool isUserAssertion() {
|
||||
return false;
|
||||
}
|
||||
virtual string toString() {
|
||||
return msg;
|
||||
}
|
||||
virtual const char* what() const throw() { return msg.c_str(); }
|
||||
};
|
||||
|
||||
@ -144,7 +150,7 @@ namespace mongo {
|
||||
virtual bool isUserAssertion() {
|
||||
return true;
|
||||
}
|
||||
virtual string toString() {
|
||||
virtual string toString() const {
|
||||
return "userassert:" + msg;
|
||||
}
|
||||
};
|
||||
@ -157,7 +163,7 @@ namespace mongo {
|
||||
virtual bool severe() {
|
||||
return false;
|
||||
}
|
||||
virtual string toString() {
|
||||
virtual string toString() const {
|
||||
return "massert:" + msg;
|
||||
}
|
||||
};
|
||||
|
13
util/log.h
13
util/log.h
@ -20,6 +20,11 @@
|
||||
|
||||
namespace mongo {
|
||||
|
||||
// If you don't want your class to inherit from Stringable (for example, if
|
||||
// you don't want a virtual destructor) then add a function like the
|
||||
// following to your class, which takes a pointer to an object of your class
|
||||
// type:
|
||||
// static string toString( void * );
|
||||
class LazyString {
|
||||
public:
|
||||
template< class T >
|
||||
@ -70,6 +75,9 @@ namespace mongo {
|
||||
virtual Nullstream& operator<<(const LazyString&) {
|
||||
return *this;
|
||||
}
|
||||
virtual Nullstream& operator<<(const Stringable&) {
|
||||
return *this;
|
||||
}
|
||||
virtual Nullstream& operator<< (ostream& ( *endl )(ostream&)) {
|
||||
return *this;
|
||||
}
|
||||
@ -104,6 +112,11 @@ namespace mongo {
|
||||
cout << x.val();
|
||||
return *this;
|
||||
}
|
||||
Logstream& operator<<(const Stringable& x) {
|
||||
boostlock lk(mutex);
|
||||
cout << x.toString();
|
||||
return *this;
|
||||
}
|
||||
Logstream& operator<< (ostream& ( *_endl )(ostream&)) {
|
||||
boostlock lk(mutex);
|
||||
cout << _endl;
|
||||
|
Loading…
Reference in New Issue
Block a user