Path: blob/master/platform/windows/windows_terminal_logger.cpp
10277 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/os/os.h"3334#ifdef WINDOWS_ENABLED3536#include <cstdio>3738#define WIN32_LEAN_AND_MEAN39#include <windows.h>4041void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err) {42if (!should_log(p_err)) {43return;44}4546const int static_buffer_size = 1024;47char static_buf[static_buffer_size];48char *buf = static_buf;49va_list list_copy;50va_copy(list_copy, p_list);51int len = vsnprintf(buf, static_buffer_size, p_format, p_list);52if (len >= static_buffer_size) {53buf = (char *)memalloc(len + 1);54len = vsnprintf(buf, len + 1, p_format, list_copy);55}56va_end(list_copy);5758String str_buf = String::utf8(buf, len).replace("\r\n", "\n").replace("\n", "\r\n");59if (len >= static_buffer_size) {60memfree(buf);61}62CharString cstr_buf = str_buf.utf8();63if (cstr_buf.length() == 0) {64return;65}6667DWORD written = 0;68HANDLE h = p_err ? GetStdHandle(STD_ERROR_HANDLE) : GetStdHandle(STD_OUTPUT_HANDLE);69WriteFile(h, cstr_buf.ptr(), cstr_buf.length(), &written, nullptr);7071#ifdef DEBUG_ENABLED72FlushFileBuffers(h);73#endif74}7576void 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) {77if (!should_log(true)) {78return;79}8081HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);82if (OS::get_singleton()->get_stdout_type() != OS::STD_HANDLE_CONSOLE || !hCon || hCon == INVALID_HANDLE_VALUE) {83StdLogger::log_error(p_function, p_file, p_line, p_code, p_rationale, p_editor_notify, p_type, p_script_backtraces);84} else {85CONSOLE_SCREEN_BUFFER_INFO sbi; //original86GetConsoleScreenBufferInfo(hCon, &sbi);8788WORD current_bg = sbi.wAttributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);8990uint32_t basecol = 0;91switch (p_type) {92case ERR_WARNING:93basecol = FOREGROUND_RED | FOREGROUND_GREEN;94break;95case ERR_SCRIPT:96basecol = FOREGROUND_RED | FOREGROUND_BLUE;97break;98case ERR_SHADER:99basecol = FOREGROUND_GREEN | FOREGROUND_BLUE;100break;101case ERR_ERROR:102default:103basecol = FOREGROUND_RED;104break;105}106107basecol |= current_bg;108109SetConsoleTextAttribute(hCon, basecol | FOREGROUND_INTENSITY);110logf_error("%s:", error_type_string(p_type));111112SetConsoleTextAttribute(hCon, basecol);113if (p_rationale && p_rationale[0]) {114logf_error(" %s\n", p_rationale);115} else {116logf_error(" %s\n", p_code);117}118119// `FOREGROUND_INTENSITY` alone results in gray text.120SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY);121if (p_rationale && p_rationale[0]) {122logf_error("%sat: (%s:%i)\n", error_type_indent(p_type), p_file, p_line);123} else {124logf_error("%sat: %s (%s:%i)\n", error_type_indent(p_type), p_function, p_file, p_line);125}126127for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {128if (!backtrace->is_empty()) {129logf_error("%s\n", backtrace->format(strlen(error_type_indent(p_type))).utf8().get_data());130}131}132133SetConsoleTextAttribute(hCon, sbi.wAttributes);134}135}136137WindowsTerminalLogger::~WindowsTerminalLogger() {}138139#endif // WINDOWS_ENABLED140141142