windows_terminal_logger.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifdef WINDOWS_ENABLED
  32. #include <stdio.h>
  33. #define WIN32_LEAN_AND_MEAN
  34. #include <windows.h>
  35. void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err) {
  36. if (!should_log(p_err)) {
  37. return;
  38. }
  39. const unsigned int BUFFER_SIZE = 16384;
  40. char buf[BUFFER_SIZE + 1]; // +1 for the terminating character
  41. int len = vsnprintf(buf, BUFFER_SIZE, p_format, p_list);
  42. if (len <= 0)
  43. return;
  44. if ((unsigned int)len >= BUFFER_SIZE)
  45. len = BUFFER_SIZE; // Output is too big, will be truncated
  46. buf[len] = 0;
  47. int wlen = MultiByteToWideChar(CP_UTF8, 0, buf, len, NULL, 0);
  48. if (wlen < 0)
  49. return;
  50. wchar_t *wbuf = (wchar_t *)memalloc((len + 1) * sizeof(wchar_t));
  51. ERR_FAIL_NULL_MSG(wbuf, "Out of memory.");
  52. MultiByteToWideChar(CP_UTF8, 0, buf, len, wbuf, wlen);
  53. wbuf[wlen] = 0;
  54. if (p_err)
  55. fwprintf(stderr, L"%ls", wbuf);
  56. else
  57. wprintf(L"%ls", wbuf);
  58. memfree(wbuf);
  59. #ifdef DEBUG_ENABLED
  60. fflush(stdout);
  61. #endif
  62. }
  63. void WindowsTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
  64. if (!should_log(true)) {
  65. return;
  66. }
  67. #ifndef UWP_ENABLED
  68. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  69. if (!hCon || hCon == INVALID_HANDLE_VALUE) {
  70. #endif
  71. StdLogger::log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
  72. #ifndef UWP_ENABLED
  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. #endif
  138. }
  139. WindowsTerminalLogger::~WindowsTerminalLogger() {}
  140. #endif