From 769d3d043790b598b62b8438c55fd03f21accf6a Mon Sep 17 00:00:00 2001 From: Wenbin Lyu Date: Thu, 6 Mar 2025 11:01:00 -0600 Subject: [PATCH] Disable color in logs when not printing to a tty (#18662) ### Ticket N/A ### Problem description The logger applies text styling that only work in the terminal, if we use the log file env var or redirect `stdout` to a file then we end up with ``` ^[[38;2;000;128;000m Metal^[[0m | ^[[1m^[[38;2;100;149;237mINFO ^[[0m | Initializing device 0. Program cache is NOT enabled ^[[38;2;000;128;000m Metal^[[0m | ^[[1m^[[38;2;100;149;237mINFO ^[[0m | AI CLK for device 0 is: 1000 MHz ^[[38;2;000;128;000m Metal^[[0m | ^[[1m^[[38;2;100;149;237mINFO ^[[0m | Enabling program cache on device 0 ``` ### What's changed Modify the logger to disable all `fmt` styling if the output isn't a tty. Signed-off-by: wenbinlyuTT --- tt_metal/api/tt-metalium/logger.hpp | 31 +++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/tt_metal/api/tt-metalium/logger.hpp b/tt_metal/api/tt-metalium/logger.hpp index e3d0a2ff924..e14a531ef82 100644 --- a/tt_metal/api/tt-metalium/logger.hpp +++ b/tt_metal/api/tt-metalium/logger.hpp @@ -7,10 +7,10 @@ #include #include #include -#include #include #include #include +#include #if defined(UTILS_LOGGER_PYTHON_OSTREAM_REDIRECT) && (UTILS_LOGGER_PYTHON_OSTREAM_REDIRECT == 1) #include #endif @@ -40,7 +40,7 @@ namespace tt { X(Reportify) \ X(GraphCompiler) \ X(Dispatch) \ - X(Fabric) \ + X(Fabric) \ X(Metal) \ X(MetalTrace) @@ -116,11 +116,14 @@ class Logger { #if defined(UTILS_LOGGER_PYTHON_OSTREAM_REDIRECT) && (UTILS_LOGGER_PYTHON_OSTREAM_REDIRECT == 1) pybind11::scoped_ostream_redirect stream(*fd); #endif - std::string level_str = fmt::format( - fmt::fg(level_color[static_cast>(level)]) | fmt::emphasis::bold, + const auto level_style = + fmt::fg(level_color[static_cast>(level)]) | fmt::emphasis::bold; + const auto level_str = fmt::format( + use_styles ? level_style : fmt::text_style{}, "{:8}", level_names[static_cast>(level)]); - std::string type_str = fmt::format(fmt::fg(fmt::color::green), "{:>23}", type_names[type]); + const auto type_style = fmt::fg(fmt::color::green); + const auto type_str = fmt::format(use_styles ? type_style : fmt::text_style{}, "{:>23}", type_names[type]); fmt::print(*fd, "{} | {} | ", type_str, level_str); fmt::print(*fd, fmt, std::forward(args)...); *fd << std::endl; @@ -129,6 +132,8 @@ class Logger { void flush() { *fd << std::flush; } + bool get_use_styles() const { return this->use_styles; } + private: Logger() { static char const* env = std::getenv("TT_METAL_LOGGER_TYPES"); @@ -161,12 +166,15 @@ class Logger { } } + use_styles = isatty(fileno(stdout)); + #if !defined(UTILS_LOGGER_PYTHON_OSTREAM_REDIRECT) || (UTILS_LOGGER_PYTHON_OSTREAM_REDIRECT == 0) static char const* file_env = std::getenv("TT_METAL_LOGGER_FILE"); if (file_env) { log_file.open(file_env); if (log_file.is_open()) { fd = &log_file; + use_styles = false; } } #endif @@ -176,6 +184,7 @@ class Logger { std::ostream* fd = &std::cout; std::uint64_t mask = (1 << LogAlways); Level min_level = Level::Info; + bool use_styles = true; }; #pragma GCC visibility pop @@ -192,12 +201,18 @@ static void log_debug(fmt::format_string fmt, Args&&... args) { template static void log_trace_( - LogType type, std::string const& src_info, fmt::format_string fmt, Args&&... args) { - Logger::get().log_level_type(Logger::Level::Trace, type, fmt, src_info, std::forward(args)...); + LogType type, + const fmt::text_style src_style, + const std::string& src_info, + fmt::format_string fmt, + Args&&... args) { + const bool use_styles = Logger::get().get_use_styles(); + const auto src_info_stylized = use_styles ? fmt::format(src_style, fmt::runtime(src_info)) : src_info; + Logger::get().log_level_type(Logger::Level::Trace, type, fmt, src_info_stylized, std::forward(args)...); } #define log_trace(log_type, ...) \ - log_trace_(log_type, fmt::format(fmt::fg(fmt::color::green), "{}:{}", __FILE__, __LINE__), "{} - " __VA_ARGS__) + log_trace_(log_type, fmt::fg(fmt::color::green), fmt::format("{}:{}", __FILE__, __LINE__), "{} - " __VA_ARGS__) #else template static void log_debug(LogType type, fmt::format_string fmt, Args&&... args) {}