Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ env:
CARGO_TERM_COLOR: always

jobs:
cpp-sdk:
name: Test C++ SDK (${{ matrix.os }})
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Check out repository
uses: actions/checkout@v7

- name: Configure C++ SDK tests
run: cmake -S sdk -B build/cpp-sdk -DCMAKE_BUILD_TYPE=Release

- name: Build C++ SDK tests
run: cmake --build build/cpp-sdk --config Release

- name: Test C++ SDK
run: ctest --test-dir build/cpp-sdk -C Release --output-on-failure

build:
name: Check, test, and build
runs-on: windows-latest
Expand Down Expand Up @@ -52,6 +71,7 @@ jobs:
New-Item -ItemType Directory -Force artifact
Copy-Item target\x86_64-pc-windows-msvc\release\bass.dll artifact\
Copy-Item README.md, LICENSE artifact\
Copy-Item -Recurse sdk artifact\sdk

- name: Upload artifact
uses: actions/upload-artifact@v7
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ Calls such as `log::error!`, `log::warn!`, and `log::info!` then use the modload

Logging remains optional for compatibility. Old mods without the bridge continue to load in new modloaders without logging, and new mods continue to load in old modloaders without logging.

##### C++ mods

For logging from a C++ mod, add `sdk/include` to your include path and define the setup hook in exactly one source file:

```cpp
#include <highfleet/log.hpp>

HIGHFLEET_SETUP_LOGGER()

extern "C" __declspec(dllexport) bool __cdecl init() {
highfleet::logging::info("Ready!");
return true;
}
```

For more details, see the [C++ logging SDK documentation](sdk/README.md).

#### `init() -> bool`
The modloader calls this function last.
If you return false, the modloader assumes that you have failed to initialize.
Expand Down
1 change: 1 addition & 0 deletions sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build/
23 changes: 23 additions & 0 deletions sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.20)
project(highfleet_cpp_sdk LANGUAGES CXX)

add_library(highfleet_logging INTERFACE)
target_include_directories(highfleet_logging INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_features(highfleet_logging INTERFACE cxx_std_11)

include(CTest)
if(BUILD_TESTING)
add_executable(highfleet_log_test tests/log_test.cpp tests/log_other.cpp)
target_link_libraries(highfleet_log_test PRIVATE highfleet_logging)
set_target_properties(highfleet_log_test PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO)
if(MSVC)
target_compile_options(highfleet_log_test PRIVATE /W4 /WX /permissive-)
else()
target_compile_options(highfleet_log_test PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif()
add_test(NAME logging COMMAND highfleet_log_test)
add_test(NAME optional_callbacks COMMAND highfleet_log_test optional)
endif()
109 changes: 109 additions & 0 deletions sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# HighFleet C++ logging SDK

A C++11 header-only logging API for mods.
Add `sdk/include` to your include path.
Highfleet and the modloader run on x86_64 Windows, so your mod DLL must also be built for x86_64 Windows.

## Use in a mod

Put the setup macro at global scope in **exactly one `.cpp` file**:

```cpp
#include <highfleet/log.hpp>

HIGHFLEET_SETUP_LOGGER()

extern "C" __declspec(dllexport) bool __cdecl init() {
highfleet::logging::info("Ready!");
highfleet::logging::warn("Optional configuration missing");
return true;
}
```

Every other translation unit can include the header and log without repeating the macro.

Initialize your mod in `init`, where logging is already available.
If you choose to initialize in `DllMain` instead, do not log there: logging is unavailable before setup, and later calls under the loader lock can deadlock.

## Logging API

All user-facing names are in `highfleet::logging`:

- `error(message)`, `warn(message)`, `info(message)`, `debug(message)`, `trace(message)`.
- `enabled(level)`: ask the host before doing expensive message formatting.
- `flush()`: flush the modloader's buffered log output; does nothing before setup.
- `enum class level : std::uint32_t`: error = 1, warn = 2, info = 3, debug = 4, trace = 5.

```cpp
if (highfleet::logging::enabled(highfleet::logging::level::debug)) {
// Build your diagnostic string here, then pass its c_str() to debug().
}
```

The functions accept UTF-8, NUL-terminated strings, borrow them synchronously, and do not format or allocate.
Keep the string alive until the call returns.
Null strings are treated as empty; embedded NUL bytes terminate the text.
Arbitrary invalid pointers are not made safe, and the SDK does not validate UTF-8.

For callers that already have diagnostic targets, each named helper also accepts `(target, message)`.
The generic entry point is `log(level, target, message)`, and `enabled(level, target)` accepts a target too.
Targets are forwarded as metadata, but **the host identifies and filters mods by their DLL stem**, not by these caller-controlled strings.
Most mods should just use the message-only helpers.

## Filtering and output

The SDK uses the modloader's logging configuration; see [Logging configuration](../README.md#logging-configuration).

## Host ABI details

Mods only need the macro and logging functions; they do not implement a logger.
`HIGHFLEET_SETUP_LOGGER()` emits this Windows export:

```cpp
extern "C" __declspec(dllexport) std::uint32_t __cdecl highfleet_mod_setup_v1(
const highfleet::ffi::HfmHostApiV1* host);
```

The raw types in `highfleet::ffi` mirror `crates/highfleet-mod-api/src/ffi.rs`.
The version-1 table carries an opaque context, an optional enabled callback, a required write callback, and an optional flush callback.
Records carry byte-counted UTF-8 views (`HfmStr`), level, size, target/message, and empty source metadata.
No C++ objects, Rust traits, ownership, or strings requiring a shared allocator cross the ABI.

Setup validates version, size, and required callbacks, then copies the table.
The table itself only needs to live through setup.
Its context and callback code must remain valid and thread-safe for the entire lifetime of mod logging.
Callbacks must not throw or unwind across the C boundary.
Each record and its strings are borrowed only until the synchronous callback returns.

Installation succeeds once, using an atomic claim followed by release/acquire publication of the immutable copied table.
There is no replacement/uninstall API.
An external-linkage inline function's local static shares state across translation units under C++11.
Calls before publication are safe no-ops.
Setup status codes match Rust: 0 success, 1 null table, 2 unsupported version, 3 short table, 4 missing write callback, 5 already installed; `UINT32_MAX` is reserved for a Rust setup panic.

C++ and Rust mods use the same `highfleet_mod_setup_v1` export.
Non-Windows builds omit Windows export/calling-convention annotations for tests.

## Standalone tests

With CMake 3.20 or later, from the repository root:

```sh
cmake -S sdk -B build/cpp-sdk
cmake --build build/cpp-sdk --config Release
ctest --test-dir build/cpp-sdk -C Release --output-on-failure
```

Or directly with a C++11 compiler:

```sh
mkdir -p sdk/build
c++ -std=c++11 -Wall -Wextra -Wpedantic -Werror -Isdk/include \
sdk/tests/log_test.cpp sdk/tests/log_other.cpp -o sdk/build/log_test
sdk/build/log_test
sdk/build/log_test optional
```

Both test modes use explicit checks that remain active with `NDEBUG`.
The optional-callback case runs in a separate process because installation is intentionally one-shot.
Tests cover ABI layouts matching Rust's assertions, setup rejection, table copying, synchronous forwarding/context, level values, filtering, flush, UTF-8/null text, and cross-translation-unit state.
198 changes: 198 additions & 0 deletions sdk/include/highfleet/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#ifndef HIGHFLEET_LOG_HPP
#define HIGHFLEET_LOG_HPP

#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstring>

#if defined(_WIN32)
#define HIGHFLEET_LOGGING_CDECL __cdecl
#define HIGHFLEET_LOGGING_EXPORT __declspec(dllexport)
#else
#define HIGHFLEET_LOGGING_CDECL
#define HIGHFLEET_LOGGING_EXPORT
#endif

namespace highfleet {
namespace ffi {

constexpr std::uint32_t HFM_HOST_ABI_VERSION_1 = 1;
constexpr std::uint32_t HFM_STATUS_OK = 0;
constexpr std::uint32_t HFM_STATUS_NULL_API = 1;
constexpr std::uint32_t HFM_STATUS_UNSUPPORTED_ABI = 2;
constexpr std::uint32_t HFM_STATUS_API_TOO_SMALL = 3;
constexpr std::uint32_t HFM_STATUS_MISSING_CALLBACK = 4;
constexpr std::uint32_t HFM_STATUS_LOGGER_ALREADY_SET = 5;
constexpr std::uint32_t HFM_STATUS_PANIC = UINT32_MAX;

struct HfmStr {
const std::uint8_t* ptr;
std::size_t len;
};

struct HfmLogRecordV1 {
std::uint32_t struct_size;
std::uint32_t level;
HfmStr target;
HfmStr message;
HfmStr module_path;
HfmStr file;
std::uint32_t line;
std::uint32_t reserved;
};

using HfmLogEnabledV1 = std::uint8_t (HIGHFLEET_LOGGING_CDECL *)(void*, std::uint32_t, HfmStr);
using HfmLogWriteV1 = void (HIGHFLEET_LOGGING_CDECL *)(void*, const HfmLogRecordV1*);
using HfmLogFlushV1 = void (HIGHFLEET_LOGGING_CDECL *)(void*);

struct HfmHostApiV1 {
std::uint32_t abi_version;
std::uint32_t struct_size;
void* context;
HfmLogEnabledV1 log_enabled;
HfmLogWriteV1 log_write;
HfmLogFlushV1 log_flush;
};

} // namespace ffi

namespace logging {

enum class level : std::uint32_t {
error = 1,
warn = 2,
info = 3,
debug = 4,
trace = 5
};

namespace detail {

struct logger_state {
std::atomic<unsigned> phase;
ffi::HfmHostApiV1 host;

logger_state() noexcept : phase(0), host{} {}
};

inline logger_state& logger() noexcept {
// External-linkage inline functions share their local static across translation units.
static logger_state state;
return state;
}

inline const ffi::HfmHostApiV1* installed_host() noexcept {
logger_state& state = logger();
return state.phase.load(std::memory_order_acquire) == 2 ? &state.host : nullptr;
}

inline ffi::HfmStr borrow(const char* text) noexcept {
return {reinterpret_cast<const std::uint8_t*>(text),
text == nullptr ? 0 : std::strlen(text)};
}

inline std::uint32_t install_host(const ffi::HfmHostApiV1* host) noexcept {
if (host == nullptr) {
return ffi::HFM_STATUS_NULL_API;
}
if (host->abi_version != ffi::HFM_HOST_ABI_VERSION_1) {
return ffi::HFM_STATUS_UNSUPPORTED_ABI;
}
if (host->struct_size < sizeof(ffi::HfmHostApiV1)) {
return ffi::HFM_STATUS_API_TOO_SMALL;
}
if (host->log_write == nullptr) {
return ffi::HFM_STATUS_MISSING_CALLBACK;
}
detail::logger_state& state = detail::logger();
unsigned expected = 0;
if (!state.phase.compare_exchange_strong(expected, 1, std::memory_order_relaxed)) {
return ffi::HFM_STATUS_LOGGER_ALREADY_SET;
}
state.host = *host;
// Readers access the immutable table only after publication is complete.
state.phase.store(2, std::memory_order_release);
return ffi::HFM_STATUS_OK;
}

} // namespace detail

inline bool enabled(level severity, const char* target = nullptr) {
const ffi::HfmHostApiV1* host = detail::installed_host();
return host != nullptr &&
(host->log_enabled == nullptr ||
host->log_enabled(host->context, static_cast<std::uint32_t>(severity),
detail::borrow(target)) != 0);
}

inline void flush() {
const ffi::HfmHostApiV1* host = detail::installed_host();
if (host != nullptr && host->log_flush != nullptr) {
host->log_flush(host->context);
}
}

inline void log(level severity, const char* target, const char* message) {
const ffi::HfmHostApiV1* host = detail::installed_host();
if (host == nullptr || !enabled(severity, target)) {
return;
}
const ffi::HfmLogRecordV1 record = {
sizeof(ffi::HfmLogRecordV1), static_cast<std::uint32_t>(severity),
detail::borrow(target), detail::borrow(message),
{nullptr, 0}, {nullptr, 0}, 0, 0
};
host->log_write(host->context, &record);
}

inline void error(const char* target, const char* message) {
log(level::error, target, message);
}

inline void warn(const char* target, const char* message) {
log(level::warn, target, message);
}

inline void info(const char* target, const char* message) {
log(level::info, target, message);
}

inline void debug(const char* target, const char* message) {
log(level::debug, target, message);
}

inline void trace(const char* target, const char* message) {
log(level::trace, target, message);
}

inline void error(const char* message) {
error(nullptr, message);
}

inline void warn(const char* message) {
warn(nullptr, message);
}

inline void info(const char* message) {
info(nullptr, message);
}

inline void debug(const char* message) {
debug(nullptr, message);
}

inline void trace(const char* message) {
trace(nullptr, message);
}

} // namespace logging
} // namespace highfleet

#define HIGHFLEET_SETUP_LOGGER() \
extern "C" HIGHFLEET_LOGGING_EXPORT std::uint32_t HIGHFLEET_LOGGING_CDECL \
highfleet_mod_setup_v1(const ::highfleet::ffi::HfmHostApiV1* host) { \
return ::highfleet::logging::detail::install_host(host); \
}

#endif // HIGHFLEET_LOG_HPP
Loading
Loading