Skip to content

Commit

Permalink
helper mongo::ErrorMsg class that builds error strings. lighter weigh…
Browse files Browse the repository at this point in the history
…t than a StringBuilder, albeit less flexible.
  • Loading branch information
dwight committed May 24, 2011
1 parent 990e708 commit 923660e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
17 changes: 17 additions & 0 deletions util/assert_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,22 @@ namespace mongo {
#endif
}

NOINLINE_DECL ErrorMsg::ErrorMsg(const char *msg, char ch) {
int l = strlen(msg);
assert( l < 128);
memcpy(buf, msg, l);
char *p = buf + l;
p[0] = ch;
p[1] = 0;
}

NOINLINE_DECL ErrorMsg::ErrorMsg(const char *msg, unsigned val) {
int l = strlen(msg);
assert( l < 128);
memcpy(buf, msg, l);
char *p = buf + l;
sprintf(p, "%u", val);
}

}

15 changes: 15 additions & 0 deletions util/assert_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ namespace mongo {
int code;
};

/** helper class that builds error strings. lighter weight than a StringBuilder, albeit less flexible.
NOINLINE_DECL used in the constructor implementations as we are assuming this is a cold code path when used.
example:
throw UserException(123, ErrorMsg("blah", num_val));
*/
class ErrorMsg {
public:
ErrorMsg(const char *msg, char ch);
ErrorMsg(const char *msg, unsigned val);
operator string() const { return buf; }
private:
char buf[256];
};

class DBException : public std::exception {
public:
DBException( const ExceptionInfo& ei ) : _ei(ei) {}
Expand Down

0 comments on commit 923660e

Please sign in to comment.