r5sdk/r5dev/core/logdef.cpp
Kawe Mazidjatari 9e6b0e567f Port everything to CCommandLine
Before, we had to do a hack of capturing the command line using GetCommandLineA, and then checking if a certain argument is present. This was required due to how early the GameSDK dll was loaded (the g_CmdLine object was far from initialized in the engine). Due to the loader refactor, the commandline can be used directly after creation in the game's entry point (which is the time the SDK is getting loaded). Therefore, no copies of the command line are required anymore.
This commit contains the following changes:

- Correctly ordered the initialization, and deinitialization of systems (first init = last shutdown).
- Factored out command line string copy in favor of game's implementation.
- Factored the R5Reloaded emblem print into its own function.
- Removed 'SpdLog_PostInit()', we can now directly call DevMsg() once SpdLog_Init() has been called, the logger callback sink deals with the formatting of the output.
- Fixed a bug where the logger did not print the correct color for 'SYSTEM_WARNING' and 'SYSTEM_ERROR' in the external console.
- Fixed a bug where the command line did not work when the game wasn't launched with the '-launcher' parameter.
- Logs now equally appear on the external, and in-game console windows.
2023-07-01 01:20:47 +02:00

101 lines
3.9 KiB
C++

#include "core/stdafx.h"
#include "core/logdef.h"
std::shared_ptr<spdlog::logger> g_TermLogger;
std::shared_ptr<spdlog::logger> g_ImGuiLogger;
std::ostringstream g_LogStream;
std::shared_ptr<spdlog::sinks::ostream_sink_st> g_LogSink;
//#############################################################################
// SPDLOG INIT
//#############################################################################
void SpdLog_Init(const bool bAnsiColor)
{
static bool bInitialized = false;
if (bInitialized)
{
Assert(bInitialized, "'SpdLog_Init()' has already been called.");
return;
}
#ifndef NETCONSOLE
g_LogSessionUUID = CreateUUID();
g_LogSessionDirectory = fmt::format("platform\\logs\\{:s}", g_LogSessionUUID);
/************************
* IMGUI LOGGER SETUP *
************************/
{
g_LogSink = std::make_shared<spdlog::sinks::ostream_sink_st>(g_LogStream);
g_ImGuiLogger = std::make_shared<spdlog::logger>("game_console", g_LogSink);
spdlog::register_logger(g_ImGuiLogger); // in-game console logger.
g_ImGuiLogger->set_pattern("%v");
g_ImGuiLogger->set_level(spdlog::level::trace);
}
#endif // !NETCONSOLE
/************************
* WINDOWS LOGGER SETUP *
************************/
{
#ifdef NETCONSOLE
g_TermLogger = spdlog::default_logger();
#else
g_TermLogger = spdlog::stdout_logger_mt("win_console");
#endif // NETCONSOLE
// Determine if user wants ansi-color logging in the terminal.
if (bAnsiColor)
{
g_TermLogger->set_pattern("%v\u001b[0m");
g_bSpdLog_UseAnsiClr = true;
}
else
{
g_TermLogger->set_pattern("%v");
}
//g_TermLogger->set_level(spdlog::level::trace);
}
#ifndef NETCONSOLE
spdlog::set_default_logger(g_TermLogger); // Set as default.
SpdLog_Create();
#endif // !NETCONSOLE
spdlog::set_level(spdlog::level::trace);
bInitialized = true;
}
void SpdLog_Create()
{
/************************
* ROTATE LOGGER SETUP *
************************/
spdlog::rotating_logger_mt<spdlog::synchronous_factory>("squirrel_re(warning)"
, fmt::format("{:s}\\{:s}", g_LogSessionDirectory, "script_warning.log"), SPDLOG_MAX_SIZE, SPDLOG_NUM_FILE)->set_pattern("[%Y-%m-%d %H:%M:%S.%e] %v");
spdlog::rotating_logger_mt<spdlog::synchronous_factory>("squirrel_re"
, fmt::format("{:s}\\{:s}", g_LogSessionDirectory, "script.log"), SPDLOG_MAX_SIZE, SPDLOG_NUM_FILE)->set_pattern("[%Y-%m-%d %H:%M:%S.%e] %v");
spdlog::rotating_logger_mt<spdlog::synchronous_factory>("sdk"
, fmt::format("{:s}\\{:s}", g_LogSessionDirectory, "message.log"), SPDLOG_MAX_SIZE, SPDLOG_NUM_FILE)->set_pattern("[%Y-%m-%d %H:%M:%S.%e] %v");
spdlog::rotating_logger_mt<spdlog::synchronous_factory>("sdk(warning)"
, fmt::format("{:s}\\{:s}", g_LogSessionDirectory, "warning.log"), SPDLOG_MAX_SIZE, SPDLOG_NUM_FILE)->set_pattern("[%Y-%m-%d %H:%M:%S.%e] %v");
spdlog::rotating_logger_mt<spdlog::synchronous_factory>("sdk(error)"
, fmt::format("{:s}\\{:s}", g_LogSessionDirectory, "error.log"), SPDLOG_MAX_SIZE, SPDLOG_NUM_FILE)->set_pattern("[%Y-%m-%d %H:%M:%S.%e] %v");
spdlog::rotating_logger_mt<spdlog::synchronous_factory>("net_trace"
, fmt::format("{:s}\\{:s}", g_LogSessionDirectory, "net_trace.log"), SPDLOG_MAX_SIZE, SPDLOG_NUM_FILE)->set_pattern("[%Y-%m-%d %H:%M:%S.%e] %v");
#ifndef DEDICATED
spdlog::rotating_logger_mt<spdlog::synchronous_factory>("netconsole"
, fmt::format("{:s}\\{:s}", g_LogSessionDirectory, "netconsole.log"), SPDLOG_MAX_SIZE, SPDLOG_NUM_FILE)->set_pattern("[%Y-%m-%d %H:%M:%S.%e] %v");
#endif // !DEDICATED
spdlog::rotating_logger_mt<spdlog::synchronous_factory>("filesystem"
, fmt::format("{:s}\\{:s}", g_LogSessionDirectory, "filesystem.log"), SPDLOG_MAX_SIZE, SPDLOG_NUM_FILE)->set_pattern("[%Y-%m-%d %H:%M:%S.%e] %v");
}
//#############################################################################
// SPDLOG SHUTDOWN
//#############################################################################
void SpdLog_Shutdown()
{
spdlog::shutdown();
}