Debugging.ConsoleWindow option

This commit is contained in:
phobos2077
2023-06-13 23:09:21 +02:00
parent d7127a4deb
commit dc665cb741
2 changed files with 69 additions and 11 deletions
+4
View File
@@ -894,3 +894,7 @@ Script=0
Criticals=1 Criticals=1
;Prints messages relating to engine fixes ;Prints messages relating to engine fixes
Fixes=1 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
+65 -11
View File
@@ -18,50 +18,69 @@
#include "main.h" #include "main.h"
#include "Logging.h" #include "Logging.h"
#include "FalloutEngine\Fallout2.h"
#ifndef NO_SFALL_DEBUG #ifndef NO_SFALL_DEBUG
#include <fstream> #include <fstream>
#include <stdio.h>
#include <iostream>
namespace sfall namespace sfall
{ {
static int DebugTypes = 0; static int DebugTypes = 0;
static std::ofstream Log; static std::ofstream Log;
static int ConsoleWindowMode = 0;
static bool NewLine = true;
template <class T> template <class T>
static void OutLog(T a) { static inline void WriteLog(std::ostream& str, T a, int type, const char* prefix, bool newLine) {
Log << a; str << prefix << DebugTypeToStr(type) << "] " << a;
Log.flush(); if (newLine) str << "\n";
} }
template <class T> template <class T>
static void OutLogN(T a) { static void OutLog(T a, int type, bool newLine = false) {
Log << a << "\n"; if (ConsoleWindowMode & 2) {
WriteLog(std::cout, a, type, (ConsoleWindowMode & 1 ? "[sfall:" : "["), newLine);
}
WriteLog(Log, a, type, "[", newLine);
Log.flush(); 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) { void dlog(const char* a, int type) {
if (type == DL_MAIN || (isDebug && (type & DebugTypes))) { if (type == DL_MAIN || (isDebug && (type & DebugTypes))) {
OutLog(a); OutLog(a, type);
} }
} }
void dlog(const std::string& a, int type) { void dlog(const std::string& a, int type) {
if (type == DL_MAIN || (isDebug && (type & DebugTypes))) { if (type == DL_MAIN || (isDebug && (type & DebugTypes))) {
OutLog(a); OutLog(a, type);
} }
} }
void dlogr(const char* a, int type) { void dlogr(const char* a, int type) {
if (type == DL_MAIN || (isDebug && (type & DebugTypes))) { if (type == DL_MAIN || (isDebug && (type & DebugTypes))) {
OutLogN(a); OutLog(a, type, true);
} }
} }
void dlogr(const std::string& a, int type) { void dlogr(const std::string& a, int type) {
if (type == DL_MAIN || (isDebug && (type & DebugTypes))) { 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); va_start(args, type);
char buf[1024]; char buf[1024];
vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, args); vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, args);
OutLog(buf); OutLog(buf, type);
va_end(args); va_end(args);
} }
} }
@@ -84,7 +103,7 @@ void devlog_f(const char* fmt, int type, ...) {
va_start(args, type); va_start(args, type);
char buf[1024]; char buf[1024];
vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, args); vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, args);
OutLog(buf); OutLog(buf, type);
va_end(args); va_end(args);
} }
} }
@@ -92,9 +111,28 @@ void devlog_f(const char* fmt, int type, ...) {
void devlog_f(...) {} void devlog_f(...) {}
#endif #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() { void LoggingInit() {
Log.open("sfall-log.txt", std::ios_base::out | std::ios_base::trunc); 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)) { if (IniReader::GetIntDefaultConfig("Debugging", "Init", 0)) {
DebugTypes |= DL_INIT; DebugTypes |= DL_INIT;
} }
@@ -110,6 +148,22 @@ void LoggingInit() {
if (IniReader::GetIntDefaultConfig("Debugging", "Fixes", 0)) { if (IniReader::GetIntDefaultConfig("Debugging", "Fixes", 0)) {
DebugTypes |= DL_FIX; 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());
}
}
} }
} }