Path: blob/master/platform/windows/windows_terminal_logger.cpp
11352 views
/**************************************************************************/1/* windows_terminal_logger.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "windows_terminal_logger.h"3132#include "core/object/script_backtrace.h"33#include "core/os/os.h"3435#ifdef WINDOWS_ENABLED3637#include <cstdio>3839#define WIN32_LEAN_AND_MEAN40#include <windows.h>4142void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err) {43if (!should_log(p_err)) {44return;45}4647const int static_buffer_size = 1024;48char static_buf[static_buffer_size];49char *buf = static_buf;50va_list list_copy;51va_copy(list_copy, p_list);52int len = vsnprintf(buf, static_buffer_size, p_format, p_list);53if (len >= static_buffer_size) {54buf = (char *)memalloc(len + 1);55len = vsnprintf(buf, len + 1, p_format, list_copy);56}57va_end(list_copy);5859String str_buf = String::utf8(buf, len).replace("\r\n", "\n").replace("\n", "\r\n");60if (len >= static_buffer_size) {61memfree(buf);62}63CharString cstr_buf = str_buf.utf8();64if (cstr_buf.length() == 0) {65return;66}6768DWORD written = 0;69HANDLE h = p_err ? GetStdHandle(STD_ERROR_HANDLE) : GetStdHandle(STD_OUTPUT_HANDLE);70WriteFile(h, cstr_buf.ptr(), cstr_buf.length(), &written, nullptr);7172#ifdef DEBUG_ENABLED73FlushFileBuffers(h);74#endif75}7677void 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) {78if (!should_log(true)) {79return;80}8182HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);83if (OS::get_singleton()->get_stdout_type() != OS::STD_HANDLE_CONSOLE || !hCon || hCon == INVALID_HANDLE_VALUE) {84StdLogger::log_error(p_function, p_file, p_line, p_code, p_rationale, p_editor_notify, p_type, p_script_backtraces);85} else {86CONSOLE_SCREEN_BUFFER_INFO sbi; //original87GetConsoleScreenBufferInfo(hCon, &sbi);8889WORD current_bg = sbi.wAttributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);9091uint32_t basecol = 0;92switch (p_type) {93case ERR_WARNING:94basecol = FOREGROUND_RED | FOREGROUND_GREEN;95break;96case ERR_SCRIPT:97basecol = FOREGROUND_RED | FOREGROUND_BLUE;98break;99case ERR_SHADER:100basecol = FOREGROUND_GREEN | FOREGROUND_BLUE;101break;102case ERR_ERROR:103default:104basecol = FOREGROUND_RED;105break;106}107108basecol |= current_bg;109110SetConsoleTextAttribute(hCon, basecol | FOREGROUND_INTENSITY);111logf_error("%s:", error_type_string(p_type));112113SetConsoleTextAttribute(hCon, basecol);114if (p_rationale && p_rationale[0]) {115logf_error(" %s\n", p_rationale);116} else {117logf_error(" %s\n", p_code);118}119120// `FOREGROUND_INTENSITY` alone results in gray text.121SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY);122if (p_rationale && p_rationale[0]) {123logf_error("%sat: (%s:%i)\n", error_type_indent(p_type), p_file, p_line);124} else {125logf_error("%sat: %s (%s:%i)\n", error_type_indent(p_type), p_function, p_file, p_line);126}127128for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {129if (!backtrace->is_empty()) {130logf_error("%s\n", backtrace->format(strlen(error_type_indent(p_type))).utf8().get_data());131}132}133134SetConsoleTextAttribute(hCon, sbi.wAttributes);135}136}137138WindowsTerminalLogger::~WindowsTerminalLogger() {}139140#endif // WINDOWS_ENABLED141142143