canic/macros/mod.rs
1//! Facade macros for downstream canister crates.
2mod build;
3mod endpoints;
4mod start;
5mod timer;
6
7// -----------------------------------------------------------------------------
8// Log macro
9// -----------------------------------------------------------------------------
10
11/// Log a runtime entry using Canic's structured logger.
12#[macro_export]
13macro_rules! log {
14 ($($tt:tt)*) => {{
15 $crate::__internal::core::log!($($tt)*);
16 }};
17}
18
19// -----------------------------------------------------------------------------
20// Perf macro
21// -----------------------------------------------------------------------------
22
23/// Record and log elapsed instruction counts since the last `perf!` invocation
24/// in this thread.
25///
26/// - Uses a thread-local `PERF_LAST` snapshot.
27/// - Computes `delta = now - last`.
28/// - Records a structured checkpoint row in the shared perf table.
29/// - Prints a human-readable line for debugging.
30///
31/// Intended usage:
32/// - Long-running maintenance tasks where you want *checkpoints* in a single call.
33///
34/// Note: `perf!` is independent of endpoint perf scopes and does not touch the
35/// endpoint stack used by dispatch. It records checkpoint rows keyed by
36/// `module_path!()` plus the formatted label.
37///
38/// Notes:
39/// - On non-wasm targets, `perf_counter()` returns 0, so this becomes a no-op-ish
40/// counter (still records 0 deltas); this keeps unit tests compiling cleanly.
41#[macro_export]
42macro_rules! perf {
43 ($($label:tt)*) => {{
44 $crate::__internal::core::perf::PERF_LAST.with(|last| {
45 // Use the wrapper so non-wasm builds compile.
46 let now = $crate::__internal::core::perf::perf_counter();
47 let then = *last.borrow();
48 let delta = now.saturating_sub(then);
49
50 // Update last checkpoint.
51 *last.borrow_mut() = now;
52
53 // Format label + pretty-print counters.
54 let label = format!($($label)*);
55 let delta_fmt = $crate::__internal::instructions::format_instructions(delta);
56 let now_fmt = $crate::__internal::instructions::format_instructions(now);
57
58 $crate::__internal::core::perf::record_checkpoint(module_path!(), &label, delta);
59
60 $crate::__internal::core::log!(
61 Info,
62 Topic::Perf,
63 "{}: '{}' used {}i since last (total: {}i)",
64 module_path!(),
65 label,
66 delta_fmt,
67 now_fmt
68 );
69 });
70 }};
71}