Skip to content

Commit

Permalink
Colorize terminal log output (microsoft#17196)
Browse files Browse the repository at this point in the history
Make eyeball log parsing a little bit easier.
  • Loading branch information
cloudhan authored and kleiti committed Mar 22, 2024
1 parent 75dccfc commit bd2fa80
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions onnxruntime/core/common/logging/sinks/ostream_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
namespace onnxruntime {
namespace logging {

#ifndef ORT_MINIMAL_BUILD
struct Color {
constexpr static const char* kWarn = "\033[0;93m"; // yellow
constexpr static const char* kError = "\033[1;31m"; // bold red
constexpr static const char* kFatal = "\033[1;37;41m"; // bold white on red background
constexpr static const char* kEnd = "\033[m";
#ifdef _WIN32
constexpr static const wchar_t* kLWarn = L"\033[0;93m"; // yellow
constexpr static const wchar_t* kLError = L"\033[1;31m"; // bold red
constexpr static const wchar_t* kLFatal = L"\033[1;37;41m"; // bold white on red background
constexpr static const wchar_t* kLEnd = L"\033[m";
#endif
};
#endif

void OStreamSink::SendImpl(const Timestamp& timestamp, const std::string& logger_id, const Capture& message) {
// operator for formatting of timestamp in ISO8601 format including microseconds
using date::operator<<;
Expand All @@ -20,9 +35,27 @@ void OStreamSink::SendImpl(const Timestamp& timestamp, const std::string& logger

std::ostringstream msg;

#ifndef ORT_MINIMAL_BUILD
if (message.Severity() == Severity::kWARNING) {
msg << Color::kWarn;
} else if (message.Severity() == Severity::kERROR) {
msg << Color::kError;
} else if (message.Severity() == Severity::kFATAL) {
msg << Color::kFatal;
}
#endif

msg << timestamp << " [" << message.SeverityPrefix() << ":" << message.Category() << ":" << logger_id << ", "
<< message.Location().ToString() << "] " << message.Message() << "\n";

#ifndef ORT_MINIMAL_BUILD
if (message.Severity() == Severity::kWARNING ||
message.Severity() == Severity::kERROR ||
message.Severity() == Severity::kFATAL) {
msg << Color::kEnd;
}
#endif

(*stream_) << msg.str();

if (flush_) {
Expand All @@ -43,9 +76,27 @@ void WOStreamSink::SendImpl(const Timestamp& timestamp, const std::string& logge

std::wostringstream msg;

#ifndef ORT_MINIMAL_BUILD
if (message.Severity() == Severity::kWARNING) {
msg << Color::kLWarn;
} else if (message.Severity() == Severity::kERROR) {
msg << Color::kLError;
} else if (message.Severity() == Severity::kFATAL) {
msg << Color::kLFatal;
}
#endif

msg << timestamp << L" [" << message.SeverityPrefix() << L":" << message.Category() << L":" << ToWideString(logger_id) << L", "
<< ToWideString(message.Location().ToString()) << L"] " << ToWideString(message.Message()) << L"\n";

#ifndef ORT_MINIMAL_BUILD
if (message.Severity() == Severity::kWARNING ||
message.Severity() == Severity::kERROR ||
message.Severity() == Severity::kFATAL) {
msg << Color::kLEnd;
}
#endif

(*stream_) << msg.str();

if (flush_) {
Expand Down

0 comments on commit bd2fa80

Please sign in to comment.