Skip to main content

troupekit_trace/
lib.rs

1//! Trace spans for every Troupekit crate, with no dependencies.
2//! A disabled trace point costs one atomic load and a branch.
3//! Records go to sinks as JSON lines; the `view` module reads them back.
4
5#![doc(
6    html_logo_url = "https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/brand/logo-mark.svg",
7    html_favicon_url = "https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/brand/favicon.svg"
8)]
9#![forbid(unsafe_code)]
10
11mod category;
12mod error;
13mod json;
14mod record;
15mod redact;
16mod sink;
17mod span;
18mod spec;
19mod switch;
20mod tracer;
21mod value;
22pub mod view;
23
24pub use category::{Trace, TraceSet};
25pub use error::TraceError;
26pub use redact::url_for_trace;
27pub use sink::{default_trace_path, CallbackSink, JsonlOptions, JsonlSink, MemorySink, TraceSink};
28pub use span::Span;
29pub use spec::TraceSpec;
30pub use switch::{enabled, live_categories};
31pub use tracer::{current, point, Ctx, Instrumented, Tracer, TracerBuilder};
32pub use value::{content, Value};
33
34/// Writes one point event: `trace!(Trace::Laws, "law.check", held = true)`.
35/// Field expressions run only when the category is on in the current scope.
36/// Keys are identifiers; values are anything that converts into `Value`.
37#[macro_export]
38macro_rules! trace {
39    ($cat:expr, $name:expr $(, $key:ident = $value:expr)* $(,)?) => {{
40        let cat: $crate::Trace = $cat;
41        if $crate::enabled(cat) {
42            $crate::point(
43                cat,
44                $name,
45                ::std::vec![$((::core::stringify!($key), $crate::Value::from($value))),*],
46            );
47        }
48    }};
49}
50
51/// Opens a span: `let span = trace_span!(Trace::Models, "model.call", model = id);`.
52/// Field expressions run only when the category is on in the current scope;
53/// otherwise the macro returns a disabled `Span`.
54#[macro_export]
55macro_rules! trace_span {
56    ($cat:expr, $name:expr $(, $key:ident = $value:expr)* $(,)?) => {{
57        let cat: $crate::Trace = $cat;
58        if $crate::enabled(cat) {
59            $crate::Span::open(
60                cat,
61                $name,
62                ::std::vec![$((::core::stringify!($key), $crate::Value::from($value))),*],
63            )
64        } else {
65            $crate::Span::none()
66        }
67    }};
68}