windows_terminal_logger.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 (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, NULL, 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_fg = sbi.wAttributes & (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
  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: basecol = FOREGROUND_RED; break;
  79. case ERR_WARNING: basecol = FOREGROUND_RED | FOREGROUND_GREEN; break;
  80. case ERR_SCRIPT: basecol = FOREGROUND_RED | FOREGROUND_BLUE; break;
  81. case ERR_SHADER: basecol = FOREGROUND_GREEN | FOREGROUND_BLUE; break;
  82. }
  83. basecol |= current_bg;
  84. if (p_rationale && p_rationale[0]) {
  85. SetConsoleTextAttribute(hCon, basecol | FOREGROUND_INTENSITY);
  86. switch (p_type) {
  87. case ERR_ERROR: logf("ERROR: "); break;
  88. case ERR_WARNING: logf("WARNING: "); break;
  89. case ERR_SCRIPT: logf("SCRIPT ERROR: "); break;
  90. case ERR_SHADER: logf("SHADER ERROR: "); break;
  91. }
  92. SetConsoleTextAttribute(hCon, current_fg | current_bg | FOREGROUND_INTENSITY);
  93. logf("%s\n", p_rationale);
  94. SetConsoleTextAttribute(hCon, basecol);
  95. switch (p_type) {
  96. case ERR_ERROR: logf(" At: "); break;
  97. case ERR_WARNING: logf(" At: "); break;
  98. case ERR_SCRIPT: logf(" At: "); break;
  99. case ERR_SHADER: logf(" At: "); break;
  100. }
  101. SetConsoleTextAttribute(hCon, current_fg | current_bg);
  102. logf("%s:%i\n", p_file, p_line);
  103. } else {
  104. SetConsoleTextAttribute(hCon, basecol | FOREGROUND_INTENSITY);
  105. switch (p_type) {
  106. case ERR_ERROR: logf("ERROR: %s: ", p_function); break;
  107. case ERR_WARNING: logf("WARNING: %s: ", p_function); break;
  108. case ERR_SCRIPT: logf("SCRIPT ERROR: %s: ", p_function); break;
  109. case ERR_SHADER: logf("SCRIPT ERROR: %s: ", p_function); break;
  110. }
  111. SetConsoleTextAttribute(hCon, current_fg | current_bg | FOREGROUND_INTENSITY);
  112. logf("%s\n", p_code);
  113. SetConsoleTextAttribute(hCon, basecol);
  114. switch (p_type) {
  115. case ERR_ERROR: logf(" At: "); break;
  116. case ERR_WARNING: logf(" At: "); break;
  117. case ERR_SCRIPT: logf(" At: "); break;
  118. case ERR_SHADER: logf(" At: "); break;
  119. }
  120. SetConsoleTextAttribute(hCon, current_fg | current_bg);
  121. logf("%s:%i\n", p_file, p_line);
  122. }
  123. SetConsoleTextAttribute(hCon, sbi.wAttributes);
  124. }
  125. #endif
  126. }
  127. WindowsTerminalLogger::~WindowsTerminalLogger() {}
  128. #endif