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

Tweak console error/warning print colors #98796

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
21 changes: 12 additions & 9 deletions drivers/unix/os_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,34 +974,37 @@ void UnixTerminalLogger::log_error(const char *p_function, const char *p_file, i
// This prevents Godot from writing ANSI escape codes when redirecting
// stdout and stderr to a file.
const bool tty = isatty(fileno(stdout));
const char *gray = tty ? "\E[0;90m" : "";
const char *red = tty ? "\E[0;91m" : "";
const char *red_bold = tty ? "\E[1;31m" : "";
const char *red_bold = tty ? "\E[1;91m" : "";
const char *red_faint = tty ? "\E[2;91m" : "";
const char *yellow = tty ? "\E[0;93m" : "";
const char *yellow_bold = tty ? "\E[1;33m" : "";
const char *yellow_bold = tty ? "\E[1;93m" : "";
const char *yellow_faint = tty ? "\E[2;93m" : "";
const char *magenta = tty ? "\E[0;95m" : "";
const char *magenta_bold = tty ? "\E[1;35m" : "";
const char *magenta_bold = tty ? "\E[1;95m" : "";
const char *magenta_faint = tty ? "\E[2;95m" : "";
const char *cyan = tty ? "\E[0;96m" : "";
const char *cyan_bold = tty ? "\E[1;36m" : "";
const char *cyan_bold = tty ? "\E[1;96m" : "";
const char *cyan_faint = tty ? "\E[2;96m" : "";
const char *reset = tty ? "\E[0m" : "";

switch (p_type) {
case ERR_WARNING:
logf_error("%sWARNING:%s %s\n", yellow_bold, yellow, err_details);
logf_error("%s at: %s (%s:%i)%s\n", gray, p_function, p_file, p_line, reset);
logf_error("%s at: %s (%s:%i)%s\n", yellow_faint, p_function, p_file, p_line, reset);
break;
case ERR_SCRIPT:
logf_error("%sSCRIPT ERROR:%s %s\n", magenta_bold, magenta, err_details);
logf_error("%s at: %s (%s:%i)%s\n", gray, p_function, p_file, p_line, reset);
logf_error("%s at: %s (%s:%i)%s\n", magenta_faint, p_function, p_file, p_line, reset);
break;
case ERR_SHADER:
logf_error("%sSHADER ERROR:%s %s\n", cyan_bold, cyan, err_details);
logf_error("%s at: %s (%s:%i)%s\n", gray, p_function, p_file, p_line, reset);
logf_error("%s at: %s (%s:%i)%s\n", cyan_faint, p_function, p_file, p_line, reset);
break;
case ERR_ERROR:
default:
logf_error("%sERROR:%s %s\n", red_bold, red, err_details);
logf_error("%s at: %s (%s:%i)%s\n", gray, p_function, p_file, p_line, reset);
logf_error("%s at: %s (%s:%i)%s\n", red_faint, p_function, p_file, p_line, reset);
break;
}
}
Expand Down
34 changes: 26 additions & 8 deletions platform/macos/macos_terminal_logger.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,53 @@
err_details = p_code;
}

// Disable color codes if stdout is not a TTY.
// This prevents Godot from writing ANSI escape codes when redirecting
// stdout and stderr to a file.
const bool tty = isatty(fileno(stdout));
const char *red = tty ? "\E[0;91m" : "";
const char *red_bold = tty ? "\E[1;91m" : "";
const char *red_faint = tty ? "\E[2;91m" : "";
const char *yellow = tty ? "\E[0;93m" : "";
const char *yellow_bold = tty ? "\E[1;93m" : "";
const char *yellow_faint = tty ? "\E[2;93m" : "";
const char *magenta = tty ? "\E[0;95m" : "";
const char *magenta_bold = tty ? "\E[1;95m" : "";
const char *magenta_faint = tty ? "\E[2;95m" : "";
const char *cyan = tty ? "\E[0;96m" : "";
const char *cyan_bold = tty ? "\E[1;96m" : "";
const char *cyan_faint = tty ? "\E[2;96m" : "";
const char *reset = tty ? "\E[0m" : "";

switch (p_type) {
case ERR_WARNING:
os_log_info(OS_LOG_DEFAULT,
"WARNING: %{public}s\nat: %{public}s (%{public}s:%i)",
err_details, p_function, p_file, p_line);
logf_error("\E[1;33mWARNING:\E[0;93m %s\n", err_details);
logf_error("\E[0;90m at: %s (%s:%i)\E[0m\n", p_function, p_file, p_line);
logf_error("%sWARNING:%s %s\n", yellow_bold, yellow, err_details);
logf_error("%s at: %s (%s:%i)%s\n", yellow_faint, p_function, p_file, p_line, reset);
break;
case ERR_SCRIPT:
os_log_error(OS_LOG_DEFAULT,
"SCRIPT ERROR: %{public}s\nat: %{public}s (%{public}s:%i)",
err_details, p_function, p_file, p_line);
logf_error("\E[1;35mSCRIPT ERROR:\E[0;95m %s\n", err_details);
logf_error("\E[0;90m at: %s (%s:%i)\E[0m\n", p_function, p_file, p_line);
logf_error("%sSCRIPT ERROR:%s %s\n", magenta_bold, magenta, err_details);
logf_error("%s at: %s (%s:%i)%s\n", magenta_faint, p_function, p_file, p_line, reset);
break;
case ERR_SHADER:
os_log_error(OS_LOG_DEFAULT,
"SHADER ERROR: %{public}s\nat: %{public}s (%{public}s:%i)",
err_details, p_function, p_file, p_line);
logf_error("\E[1;36mSHADER ERROR:\E[0;96m %s\n", err_details);
logf_error("\E[0;90m at: %s (%s:%i)\E[0m\n", p_function, p_file, p_line);
logf_error("%sSHADER ERROR:%s %s\n", cyan_bold, cyan, err_details);
logf_error("%s at: %s (%s:%i)%s\n", cyan_faint, p_function, p_file, p_line, reset);
break;
case ERR_ERROR:
default:
os_log_error(OS_LOG_DEFAULT,
"ERROR: %{public}s\nat: %{public}s (%{public}s:%i)",
err_details, p_function, p_file, p_line);
logf_error("\E[1;31mERROR:\E[0;91m %s\n", err_details);
logf_error("\E[0;90m at: %s (%s:%i)\E[0m\n", p_function, p_file, p_line);
logf_error("%sERROR:%s %s\n", red_bold, red, err_details);
logf_error("%s at: %s (%s:%i)%s\n", red_faint, p_function, p_file, p_line, reset);
break;
}
}
Expand Down
89 changes: 32 additions & 57 deletions platform/windows/windows_terminal_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,76 +81,51 @@
if (!hCon || hCon == INVALID_HANDLE_VALUE) {
StdLogger::log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
} else {
CONSOLE_SCREEN_BUFFER_INFO sbi; //original
GetConsoleScreenBufferInfo(hCon, &sbi);

WORD current_bg = sbi.wAttributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);

uint32_t basecol = 0;
switch (p_type) {
case ERR_ERROR:
basecol = FOREGROUND_RED;
break;
case ERR_WARNING:
basecol = FOREGROUND_RED | FOREGROUND_GREEN;
break;
case ERR_SCRIPT:
basecol = FOREGROUND_RED | FOREGROUND_BLUE;
break;
case ERR_SHADER:
basecol = FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
const char *err_details;
if (p_rationale && p_rationale[0]) {
err_details = p_rationale;
} else {
err_details = p_code;
}

basecol |= current_bg;
// Disable color codes if stdout is not a TTY.
// This prevents Godot from writing ANSI escape codes when redirecting
// stdout and stderr to a file.
//
// FIXME: Is there a way to check whether stdout is a TTY on Windows?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const char *red = "\E[0;91m";

Check failure on line 96 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

the following warning is treated as an error

Check warning on line 96 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'E': unrecognized character escape sequence

Check failure on line 96 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

the following warning is treated as an error

Check warning on line 96 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'E': unrecognized character escape sequence
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably avoid using \E as our escape code, as it doesn't actually appear in the standard1. All existing instances can be converted to \x1b or \033 instead, similar to what we use in SCons

Suggested change
const char *red = "\E[0;91m";
const char *red = "\x1b[0;91m";

Footnotes

  1. https://en.wikipedia.org/wiki/Escape_sequences_in_C#endnote_Note1

const char *red_bold = "\E[1;91m";

Check warning on line 97 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'E': unrecognized character escape sequence

Check warning on line 97 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'E': unrecognized character escape sequence
const char *red_faint = "\E[2;91m";

Check warning on line 98 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'E': unrecognized character escape sequence

Check warning on line 98 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'E': unrecognized character escape sequence
const char *yellow = "\E[0;93m";

Check warning on line 99 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'E': unrecognized character escape sequence

Check warning on line 99 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'E': unrecognized character escape sequence
const char *yellow_bold = "\E[1;93m";

Check warning on line 100 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'E': unrecognized character escape sequence

Check warning on line 100 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'E': unrecognized character escape sequence
const char *yellow_faint = "\E[2;93m";

Check warning on line 101 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'E': unrecognized character escape sequence

Check warning on line 101 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'E': unrecognized character escape sequence
const char *magenta = "\E[0;95m";

Check warning on line 102 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'E': unrecognized character escape sequence

Check warning on line 102 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'E': unrecognized character escape sequence
const char *magenta_bold = "\E[1;95m";

Check warning on line 103 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'E': unrecognized character escape sequence

Check warning on line 103 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'E': unrecognized character escape sequence
const char *magenta_faint = "\E[2;95m";

Check warning on line 104 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'E': unrecognized character escape sequence

Check warning on line 104 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'E': unrecognized character escape sequence
const char *cyan = "\E[0;96m";

Check warning on line 105 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'E': unrecognized character escape sequence

Check warning on line 105 in platform/windows/windows_terminal_logger.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release, tests=yes)

'E': unrecognized character escape sequence
const char *cyan_bold = "\E[1;96m";
const char *cyan_faint = "\E[2;96m";
const char *reset = "\E[0m";

SetConsoleTextAttribute(hCon, basecol | FOREGROUND_INTENSITY);
switch (p_type) {
case ERR_ERROR:
logf_error("ERROR:");
break;
case ERR_WARNING:
logf_error("WARNING:");
logf_error("%sWARNING:%s %s\n", yellow_bold, yellow, err_details);
logf_error("%s at: %s (%s:%i)%s\n", yellow_faint, p_function, p_file, p_line, reset);
break;
case ERR_SCRIPT:
logf_error("SCRIPT ERROR:");
logf_error("%sSCRIPT ERROR:%s %s\n", magenta_bold, magenta, err_details);
logf_error("%s at: %s (%s:%i)%s\n", magenta_faint, p_function, p_file, p_line, reset);
break;
case ERR_SHADER:
logf_error("SHADER ERROR:");
logf_error("%sSHADER ERROR:%s %s\n", cyan_bold, cyan, err_details);
logf_error("%s at: %s (%s:%i)%s\n", cyan_faint, p_function, p_file, p_line, reset);
break;
}

SetConsoleTextAttribute(hCon, basecol);
if (p_rationale && p_rationale[0]) {
logf_error(" %s\n", p_rationale);
} else {
logf_error(" %s\n", p_code);
}

// `FOREGROUND_INTENSITY` alone results in gray text.
SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY);
switch (p_type) {
case ERR_ERROR:
logf_error(" at: ");
break;
case ERR_WARNING:
logf_error(" at: ");
break;
case ERR_SCRIPT:
logf_error(" at: ");
break;
case ERR_SHADER:
logf_error(" at: ");
default:
logf_error("%sERROR:%s %s\n", red_bold, red, err_details);
logf_error("%s at: %s (%s:%i)%s\n", red_faint, p_function, p_file, p_line, reset);
break;
}

if (p_rationale && p_rationale[0]) {
logf_error("(%s:%i)\n", p_file, p_line);
} else {
logf_error("%s (%s:%i)\n", p_function, p_file, p_line);
}

SetConsoleTextAttribute(hCon, sbi.wAttributes);
}
}

Expand Down
Loading