Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging: Generalize terminal color detection #9808

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions external/easylogging++/easylogging++.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1244,11 +1244,19 @@ std::string OS::currentHost(void) {
#endif // ELPP_OS_UNIX && !ELPP_OS_ANDROID
}

static bool endswith(const std::string &s, const std::string &ending)
{
return s.size() >= ending.size() && s.substr(s.size() - ending.size()) == ending;
}

bool OS::termSupportsColor(std::string& term) {
return term == "xterm" || term == "screen" || term == "linux" || term == "cygwin"
|| endswith(term, "-color") || endswith(term, "-256color");
}

bool OS::termSupportsColor(void) {
std::string term = getEnvironmentVariable("TERM", "");
return term == "xterm" || term == "xterm-color" || term == "xterm-256color"
|| term == "screen" || term == "linux" || term == "cygwin"
|| term == "screen-256color" || term == "screen.xterm-256color";
return termSupportsColor(term);
}

// DateTime
Expand Down
2 changes: 2 additions & 0 deletions external/easylogging++/easylogging++.h
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,8 @@ class OS : base::StaticClass {
/// @detail For android systems this is device name with its manufacturer and model seperated by hyphen
static std::string currentHost(void);
/// @brief Whether or not terminal supports colors
static bool termSupportsColor(std::string& term);
/// @brief Whether or not terminal supports colors
static bool termSupportsColor(void);
};
/// @brief Contains utilities for cross-platform date/time. This class make use of el::base::utils::Str
Expand Down
35 changes: 35 additions & 0 deletions tests/unit_tests/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,41 @@ TEST(logging, multiline)
cleanup();
}

class LoggingTermSupportsColorSuite : public testing::TestWithParam<std::tuple<std::string, bool>> {};

TEST_P(LoggingTermSupportsColorSuite, Detection)
{
std::tuple<std::string, bool> param = GetParam();
auto term = std::get<0>(param);
auto is_color = std::get<1>(param);
ASSERT_EQ(el::base::utils::OS::termSupportsColor(term), is_color) << term;
}
INSTANTIATE_TEST_SUITE_P(
TerminalStrings,
LoggingTermSupportsColorSuite,
testing::Values(
std::make_tuple("", false),
// unrecognized terminals
std::make_tuple("basic", false),
std::make_tuple("vt100", false),
// known color terminals
std::make_tuple("xterm", true),
std::make_tuple("screen", true),
std::make_tuple("linux", true),
std::make_tuple("cygwin", true),
std::make_tuple("xterm-color", true),
std::make_tuple("xterm-256color", true),
std::make_tuple("screen-256color", true),
std::make_tuple("screen.xterm-256color", true),
// generic color terminal detection by suffix
std::make_tuple("unrecognized-color", true),
std::make_tuple("unrecognized-256color", true),
std::make_tuple("basic-nocolor", false),
std::make_tuple("basic-no256color", false),
std::make_tuple("basic-color-unsupported", false),
std::make_tuple("basic-256color-unsupported", false)
));

// These operations might segfault
TEST(logging, copy_ctor_segfault)
{
Expand Down
Loading