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