1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
#![warn(missing_docs)] /*! # 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: ```text [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 ```text 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 ```text > 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" } ``` */ pub use call_trace_macro::trace; pub use call_trace_macro::trace_with; pub use call_trace_macro::inject_with; use std::cell::RefCell; use std::rc::Rc; /// A callback. It gets called by `#[trace]`. pub type Callback = Rc<dyn Fn(&mut Context, Event)>; /// The thread-local callback context for `#[trace]`. pub struct Context { callback: Option<Callback>, stack: Vec<CallContext>, } impl Context { fn new() -> Self { let mut r = Context { callback: None, stack: Vec::new(), }; r.register_callback(|_| Self::default_callback); r } /// The default callback registered by `Self::new()`. pub fn default_callback(ctx: &mut Context, event: Event) { match event { Event::Call => { eprintln!("[{}:{}] => {}()", ctx.top().file, ctx.top().line, ctx.top().fn_name); } Event::Return => { eprintln!("[{}:{}] <= {}()", ctx.top().file, ctx.top().line, ctx.top().fn_name); } } } /// Returns the current call stack. pub fn stack(&self) -> &[CallContext] { &self.stack } /// Returns the top of the current call stack. pub fn top(&self) -> &CallContext { self.stack.last().expect("something went wrong with the callstack") } /// Registers a callback. A previously registered callback is /// passed to the outer closure. pub fn register_callback<F, G>(&mut self, f: F) where F: FnOnce(Option<Callback>) -> G, G: Fn(&mut Context, Event) + 'static { self.callback = Some(Rc::new(f(self.callback.take()))); } /// Unregisters a callback. pub fn unregister_callback(&mut self) -> Option<Callback> { self.callback.take() } } #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] /// Contains information about the current call site. pub struct CallContext { /// Current file, as returned by `file!()` pub file: &'static str, /// Current line, as returned by `line!()`. Will point at the `#[trace]` attribute. pub line: u32, /// Name of the called function. Does not contain the module path. pub fn_name: &'static str, } #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] /// Indicates why the callback got called by `#[trace]` pub enum Event { /// The function just got called. Call, /// The function is about to return. Return, } thread_local! { static CONTEXT: RefCell<Context> = RefCell::new(Context::new()); } /// Access the thread-local context of `#[trace]` pub fn thread_access_with<T, F: FnOnce(&mut Context) -> T>(f: F) -> T { CONTEXT.with(move |ctx| f(&mut ctx.borrow_mut())) } /// Registers a callback in the thread-local context. This is a shortcut /// for accessing the `Context` via `thread_access_with()`. pub fn thread_register_callback<F, G>(f: F) where F: FnOnce(Option<Callback>) -> G, G: Fn(&mut Context, Event) + 'static { thread_access_with(move |ctx| { ctx.register_callback(f) }) } /// Unregisters a callback in the thread-local context. This is a shortcut /// for accessing the `Context` via `thread_access_with()`. pub fn thread_unregister_callback() -> Option<Callback> { thread_access_with(move |ctx| { ctx.unregister_callback() }) } fn on_event(cctx: &CallContext, event: Event) { CONTEXT.with(|ctx| { let mut ctx = ctx.borrow_mut(); if let Event::Call = event { ctx.stack.push(cctx.clone()); } let ctx = &mut *ctx; if let Some(cb) = &ctx.callback { let cb: Callback = cb.clone(); cb(ctx, event); } if let Event::Return = event { ctx.stack.pop().expect("something went wrong with the call stack"); } }) } /// Called by `#[trace]`. pub fn on_trace<T, F: FnOnce() -> T>(f: F, ctx: CallContext) -> T { on_event(&ctx, Event::Call); let r = f(); on_event(&ctx, Event::Return); r }