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 colorful support #1735

Merged
merged 2 commits into from
Mar 7, 2024
Merged

Logging colorful support #1735

merged 2 commits into from
Mar 7, 2024

Conversation

halx99
Copy link
Collaborator

@halx99 halx99 commented Mar 7, 2024

Describe your changes

f326f483ed3d6cdf6d543d2e0e61258f
constexpr auto colorCodeOfLevel = [](LogLevel level) ->std::string_view
{
    switch (level)
    {
    case LogLevel::Info:
        return "\x1b[92m"sv; // rgb: \x1b[38;2;144;238;144m"sv
    case LogLevel::Warn:
        return "\x1b[33m"sv; // rgb: "\x1b[38;2;255;255;000m"sv
    case LogLevel::Error:
        return  "\x1b[31m"sv; // rgb: "\x1b[38;2;255;000;000m"sv
    default:
        return std::string_view{};
    }
};

Issue ticket number and link

Checklist before requesting a review

For each PR

  • Add Copyright if it missed:
    - "Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md)."

  • I have performed a self-review of my code.

    Optional:

    • I have checked readme and add important infos to this PR.
    • I have added/adapted some tests too.

For core/new feature PR

  • I have checked readme and add important infos to this PR.
  • I have added thorough tests.

Further works

check does in terminal and is color terminal

// Determine if the terminal supports colors
bool is_color_terminal() {
#ifdef _WIN32
    return true;
#else

    static const bool result = []() {
        const char *env_colorterm_p = std::getenv("COLORTERM");
        if (env_colorterm_p != nullptr) {
            return true;
        }

        static constexpr std::array<const char *, 16> terms = {
            {"ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm", "linux", "msys",
             "putty", "rxvt", "screen", "vt100", "xterm", "alacritty", "vt102"}};

        const char *env_term_p = std::getenv("TERM");
        if (env_term_p == nullptr) {
            return false;
        }

        return std::any_of(terms.begin(), terms.end(), [&](const char *term) {
            return std::strstr(env_term_p, term) != nullptr;
        });
    }();

    return result;
#endif
}

// Determine if the terminal attached
bool in_terminal(FILE *file) {
#ifdef _WIN32
    return ::_isatty(_fileno(file)) != 0;
#else
    return ::isatty(fileno(file)) != 0;
#endif
}

@halx99 halx99 added this to the 2.1.3 milestone Mar 7, 2024
@halx99 halx99 added the enhancement New feature or request label Mar 7, 2024
@halx99 halx99 merged commit 931ca91 into dev Mar 7, 2024
15 checks passed
@halx99 halx99 deleted the logging-colorful-support branch March 7, 2024 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant