feat(connlib): add FFI for changing log-level on MacOS (#8927)

This isn't plugged into anything yet on the Swift side but lays the
foundation for changing the log-level at runtime without having to sign
the user out.
This commit is contained in:
Thomas Eizinger
2025-04-29 23:51:46 +10:00
committed by GitHub
parent 122d84cfa2
commit 2ba7a87899
3 changed files with 21 additions and 5 deletions

View File

@@ -77,6 +77,9 @@ mod ffi {
#[swift_bridge(swift_name = "setDisabledResources", return_with = err_to_string)]
fn set_disabled_resources(&mut self, disabled_resources: String) -> Result<(), String>;
#[swift_bridge(swift_name = "setLogDirectives", return_with = err_to_string)]
fn set_log_directives(&mut self, directives: String) -> Result<(), String>;
}
extern "Swift" {
@@ -174,6 +177,11 @@ impl Callbacks for CallbackHandler {
}
}
static LOGGER_STATE: OnceLock<(
firezone_logging::file::Handle,
firezone_logging::FilterReloadHandle,
)> = OnceLock::new();
/// Initialises a global logger with the specified log filter.
///
/// A global logger can only be set once, hence this function uses `static` state to check whether a logger has already been set.
@@ -181,11 +189,6 @@ impl Callbacks for CallbackHandler {
///
/// From within the FFI module, we have no control over our memory lifecycle and we may get initialised multiple times within the same process.
fn init_logging(log_dir: PathBuf, log_filter: String) -> Result<()> {
static LOGGER_STATE: OnceLock<(
firezone_logging::file::Handle,
firezone_logging::FilterReloadHandle,
)> = OnceLock::new();
if let Some((_, reload_handle)) = LOGGER_STATE.get() {
reload_handle
.reload(&log_filter)
@@ -323,6 +326,14 @@ impl WrappedSession {
Ok(())
}
fn set_log_directives(&mut self, directives: String) -> Result<()> {
let (_, handle) = LOGGER_STATE.get().context("Logger is not initialised")?;
handle.reload(&directives)?;
Ok(())
}
}
impl Drop for WrappedSession {

View File

@@ -15,5 +15,6 @@ struct __private__ResultPtrAndPtr __swift_bridge__$WrappedSession$connect(void*
void __swift_bridge__$WrappedSession$reset(void* self);
void* __swift_bridge__$WrappedSession$set_dns(void* self, void* dns_servers);
void* __swift_bridge__$WrappedSession$set_disabled_resources(void* self, void* disabled_resources);
void* __swift_bridge__$WrappedSession$set_log_directives(void* self, void* directives);

View File

@@ -49,6 +49,10 @@ extension WrappedSessionRefMut {
public func setDisabledResources<GenericIntoRustString: IntoRustString>(_ disabled_resources: GenericIntoRustString) throws -> () {
try { let val = __swift_bridge__$WrappedSession$set_disabled_resources(ptr, { let rustString = disabled_resources.intoRustString(); rustString.isOwned = false; return rustString.ptr }()); if val != nil { throw RustString(ptr: val!) } else { return } }()
}
public func setLogDirectives<GenericIntoRustString: IntoRustString>(_ directives: GenericIntoRustString) throws -> () {
try { let val = __swift_bridge__$WrappedSession$set_log_directives(ptr, { let rustString = directives.intoRustString(); rustString.isOwned = false; return rustString.ptr }()); if val != nil { throw RustString(ptr: val!) } else { return } }()
}
}
public class WrappedSessionRef {
var ptr: UnsafeMutableRawPointer