windows_terminal_logger.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*************************************************************************/
  2. /* windows_terminal_logger.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include <windows.h>
  34. void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err) {
  35. if (!should_log(p_err)) {
  36. return;
  37. }
  38. const unsigned int BUFFER_SIZE = 16384;
  39. char buf[BUFFER_SIZE + 1]; // +1 for the terminating character
  40. int len = vsnprintf(buf, BUFFER_SIZE, p_format, p_list);
  41. if (len <= 0)
  42. return;
  43. if ((unsigned int)len >= BUFFER_SIZE)
  44. len = BUFFER_SIZE; // Output is too big, will be truncated
  45. buf[len] = 0;
  46. int wlen = MultiByteToWideChar(CP_UTF8, 0, buf, len, nullptr, 0);
  47. if (wlen < 0)
  48. return;
  49. wchar_t *wbuf = (wchar_t *)memalloc((len + 1) * sizeof(wchar_t));
  50. ERR_FAIL_NULL_MSG(wbuf, "Out of memory.");
  51. MultiByteToWideChar(CP_UTF8, 0, buf, len, wbuf, wlen);
  52. wbuf[wlen] = 0;
  53. if (p_err)
  54. fwprintf(stderr, L"%ls", wbuf);
  55. else
  56. wprintf(L"%ls", wbuf);
  57. memfree(wbuf);
  58. #ifdef DEBUG_ENABLED
  59. fflush(stdout);
  60. #endif
  61. }
  62. 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) {
  63. if (!should_log(true)) {
  64. return;
  65. }
  66. #ifndef UWP_ENABLED
  67. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  68. if (!hCon || hCon == INVALID_HANDLE_VALUE) {
  69. #endif
  70. StdLogger::log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
  71. #ifndef UWP_ENABLED
  72. } else {
  73. CONSOLE_SCREEN_BUFFER_INFO sbi; //original
  74. GetConsoleScreenBufferInfo(hCon, &sbi);
  75. WORD current_bg = sbi.wAttributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
  76. uint32_t basecol = 0;
  77. switch (p_type) {
  78. case ERR_ERROR:
  79. basecol = FOREGROUND_RED;
  80. break;
  81. case ERR_WARNING:
  82. basecol = FOREGROUND_RED | FOREGROUND_GREEN;
  83. break;
  84. case ERR_SCRIPT:
  85. basecol = FOREGROUND_RED | FOREGROUND_BLUE;
  86. break;
  87. case ERR_SHADER:
  88. basecol = FOREGROUND_GREEN | FOREGROUND_BLUE;
  89. break;
  90. }
  91. basecol |= current_bg;
  92. SetConsoleTextAttribute(hCon, basecol | FOREGROUND_INTENSITY);
  93. switch (p_type) {
  94. case ERR_ERROR:
  95. logf("ERROR:");
  96. break;
  97. case ERR_WARNING:
  98. logf("WARNING:");
  99. break;
  100. case ERR_SCRIPT:
  101. logf("SCRIPT ERROR:");
  102. break;
  103. case ERR_SHADER:
  104. logf("SHADER ERROR:");
  105. break;
  106. }
  107. SetConsoleTextAttribute(hCon, basecol);
  108. if (p_rationale && p_rationale[0]) {
  109. logf(" %s\n", p_rationale);
  110. } else {
  111. logf(" %s\n", p_code);
  112. }
  113. // `FOREGROUND_INTENSITY` alone results in gray text.
  114. SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY);
  115. switch (p_type) {
  116. case ERR_ERROR:
  117. logf(" at: ");
  118. break;
  119. case ERR_WARNING:
  120. logf(" at: ");
  121. break;
  122. case ERR_SCRIPT:
  123. logf(" at: ");
  124. break;
  125. case ERR_SHADER:
  126. logf(" at: ");
  127. break;
  128. }
  129. if (p_rationale && p_rationale[0]) {
  130. logf("(%s:%i)\n", p_file, p_line);
  131. } else {
  132. logf("%s (%s:%i)\n", p_function, p_file, p_line);
  133. }
  134. SetConsoleTextAttribute(hCon, sbi.wAttributes);
  135. }
  136. #endif
  137. }
  138. WindowsTerminalLogger::~WindowsTerminalLogger() {}
  139. #endif