Skip to content

Commit

Permalink
Remove dependencies and shorten log filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
AkaiiKitsune committed Nov 27, 2024
1 parent 91a9f9a commit cfefd5c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 54 deletions.
48 changes: 14 additions & 34 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -171,15 +157,18 @@ 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
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
Expand All @@ -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)
Expand All @@ -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"
)
)

# Set a default target
add_custom_target(default ALL DEPENDS bnusio)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
17 changes: 6 additions & 11 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ time_t rawTime;
tm *timeInfo;
SYSTEMTIME systemTime;

/*static std::unique_ptr<plog::RollingFileAppender<plog::TxtFormatter>> fileAppender;
static std::unique_ptr<plog::ConsoleAppender<plog::TxtFormatter>> consoleAppender;*/

void
InitializeLogger (const LogLevel level, const bool logToFile) {
if (loggerInstance == nullptr) {
Expand All @@ -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<plog::ConsoleAppender<plog::TxtFormatter>> ();
auto& init = plog::init (plog::verbose, consoleAppender.get ());
if (logToFile) {
fileAppender = std::make_unique<plog::RollingFileAppender<plog::TxtFormatter>> ("TaikoArcadeLoader.log", 1000000, 3);
init.addAppender (fileAppender.get ());
}*/
}

void
Expand All @@ -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);
Expand All @@ -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
Expand Down
30 changes: 22 additions & 8 deletions src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <source_location>
#include <string_view>
#include <wincon.h>
#include <magic_enum/magic_enum.hpp>

#define STRING(x) #x
#define XSTRING(x) STRING (x)

enum class LogLevel {
NONE = 0,
Expand Down Expand Up @@ -53,6 +55,9 @@ struct LogMessage {
}
};

/* *
* Logs a message with file and line information, if the log level permits.
*/
template <>
struct LogMessage<void> {
LogMessage (const LogLevel level, const std::string_view format, const std::source_location &loc = std::source_location::current ()) {
Expand All @@ -65,29 +70,38 @@ struct LogMessage<void> {
};

LogMessage (LogLevel level, std::string_view format) -> LogMessage<void>;

LogMessage (LogLevel level, std::wstring_view format) -> LogMessage<void>;

template <typename... Args>
LogMessage (LogLevel level, std::string_view format, Args &&...ts) -> LogMessage<Args...>;

template <typename... Args>
LogMessage (LogLevel level, std::wstring_view format, Args &&...ts) -> LogMessage<Args...>;

/* Converts a string to a LogLevel type. */
inline LogLevel
GetLogLevel (const std::string &logLevelStr) {
const auto level = magic_enum::enum_cast<LogLevel> (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
Expand Down

0 comments on commit cfefd5c

Please # to comment.