#![allow(unsafe_code)]
use std::{slice, str};
use acta_abi as abi;
#[allow(non_camel_case_types)]
pub type acta_ffi_handle = u64;
#[allow(non_camel_case_types)]
#[repr(u32)]
pub enum acta_ffi_level {
Error = 0,
Warn = 1,
Info = 2,
Debug = 3,
Trace = 4,
}
#[allow(non_camel_case_types)]
#[repr(i32)]
pub enum acta_ffi_status {
Ok = 0,
InvalidArgument = 1,
InvalidHandle = 2,
LoggerError = 3,
InternalError = 4,
}
const HANDLE_MAGIC: u64 = u64::from_be_bytes(*b"ACTALOG2");
struct LoggerBox {
magic: u64,
logger: abi::Logger,
}
thread_local! {
static LAST_ERROR: std::cell::RefCell<Option<std::ffi::CString>> =
const { std::cell::RefCell::new(None) };
}
fn fail(error: &abi::Error) -> acta_ffi_status {
LAST_ERROR.with(|slot| {
*slot.borrow_mut() = std::ffi::CString::new(error.message.as_str()).ok();
});
match error.status {
abi::STATUS_INVALID_ARGUMENT => acta_ffi_status::InvalidArgument,
abi::STATUS_INVALID_HANDLE => acta_ffi_status::InvalidHandle,
abi::STATUS_LOGGER_ERROR => acta_ffi_status::LoggerError,
_ => acta_ffi_status::InternalError,
}
}
fn reject(message: &str) -> acta_ffi_status {
fail(&abi::Error::new(abi::STATUS_INVALID_ARGUMENT, message))
}
fn finish<T>(result: abi::Result<T>, commit: impl FnOnce(T)) -> acta_ffi_status {
match result {
Ok(value) => {
commit(value);
acta_ffi_status::Ok
}
Err(error) => fail(&error),
}
}
unsafe fn logger(handle: acta_ffi_handle) -> abi::Result<&'static abi::Logger> {
if handle == 0 {
return Err(abi::Error::new(
abi::STATUS_INVALID_HANDLE,
"logger handle is null",
));
}
let boxed = unsafe { &*(handle as *const LoggerBox) };
if boxed.magic != HANDLE_MAGIC {
return Err(abi::Error::new(
abi::STATUS_INVALID_HANDLE,
"logger handle is invalid or already destroyed",
));
}
Ok(&boxed.logger)
}
unsafe fn read_str<'a>(pointer: *const u8, length: usize) -> abi::Result<&'a str> {
if length == 0 {
return Ok("");
}
if pointer.is_null() || length > isize::MAX as usize {
return Err(abi::Error::new(
abi::STATUS_INVALID_ARGUMENT,
"string pointer is null",
));
}
let bytes = unsafe { slice::from_raw_parts(pointer, length) };
str::from_utf8(bytes).map_err(|error| {
abi::Error::new(
abi::STATUS_INVALID_ARGUMENT,
format!("input is not valid UTF-8: {error}"),
)
})
}
#[unsafe(no_mangle)]
pub extern "C" fn acta_abi_version() -> u32 {
abi::ABI_VERSION
}
#[unsafe(no_mangle)]
pub extern "C" fn acta_version() -> *const std::ffi::c_char {
concat!(env!("CARGO_PKG_VERSION"), "\0").as_ptr().cast()
}
#[unsafe(no_mangle)]
pub extern "C" fn acta_last_error() -> *const std::ffi::c_char {
LAST_ERROR.with(|slot| {
slot.borrow()
.as_ref()
.map_or(std::ptr::null(), |error| error.as_ptr())
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn acta_logger_create(
config_json: *const u8,
config_json_len: usize,
capacity: usize,
out_handle: *mut acta_ffi_handle,
) -> acta_ffi_status {
if out_handle.is_null() {
return reject("out_handle pointer is null");
}
unsafe { out_handle.write(0) };
finish(
unsafe { read_str(config_json, config_json_len) }
.and_then(|config_json| abi::Logger::from_config_json(config_json, capacity))
.map(|logger| {
Box::into_raw(Box::new(LoggerBox {
magic: HANDLE_MAGIC,
logger,
})) as acta_ffi_handle
}),
|handle| {
unsafe { out_handle.write(handle) };
},
)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn acta_logger_log(
handle: acta_ffi_handle,
level: acta_ffi_level,
message: *const u8,
message_len: usize,
) -> acta_ffi_status {
finish(
unsafe { logger(handle) }.and_then(|logger| {
let message = unsafe { read_str(message, message_len) }?;
logger.log(level as u32, message)
}),
|_| {},
)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn acta_logger_event(
handle: acta_ffi_handle,
event_json: *const u8,
event_json_len: usize,
) -> acta_ffi_status {
finish(
unsafe { logger(handle) }.and_then(|logger| {
let event_json = unsafe { read_str(event_json, event_json_len) }?;
logger.event_json(event_json)
}),
|_| {},
)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn acta_logger_create_span(
handle: acta_ffi_handle,
event_json: *const u8,
event_json_len: usize,
out_id: *mut u32,
) -> acta_ffi_status {
if out_id.is_null() {
return reject("out_id pointer is null");
}
unsafe { out_id.write(0) };
finish(
unsafe { logger(handle) }.and_then(|logger| {
let event_json = unsafe { read_str(event_json, event_json_len) }?;
logger.create_span_json(event_json)
}),
|id| {
unsafe { out_id.write(id) };
},
)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn acta_logger_record_span(
handle: acta_ffi_handle,
id: u32,
fields_json: *const u8,
fields_json_len: usize,
) -> acta_ffi_status {
finish(
unsafe { logger(handle) }.and_then(|logger| {
let fields_json = unsafe { read_str(fields_json, fields_json_len) }?;
logger.record_span_json(id, fields_json)
}),
|_| {},
)
}
#[unsafe(no_mangle)]
pub extern "C" fn acta_logger_close_span(handle: acta_ffi_handle, id: u32) -> acta_ffi_status {
finish(
unsafe { logger(handle) }.and_then(|logger| logger.close_span(id)),
|_| {},
)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn acta_logger_drain(
handle: acta_ffi_handle,
output: *mut u8,
output_capacity: usize,
out_written: *mut usize,
out_dropped: *mut u64,
) -> acta_ffi_status {
if out_written.is_null()
|| out_dropped.is_null()
|| output_capacity > isize::MAX as usize
|| (output_capacity > 0 && output.is_null())
{
return reject("output pointers are invalid");
}
unsafe {
out_written.write(0);
out_dropped.write(0);
}
finish(
unsafe { logger(handle) }.map(|logger| {
let bytes = logger.drain(output_capacity);
if !bytes.is_empty() {
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), output, bytes.len()) };
}
unsafe {
out_written.write(bytes.len());
out_dropped.write(logger.dropped());
}
}),
|_| {},
)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn acta_logger_set_filter(
handle: acta_ffi_handle,
filter: *const u8,
filter_len: usize,
) -> acta_ffi_status {
finish(
unsafe { logger(handle) }.and_then(|logger| {
let filter = unsafe { read_str(filter, filter_len) }?;
logger.set_filter(filter)
}),
|_| {},
)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn acta_logger_destroy(handle: acta_ffi_handle) -> acta_ffi_status {
match unsafe { logger(handle) } {
Err(error) => fail(&error),
Ok(_) => {
drop(unsafe { Box::from_raw(handle as *mut LoggerBox) });
acta_ffi_status::Ok
}
}
}