diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..8961f65 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,33 @@ +unsafe extern "C" { + pub fn debug(fmt: *const u8, ...); + pub fn info(fmt: *const u8, ...); + pub fn warn(fmt: *const u8, ...); + pub fn error(fmt: *const u8, ...); + pub fn fatal(fmt: *const u8, ...); +} + +#[macro_export] +macro_rules! debug { + ($fmt:expr) => {{ $crate::error::debug($fmt.as_ptr()); }}; + ($fmt:expr, $($arg:expr),*) => {{ $crate::error::debug($fmt.as_ptr(), ($($arg),*)); }}; +} +#[macro_export] +macro_rules! info { + ($fmt:expr) => {{ $crate::error::info($fmt.as_ptr()); }}; + ($fmt:expr, $($arg:expr),*) => {{ $crate::error::info($fmt.as_ptr(), ($($arg),*)); }}; +} +#[macro_export] +macro_rules! warn { + ($fmt:expr) => {{ $crate::error::warn($fmt.as_ptr()); }}; + ($fmt:expr, $($arg:expr),*) => {{ $crate::error::warn($fmt.as_ptr(), ($($arg),*)); }}; +} +#[macro_export] +macro_rules! error { + ($fmt:expr) => {{ $crate::error::error($fmt.as_ptr()); }}; + ($fmt:expr, $($arg:expr),*) => {{ $crate::error::error($fmt.as_ptr(), ($($arg),*)); }}; +} +#[macro_export] +macro_rules! fatal { + ($fmt:expr) => {{ $crate::error::fatal($fmt.as_ptr()); }}; + ($fmt:expr, $($arg:expr),*) => {{ $crate::error::fatal($fmt.as_ptr(), ($($arg),*)); }}; +} diff --git a/src/lib.rs b/src/lib.rs index d24ba41..eeb91ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,8 +6,16 @@ fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} } -#[unsafe(no_mangle)] -pub unsafe extern "C" fn get_str() -> *const u8 { - let str = "Hello, World\0"; - return str.as_ptr(); +// contains all publicly facing functions + +mod error; + +pub extern "C" fn test() { + unsafe { + debug!("%s", "hi"); + info!("%s", "hi"); + warn!("%s", "hi"); + error!("%s", "hi"); + fatal!("%s", "hi"); + } } diff --git a/src/main.c b/src/main.c index 1b9ea04..b56cbd7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,9 @@ -#include +#include "error.h" -extern char const* get_str(void); +extern void test(void); int main(int argc, char** argv) { (void)argc, (void)argv; - printf("%s\n", get_str()); + test(); + debug("owo"); }