diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..8392d15 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake \ No newline at end of file diff --git a/.gitignore b/.gitignore index ba3885a..7e48316 100644 --- a/.gitignore +++ b/.gitignore @@ -3,24 +3,10 @@ result result-* -# ---> VirtualEnv -# Virtualenv -# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ -.Python -[Bb]in -[Ii]nclude -[Ll]ib -[Ll]ib64 -[Ll]ocal -[Ss]cripts -pyvenv.cfg -.venv -pip-selfcheck.json - # ---> C++ # Prerequisites *.d - +.direnv # Compiled Object files *.slo *.lo diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a803af7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,56 @@ +cmake_minimum_required(VERSION 3.15) +project(VoidLoaderApi LANGUAGES CXX) + +# ============================================================ +# Project settings +# ============================================================ +set(CMAKE_CXX_STANDARD 26) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +add_compile_options(-ffunction-sections -fdata-sections) +add_link_options(-Wl,--gc-sections) +# ============================================================ +# Source / includes +# ============================================================ +file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "src/*.cpp") + +# ============================================================ +# Platform flags +# ============================================================ +if (CMAKE_SYSTEM_NAME STREQUAL "Windows") + add_definitions(-DWIN32_LEAN_AND_MEAN -DNOMINMAX) +else() + add_compile_options(-fPIC) +endif() + +# ============================================================ +# Build shared library +# ============================================================ +add_library(${PROJECT_NAME} STATIC ${SOURCES}) +target_include_directories(${PROJECT_NAME} + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include +) + +if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + target_link_options(${PROJECT_NAME} PRIVATE + -Wl,-z,defs # no undefined symbols at link + -Wl,--no-undefined + -Wl,--as-needed + -static-libstdc++ # pull libstdc++ into .so + -static-libgcc # pull libgcc into .so + -Wl,-O1 + -Wl,--gc-sections + ) +endif() + + +# Output name: YourAgent.dll / libYourAgent.so +set_target_properties(${PROJECT_NAME} PROPERTIES + PREFIX "" + OUTPUT_NAME "VoidLoaderApi" +) +if(UNIX AND NOT APPLE) + target_link_options(${PROJECT_NAME} PRIVATE -Wl,--enable-new-dtags -Wl,-rpath,\$ORIGIN) + set_target_properties(${PROJECT_NAME} PROPERTIES BUILD_RPATH "\$ORIGIN" INSTALL_RPATH "\$ORIGIN") +endif() \ No newline at end of file diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..3509d15 --- /dev/null +++ b/flake.nix @@ -0,0 +1,39 @@ +{ + description = "Gradle C++ cross-compile (Linux → Windows/Mac)"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = + { + self, + nixpkgs, + flake-utils, + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { inherit system; }; + in + { + devShells.default = pkgs.mkShell { + name = "CppShell"; + + nativeBuildInputs = with pkgs; [ + gcc15Stdenv + gcc15 + ninja + cmake + clang-tools + ]; + + buildInputs = with pkgs; [ + ]; + + shellHook = ''''; + }; + } + ); +} diff --git a/include/Logging/Level.hpp b/include/Logging/Level.hpp new file mode 100644 index 0000000..c999ea3 --- /dev/null +++ b/include/Logging/Level.hpp @@ -0,0 +1,19 @@ +#pragma once +#include + +namespace vl::logging { + +enum class Level : unsigned char { Trace, Debug, Info, Warn, Error }; + +constexpr std::string_view toString(Level lvl) noexcept { + switch (lvl) { + case Level::Trace: return "TRACE"; + case Level::Debug: return "DEBUG"; + case Level::Info: return "INFO"; + case Level::Warn: return "WARN"; + case Level::Error: return "ERROR"; + } + return "UNKNOWN"; +} + +} // namespace vl::log diff --git a/include/Logging/Logger.hpp b/include/Logging/Logger.hpp new file mode 100644 index 0000000..3e01c5d --- /dev/null +++ b/include/Logging/Logger.hpp @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +namespace vl::logging { + +struct Logger : public std::enable_shared_from_this { + virtual ~Logger() = default; + + virtual std::string_view prefix() const noexcept = 0; + virtual void setLevel(Level) noexcept = 0; + virtual Level level() const noexcept = 0; + + template + void log(Level lvl, std::format_string fmt, Args&&... args) { + if (lvl < level()) return; + Message m; + m.ts = std::chrono::system_clock::now(); + m.level = lvl; + m.prefix = std::string(prefix()); + m.text = std::vformat(fmt.get(), std::make_format_args(std::forward(args)...)); + enqueue(std::move(m)); + } + + template void trace(std::format_string f, Args&&... a){ log(Level::Trace,f,std::forward(a)...); } + template void debug(std::format_string f, Args&&... a){ log(Level::Debug,f,std::forward(a)...); } + template void info (std::format_string f, Args&&... a){ log(Level::Info ,f,std::forward(a)...); } + template void warn (std::format_string f, Args&&... a){ log(Level::Warn ,f,std::forward(a)...); } + template void error(std::format_string f, Args&&... a){ log(Level::Error,f,std::forward(a)...); } + +protected: + virtual void enqueue(Message&&) = 0; +}; + +} // namespace vl::log diff --git a/include/Logging/LoggerManager.hpp b/include/Logging/LoggerManager.hpp new file mode 100644 index 0000000..396a41f --- /dev/null +++ b/include/Logging/LoggerManager.hpp @@ -0,0 +1,37 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +namespace vl::logging { + +class LoggerManager { +public: + static LoggerManager& instance(); // Meyers singleton + + // Called by VoidLoaderCore once it’s ready + void setProvider(Provider* p); + + // Throws (E1) if provider not set yet + Provider& provider() const; + + // Convenience forwarding for mods + std::shared_ptr get(std::string_view prefix) const; + std::shared_ptr createConsole(std::string prefix, Level lvl = Level::Info) const; + std::shared_ptr createFile(std::string prefix, std::string basePath, + std::uintmax_t maxBytes = 0, Level lvl = Level::Info) const; + std::shared_ptr createCombined(std::string prefix, std::string basePath, + std::uintmax_t maxBytes = 0, Level lvl = Level::Info) const; + +private: + LoggerManager() = default; + LoggerManager(const LoggerManager&) = delete; + LoggerManager& operator=(const LoggerManager&) = delete; + + std::atomic provider_{nullptr}; +}; + +} // namespace vl::log diff --git a/include/Logging/Message.hpp b/include/Logging/Message.hpp new file mode 100644 index 0000000..bf6d4c5 --- /dev/null +++ b/include/Logging/Message.hpp @@ -0,0 +1,15 @@ +#pragma once +#include +#include +#include + +namespace vl::logging { + +struct Message { + std::chrono::system_clock::time_point ts{}; + Level level{Level::Info}; + std::string prefix; // e.g. "[DB]" + std::string text; +}; + +} // namespace vl::log diff --git a/include/Logging/Provider.hpp b/include/Logging/Provider.hpp new file mode 100644 index 0000000..ac780c9 --- /dev/null +++ b/include/Logging/Provider.hpp @@ -0,0 +1,24 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace vl::logging { + +struct Provider { + virtual ~Provider() = default; + + // P3: get-or-create + virtual std::shared_ptr createConsole(std::string prefix, Level lvl) = 0; + virtual std::shared_ptr createFile(std::string prefix, std::string basePath, + std::uintmax_t maxBytes, Level lvl) = 0; + virtual std::shared_ptr createCombined(std::string prefix, std::string basePath, + std::uintmax_t maxBytes, Level lvl) = 0; + + virtual std::shared_ptr get(std::string_view prefix) const = 0; // throws if absent + virtual bool exists(std::string_view prefix) const = 0; +}; + +} // namespace vl::log diff --git a/include/Logging/Sink.hpp b/include/Logging/Sink.hpp new file mode 100644 index 0000000..012bc9f --- /dev/null +++ b/include/Logging/Sink.hpp @@ -0,0 +1,11 @@ +#pragma once +#include + +namespace vl::logging { + +struct Sink { + virtual ~Sink() = default; + virtual void write(const Message& msg) = 0; +}; + +} // namespace vl::log diff --git a/mingw.cmake b/mingw.cmake new file mode 100644 index 0000000..c7cca00 --- /dev/null +++ b/mingw.cmake @@ -0,0 +1,4 @@ +set(CMAKE_SYSTEM_NAME Windows) +set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc) +set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++) +set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres) diff --git a/src/Logging/LoggerManager.cpp b/src/Logging/LoggerManager.cpp new file mode 100644 index 0000000..4a811d4 --- /dev/null +++ b/src/Logging/LoggerManager.cpp @@ -0,0 +1,40 @@ +#include + +namespace vl::log { + +LoggerManager& LoggerManager::instance() { + static LoggerManager inst; + return inst; +} + +void LoggerManager::setProvider(Provider* p) { + if (!p) throw std::runtime_error("LoggerManager::setProvider: null provider"); + provider_.store(p, std::memory_order_release); +} + +Provider& LoggerManager::provider() const { + Provider* p = provider_.load(std::memory_order_acquire); + if (!p) [[unlikely]] + throw std::runtime_error("LoggerManager::provider: not initialized (Core not registered yet)"); + return *p; +} + +std::shared_ptr LoggerManager::get(std::string_view prefix) const { + return provider().get(prefix); +} + +std::shared_ptr LoggerManager::createConsole(std::string prefix, Level lvl) const { + return provider().createConsole(std::move(prefix), lvl); +} + +std::shared_ptr LoggerManager::createFile(std::string prefix, std::string basePath, + std::uintmax_t maxBytes, Level lvl) const { + return provider().createFile(std::move(prefix), std::move(basePath), maxBytes, lvl); +} + +std::shared_ptr LoggerManager::createCombined(std::string prefix, std::string basePath, + std::uintmax_t maxBytes, Level lvl) const { + return provider().createCombined(std::move(prefix), std::move(basePath), maxBytes, lvl); +} + +} // namespace vl::log