iw4x-client/src/Components/Modules/Logger.cpp

388 lines
8.8 KiB
C++
Raw Normal View History

2022-02-27 07:53:44 -05:00
#include <STDInclude.hpp>
2017-01-19 16:23:59 -05:00
namespace Components
{
std::mutex Logger::MessageMutex;
std::vector<std::string> Logger::MessageQueue;
std::vector<Network::Address> Logger::LoggingAddresses[2];
2018-12-17 08:29:18 -05:00
void(*Logger::PipeCallback)(const std::string&) = nullptr;
2017-01-19 16:23:59 -05:00
bool Logger::IsConsoleReady()
{
return (IsWindow(Console::GetWindow()) != FALSE || (Dedicated::IsEnabled() && !Flags::HasFlag("console")));
2017-01-19 16:23:59 -05:00
}
2022-06-12 17:07:53 -04:00
void Logger::PrintStub(const int channel, const char* message, ...)
2017-01-19 16:23:59 -05:00
{
2022-06-12 17:07:53 -04:00
char buf[4096] = {0};
2017-01-19 16:23:59 -05:00
2022-06-12 17:07:53 -04:00
va_list va;
va_start(va, message);
_vsnprintf_s(buf, _TRUNCATE, message, va);
va_end(va);
2017-01-19 16:23:59 -05:00
2022-06-12 17:07:53 -04:00
Logger::MessagePrint(channel, {buf});
2017-01-19 16:23:59 -05:00
}
2022-06-12 17:07:53 -04:00
void Logger::MessagePrint(const int channel, const std::string& msg)
2017-01-19 16:23:59 -05:00
{
2017-07-03 09:40:32 -04:00
if (Flags::HasFlag("stdout") || Loader::IsPerformingUnitTests())
2017-01-19 16:23:59 -05:00
{
2022-06-12 17:07:53 -04:00
printf("%s", msg.data());
2017-01-19 16:23:59 -05:00
fflush(stdout);
return;
}
if (!Logger::IsConsoleReady())
{
2022-06-12 17:07:53 -04:00
OutputDebugStringA(msg.data());
2017-01-19 16:23:59 -05:00
}
if (!Game::Sys_IsMainThread())
{
2022-06-12 17:07:53 -04:00
Logger::EnqueueMessage(msg);
2017-01-19 16:23:59 -05:00
}
else
{
2022-06-12 17:07:53 -04:00
Game::Com_PrintMessage(channel, msg.data(), 0);
2017-01-19 16:23:59 -05:00
}
}
2022-06-12 17:07:53 -04:00
void Logger::DebugInternal(const bool verbose, const std::source_location& srcLoc, const std::string_view fmt, std::format_args&& args)
2017-01-19 16:23:59 -05:00
{
2022-06-12 17:07:53 -04:00
const auto msg = std::vformat(fmt, args);
2022-06-12 17:07:53 -04:00
const auto out = verbose
? std::format("Debug:\n {}\nFile: {}\nLine: {}\n", msg, srcLoc.file_name(), srcLoc.line())
: std::format("Debug:\n {}\n", msg);
2017-01-19 16:23:59 -05:00
2022-06-12 17:07:53 -04:00
Logger::MessagePrint(Game::CON_CHANNEL_DONT_FILTER, out);
2017-01-19 16:23:59 -05:00
}
2022-06-12 17:07:53 -04:00
void Logger::PrintInternal(int channel, std::string_view fmt, std::format_args&& args)
2017-01-19 16:23:59 -05:00
{
2022-06-12 17:07:53 -04:00
const auto msg = std::vformat(fmt, args);
Logger::MessagePrint(channel, msg);
2017-01-19 16:23:59 -05:00
}
2022-06-12 17:07:53 -04:00
void Logger::ErrorInternal(const Game::errorParm_t error, const std::string_view fmt, std::format_args&& args)
2017-01-19 16:23:59 -05:00
{
2022-06-12 17:07:53 -04:00
#ifdef _DEBUG
if (IsDebuggerPresent()) __debugbreak();
#endif
const auto msg = std::vformat(fmt, args);
Game::Com_Error(error, "%s", msg.data());
2017-01-19 16:23:59 -05:00
}
2022-06-12 17:07:53 -04:00
void Logger::PrintErrorInternal(int channel, std::string_view fmt, std::format_args&& args)
2017-01-19 16:23:59 -05:00
{
2022-06-12 17:07:53 -04:00
const auto msg = "^1Error: " + std::vformat(fmt, args);
++(*Game::com_errorPrintsCount);
Logger::MessagePrint(channel, msg);
2017-01-19 16:23:59 -05:00
2022-06-12 17:07:53 -04:00
if (*Game::cls_uiStarted != 0 && (*Game::com_fixedConsolePosition == 0))
{
Game::CL_ConsoleFixPosition();
}
}
2022-04-16 04:01:25 -04:00
2022-06-12 17:07:53 -04:00
void Logger::WarningInternal(int channel, std::string_view fmt, std::format_args&& args)
{
const auto msg = "^3" + std::vformat(fmt, args);
2017-01-19 16:23:59 -05:00
2022-06-12 17:07:53 -04:00
Logger::MessagePrint(channel, msg);
2017-01-19 16:23:59 -05:00
}
void Logger::Flush()
{
// if (!Game::Sys_IsMainThread())
// {
// while (!Logger::MessageQueue.empty())
// {
// std::this_thread::sleep_for(10ms);
// }
// }
// else
{
Logger::Frame();
}
}
void Logger::Frame()
{
std::unique_lock _(Logger::MessageMutex);
2017-01-19 16:23:59 -05:00
for (auto i = Logger::MessageQueue.begin(); i != Logger::MessageQueue.end();)
2017-01-19 16:23:59 -05:00
{
Game::Com_PrintMessage(Game::CON_CHANNEL_DONT_FILTER, i->data(), 0);
2017-01-19 16:23:59 -05:00
if (!Logger::IsConsoleReady())
{
OutputDebugStringA(i->data());
2017-01-19 16:23:59 -05:00
}
i = Logger::MessageQueue.erase(i);
}
2017-01-19 16:23:59 -05:00
}
2018-12-17 08:29:18 -05:00
void Logger::PipeOutput(void(*callback)(const std::string&))
2017-01-19 16:23:59 -05:00
{
Logger::PipeCallback = callback;
}
void Logger::PrintMessagePipe(const char* data)
{
if (Logger::PipeCallback)
{
Logger::PipeCallback(data);
}
}
void Logger::NetworkLog(const char* data, bool gLog)
{
if (!data) return;
2022-04-16 04:01:25 -04:00
const std::string buffer(data);
for (const auto& addr : Logger::LoggingAddresses[gLog & 1])
2017-01-19 16:23:59 -05:00
{
Network::SendCommand(addr, "print", buffer);
}
}
__declspec(naked) void Logger::GameLogStub()
{
__asm
{
2017-02-01 07:44:25 -05:00
pushad
2017-01-19 16:23:59 -05:00
push 1
2017-02-01 07:44:25 -05:00
push [esp + 28h]
2017-01-19 16:23:59 -05:00
call Logger::NetworkLog
add esp, 8h
2017-02-01 07:44:25 -05:00
popad
push 4576C0h
retn
2017-01-19 16:23:59 -05:00
}
}
__declspec(naked) void Logger::PrintMessageStub()
{
__asm
{
mov eax, Logger::PipeCallback
test eax, eax
jz returnPrint
2017-02-01 07:44:25 -05:00
pushad
push [esp + 28h]
2017-01-19 16:23:59 -05:00
call Logger::PrintMessagePipe
add esp, 4h
2017-02-01 07:44:25 -05:00
popad
2017-01-19 16:23:59 -05:00
retn
returnPrint:
2017-02-01 07:44:25 -05:00
pushad
2017-01-19 16:23:59 -05:00
push 0
2017-02-01 07:44:25 -05:00
push [esp + 2Ch]
2017-01-19 16:23:59 -05:00
call Logger::NetworkLog
add esp, 8h
2017-02-01 07:44:25 -05:00
popad
2017-01-19 16:23:59 -05:00
push esi
mov esi, [esp + 0Ch]
2017-02-01 07:44:25 -05:00
push 4AA835h
retn
2017-01-19 16:23:59 -05:00
}
}
2018-12-17 08:29:18 -05:00
void Logger::EnqueueMessage(const std::string& message)
2017-01-19 16:23:59 -05:00
{
std::unique_lock _(Logger::MessageMutex);
2017-01-19 16:23:59 -05:00
Logger::MessageQueue.push_back(message);
}
2017-02-24 19:17:11 -05:00
void Logger::RedirectOSPath(const char* file, char* folder)
{
if (Dvar::Var("g_log").get<std::string>() == file)
{
if (folder != "userraw"s)
{
2017-03-18 12:26:35 -04:00
if (Dvar::Var("iw4x_onelog").get<bool>())
2017-02-24 19:17:11 -05:00
{
strcpy_s(folder, 256, "userraw");
}
}
}
}
__declspec(naked) void Logger::BuildOSPathStub()
{
__asm
{
pushad
push [esp + 28h]
push [esp + 30h]
call Logger::RedirectOSPath
add esp, 8h
popad
mov eax, [esp + 8h]
push ebp
push esi
mov esi, [esp + 0Ch]
push 64213Fh
retn
}
}
2017-01-19 16:23:59 -05:00
Logger::Logger()
{
2022-04-07 05:41:20 -04:00
Dvar::Register<bool>("iw4x_onelog", false, Game::dvar_flag::DVAR_LATCH | Game::dvar_flag::DVAR_ARCHIVE, "Only write the game log to the 'userraw' OS folder");
2017-02-24 19:17:11 -05:00
Utils::Hook(0x642139, Logger::BuildOSPathStub, HOOK_JUMP).install()->quick();
2017-01-19 16:23:59 -05:00
Logger::PipeOutput(nullptr);
Scheduler::Loop(Logger::Frame, Scheduler::Pipeline::MAIN);
2017-01-19 16:23:59 -05:00
Utils::Hook(0x4B0218, Logger::GameLogStub, HOOK_CALL).install()->quick();
Utils::Hook(Game::Com_PrintMessage, Logger::PrintMessageStub, HOOK_JUMP).install()->quick();
2017-07-03 09:40:32 -04:00
if (Loader::IsPerformingUnitTests())
2017-01-19 16:23:59 -05:00
{
Utils::Hook(Game::Com_Printf, Logger::PrintStub, HOOK_JUMP).install()->quick();
}
Scheduler::Once([]
2017-01-19 16:23:59 -05:00
{
Command::AddSV("log_add", [](Command::Params* params)
2017-01-19 16:23:59 -05:00
{
2022-04-07 05:41:20 -04:00
if (params->size() < 2) return;
2017-01-19 16:23:59 -05:00
Network::Address addr(params->get(1));
if (std::find(Logger::LoggingAddresses[0].begin(), Logger::LoggingAddresses[0].end(), addr) == Logger::LoggingAddresses[0].end())
{
Logger::LoggingAddresses[0].push_back(addr);
}
});
Command::AddSV("log_del", [](Command::Params* params)
2017-01-19 16:23:59 -05:00
{
2022-04-07 05:41:20 -04:00
if (params->size() < 2) return;
2017-01-19 16:23:59 -05:00
int num = atoi(params->get(1));
if (Utils::String::VA("%i", num) == std::string(params->get(1)) && static_cast<unsigned int>(num) < Logger::LoggingAddresses[0].size())
{
auto addr = Logger::LoggingAddresses[0].begin() + num;
Logger::Print("Address %s removed\n", addr->getCString());
Logger::LoggingAddresses[0].erase(addr);
}
else
{
Network::Address addr(params->get(1));
auto i = std::find(Logger::LoggingAddresses[0].begin(), Logger::LoggingAddresses[0].end(), addr);
if (i != Logger::LoggingAddresses[0].end())
{
Logger::LoggingAddresses[0].erase(i);
Logger::Print("Address %s removed\n", addr.getCString());
}
else
{
Logger::Print("Address %s not found!\n", addr.getCString());
}
}
});
Command::AddSV("log_list", [](Command::Params*)
2017-01-19 16:23:59 -05:00
{
Logger::Print("# ID: Address\n");
Logger::Print("-------------\n");
for (unsigned int i = 0; i < Logger::LoggingAddresses[0].size(); ++i)
{
Logger::Print("#%03d: %5s\n", i, Logger::LoggingAddresses[0][i].getCString());
}
});
Command::AddSV("g_log_add", [](Command::Params* params)
2017-01-19 16:23:59 -05:00
{
2022-04-07 05:41:20 -04:00
if (params->size() < 2) return;
2017-01-19 16:23:59 -05:00
Network::Address addr(params->get(1));
if (std::find(Logger::LoggingAddresses[1].begin(), Logger::LoggingAddresses[1].end(), addr) == Logger::LoggingAddresses[1].end())
{
Logger::LoggingAddresses[1].push_back(addr);
}
});
Command::AddSV("g_log_del", [](Command::Params* params)
2017-01-19 16:23:59 -05:00
{
2022-04-07 05:41:20 -04:00
if (params->size() < 2) return;
2017-01-19 16:23:59 -05:00
int num = atoi(params->get(1));
if (Utils::String::VA("%i", num) == std::string(params->get(1)) && static_cast<unsigned int>(num) < Logger::LoggingAddresses[1].size())
{
auto addr = Logger::LoggingAddresses[1].begin() + num;
Logger::Print("Address %s removed\n", addr->getCString());
Logger::LoggingAddresses[1].erase(addr);
}
else
{
Network::Address addr(params->get(1));
auto i = std::find(Logger::LoggingAddresses[1].begin(), Logger::LoggingAddresses[1].end(), addr);
if (i != Logger::LoggingAddresses[1].end())
{
Logger::LoggingAddresses[1].erase(i);
Logger::Print("Address %s removed\n", addr.getCString());
}
else
{
Logger::Print("Address %s not found!\n", addr.getCString());
}
}
});
Command::AddSV("g_log_list", [](Command::Params*)
2017-01-19 16:23:59 -05:00
{
Logger::Print("# ID: Address\n");
Logger::Print("-------------\n");
for (unsigned int i = 0; i < Logger::LoggingAddresses[1].size(); ++i)
{
Logger::Print("#%03d: %5s\n", i, Logger::LoggingAddresses[1][i].getCString());
}
});
}, Scheduler::Pipeline::MAIN);
2017-01-19 16:23:59 -05:00
}
Logger::~Logger()
{
Logger::LoggingAddresses[0].clear();
Logger::LoggingAddresses[1].clear();
std::unique_lock lock(Logger::MessageMutex);
2017-01-19 16:23:59 -05:00
Logger::MessageQueue.clear();
lock.unlock();
2017-01-19 16:23:59 -05:00
// Flush the console log
2022-04-16 04:01:25 -04:00
if (const auto logfile = *reinterpret_cast<int*>(0x1AD8F28))
2017-01-19 16:23:59 -05:00
{
2022-04-16 04:01:25 -04:00
Game::FS_FCloseFile(logfile);
2017-01-19 16:23:59 -05:00
}
}
}