diff --git a/include/dmlc/logging.h b/include/dmlc/logging.h index cd03a97461..c0ce17a7bf 100644 --- a/include/dmlc/logging.h +++ b/include/dmlc/logging.h @@ -426,24 +426,27 @@ class LogMessageFatal : public LogMessage { #else class LogMessageFatal { public: - LogMessageFatal(const char* file, int line) { + LogMessageFatal(const char *file, int line) { Entry::ThreadLocal()->Init(file, line); } std::ostringstream &stream() { return Entry::ThreadLocal()->log_stream; } DMLC_NO_INLINE ~LogMessageFatal() DMLC_THROW_EXCEPTION { #if DMLC_LOG_STACK_TRACE - Entry::ThreadLocal()->log_stream << "\n" << StackTrace(1, LogStackTraceLevel()) << "\n"; + Entry::ThreadLocal()->log_stream << "\n" + << StackTrace(1, LogStackTraceLevel()) + << "\n"; #endif throw Entry::ThreadLocal()->Finalize(); } + private: struct Entry { std::ostringstream log_stream; - DMLC_NO_INLINE void Init(const char* file, int line) { + DMLC_NO_INLINE void Init(const char *file, int line) { DateLogger date; log_stream = std::ostringstream(); - log_stream << "[" << date.HumanDate() << "] " << file << ":" - << line << ": "; + log_stream << "[" << date.HumanDate() << "] " << file << ":" << line + << ": "; } dmlc::Error Finalize() { #if DMLC_LOG_BEFORE_THROW @@ -451,13 +454,13 @@ class LogMessageFatal { #endif return dmlc::Error(log_stream.str()); } - DMLC_NO_INLINE static Entry* ThreadLocal() { + DMLC_NO_INLINE static Entry *ThreadLocal() { static thread_local Entry *result = new Entry(); return result; } }; - LogMessageFatal(const LogMessageFatal&); - void operator=(const LogMessageFatal&); + LogMessageFatal(const LogMessageFatal &); + void operator=(const LogMessageFatal &); }; #endif diff --git a/test/unittest/unittest_logging_throw.cc b/test/unittest/unittest_logging_throw.cc new file mode 100644 index 0000000000..e76efcca14 --- /dev/null +++ b/test/unittest/unittest_logging_throw.cc @@ -0,0 +1,11 @@ +// Copyright by Contributors +#define DMLC_LOG_FATAL_THROW 1 + +#include +#include + +TEST(LoggingThrow, exception) { + EXPECT_THROW({ + LOG(FATAL) << "message"; + }, dmlc::Error); +}