2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* windows_terminal_logger.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2017-09-22 05:56:02 +00:00
|
|
|
|
|
|
|
#include "windows_terminal_logger.h"
|
|
|
|
|
|
|
|
#ifdef WINDOWS_ENABLED
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2023-06-08 12:51:32 +00:00
|
|
|
|
2021-09-23 06:56:12 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2017-09-22 05:56:02 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err) {
|
|
|
|
if (!should_log(p_err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-20 10:24:10 +00:00
|
|
|
const int static_buffer_size = 1024;
|
|
|
|
char static_buf[static_buffer_size];
|
|
|
|
char *buf = static_buf;
|
|
|
|
va_list list_copy;
|
|
|
|
va_copy(list_copy, p_list);
|
|
|
|
int len = vsnprintf(buf, static_buffer_size, p_format, p_list);
|
|
|
|
if (len >= static_buffer_size) {
|
|
|
|
buf = (char *)memalloc(len + 1);
|
|
|
|
len = vsnprintf(buf, len + 1, p_format, list_copy);
|
|
|
|
}
|
|
|
|
va_end(list_copy);
|
|
|
|
|
|
|
|
String str_buf = String::utf8(buf, len).replace("\r\n", "\n").replace("\n", "\r\n");
|
|
|
|
if (len >= static_buffer_size) {
|
|
|
|
memfree(buf);
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2024-05-20 10:24:10 +00:00
|
|
|
CharString cstr_buf = str_buf.utf8();
|
|
|
|
if (cstr_buf.length() == 0) {
|
|
|
|
return;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2017-09-22 05:56:02 +00:00
|
|
|
|
2024-04-25 09:56:07 +00:00
|
|
|
DWORD written = 0;
|
|
|
|
HANDLE h = p_err ? GetStdHandle(STD_ERROR_HANDLE) : GetStdHandle(STD_OUTPUT_HANDLE);
|
2024-05-20 10:24:10 +00:00
|
|
|
WriteFile(h, cstr_buf.ptr(), cstr_buf.length(), &written, nullptr);
|
2017-09-22 05:56:02 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
2024-04-25 09:56:07 +00:00
|
|
|
FlushFileBuffers(h);
|
2017-09-22 05:56:02 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-11-26 09:18:15 +00:00
|
|
|
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) {
|
2017-09-22 05:56:02 +00:00
|
|
|
if (!should_log(true)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if (!hCon || hCon == INVALID_HANDLE_VALUE) {
|
|
|
|
StdLogger::log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
|
|
|
|
} else {
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO sbi; //original
|
|
|
|
GetConsoleScreenBufferInfo(hCon, &sbi);
|
|
|
|
|
|
|
|
WORD current_bg = sbi.wAttributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
|
|
|
|
|
|
|
|
uint32_t basecol = 0;
|
|
|
|
switch (p_type) {
|
2020-05-10 11:00:47 +00:00
|
|
|
case ERR_ERROR:
|
|
|
|
basecol = FOREGROUND_RED;
|
|
|
|
break;
|
|
|
|
case ERR_WARNING:
|
|
|
|
basecol = FOREGROUND_RED | FOREGROUND_GREEN;
|
|
|
|
break;
|
|
|
|
case ERR_SCRIPT:
|
|
|
|
basecol = FOREGROUND_RED | FOREGROUND_BLUE;
|
|
|
|
break;
|
|
|
|
case ERR_SHADER:
|
|
|
|
basecol = FOREGROUND_GREEN | FOREGROUND_BLUE;
|
|
|
|
break;
|
2017-09-22 05:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
basecol |= current_bg;
|
|
|
|
|
2020-01-18 22:26:28 +00:00
|
|
|
SetConsoleTextAttribute(hCon, basecol | FOREGROUND_INTENSITY);
|
|
|
|
switch (p_type) {
|
2020-05-10 11:00:47 +00:00
|
|
|
case ERR_ERROR:
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error("ERROR:");
|
2020-05-10 11:00:47 +00:00
|
|
|
break;
|
|
|
|
case ERR_WARNING:
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error("WARNING:");
|
2020-05-10 11:00:47 +00:00
|
|
|
break;
|
|
|
|
case ERR_SCRIPT:
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error("SCRIPT ERROR:");
|
2020-05-10 11:00:47 +00:00
|
|
|
break;
|
|
|
|
case ERR_SHADER:
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error("SHADER ERROR:");
|
2020-05-10 11:00:47 +00:00
|
|
|
break;
|
2020-01-18 22:26:28 +00:00
|
|
|
}
|
2017-09-22 05:56:02 +00:00
|
|
|
|
2020-01-18 22:26:28 +00:00
|
|
|
SetConsoleTextAttribute(hCon, basecol);
|
|
|
|
if (p_rationale && p_rationale[0]) {
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error(" %s\n", p_rationale);
|
2020-01-18 22:26:28 +00:00
|
|
|
} else {
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error(" %s\n", p_code);
|
2020-01-18 22:26:28 +00:00
|
|
|
}
|
2017-09-22 05:56:02 +00:00
|
|
|
|
2020-01-18 22:26:28 +00:00
|
|
|
// `FOREGROUND_INTENSITY` alone results in gray text.
|
|
|
|
SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY);
|
|
|
|
switch (p_type) {
|
2020-05-10 11:00:47 +00:00
|
|
|
case ERR_ERROR:
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error(" at: ");
|
2020-05-10 11:00:47 +00:00
|
|
|
break;
|
|
|
|
case ERR_WARNING:
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error(" at: ");
|
2020-05-10 11:00:47 +00:00
|
|
|
break;
|
|
|
|
case ERR_SCRIPT:
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error(" at: ");
|
2020-05-10 11:00:47 +00:00
|
|
|
break;
|
|
|
|
case ERR_SHADER:
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error(" at: ");
|
2020-05-10 11:00:47 +00:00
|
|
|
break;
|
2020-01-18 22:26:28 +00:00
|
|
|
}
|
2017-09-22 05:56:02 +00:00
|
|
|
|
2020-01-18 22:26:28 +00:00
|
|
|
if (p_rationale && p_rationale[0]) {
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error("(%s:%i)\n", p_file, p_line);
|
2017-09-22 05:56:02 +00:00
|
|
|
} else {
|
2020-05-29 03:34:17 +00:00
|
|
|
logf_error("%s (%s:%i)\n", p_function, p_file, p_line);
|
2017-09-22 05:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SetConsoleTextAttribute(hCon, sbi.wAttributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowsTerminalLogger::~WindowsTerminalLogger() {}
|
|
|
|
|
2023-06-08 12:51:32 +00:00
|
|
|
#endif // WINDOWS_ENABLED
|