Debian Codegraph @

platform/windows/windows_terminal_logger.cpp

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