From bf09852285941e20a04731443d03c693e3f8ba2d Mon Sep 17 00:00:00 2001 From: Emma Miler Date: Sat, 12 Nov 2022 15:39:39 +0100 Subject: Logging improvements + Colours (new) (#320) * log colours :) * commend the freopen stuff (it causes problems) * formatting * add explanatory comment * gaming maybe * move set pattern * ok this works now i think * trailing space :) * Revert "Merge branch 'main' into log-colours" This reverts commit d40b6496b7bd3b3f20f51cc2311b0369aeabe735, reversing changes made to a98bcc22e8961b325806f6a9ca85e64353fd574c. * Revert "Revert "Merge branch 'main' into log-colours"" This reverts commit da792245b1cd7d0ac9e22d5633770142a43ce10c. * fix shit maybe * fix shit but better * initial, still WIP * Add GetContextName_Short * colours for in game console * working, time to improve code * rename var + add a colour or two * whoops * change rpak filesystem to RP NATIVE * add RP NATIVE and NORTHSTAR tags * clang-format off because formatting * formatting 2 * formatting 3: the formattening * bruh * revert unnecessary change * improve comment * Set console title to show profile * pain * Made code not shit * Formatting and remove warning box * Update main.cpp * Formatting * More format changes * Again * Fix small things from code review * Remove empty line * renamed NATIVE_EN -> NATIVE_ENGINE * Update logging.cpp * Update sourceconsole.cpp Co-authored-by: ASpoonPlaysGames <66967891+ASpoonPlaysGames@users.noreply.github.com> --- NorthstarDLL/squirrel.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) (limited to 'NorthstarDLL/squirrel.cpp') diff --git a/NorthstarDLL/squirrel.cpp b/NorthstarDLL/squirrel.cpp index be0dceeb..c90b2d7f 100644 --- a/NorthstarDLL/squirrel.cpp +++ b/NorthstarDLL/squirrel.cpp @@ -8,6 +8,35 @@ AUTOHOOK_INIT() +std::shared_ptr getSquirrelLoggerByContext(ScriptContext context) +{ + switch (context) + { + case ScriptContext::UI: + return NS::log::SCRIPT_UI; + case ScriptContext::CLIENT: + return NS::log::SCRIPT_CL; + case ScriptContext::SERVER: + return NS::log::SCRIPT_SV; + default: + throw std::runtime_error("getSquirrelLoggerByContext called with invalid context"); + return nullptr; + } +} + +namespace NS::log +{ + template std::shared_ptr squirrel_logger() + { + // Switch statements can't be constexpr afaik + // clang-format off + if constexpr (context == ScriptContext::UI) { return SCRIPT_UI; } + if constexpr (context == ScriptContext::CLIENT) { return SCRIPT_CL; } + if constexpr (context == ScriptContext::SERVER) { return SCRIPT_SV; } + // clang-format on + } +}; // namespace NS::log + const char* GetContextName(ScriptContext context) { switch (context) @@ -23,6 +52,21 @@ const char* GetContextName(ScriptContext context) } } +const char* GetContextName_Short(ScriptContext context) +{ + switch (context) + { + case ScriptContext::CLIENT: + return "CL"; + case ScriptContext::SERVER: + return "SV"; + case ScriptContext::UI: + return "UI"; + default: + return "??"; + } +} + eSQReturnType SQReturnTypeFromString(const char* pReturnType) { static const std::map sqReturnTypeNameToString = { @@ -230,8 +274,7 @@ template SQInteger SQPrintHook(HSquirrelVM* sqvm, const { if (buf[charsWritten - 1] == '\n') buf[charsWritten - 1] = '\0'; - - spdlog::info("[{} SCRIPT] {}", GetContextName(context), buf); + g_pSquirrel->logger->info("{}", buf); } va_end(va); @@ -279,8 +322,10 @@ void __fastcall ScriptCompileErrorHook(HSquirrelVM* sqvm, const char* error, con bIsFatalError = g_pSquirrel->m_bFatalCompilationErrors; } - spdlog::error("{} SCRIPT COMPILE ERROR {}", GetContextName(realContext), error); - spdlog::error("{} line [{}] column [{}]", file, line, column); + auto logger = getSquirrelLoggerByContext(realContext); + + logger->error("COMPILE ERROR {}", error); + logger->error("{} line [{}] column [{}]", file, line, column); // use disconnect to display an error message for the compile error, but only if the compilation error was fatal // todo, we could get this from sqvm itself probably, rather than hooking sq_compiler_create @@ -518,6 +563,9 @@ ON_DLL_LOAD_RELIESON("client.dll", ClientSquirrel, ConCommand, (CModule module)) &g_pSquirrel->RegisterSquirrelFunc); g_pSquirrel->RegisterSquirrelFunc = g_pSquirrel->RegisterSquirrelFunc; + g_pSquirrel->logger = NS::log::SCRIPT_CL; + g_pSquirrel->logger = NS::log::SCRIPT_UI; + // uiscript_reset concommand: don't loop forever if compilation fails module.Offset(0x3C6E4C).NOP(6); @@ -582,6 +630,8 @@ ON_DLL_LOAD_RELIESON("server.dll", ServerSquirrel, ConCommand, (CModule module)) g_pSquirrel->__sq_GetEntityConstant_CBaseEntity = module.Offset(0x418AF0).As(); g_pSquirrel->__sq_getentityfrominstance = module.Offset(0x1E920).As(); + g_pSquirrel->logger = NS::log::SCRIPT_SV; + MAKEHOOK( module.Offset(0x1DD10), &RegisterSquirrelFunctionHook, -- cgit v1.2.3