windows_terminal_logger.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/os/os.h"
  32. #ifdef WINDOWS_ENABLED
  33. #include <stdio.h>
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <windows.h>
  36. void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err) {
  37. if (!should_log(p_err)) {
  38. return;
  39. }
  40. const int static_buffer_size = 1024;
  41. char static_buf[static_buffer_size];
  42. char *buf = static_buf;
  43. va_list list_copy;
  44. va_copy(list_copy, p_list);
  45. int len = vsnprintf(buf, static_buffer_size, p_format, p_list);
  46. if (len >= static_buffer_size) {
  47. buf = (char *)memalloc(len + 1);
  48. len = vsnprintf(buf, len + 1, p_format, list_copy);
  49. }
  50. va_end(list_copy);
  51. String str_buf = String::utf8(buf, len).replace("\r\n", "\n").replace("\n", "\r\n");
  52. if (len >= static_buffer_size) {
  53. memfree(buf);
  54. }
  55. CharString cstr_buf = str_buf.utf8();
  56. if (cstr_buf.length() == 0) {
  57. return;
  58. }
  59. DWORD written = 0;
  60. HANDLE h = p_err ? GetStdHandle(STD_ERROR_HANDLE) : GetStdHandle(STD_OUTPUT_HANDLE);
  61. WriteFile(h, cstr_buf.ptr(), cstr_buf.length(), &written, nullptr);
  62. #ifdef DEBUG_ENABLED
  63. FlushFileBuffers(h);
  64. #endif
  65. }
  66. 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) {
  67. if (!should_log(true)) {
  68. return;
  69. }
  70. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  71. if (OS::get_singleton()->get_stdout_type() != OS::STD_HANDLE_CONSOLE || !hCon || hCon == INVALID_HANDLE_VALUE) {
  72. StdLogger::log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
  73. } else {
  74. CONSOLE_SCREEN_BUFFER_INFO sbi; //original
  75. GetConsoleScreenBufferInfo(hCon, &sbi);
  76. WORD current_bg = sbi.wAttributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
  77. uint32_t basecol = 0;
  78. switch (p_type) {
  79. case ERR_ERROR:
  80. basecol = FOREGROUND_RED;
  81. break;
  82. case ERR_WARNING:
  83. basecol = FOREGROUND_RED | FOREGROUND_GREEN;
  84. break;
  85. case ERR_SCRIPT:
  86. basecol = FOREGROUND_RED | FOREGROUND_BLUE;
  87. break;
  88. case ERR_SHADER:
  89. basecol = FOREGROUND_GREEN | FOREGROUND_BLUE;
  90. break;
  91. }
  92. basecol |= current_bg;
  93. SetConsoleTextAttribute(hCon, basecol | FOREGROUND_INTENSITY);
  94. switch (p_type) {
  95. case ERR_ERROR:
  96. logf_error("ERROR:");
  97. break;
  98. case ERR_WARNING:
  99. logf_error("WARNING:");
  100. break;
  101. case ERR_SCRIPT:
  102. logf_error("SCRIPT ERROR:");
  103. break;
  104. case ERR_SHADER:
  105. logf_error("SHADER ERROR:");
  106. break;
  107. }
  108. SetConsoleTextAttribute(hCon, basecol);
  109. if (p_rationale && p_rationale[0]) {
  110. logf_error(" %s\n", p_rationale);
  111. } else {
  112. logf_error(" %s\n", p_code);
  113. }
  114. // `FOREGROUND_INTENSITY` alone results in gray text.
  115. SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY);
  116. switch (p_type) {
  117. case ERR_ERROR:
  118. logf_error(" at: ");
  119. break;
  120. case ERR_WARNING:
  121. logf_error(" at: ");
  122. break;
  123. case ERR_SCRIPT:
  124. logf_error(" at: ");
  125. break;
  126. case ERR_SHADER:
  127. logf_error(" at: ");
  128. break;
  129. }
  130. if (p_rationale && p_rationale[0]) {
  131. logf_error("(%s:%i)\n", p_file, p_line);
  132. } else {
  133. logf_error("%s (%s:%i)\n", p_function, p_file, p_line);
  134. }
  135. SetConsoleTextAttribute(hCon, sbi.wAttributes);
  136. }
  137. }
  138. WindowsTerminalLogger::~WindowsTerminalLogger() {}
  139. #endif // WINDOWS_ENABLED