windows_terminal_logger.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 *)malloc((len + 1) * sizeof(wchar_t));
  50. MultiByteToWideChar(CP_UTF8, 0, buf, len, wbuf, wlen);
  51. wbuf[wlen] = 0;
  52. if (p_err)
  53. fwprintf(stderr, L"%ls", wbuf);
  54. else
  55. wprintf(L"%ls", wbuf);
  56. free(wbuf);
  57. #ifdef DEBUG_ENABLED
  58. fflush(stdout);
  59. #endif
  60. }
  61. 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) {
  62. if (!should_log(true)) {
  63. return;
  64. }
  65. #ifndef UWP_ENABLED
  66. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  67. if (!hCon || hCon == INVALID_HANDLE_VALUE) {
  68. #endif
  69. StdLogger::log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
  70. #ifndef UWP_ENABLED
  71. } else {
  72. CONSOLE_SCREEN_BUFFER_INFO sbi; //original
  73. GetConsoleScreenBufferInfo(hCon, &sbi);
  74. WORD current_bg = sbi.wAttributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
  75. uint32_t basecol = 0;
  76. switch (p_type) {
  77. case ERR_ERROR:
  78. basecol = FOREGROUND_RED;
  79. break;
  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. }
  90. basecol |= current_bg;
  91. SetConsoleTextAttribute(hCon, basecol | FOREGROUND_INTENSITY);
  92. switch (p_type) {
  93. case ERR_ERROR:
  94. logf("ERROR:");
  95. break;
  96. case ERR_WARNING:
  97. logf("WARNING:");
  98. break;
  99. case ERR_SCRIPT:
  100. logf("SCRIPT ERROR:");
  101. break;
  102. case ERR_SHADER:
  103. logf("SHADER ERROR:");
  104. break;
  105. }
  106. SetConsoleTextAttribute(hCon, basecol);
  107. if (p_rationale && p_rationale[0]) {
  108. logf(" %s\n", p_rationale);
  109. } else {
  110. logf(" %s\n", p_code);
  111. }
  112. // `FOREGROUND_INTENSITY` alone results in gray text.
  113. SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY);
  114. switch (p_type) {
  115. case ERR_ERROR:
  116. logf(" at: ");
  117. break;
  118. case ERR_WARNING:
  119. logf(" at: ");
  120. break;
  121. case ERR_SCRIPT:
  122. logf(" at: ");
  123. break;
  124. case ERR_SHADER:
  125. logf(" at: ");
  126. break;
  127. }
  128. if (p_rationale && p_rationale[0]) {
  129. logf("(%s:%i)\n", p_file, p_line);
  130. } else {
  131. logf("%s (%s:%i)\n", p_function, p_file, p_line);
  132. }
  133. SetConsoleTextAttribute(hCon, sbi.wAttributes);
  134. }
  135. #endif
  136. }
  137. WindowsTerminalLogger::~WindowsTerminalLogger() {}
  138. #endif