From f432851d98109e25ac76cf886b660e633a8f26f8 Mon Sep 17 00:00:00 2001 From: pizzaboxer <41478239+pizzaboxer@users.noreply.github.com> Date: Fri, 21 Jan 2022 09:57:38 +0000 Subject: [PATCH] Add Logging utility for logging Http and Output calls --- PolygonClientUtilities/Logger.cpp | 42 +++++++++++++++++++ PolygonClientUtilities/Logger.h | 17 ++++++++ .../PolygonClientUtilities.vcxproj | 2 + .../PolygonClientUtilities.vcxproj.filters | 6 +++ PolygonClientUtilities/RobloxMFCHooks.cpp | 38 +++++++---------- PolygonClientUtilities/pch.h | 2 + 6 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 PolygonClientUtilities/Logger.cpp create mode 100644 PolygonClientUtilities/Logger.h diff --git a/PolygonClientUtilities/Logger.cpp b/PolygonClientUtilities/Logger.cpp new file mode 100644 index 0000000..42d7a31 --- /dev/null +++ b/PolygonClientUtilities/Logger.cpp @@ -0,0 +1,42 @@ +#include "pch.h" +#include "Logger.h" + +#pragma warning(disable : 4996) + +HANDLE Logger::handle; +std::ofstream Logger::outputLog; +std::ofstream Logger::httpLog; + +void Logger::Initialize(const std::string jobId) +{ + AllocConsole(); + freopen_s((FILE**)stdout, "CONOUT$", "w", stdout); + Logger::handle = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + SetStdHandle(STD_OUTPUT_HANDLE, Logger::handle); + + Logger::outputLog = std::ofstream(jobId + std::string("-Output.txt")); + Logger::httpLog = std::ofstream(jobId + std::string("-Http.txt")); +} + +void Logger::Log(LogType type, const std::string message) +{ + if (!handle) return; + + switch (type) + { + case LogType::Output: + outputLog << "[" << Logger::UtcTime() << "] " << message.c_str() << std::endl; + break; + case LogType::Http: + httpLog << "[" << Logger::UtcTime() << "] " << message.c_str() << std::endl; + break; + } +} + +std::string Logger::UtcTime() +{ + std::stringstream time; + std::time_t now = std::time(NULL); + time << std::put_time(std::localtime(&now), "%F %T"); + return time.str(); +} \ No newline at end of file diff --git a/PolygonClientUtilities/Logger.h b/PolygonClientUtilities/Logger.h new file mode 100644 index 0000000..17a3b68 --- /dev/null +++ b/PolygonClientUtilities/Logger.h @@ -0,0 +1,17 @@ +#pragma once + +#include "pch.h" + +enum class LogType { Output, Http }; + +class Logger +{ +private: + static std::ofstream outputLog; + static std::ofstream httpLog; +public: + static HANDLE handle; + static void Initialize(const std::string jobId); + static void Log(LogType type, const std::string message); + static std::string UtcTime(); +}; \ No newline at end of file diff --git a/PolygonClientUtilities/PolygonClientUtilities.vcxproj b/PolygonClientUtilities/PolygonClientUtilities.vcxproj index 88f7fe7..1414d16 100644 --- a/PolygonClientUtilities/PolygonClientUtilities.vcxproj +++ b/PolygonClientUtilities/PolygonClientUtilities.vcxproj @@ -158,6 +158,7 @@ + @@ -166,6 +167,7 @@ + NotUsing NotUsing diff --git a/PolygonClientUtilities/PolygonClientUtilities.vcxproj.filters b/PolygonClientUtilities/PolygonClientUtilities.vcxproj.filters index 8d978f6..078da28 100644 --- a/PolygonClientUtilities/PolygonClientUtilities.vcxproj.filters +++ b/PolygonClientUtilities/PolygonClientUtilities.vcxproj.filters @@ -33,6 +33,9 @@ Header Files + + Header Files + @@ -50,5 +53,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/PolygonClientUtilities/RobloxMFCHooks.cpp b/PolygonClientUtilities/RobloxMFCHooks.cpp index abb50d5..164426a 100644 --- a/PolygonClientUtilities/RobloxMFCHooks.cpp +++ b/PolygonClientUtilities/RobloxMFCHooks.cpp @@ -1,12 +1,8 @@ #include "pch.h" -#include "Config.h" #include "RobloxMFCHooks.h" +#include "Logger.h" +#include "Config.h" #include "LUrlParser.h" - -static HANDLE handle; -static std::ofstream jobOutputLog; -static std::ofstream jobHttpLog; - static bool hasAuthUrlArg = false; static bool hasAuthTicketArg = false; static bool hasJoinArg = false; @@ -91,13 +87,7 @@ void __fastcall CRobloxCommandLineInfo__ParseParam_hook(CRobloxCommandLineInfo* if (hasJobId && jobId.empty()) { jobId = std::string(pszParam); - jobOutputLog = std::ofstream(jobId + std::string("-Output.txt")); - jobHttpLog = std::ofstream(jobId + std::string("-Http.txt")); - - AllocConsole(); - freopen_s((FILE**)stdout, "CONOUT$", "w", stdout); - handle = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - SetStdHandle(STD_OUTPUT_HANDLE, handle); + Logger::Initialize(jobId); CCommandLineInfo__ParseLast(_this, bLast); return; @@ -174,7 +164,9 @@ BOOL __fastcall Http__trustCheck_hook(const char* url) if (!parsedUrl.isValid()) return false; - jobHttpLog << url << std::endl; +#ifdef ARBITERBUILD + Logger::Log(LogType::Http, url); +#endif if (std::string("about:blank") == url) return true; @@ -196,24 +188,24 @@ void __fastcall StandardOut__print_hook(void* _this, void*, int type, const std: switch (type) { case 1: // RBX::MESSAGE_OUTPUT: - jobOutputLog << "[RBX::MESSAGE_OUTPUT] " << message.c_str() << std::endl; - SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_INTENSITY); + Logger::Log(LogType::Output, std::string("[MESSAGE_OUTPUT] ") + message); + SetConsoleTextAttribute(Logger::handle, FOREGROUND_BLUE | FOREGROUND_INTENSITY); break; case 0: // RBX::MESSAGE_INFO: - jobOutputLog << "[RBX::MESSAGE_INFO] " << message.c_str() << std::endl; - SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); + Logger::Log(LogType::Output, std::string("[MESSAGE_INFO] ") + message); + SetConsoleTextAttribute(Logger::handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break; case 2: // RBX::MESSAGE_WARNING: - jobOutputLog << "[RBX::MESSAGE_WARNING] " << message.c_str() << std::endl; - SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN); + Logger::Log(LogType::Output, std::string("[MESSAGE_WARNING] ") + message); + SetConsoleTextAttribute(Logger::handle, FOREGROUND_RED | FOREGROUND_GREEN); break; case 3: // RBX::MESSAGE_ERROR: - jobOutputLog << "[RBX::MESSAGE_ERROR] " << message.c_str() << std::endl; - SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_INTENSITY); + Logger::Log(LogType::Output, std::string("[MESSAGE_ERROR] ") + message); + SetConsoleTextAttribute(Logger::handle, FOREGROUND_RED | FOREGROUND_INTENSITY); break; } printf("%s\n", message.c_str()); - SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); + SetConsoleTextAttribute(Logger::handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); StandardOut__print(_this, type, message); } diff --git a/PolygonClientUtilities/pch.h b/PolygonClientUtilities/pch.h index ec322c5..bb86dd1 100644 --- a/PolygonClientUtilities/pch.h +++ b/PolygonClientUtilities/pch.h @@ -2,7 +2,9 @@ #include #include +#include #include +#include #include #include #include \ No newline at end of file