Skip to content

Commit

Permalink
feat(//core/util): New logging level for Graph Dumping
Browse files Browse the repository at this point in the history
Signed-off-by: Naren Dasan <naren@narendasan.com>
Signed-off-by: Naren Dasan <narens@nvidia.com>
  • Loading branch information
narendasan committed Apr 29, 2020
1 parent 9e8fb38 commit 90c44b9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
51 changes: 27 additions & 24 deletions core/util/logging/TRTorchLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,36 @@ namespace util {
namespace logging {

TRTorchLogger::TRTorchLogger(std::string prefix, Severity severity, bool color)
: prefix_(prefix), reportable_severity_(severity), color_(color) {}
: prefix_(prefix), reportable_severity_((LogLevel) severity), color_(color) {}

TRTorchLogger::TRTorchLogger(std::string prefix, LogLevel lvl, bool color)
: prefix_(prefix), reportable_severity_((Severity) lvl), color_(color) {}
: prefix_(prefix), reportable_severity_(lvl), color_(color) {}

void TRTorchLogger::log(LogLevel lvl, std::string msg) {
Severity severity = (Severity) lvl;
log(severity, msg.c_str());
}

void TRTorchLogger::log(Severity severity, const char* msg) {
// suppress messages with severity enum value greater than the reportable
if (severity > reportable_severity_) {
if (lvl > reportable_severity_) {
return;
}

if (color_) {
switch (severity) {
case Severity::kINTERNAL_ERROR: std::cerr << TERM_RED; break;
case Severity::kERROR: std::cerr << TERM_RED; break;
case Severity::kWARNING: std::cerr << TERM_YELLOW; break;
case Severity::kINFO: std::cerr << TERM_GREEN; break;
case Severity::kVERBOSE: std::cerr << TERM_MAGENTA; break;
switch (lvl) {
case LogLevel::kINTERNAL_ERROR: std::cerr << TERM_RED; break;
case LogLevel::kERROR: std::cerr << TERM_RED; break;
case LogLevel::kWARNING: std::cerr << TERM_YELLOW; break;
case LogLevel::kINFO: std::cerr << TERM_GREEN; break;
case LogLevel::kDEBUG: std::cerr << TERM_MAGENTA; break;
case LogLevel::kGRAPH: std::cerr << TERM_NORMAL; break;
default: break;
}
}

switch (severity) {
case Severity::kINTERNAL_ERROR: std::cerr << "INTERNAL_ERROR: "; break;
case Severity::kERROR: std::cerr << "ERROR: "; break;
case Severity::kWARNING: std::cerr << "WARNING: "; break;
case Severity::kINFO: std::cerr << "INFO: "; break;
case Severity::kVERBOSE: std::cerr << "DEBUG: "; break;
switch (lvl) {
case LogLevel::kINTERNAL_ERROR: std::cerr << "INTERNAL_ERROR: "; break;
case LogLevel::kERROR: std::cerr << "ERROR: "; break;
case LogLevel::kWARNING: std::cerr << "WARNING: "; break;
case LogLevel::kINFO: std::cerr << "INFO: "; break;
case LogLevel::kDEBUG: std::cerr << "DEBUG: "; break;
case LogLevel::kGRAPH: std::cerr << "GRAPH: "; break;
default: std::cerr << "UNKNOWN: "; break;
}

Expand All @@ -60,16 +57,22 @@ void TRTorchLogger::log(Severity severity, const char* msg) {
std::cerr << prefix_ << msg << std::endl;
}


void TRTorchLogger::log(Severity severity, const char* msg) {
LogLevel lvl = (LogLevel) severity;
log(lvl, std::string(msg));
}

void TRTorchLogger::set_logging_prefix(std::string prefix) {
prefix_ = prefix;
}

void TRTorchLogger::set_reportable_severity(Severity severity) {
reportable_severity_ = severity;
reportable_severity_ = (LogLevel) severity;
}

void TRTorchLogger::set_reportable_log_level(LogLevel lvl) {
reportable_severity_ = (Severity) lvl;
reportable_severity_ = lvl;
}

void TRTorchLogger::set_is_colored_output_on(bool colored_output_on) {
Expand All @@ -81,11 +84,11 @@ std::string TRTorchLogger::get_logging_prefix() {
}

nvinfer1::ILogger::Severity TRTorchLogger::get_reportable_severity() {
return reportable_severity_;
return (Severity) reportable_severity_;
}

LogLevel TRTorchLogger::get_reportable_log_level() {
return (LogLevel) reportable_severity_;
return reportable_severity_;
}

bool TRTorchLogger::get_is_colored_output_on() {
Expand Down
7 changes: 4 additions & 3 deletions core/util/logging/TRTorchLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ namespace core {
namespace util {
namespace logging {

enum LogLevel {
enum class LogLevel : uint8_t {
kINTERNAL_ERROR = (int) nvinfer1::ILogger::Severity::kINTERNAL_ERROR,
kERROR = (int) nvinfer1::ILogger::Severity::kERROR,
kWARNING = (int) nvinfer1::ILogger::Severity::kWARNING,
kINFO = (int) nvinfer1::ILogger::Severity::kINFO,
kDEBUG = (int) nvinfer1::ILogger::Severity::kVERBOSE,
kGRAPH
};

// Logger for TensorRT info/warning/errors
Expand All @@ -38,10 +39,10 @@ class TRTorchLogger : public nvinfer1::ILogger {

private:
std::string prefix_;
Severity reportable_severity_;
LogLevel reportable_severity_;
bool color_;
};

TRTorchLogger& get_logger();

} // namespace logging
Expand Down
7 changes: 5 additions & 2 deletions core/util/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
l.log(sev, ss.str()); \
} while (0)

#define GRAPH_DUMP_GLOBAL(s) TRTORCH_LOG(core::util::logging::get_logger(), core::util::logging::LogLevel::kGRAPH, s)
#define LOG_DEBUG_GLOBAL(s) TRTORCH_LOG(core::util::logging::get_logger(), core::util::logging::LogLevel::kDEBUG, s)
#define LOG_INFO_GLOBAL(s) TRTORCH_LOG(core::util::logging::get_logger(), core::util::logging::LogLevel::kINFO, s)
#define LOG_WARNING_GLOBAL(s) TRTORCH_LOG(core::util::logging::get_logger(), core::util::logging::LogLevel::kWARNING, s)
#define LOG_ERROR_GLOBAL(s) TRTORCH_LOG(core::util::logging::get_logger(), core::util::logging::LogLevel::kERROR, s)
#define LOG_INTERNAL_ERROR_GLOBAL(s) TRTORCH_LOG(core::util::logging::get_logger(), core::util::logging::LogLevel::kINTERNAL_ERROR, s)
#define LOG_INTERNAL_ERROR_GLOBAL(s) TRTORCH_LOG(core::util::logging::get_logger(), core::util::logging::LogLevel::kINTERNAL_ERROR, s)

#define GRAPH_DUMP_OWN(l,s) TRTORCH_LOG(l, core::util::logging::LogLevel::kGRAPH, s)
#define LOG_DEBUG_OWN(l,s) TRTORCH_LOG(l, core::util::logging::LogLevel::kDEBUG, s)
#define LOG_INFO_OWN(l,s) TRTORCH_LOG(l, core::util::logging::LogLevel::kINFO, s)
#define LOG_WARNING_OWN(l,s) TRTORCH_LOG(l, core::util::logging::LogLevel::kWARNING, s)
#define LOG_ERROR_OWN(l,s) TRTORCH_LOG(l, core::util::logging::LogLevel::kERROR, s)
#define LOG_INTERNAL_ERROR_OWN(l,s) TRTORCH_LOG(l, core::util::logging::LogLevel::kINTERNAL_ERROR, s)

#define GRAPH_DUMP(...) GET_MACRO(__VA_ARGS__, GRAPH_DUMP_OWN, GRAPH_DUMP_GLOBAL)(__VA_ARGS__)
#define LOG_DEBUG(...) GET_MACRO(__VA_ARGS__, LOG_DEBUG_OWN, LOG_DEBUG_GLOBAL)(__VA_ARGS__)
#define LOG_INFO(...) GET_MACRO(__VA_ARGS__, LOG_INFO_OWN, LOG_INFO_GLOBAL)(__VA_ARGS__)
#define LOG_WARNING(...) GET_MACRO(__VA_ARGS__, LOG_WARNING_OWN, LOG_WARNING_GLOBAL)(__VA_ARGS__)
Expand All @@ -47,7 +50,7 @@
<< ", consider filing a bug: https://www.github.com/NVIDIA/TRTorch/issues\n" \
<< __VA_ARGS__); \
}

#define TRTORCH_CHECK(cond, ...) \
if (!(cond)) { \
TRTORCH_THROW_ERROR("Expected " << #cond \
Expand Down

0 comments on commit 90c44b9

Please sign in to comment.