Colorize terminal log output (#17196)

Make eyeball log parsing a little bit easier.
This commit is contained in:
cloudhan 2023-08-24 17:38:21 +08:00 committed by GitHub
parent 34d18ee076
commit 94f23882f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<<;
@ -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_) {
@ -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_) {