mirror of
https://github.com/sfall-team/sfall.git
synced 2026-07-27 16:52:34 -07:00
Make sfall logs a little prettier
This commit is contained in:
+52
-27
@@ -23,40 +23,58 @@
|
||||
#ifndef NO_SFALL_DEBUG
|
||||
|
||||
#include <fstream>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace sfall
|
||||
{
|
||||
|
||||
static int DebugTypes = 0;
|
||||
static std::ofstream Log;
|
||||
static int ConsoleWindowMode = 0;
|
||||
static bool NewLine = true;
|
||||
enum ConsoleSource : int {
|
||||
GAME = 1,
|
||||
SFALL = 2
|
||||
};
|
||||
|
||||
template <class T>
|
||||
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";
|
||||
}
|
||||
static int DebugTypes = 0;
|
||||
static int ConsoleWindowMode = 0;
|
||||
static std::ofstream Log;
|
||||
|
||||
static int LastType = -1;
|
||||
static int LastNewLine;
|
||||
static int LastConsoleSource;
|
||||
|
||||
template <class T>
|
||||
static void OutLog(T a, int type, bool newLine = false) {
|
||||
if (ConsoleWindowMode & 2) {
|
||||
WriteLog(std::cout, a, type, (ConsoleWindowMode & 1 ? "[sfall:" : "["), newLine);
|
||||
std::ostringstream ss;
|
||||
if (LastNewLine || type != LastType) {
|
||||
ss << "[" << DebugTypeToStr(type) << "] ";
|
||||
}
|
||||
WriteLog(Log, a, type, "[", newLine);
|
||||
ss << a;
|
||||
if (newLine) ss << std::endl;
|
||||
std::string str = ss.str();
|
||||
|
||||
if (ConsoleWindowMode & ConsoleSource::SFALL) {
|
||||
if (LastConsoleSource == ConsoleSource::GAME) {
|
||||
std::cout << std::endl; // To make logs prettier, because debug_msg places newline before the message.
|
||||
}
|
||||
std::cout << a;
|
||||
LastConsoleSource = ConsoleSource::SFALL;
|
||||
}
|
||||
|
||||
Log << a;
|
||||
Log.flush();
|
||||
|
||||
LastType = type;
|
||||
LastNewLine = str.back() == '\n';
|
||||
}
|
||||
|
||||
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";
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,8 +107,10 @@ void dlog_f(const char* fmt, int type, ...) {
|
||||
va_list args;
|
||||
va_start(args, type);
|
||||
char buf[1024];
|
||||
vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, args);
|
||||
OutLog(buf, type);
|
||||
int written = vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, args);
|
||||
if (written > 0) {
|
||||
OutLog(buf, type);
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
}
|
||||
@@ -102,8 +122,10 @@ void devlog_f(const char* fmt, int type, ...) {
|
||||
va_list args;
|
||||
va_start(args, type);
|
||||
char buf[1024];
|
||||
vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, args);
|
||||
OutLog(buf, type);
|
||||
int written = vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, args);
|
||||
if (written > 0) {
|
||||
OutLog(buf, type);
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
}
|
||||
@@ -111,8 +133,9 @@ void devlog_f(const char* fmt, int type, ...) {
|
||||
void devlog_f(...) {}
|
||||
#endif
|
||||
|
||||
static void __fastcall OutLogC(const char* a) {
|
||||
static void __fastcall PrintToConsole(const char* a) {
|
||||
std::cout << a;
|
||||
LastConsoleSource = ConsoleSource::GAME;
|
||||
}
|
||||
|
||||
static void __declspec(naked) debug_printf_hack() {
|
||||
@@ -122,7 +145,7 @@ static void __declspec(naked) debug_printf_hack() {
|
||||
pushadc;
|
||||
mov ecx, esp;
|
||||
add ecx, 12;
|
||||
call OutLogC;
|
||||
call PrintToConsole;
|
||||
popadc;
|
||||
jmp backRet;
|
||||
}
|
||||
@@ -152,13 +175,15 @@ void LoggingInit() {
|
||||
if (AllocConsole()) {
|
||||
freopen("CONOUT$", "w", stdout);
|
||||
|
||||
if (ConsoleWindowMode & 1) {
|
||||
if (ConsoleWindowMode & ConsoleSource::GAME) {
|
||||
std::cout << "Displaying debug_printf output." << std::endl;
|
||||
MakeJump(0x4C6F77, debug_printf_hack);
|
||||
}
|
||||
if (ConsoleWindowMode & 2) {
|
||||
if (ConsoleWindowMode & ConsoleSource::SFALL) {
|
||||
std::cout << "Displaying sfall debug output." << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
}
|
||||
else {
|
||||
dlog_f("Console Failed: %x", DL_MAIN, GetLastError());
|
||||
|
||||
@@ -35,6 +35,10 @@
|
||||
|
||||
#include "Metarule.h"
|
||||
|
||||
#ifndef NDEBUG
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
namespace sfall
|
||||
{
|
||||
namespace script
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "..\..\..\main.h"
|
||||
|
||||
Reference in New Issue
Block a user