Hotfix for glog PR (#2734)

This commit is contained in:
Yuhong Guo 2018-08-25 07:30:51 +08:00 committed by Robert Nishihara
parent d16b6f6a32
commit 697bfb14db
3 changed files with 45 additions and 23 deletions

View file

@ -114,10 +114,11 @@ void RayLog::ShutDownRayLog() {
#endif
}
RayLog::RayLog(const char *file_name, int line_number, int severity) {
RayLog::RayLog(const char *file_name, int line_number, int severity)
// glog does not have DEBUG level, we can handle it here.
: is_enabled_(severity >= severity_threshold_) {
#ifdef RAY_USE_GLOG
// glog does not have DEBUG level, we can handle it here.
if (severity >= severity_threshold_) {
if (is_enabled_) {
logging_provider_.reset(
new google::LogMessage(file_name, line_number, GetMappedSeverity(severity)));
}
@ -129,12 +130,16 @@ RayLog::RayLog(const char *file_name, int line_number, int severity) {
std::ostream &RayLog::Stream() {
#ifdef RAY_USE_GLOG
// Before calling this function, user should check IsEnabled.
// When IsEnabled == false, logging_provider_ will be empty.
return logging_provider_->stream();
#else
return logging_provider_->Stream();
#endif
}
bool RayLog::IsEnabled() const { return is_enabled_; }
RayLog::~RayLog() { logging_provider_.reset(); }
} // namespace ray

View file

@ -65,28 +65,32 @@ class RayLogBase {
public:
virtual ~RayLogBase(){};
virtual bool IsEnabled() const { return false; };
template <typename T>
RayLogBase &operator<<(const T &t) {
RAY_IGNORE_EXPR(t);
if (IsEnabled()) {
Stream() << t;
} else {
RAY_IGNORE_EXPR(t);
}
return *this;
}
protected:
virtual std::ostream &Stream() { return std::cerr; };
};
class RayLog : public RayLogBase {
public:
RayLog(const char *file_name, int line_number, int severity);
virtual ~RayLog();
template <typename T>
RayLogBase &operator<<(const T &t) {
if (logging_provider_ == nullptr) {
// This means the logging level is lower than the threshold.
RAY_IGNORE_EXPR(t);
} else {
this->Stream() << t;
}
return *this;
}
/// Return whether or not logging is enabled.
///
/// \return True if logging is enabled and false otherwise.
virtual bool IsEnabled() const;
// The init function of ray log for a program which should be called only once.
// If logDir is empty, the log won't output to file.
@ -96,9 +100,13 @@ class RayLog : public RayLogBase {
static void ShutDownRayLog();
private:
std::ostream &Stream();
std::unique_ptr<LoggingProvider> logging_provider_;
/// True if log messages should be logged and false if they should be ignored.
bool is_enabled_;
static int severity_threshold_;
protected:
virtual std::ostream &Stream();
};
// This class make RAY_CHECK compilation pass to change the << operator to void.

View file

@ -18,11 +18,17 @@ int64_t current_time_ms() {
// This file just print some information using the logging macro.
void PrintLog() {
RAY_LOG(DEBUG) << "This is the DEBUG message";
RAY_LOG(INFO) << "This is the INFO message";
RAY_LOG(WARNING) << "This is the WARNING message";
RAY_LOG(ERROR) << "This is the ERROR message";
RAY_CHECK(true) << "This is a RAY_CHECK message but it won't show up";
RAY_LOG(DEBUG) << "This is the"
<< " DEBUG"
<< " message";
RAY_LOG(INFO) << "This is the"
<< " INFO message";
RAY_LOG(WARNING) << "This is the"
<< " WARNING message";
RAY_LOG(ERROR) << "This is the"
<< " ERROR message";
RAY_CHECK(true) << "This is a RAY_CHECK"
<< " message but it won't show up";
// The following 2 lines should not run since it will cause program failure.
// RAY_LOG(FATAL) << "This is the FATAL message";
// RAY_CHECK(false) << "This is a RAY_CHECK message but it won't show up";
@ -47,7 +53,8 @@ TEST(LogPerfTest, PerfTest) {
int64_t start_time = current_time_ms();
for (int i = 0; i < rounds; ++i) {
RAY_LOG(DEBUG) << "This is the RAY_DEBUG message";
RAY_LOG(DEBUG) << "This is the "
<< "RAY_DEBUG message";
}
int64_t elapsed = current_time_ms() - start_time;
std::cout << "Testing DEBUG log for " << rounds << " rounds takes " << elapsed << " ms."
@ -55,7 +62,8 @@ TEST(LogPerfTest, PerfTest) {
start_time = current_time_ms();
for (int i = 0; i < rounds; ++i) {
RAY_LOG(ERROR) << "This is the RAY_ERROR message";
RAY_LOG(ERROR) << "This is the "
<< "RAY_ERROR message";
}
elapsed = current_time_ms() - start_time;
std::cout << "Testing RAY_ERROR log for " << rounds << " rounds takes " << elapsed
@ -63,7 +71,8 @@ TEST(LogPerfTest, PerfTest) {
start_time = current_time_ms();
for (int i = 0; i < rounds; ++i) {
RAY_CHECK(i >= 0) << "This is a RAY_CHECK message but it won't show up";
RAY_CHECK(i >= 0) << "This is a RAY_CHECK "
<< "message but it won't show up";
}
elapsed = current_time_ms() - start_time;
std::cout << "Testing RAY_CHECK(true) for " << rounds << " rounds takes " << elapsed