-
Notifications
You must be signed in to change notification settings - Fork 519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Logging] Add DMLC_NO_INLINE
to LogMessageFatal
#615
Changes from all commits
3c2cea7
6d84163
cda96e4
32acd5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,8 +205,7 @@ class LogCheckError { | |
|
||
#define CHECK_BINARY_OP(name, op, x, y) \ | ||
if (dmlc::LogCheckError _check_err = dmlc::LogCheck##name(x, y)) \ | ||
std::unique_ptr<dmlc::LogMessageFatal>( \ | ||
new dmlc::LogMessageFatal(__FILE__, __LINE__))->stream() \ | ||
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ | ||
<< "Check failed: " << #x " " #op " " #y << *(_check_err.str) << ": " | ||
|
||
#pragma GCC diagnostic push | ||
|
@@ -220,10 +219,9 @@ DEFINE_CHECK_FUNC(_NE, !=) | |
#pragma GCC diagnostic pop | ||
|
||
// Always-on checking | ||
#define CHECK(x) \ | ||
if (!(x)) \ | ||
std::unique_ptr<dmlc::LogMessageFatal>( \ | ||
new dmlc::LogMessageFatal(__FILE__, __LINE__))->stream() \ | ||
#define CHECK(x) \ | ||
if (!(x)) \ | ||
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ | ||
<< "Check failed: " #x << ": " | ||
#define CHECK_LT(x, y) CHECK_BINARY_OP(_LT, <, x, y) | ||
#define CHECK_GT(x, y) CHECK_BINARY_OP(_GT, >, x, y) | ||
|
@@ -428,30 +426,42 @@ class LogMessageFatal : public LogMessage { | |
#else | ||
class LogMessageFatal { | ||
public: | ||
LogMessageFatal(const char* file, int line) { | ||
log_stream_ << "[" << pretty_date_.HumanDate() << "] " << file << ":" | ||
<< line << ": "; | ||
LogMessageFatal(const char *file, int line) { | ||
Entry::ThreadLocal()->Init(file, line); | ||
} | ||
std::ostringstream &stream() { return log_stream_; } | ||
~LogMessageFatal() DMLC_THROW_EXCEPTION { | ||
std::ostringstream &stream() { return Entry::ThreadLocal()->log_stream; } | ||
DMLC_NO_INLINE ~LogMessageFatal() DMLC_THROW_EXCEPTION { | ||
#if DMLC_LOG_STACK_TRACE | ||
log_stream_ << "\n" << StackTrace(1, LogStackTraceLevel()) << "\n"; | ||
#endif | ||
|
||
// throwing out of destructor is evil | ||
// hopefully we can do it here | ||
// also log the message before throw | ||
#if DMLC_LOG_BEFORE_THROW | ||
LOG(ERROR) << log_stream_.str(); | ||
Entry::ThreadLocal()->log_stream << "\n" | ||
<< StackTrace(1, LogStackTraceLevel()) | ||
<< "\n"; | ||
#endif | ||
throw Error(log_stream_.str()); | ||
throw Entry::ThreadLocal()->Finalize(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We moved all the logic except |
||
} | ||
|
||
private: | ||
std::ostringstream log_stream_; | ||
DateLogger pretty_date_; | ||
LogMessageFatal(const LogMessageFatal&); | ||
void operator=(const LogMessageFatal&); | ||
struct Entry { | ||
std::ostringstream log_stream; | ||
DMLC_NO_INLINE void Init(const char *file, int line) { | ||
DateLogger date; | ||
log_stream.str(""); | ||
log_stream.clear(); | ||
log_stream << "[" << date.HumanDate() << "] " << file << ":" << line | ||
<< ": "; | ||
} | ||
dmlc::Error Finalize() { | ||
#if DMLC_LOG_BEFORE_THROW | ||
LOG(ERROR) << log_stream.str(); | ||
#endif | ||
return dmlc::Error(log_stream.str()); | ||
} | ||
DMLC_NO_INLINE static Entry *ThreadLocal() { | ||
static thread_local Entry *result = new Entry(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The introduction of TLS helps to reduce the size of class |
||
return result; | ||
} | ||
}; | ||
LogMessageFatal(const LogMessageFatal &); | ||
void operator=(const LogMessageFatal &); | ||
}; | ||
#endif | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright by Contributors | ||
#define DMLC_LOG_FATAL_THROW 1 | ||
|
||
#include <dmlc/logging.h> | ||
#include <gtest/gtest.h> | ||
|
||
TEST(LoggingThrow, exception) { | ||
EXPECT_THROW({ | ||
LOG(FATAL) << "message"; | ||
}, dmlc::Error); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The no-inline trick here instructs the compiler not to merge the complicated logic of throwing in the destructor into the caller. This usually wouldn't hurt performance because it is quite a slow path