Skip to content

Commit

Permalink
Add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
junrushao committed May 21, 2020
1 parent 0080b31 commit 547935b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
19 changes: 11 additions & 8 deletions include/dmlc/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,38 +426,41 @@ 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
LOG(ERROR) << log_stream.str();
#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

Expand Down
11 changes: 11 additions & 0 deletions test/unittest/unittest_logging_throw.cc
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);
}

0 comments on commit 547935b

Please sign in to comment.