From 6224728d4bb42651f42bcf51c07219589b098091 Mon Sep 17 00:00:00 2001 From: Edo Date: Tue, 28 Mar 2023 19:13:47 +0100 Subject: [PATCH] [Download]: Log Mongoose stuff properly (#879) --- src/Components/Modules/Download.cpp | 28 +++++++++++++++++++++++++--- src/Components/Modules/Download.hpp | 4 ++++ src/Components/Modules/Logger.cpp | 7 ++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/Components/Modules/Download.cpp b/src/Components/Modules/Download.cpp index 91572ce3..667f9aa0 100644 --- a/src/Components/Modules/Download.cpp +++ b/src/Components/Modules/Download.cpp @@ -26,6 +26,8 @@ namespace Components volatile bool Download::Terminate; bool Download::ServerRunning; + std::string Download::MongooseLogBuffer; + #pragma region Client void Download::InitiateMapDownload(const std::string& map, bool needPassword) @@ -416,6 +418,19 @@ namespace Components #pragma region Server + void Download::LogFn(char c, [[maybe_unused]] void* param) + { + // Truncate & print if buffer is 1024 characters in length or otherwise only print when we reached a 'new line' + if (!std::isprint(static_cast(c)) || MongooseLogBuffer.size() == 1024) + { + Logger::Print(Game::CON_CHANNEL_NETWORK, "{}\n", MongooseLogBuffer); + MongooseLogBuffer.clear(); + return; + } + + MongooseLogBuffer.push_back(c); + } + static std::string InfoHandler() { const auto status = ServerInfo::GetInfo(); @@ -462,7 +477,7 @@ namespace Components } info["players"] = players; - return {nlohmann::json(info).dump()}; + return nlohmann::json(info).dump(); } static std::string ListHandler() @@ -508,7 +523,7 @@ namespace Components jsonList = fileList; } - return {jsonList.dump()}; + return jsonList.dump(); } static std::string MapHandler() @@ -552,7 +567,7 @@ namespace Components jsonList = fileList; } - return {jsonList.dump()}; + return jsonList.dump(); } static void FileHandler(mg_connection* c, const mg_http_message* hm) @@ -668,6 +683,13 @@ namespace Components { if (!Flags::HasFlag("disable-mongoose")) { +#ifdef _DEBUG + mg_log_set(MG_LL_INFO); +#else + mg_log_set(MG_LL_ERROR); +#endif + + mg_log_set_fn(LogFn, nullptr); mg_mgr_init(&Mgr); Network::OnStart([] diff --git a/src/Components/Modules/Download.hpp b/src/Components/Modules/Download.hpp index fa3dd654..fe8ab847 100644 --- a/src/Components/Modules/Download.hpp +++ b/src/Components/Modules/Download.hpp @@ -91,10 +91,14 @@ namespace Components static volatile bool Terminate; static bool ServerRunning; + static std::string MongooseLogBuffer; + static void DownloadProgress(FileDownload* fDownload, std::size_t bytes); static void ModDownloader(ClientDownload* download); static bool ParseModList(ClientDownload* download, const std::string& list); static bool DownloadFile(ClientDownload* download, unsigned int index); + + static void LogFn(char c, void* param); }; } diff --git a/src/Components/Modules/Logger.cpp b/src/Components/Modules/Logger.cpp index f10d70a4..e5798d5b 100644 --- a/src/Components/Modules/Logger.cpp +++ b/src/Components/Modules/Logger.cpp @@ -28,7 +28,12 @@ namespace Components void Logger::MessagePrint(const int channel, const std::string& msg) { - if (Flags::HasFlag("stdout") || Loader::IsPerformingUnitTests()) + static const auto shouldPrint = []() -> bool + { + return Flags::HasFlag("stdout") || Loader::IsPerformingUnitTests(); + }(); + + if (shouldPrint) { std::printf("%s", msg.data()); std::fflush(stdout);