From dc665cb7410d1086734fc2f3821ac76a9da73264 Mon Sep 17 00:00:00 2001 From: phobos2077 Date: Tue, 13 Jun 2023 23:09:21 +0200 Subject: [PATCH] Debugging.ConsoleWindow option --- artifacts/ddraw.ini | 4 +++ sfall/Logging.cpp | 76 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/artifacts/ddraw.ini b/artifacts/ddraw.ini index 352963e4..40424170 100644 --- a/artifacts/ddraw.ini +++ b/artifacts/ddraw.ini @@ -894,3 +894,7 @@ Script=0 Criticals=1 ;Prints messages relating to engine fixes Fixes=1 + +;Duplicate log to dedicated console window, shown alongside the main game window +;Set to 1 to display debug.log output (requires DebugMode > 0), 2 to display sfall log, 3 for both. +ConsoleWindow=0 diff --git a/sfall/Logging.cpp b/sfall/Logging.cpp index a1e2a24f..c914bd47 100644 --- a/sfall/Logging.cpp +++ b/sfall/Logging.cpp @@ -18,50 +18,69 @@ #include "main.h" #include "Logging.h" +#include "FalloutEngine\Fallout2.h" #ifndef NO_SFALL_DEBUG #include +#include +#include namespace sfall { static int DebugTypes = 0; static std::ofstream Log; +static int ConsoleWindowMode = 0; +static bool NewLine = true; template -static void OutLog(T a) { - Log << a; - Log.flush(); +static inline void WriteLog(std::ostream& str, T a, int type, const char* prefix, bool newLine) { + str << prefix << DebugTypeToStr(type) << "] " << a; + if (newLine) str << "\n"; } template -static void OutLogN(T a) { - Log << a << "\n"; +static void OutLog(T a, int type, bool newLine = false) { + if (ConsoleWindowMode & 2) { + WriteLog(std::cout, a, type, (ConsoleWindowMode & 1 ? "[sfall:" : "["), newLine); + } + WriteLog(Log, a, type, "[", newLine); Log.flush(); } +const char* DebugTypeToStr(int type) { + switch (type) { + case DL_INIT: return "init"; + case DL_HOOK: return "hook"; + case DL_SCRIPT: return "script"; + case DL_CRITICALS: return "crits"; + case DL_FIX: return "fix"; + default: return "main"; + } +} + void dlog(const char* a, int type) { if (type == DL_MAIN || (isDebug && (type & DebugTypes))) { - OutLog(a); + OutLog(a, type); } } void dlog(const std::string& a, int type) { if (type == DL_MAIN || (isDebug && (type & DebugTypes))) { - OutLog(a); + OutLog(a, type); } } void dlogr(const char* a, int type) { if (type == DL_MAIN || (isDebug && (type & DebugTypes))) { - OutLogN(a); + OutLog(a, type, true); } } void dlogr(const std::string& a, int type) { if (type == DL_MAIN || (isDebug && (type & DebugTypes))) { - OutLogN(a); + OutLog(a, type, true); } } @@ -71,7 +90,7 @@ void dlog_f(const char* fmt, int type, ...) { va_start(args, type); char buf[1024]; vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, args); - OutLog(buf); + OutLog(buf, type); va_end(args); } } @@ -84,7 +103,7 @@ void devlog_f(const char* fmt, int type, ...) { va_start(args, type); char buf[1024]; vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, args); - OutLog(buf); + OutLog(buf, type); va_end(args); } } @@ -92,9 +111,28 @@ void devlog_f(const char* fmt, int type, ...) { void devlog_f(...) {} #endif +static void __fastcall OutLogC(const char* a) { + std::cout << a; +} + +static void __declspec(naked) debug_printf_hack() { + static const DWORD backRet = 0x4C6F7C; + __asm { + call fo::funcoffs::vsprintf_; + pushadc; + mov ecx, esp; + add ecx, 12; + call OutLogC; + popadc; + jmp backRet; + } +} + void LoggingInit() { Log.open("sfall-log.txt", std::ios_base::out | std::ios_base::trunc); + ConsoleWindowMode = IniReader::GetIntDefaultConfig("Debugging", "ConsoleWindow", 0); + if (IniReader::GetIntDefaultConfig("Debugging", "Init", 0)) { DebugTypes |= DL_INIT; } @@ -110,6 +148,22 @@ void LoggingInit() { if (IniReader::GetIntDefaultConfig("Debugging", "Fixes", 0)) { DebugTypes |= DL_FIX; } + if (ConsoleWindowMode > 0) { + if (AllocConsole()) { + freopen("CONOUT$", "w", stdout); + + if (ConsoleWindowMode & 1) { + std::cout << "Displaying debug_printf output." << std::endl; + MakeJump(0x4C6F77, debug_printf_hack); + } + if (ConsoleWindowMode & 2) { + std::cout << "Displaying sfall debug output." << std::endl; + } + } + else { + dlog_f("Console Failed: %x", DL_MAIN, GetLastError()); + } + } } }