From cfefd5c38f9a15385610f98b5554cbc41746338a Mon Sep 17 00:00:00 2001 From: KIT! Date: Wed, 27 Nov 2024 22:26:35 +0100 Subject: [PATCH] Remove dependencies and shorten log filepath --- CMakeLists.txt | 48 ++++++++++++++---------------------------------- README.md | 2 +- src/logger.cpp | 17 ++++++----------- src/logger.h | 30 ++++++++++++++++++++++-------- 4 files changed, 43 insertions(+), 54 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4eebb3a..e8ddce0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,20 +122,6 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(minhook) -FetchContent_Declare( - magic_enum - GIT_REPOSITORY https://github.com/Neargye/magic_enum.git - GIT_TAG v0.9.7 -) -FetchContent_MakeAvailable(magic_enum) - -FetchContent_Declare( - plog - GIT_REPOSITORY https://github.com/SergiusTheBest/plog.git - GIT_TAG 1.1.10 -) -FetchContent_MakeAvailable(plog) - # Source files set(SOURCES src/dllmain.cpp @@ -171,8 +157,6 @@ target_include_directories(bnusio PRIVATE ${zlib_SOURCE_DIR} ${libtomcrypt_SOURCE_DIR}/src/headers ${minhook_SOURCE_DIR}/include - ${magic_enum_SOURCE_DIR} - ${plog_SOURCE_DIR}/include ) # Compiler definitions @@ -180,6 +164,11 @@ target_compile_definitions(bnusio PRIVATE _WIN32_WINNT=_WIN32_WINNT_WIN10 ) +# Add link options +if(NOT MSVC) + target_link_options(bnusio PRIVATE -Wl,--allow-multiple-definition) +endif() + # Link libraries target_link_libraries(bnusio PRIVATE tomlc99 @@ -194,16 +183,11 @@ target_link_libraries(bnusio PRIVATE ws2_32 ntdll minhook - magic_enum - plog ) -# Add link options -# if(MSVC) -# target_link_options(bnusio PRIVATE /FORCE:MULTIPLE) -# else() -# target_link_options(bnusio PRIVATE -Wl,--allow-multiple-definition) -# endif() +# Define log path; used to make the file path relative in the log calls. +# Last character (-) to remove the trailing slash in the log path +add_compile_definitions("SOURCE_ROOT=${CMAKE_CURRENT_SOURCE_DIR}/src-") # Set runtime library to static if(MSVC) @@ -218,17 +202,13 @@ if (MSVC) set_target_properties(bnusio PROPERTIES LINK_FLAGS "/DEF:${DEF_FILE}") endif() -# Set a default target -add_custom_target(default ALL DEPENDS bnusio) - -## Copy the build output to the Out folder -set(OUT_DIR "${CMAKE_SOURCE_DIR}/dist") -set(DLL_SOURCE_PATH "${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/bnusio.dll") - add_custom_command( TARGET bnusio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different - "${DLL_SOURCE_PATH}" - "${OUT_DIR}/bnusio.dll" + "${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/bnusio.dll" + "${CMAKE_SOURCE_DIR}/dist/bnusio.dll" COMMENT "Copying bnusio.dll to dist directory" -) \ No newline at end of file +) + +# Set a default target +add_custom_target(default ALL DEPENDS bnusio) \ No newline at end of file diff --git a/README.md b/README.md index cacd972..7c56486 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build cmake -B build -S . -G "Visual Studio 17 2022" -A x64 -DCMAKE_BUILD_TYPE=Release # Build TaikoArcadeLoader -cmake --build build --config Release +cmake --build build --config Release --target bnusio ``` The compiled build of TaikoArcadeLoader will be written in the `dist` folder. diff --git a/src/logger.cpp b/src/logger.cpp index a028993..820580c 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -25,9 +25,6 @@ time_t rawTime; tm *timeInfo; SYSTEMTIME systemTime; -/*static std::unique_ptr> fileAppender; -static std::unique_ptr> consoleAppender;*/ - void InitializeLogger (const LogLevel level, const bool logToFile) { if (loggerInstance == nullptr) { @@ -41,13 +38,6 @@ InitializeLogger (const LogLevel level, const bool logToFile) { loggerInstance->logFile = fopen ("TaikoArcadeLoader.log", "w"); // Open in write mode if (!loggerInstance->logFile) LogMessage (LogLevel::WARN, std::string ("Failed to open TaikoArcadeLoader.log for writing.")); } else loggerInstance->logFile = nullptr; // No file logging - - /*consoleAppender = std::make_unique> (); - auto& init = plog::init (plog::verbose, consoleAppender.get ()); - if (logToFile) { - fileAppender = std::make_unique> ("TaikoArcadeLoader.log", 1000000, 3); - init.addAppender (fileAppender.get ()); - }*/ } void @@ -70,6 +60,11 @@ LogMessageHandler (const char *function, const char *codeFile, int codeLine, Log // Determine log type string std::string logType = GetLogLevelString (messageLevel); + // Remove the absolute path of the build dir + constexpr std::string_view build_dir = XSTRING (SOURCE_ROOT); + std::string_view filename = codeFile; + filename.remove_prefix (build_dir.size ()); + // Get current time and milliseconds SYSTEMTIME systemTime; GetSystemTime (&systemTime); @@ -80,7 +75,7 @@ LogMessageHandler (const char *function, const char *codeFile, int codeLine, Log // Construct the log message std::ostringstream logStream; - logStream << function << " (" << codeFile << ":" << codeLine << "): " << formattedMessage; + logStream << function << " (" << filename << ":" << codeLine << "): " << formattedMessage; std::string logMessage = logStream.str (); // Print the log message diff --git a/src/logger.h b/src/logger.h index b9228f3..1375a4b 100644 --- a/src/logger.h +++ b/src/logger.h @@ -7,7 +7,9 @@ #include #include #include -#include + +#define STRING(x) #x +#define XSTRING(x) STRING (x) enum class LogLevel { NONE = 0, @@ -53,6 +55,9 @@ struct LogMessage { } }; +/* * + * Logs a message with file and line information, if the log level permits. + */ template <> struct LogMessage { LogMessage (const LogLevel level, const std::string_view format, const std::source_location &loc = std::source_location::current ()) { @@ -65,29 +70,38 @@ struct LogMessage { }; LogMessage (LogLevel level, std::string_view format) -> LogMessage; - LogMessage (LogLevel level, std::wstring_view format) -> LogMessage; template LogMessage (LogLevel level, std::string_view format, Args &&...ts) -> LogMessage; - template LogMessage (LogLevel level, std::wstring_view format, Args &&...ts) -> LogMessage; /* Converts a string to a LogLevel type. */ inline LogLevel GetLogLevel (const std::string &logLevelStr) { - const auto level = magic_enum::enum_cast (logLevelStr); - return level.value_or (LogLevel::NONE); + if (logLevelStr == "DEBUG") return LogLevel::DEBUG; + else if (logLevelStr == "INFO") return LogLevel::INFO; + else if (logLevelStr == "WARN") return LogLevel::WARN; + else if (logLevelStr == "ERROR") return LogLevel::ERROR; + else if (logLevelStr == "HOOKS") return LogLevel::HOOKS; + return LogLevel::NONE; } /* Converts a LogLevel type to a string for logging. */ inline std::string -GetLogLevelString (const LogLevel messageLevel) { - const auto level = magic_enum::enum_name (messageLevel); - return std::string (level) + ": "; +GetLogLevelString (LogLevel messageLevel) { + switch (messageLevel) { + case LogLevel::DEBUG: return "DEBUG: "; + case LogLevel::INFO: return "INFO: "; + case LogLevel::WARN: return "WARN: "; + case LogLevel::ERROR: return "ERROR: "; + case LogLevel::HOOKS: return "HOOKS: "; + default: return "NONE: "; + } } +/* Converts a LogLevel type to an int for colors display in the console. */ inline int GetLogLevelColor (const LogLevel messageLevel) { // Colors: https://i.sstatic.net/ZG625.png