Skip to content

Commit

Permalink
LOG: use sstream+write in case cerr is not initialized
Browse files Browse the repository at this point in the history
Thanks to Eric Rannaud for reporting the bug.

R=rsc
CC=re2-dev
http://codereview.appspot.com/4962053
  • Loading branch information
rsc committed Sep 5, 2011
1 parent 3b1bc65 commit 8c84071
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions util/logging.h
Expand Up @@ -7,6 +7,8 @@
#ifndef RE2_UTIL_LOGGING_H__
#define RE2_UTIL_LOGGING_H__

#include <sstream>

// Debug-only checking.
#define DCHECK(condition) assert(condition)
#define DCHECK_EQ(val1, val2) assert((val1) == (val2))
Expand Down Expand Up @@ -46,12 +48,17 @@
class LogMessage {
public:
LogMessage(const char* file, int line) {
std::cerr << file << ":" << line << ": ";
stream() << file << ":" << line << ": ";
}
~LogMessage() {
stream() << "\n";
string s = str_.str();
if(write(2, s.data(), s.size()) < 0) {} // shut up gcc
}
~LogMessage() { std::cerr << "\n"; }
ostream& stream() { return std::cerr; }
ostream& stream() { return str_; }

private:
std::ostringstream str_;
DISALLOW_EVIL_CONSTRUCTORS(LogMessage);
};

Expand Down

0 comments on commit 8c84071

Please sign in to comment.