pub mod error;
mod remotery_ffi;
mod cfixed_string;
use std::ptr;
use std::os::raw::c_void;
use error::RemoteryError;
use cfixed_string::CFixedString;
pub struct Remotery {
instance: *mut c_void,
}
#[derive(Clone, Copy)]
pub enum SampleFlags {
Default,
Aggregate,
}
impl Remotery {
pub fn create_global_instance() -> Result<Remotery, RemoteryError> {
let mut instance = 0 as *mut c_void;
let res = unsafe {
remotery_ffi::_rmt_CreateGlobalInstance(&mut instance)
};
if res != 0 {
return Err(error::get_error(res));
}
Ok(Remotery { instance: instance })
}
pub fn begin_cpu_sample(name: &str, flags: SampleFlags) {
unsafe {
let temp_str = CFixedString::from_str(name);
remotery_ffi::_rmt_BeginCPUSample(temp_str.as_ptr(), flags as u32, ptr::null_mut());
}
}
pub fn end_cpu_sample() {
unsafe {
remotery_ffi::_rmt_EndCPUSample();
}
}
pub fn set_current_thread_name(name: &str) {
unsafe {
let temp_str = CFixedString::from_str(name);
remotery_ffi::_rmt_SetCurrentThreadName(temp_str.as_ptr());
}
}
pub fn log_text(text: &str) {
unsafe {
let temp_str = CFixedString::from_str(text);
remotery_ffi::_rmt_LogText(temp_str.as_ptr());
}
}
}
pub struct RemoteryScope;
impl RemoteryScope {
pub fn new(name: &str, flags: SampleFlags) -> RemoteryScope {
Remotery::begin_cpu_sample(name, flags);
RemoteryScope {}
}
}
impl Drop for RemoteryScope {
fn drop(&mut self) {
Remotery::end_cpu_sample()
}
}
impl Drop for Remotery {
fn drop(&mut self) {
if self.instance == ptr::null_mut() {
return
}
unsafe {
remotery_ffi::_rmt_DestroyGlobalInstance(self.instance);
}
}
}