Skip to content

Commit

Permalink
Disable color in logs when not printing to a tty (#18662)
Browse files Browse the repository at this point in the history
### 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 <wenbinlyu@tenstorrent.com>
  • Loading branch information
wenbinlyuTT authored Mar 6, 2025
1 parent 07567d1 commit 769d3d0
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions tt_metal/api/tt-metalium/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <exception>
#include <fstream>
#include <iostream>
#include <type_traits>
#include <unistd.h>
#if defined(UTILS_LOGGER_PYTHON_OSTREAM_REDIRECT) && (UTILS_LOGGER_PYTHON_OSTREAM_REDIRECT == 1)
#include <pybind11/iostream.h>
#endif
Expand Down Expand Up @@ -40,7 +40,7 @@ namespace tt {
X(Reportify) \
X(GraphCompiler) \
X(Dispatch) \
X(Fabric) \
X(Fabric) \
X(Metal) \
X(MetalTrace)

Expand Down Expand Up @@ -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<std::underlying_type_t<Level>>(level)]) | fmt::emphasis::bold,
const auto level_style =
fmt::fg(level_color[static_cast<std::underlying_type_t<Level>>(level)]) | fmt::emphasis::bold;
const auto level_str = fmt::format(
use_styles ? level_style : fmt::text_style{},
"{:8}",
level_names[static_cast<std::underlying_type_t<Level>>(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>(args)...);
*fd << std::endl;
Expand All @@ -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");
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -192,12 +201,18 @@ static void log_debug(fmt::format_string<Args...> fmt, Args&&... args) {

template <typename... Args>
static void log_trace_(
LogType type, std::string const& src_info, fmt::format_string<std::string const&, Args...> fmt, Args&&... args) {
Logger::get().log_level_type(Logger::Level::Trace, type, fmt, src_info, std::forward<Args>(args)...);
LogType type,
const fmt::text_style src_style,
const std::string& src_info,
fmt::format_string<const std::string&, Args...> 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>(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 <typename... Args>
static void log_debug(LogType type, fmt::format_string<Args...> fmt, Args&&... args) {}
Expand Down

0 comments on commit 769d3d0

Please sign in to comment.