[][src]Crate call_trace

Example (simple)

use call_trace::trace;

#[inline]
#[trace]
/// test
fn foo() {
    bar();
}

#[trace]
fn bar() {
    let x = baz();
    let y = baz();
    println!("x + y = {}", x + y);
}

#[trace]
fn baz() -> i32 {
    15
}

fn main() {
    foo();
}

Output:

[call-trace/tests/call-trace.rs:4] => foo()
[call-trace/tests/call-trace.rs:10] => bar()
[call-trace/tests/call-trace.rs:17] => baz()
[call-trace/tests/call-trace.rs:17] <= baz()
[call-trace/tests/call-trace.rs:17] => baz()
[call-trace/tests/call-trace.rs:17] <= baz()
x + y = 30
[call-trace/tests/call-trace.rs:10] <= bar()
[call-trace/tests/call-trace.rs:4] <= foo()

Example (custom callback)

use call_trace::{trace, Event};

#[inline]
#[trace]
/// test
fn foo() {
    bar();
}

#[trace]
fn bar() {
    let x = baz();
    let y = baz();
    println!("x + y = {}", x + y);
}

#[trace]
fn baz() -> i32 {
    15
}

fn main() {
    call_trace::thread_register_callback(|_||_, event| {
        // discard the previous callback handler
        if event == Event::Call {
            println!("Hello world");
        }
    });
    call_trace::thread_register_callback(|prev|move|ctx, event| {
        // call the previous callback handler
        if let Some(prev) = &prev {
            prev(ctx, event);
        }
        match event {
            Event::Call => println!("> {:?}", ctx.top()),
            Event::Return => println!("< {:?}", ctx.top()),
        }
    });

    foo();
}

Output

Hello world
> CallContext { file: "call-trace/tests/call-trace-register.rs", line: 4, fn_name: "foo" }
Hello world
> CallContext { file: "call-trace/tests/call-trace-register.rs", line: 10, fn_name: "bar" }
Hello world
> CallContext { file: "call-trace/tests/call-trace-register.rs", line: 17, fn_name: "baz" }
< CallContext { file: "call-trace/tests/call-trace-register.rs", line: 17, fn_name: "baz" }
Hello world
> CallContext { file: "call-trace/tests/call-trace-register.rs", line: 17, fn_name: "baz" }
< CallContext { file: "call-trace/tests/call-trace-register.rs", line: 17, fn_name: "baz" }
x + y = 30
< CallContext { file: "call-trace/tests/call-trace-register.rs", line: 10, fn_name: "bar" }
< CallContext { file: "call-trace/tests/call-trace-register.rs", line: 4, fn_name: "foo" }

Example (custom context)

use call_trace::{trace_with, CallContext};

struct My;

impl My {
    #[inline]
    #[trace_with(self.trace())]
    /// test
    fn foo(&mut self) {
        self.bar();
    }

    #[trace_with(self.trace())]
    fn bar(&mut self) {
        let x = self.baz();
        let y = self.baz();
        println!("x + y = {}", x + y);
    }

    #[trace_with(self.trace())]
    fn baz(&mut self) -> i32 {
        15
    }

    fn trace<T, F: FnOnce() -> T>(&mut self) -> impl FnOnce(F, CallContext) -> T {
        |f, ctx| {
            println!("> {:?}", ctx);
            let r = f();
            println!("< {:?}", ctx);
            r
        }
    }
}

fn main() {
    My.foo();
}

Output

> CallContext { file: "call-trace/tests/call-trace-with.rs", line: 7, fn_name: "foo" }
> CallContext { file: "call-trace/tests/call-trace-with.rs", line: 13, fn_name: "bar" }
> CallContext { file: "call-trace/tests/call-trace-with.rs", line: 20, fn_name: "baz" }
< CallContext { file: "call-trace/tests/call-trace-with.rs", line: 20, fn_name: "baz" }
> CallContext { file: "call-trace/tests/call-trace-with.rs", line: 20, fn_name: "baz" }
< CallContext { file: "call-trace/tests/call-trace-with.rs", line: 20, fn_name: "baz" }
x + y = 30
< CallContext { file: "call-trace/tests/call-trace-with.rs", line: 13, fn_name: "bar" }
< CallContext { file: "call-trace/tests/call-trace-with.rs", line: 7, fn_name: "foo" }

Re-exports

pub use call_trace_macro::trace;
pub use call_trace_macro::trace_with;
pub use call_trace_macro::inject_with;

Structs

CallContext

Contains information about the current call site.

Context

The thread-local callback context for #[trace].

Enums

Event

Indicates why the callback got called by #[trace]

Functions

on_trace

Called by #[trace].

thread_access_with

Access the thread-local context of #[trace]

thread_register_callback

Registers a callback in the thread-local context. This is a shortcut for accessing the Context via thread_access_with().

thread_unregister_callback

Unregisters a callback in the thread-local context. This is a shortcut for accessing the Context via thread_access_with().

Type Definitions

Callback

A callback. It gets called by #[trace].