windows_terminal_logger.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**************************************************************************/
  2. /* windows_terminal_logger.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "windows_terminal_logger.h"
  31. #include "core/object/script_backtrace.h"
  32. #include "core/os/os.h"
  33. #ifdef WINDOWS_ENABLED
  34. #include <cstdio>
  35. #define WIN32_LEAN_AND_MEAN
  36. #include <windows.h>
  37. void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err) {
  38. if (!should_log(p_err)) {
  39. return;
  40. }
  41. const int static_buffer_size = 1024;
  42. char static_buf[static_buffer_size];
  43. char *buf = static_buf;
  44. va_list list_copy;
  45. va_copy(list_copy, p_list);
  46. int len = vsnprintf(buf, static_buffer_size, p_format, p_list);
  47. if (len >= static_buffer_size) {
  48. buf = (char *)memalloc(len + 1);
  49. len = vsnprintf(buf, len + 1, p_format, list_copy);
  50. }
  51. va_end(list_copy);
  52. String str_buf = String::utf8(buf, len).replace("\r\n", "\n").replace("\n", "\r\n");
  53. if (len >= static_buffer_size) {
  54. memfree(buf);
  55. }
  56. CharString cstr_buf = str_buf.utf8();
  57. if (cstr_buf.length() == 0) {
  58. return;
  59. }
  60. DWORD written = 0;
  61. HANDLE h = p_err ? GetStdHandle(STD_ERROR_HANDLE) : GetStdHandle(STD_OUTPUT_HANDLE);
  62. WriteFile(h, cstr_buf.ptr(), cstr_buf.length(), &written, nullptr);
  63. #ifdef DEBUG_ENABLED
  64. FlushFileBuffers(h);
  65. #endif
  66. }
  67. void WindowsTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify, ErrorType p_type, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces) {
  68. if (!should_log(true)) {
  69. return;
  70. }
  71. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  72. if (OS::get_singleton()->get_stdout_type() != OS::STD_HANDLE_CONSOLE || !hCon || hCon == INVALID_HANDLE_VALUE) {
  73. StdLogger::log_error(p_function, p_file, p_line, p_code, p_rationale, p_editor_notify, p_type, p_script_backtraces);
  74. } else {
  75. CONSOLE_SCREEN_BUFFER_INFO sbi; //original
  76. GetConsoleScreenBufferInfo(hCon, &sbi);
  77. WORD current_bg = sbi.wAttributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
  78. uint32_t basecol = 0;
  79. switch (p_type) {
  80. case ERR_WARNING:
  81. basecol = FOREGROUND_RED | FOREGROUND_GREEN;
  82. break;
  83. case ERR_SCRIPT:
  84. basecol = FOREGROUND_RED | FOREGROUND_BLUE;
  85. break;
  86. case ERR_SHADER:
  87. basecol = FOREGROUND_GREEN | FOREGROUND_BLUE;
  88. break;
  89. case ERR_ERROR:
  90. default:
  91. basecol = FOREGROUND_RED;
  92. break;
  93. }
  94. basecol |= current_bg;
  95. SetConsoleTextAttribute(hCon, basecol | FOREGROUND_INTENSITY);
  96. logf_error("%s:", error_type_string(p_type));
  97. SetConsoleTextAttribute(hCon, basecol);
  98. if (p_rationale && p_rationale[0]) {
  99. logf_error(" %s\n", p_rationale);
  100. } else {
  101. logf_error(" %s\n", p_code);
  102. }
  103. // `FOREGROUND_INTENSITY` alone results in gray text.
  104. SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY);
  105. if (p_rationale && p_rationale[0]) {
  106. logf_error("%sat: (%s:%i)\n", error_type_indent(p_type), p_file, p_line);
  107. } else {
  108. logf_error("%sat: %s (%s:%i)\n", error_type_indent(p_type), p_function, p_file, p_line);
  109. }
  110. for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
  111. if (!backtrace->is_empty()) {
  112. logf_error("%s\n", backtrace->format(strlen(error_type_indent(p_type))).utf8().get_data());
  113. }
  114. }
  115. SetConsoleTextAttribute(hCon, sbi.wAttributes);
  116. }
  117. }
  118. WindowsTerminalLogger::~WindowsTerminalLogger() {}
  119. #endif // WINDOWS_ENABLED