forked from PaddlePaddle/VisualDL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature/glog to exception (PaddlePaddle#73)
- Loading branch information
Showing
6 changed files
with
187 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#ifndef VISUALDL_UTILS_LOGGING_H | ||
#define VISUALDL_UTILS_LOGGING_H | ||
|
||
#include <csignal> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#if defined(VISUALDL_WITH_GLOG) | ||
#include <glog/logging.h> | ||
#endif | ||
|
||
namespace visualdl { | ||
namespace logging { | ||
#if !defined(VISUALDL_WITH_GLOG) | ||
|
||
// basic log stream for INFO, ERROR, WARNING | ||
struct LogStream { | ||
LogStream(const char* file, int line) : os_(std::cerr) { | ||
os_ << "[" << file << ":" << line << "] "; | ||
} | ||
|
||
~LogStream() { os_ << "\n"; } | ||
|
||
std::ostream& stream() { return os_; } | ||
|
||
void operator=(const LogStream&) = delete; | ||
LogStream(const LogStream&) = delete; | ||
|
||
private: | ||
std::ostream& os_; | ||
}; | ||
#endif | ||
|
||
#if !defined(VISUALDL_WITH_GLOG) | ||
#if defined(VISUALDL_FATAL_ABORT) | ||
// log stream for FATAL | ||
struct LogStreamFatal : public LogStream { | ||
LogStreamFatal(const char* file, int line) : LogStream(file, line) {} | ||
~LogStreamFatal() { abort(); } | ||
|
||
LogStreamFatal(const LogStreamFatal&) = delete; | ||
void operator=(const LogStreamFatal&) = delete; | ||
}; | ||
|
||
#else | ||
|
||
struct Error : public std::runtime_error { | ||
explicit Error(const std::string& s) : std::runtime_error(s) {} | ||
}; | ||
|
||
// With exception. | ||
struct LogStreamFatal { | ||
LogStreamFatal(const char* file, int line) { | ||
ss << "[" << file << ":" << line << "] "; | ||
throw Error(ss.str()); | ||
} | ||
|
||
std::stringstream& stream() { return ss; } | ||
|
||
~LogStreamFatal() { | ||
if (!has_throw_) { | ||
std::cerr << "throw exception" << std::endl; | ||
throw Error(ss.str()); | ||
} | ||
} | ||
|
||
private: | ||
bool has_throw_{false}; | ||
mutable std::stringstream ss; | ||
}; | ||
|
||
#endif // VISUALDL_FATAL_ABORT | ||
#endif // VISUALDL_WITH_GLOG | ||
|
||
#ifndef VISUALDL_WITH_GLOG | ||
|
||
#define LOG(severity) LOG_##severity | ||
#define LOG_INFO visualdl::logging::LogStream(__FILE__, __LINE__).stream() | ||
#define LOG_WARNING LOG_INFO | ||
#define LOG_ERROR LOG_INFO | ||
#define LOG_FATAL visualdl::logging::LogStreamFatal(__FILE__, __LINE__).stream() | ||
// basic version without support for debug level. | ||
#define VLOG(x) LOG_INFO | ||
|
||
#define CHECK(cond) \ | ||
if (!(cond)) \ | ||
visualdl::logging::LogStreamFatal(__FILE__, __LINE__).stream() \ | ||
<< "Check failed: " << #cond << " " | ||
#define CHECK_EQ(v0, v1) CHECK_BINARY(v0, v1, ==) | ||
#define CHECK_GE(v0, v1) CHECK_BINARY(v0, v1, >=) | ||
#define CHECK_GT(v0, v1) CHECK_BINARY(v0, v1, >) | ||
#define CHECK_LE(v0, v1) CHECK_BINARY(v0, v1, <=) | ||
#define CHECK_LT(v0, v1) CHECK_BINARY(v0, v1, <) | ||
#define CHECK_BINARY(v0, v1, op) \ | ||
if (!(v0 op v1)) LOG_FATAL << " Check failed: " << v0 << #op << v1 << " " | ||
|
||
#endif // ifndef VISUALDL_WITH_GLOG | ||
|
||
} // namespace logging | ||
} // namespace visualdl | ||
|
||
namespace visualdl { | ||
|
||
namespace log { | ||
|
||
class NotImplementedException : public std::logic_error { | ||
public: | ||
NotImplementedException() : std::logic_error{"Function not implemented"} {} | ||
}; | ||
|
||
static void SignalHandler(int sig) { | ||
LOG(INFO) << "get signal " << sig; | ||
exit(sig); | ||
} | ||
|
||
struct __once_caller__ { | ||
__once_caller__() { std::signal(SIGINT, SignalHandler); } | ||
}; | ||
|
||
} // namespace log | ||
|
||
} // namespace visualdl | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifdef VISUALDL_WITH_GLOG | ||
#undef VISUALDL_WITH_GLOG | ||
#endif | ||
#include "visualdl/utils/logging.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(Log, LOG) { LOG(INFO) << "hello world"; } | ||
|
||
TEST(Log, CHECK) { CHECK_EQ(1, 1) << "yes this works"; } | ||
TEST(Log, CHECK_FAIL) { | ||
try { | ||
CHECK_LE(3, 2) << "this is wrong"; | ||
// throw std::runtime_error("some error"); | ||
} catch (const visualdl::logging::Error& e) { | ||
LOG(INFO) << e.what(); | ||
auto msg = std::string(e.what()); | ||
auto it = msg.find("test_logging.cc:14"); | ||
ASSERT_GT(it, 0); | ||
} catch (...) { | ||
LOG(INFO) << "catch something"; | ||
} | ||
} |