This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
generated from opensound-org/template-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉: Initial commit of example
remote-tracing
- Loading branch information
Showing
4 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
const CCC: &str = "ccc"; | ||
|
||
fn emit_span() -> tracing::Span { | ||
let ccc = 333; | ||
|
||
use tracing::__macro_support::Callsite as _; | ||
static __CALLSITE: tracing::callsite::DefaultCallsite = { | ||
static META: tracing::Metadata<'static> = { | ||
tracing_core::metadata::Metadata::new( | ||
"my-span", | ||
"span-target", | ||
tracing::Level::INFO, | ||
Some("examples\\remote-tracing\\emit_macro.rs"), | ||
Some(6u32), | ||
Some("remote_tracing::emit_macro"), | ||
tracing_core::field::FieldSet::new( | ||
&["aaa", "bbb", CCC, "ddd"], | ||
tracing_core::callsite::Identifier(&__CALLSITE), | ||
), | ||
tracing::metadata::Kind::SPAN, | ||
) | ||
}; | ||
|
||
tracing::callsite::DefaultCallsite::new(&META) | ||
}; | ||
let mut interest = tracing::subscriber::Interest::never(); | ||
|
||
if tracing::Level::INFO <= tracing::level_filters::STATIC_MAX_LEVEL | ||
&& tracing::Level::INFO <= tracing::level_filters::LevelFilter::current() | ||
&& { | ||
interest = __CALLSITE.interest(); | ||
!interest.is_never() | ||
} | ||
&& tracing::__macro_support::__is_enabled(__CALLSITE.metadata(), interest) | ||
{ | ||
let meta = __CALLSITE.metadata(); | ||
|
||
tracing::Span::new(meta, &{ | ||
let mut iter = meta.fields().iter(); | ||
|
||
meta.fields().value_set(&[ | ||
( | ||
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"), | ||
Some(&111 as &dyn tracing::field::Value), | ||
), | ||
( | ||
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"), | ||
Some(&222 as &dyn tracing::field::Value), | ||
), | ||
( | ||
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"), | ||
Some(&tracing::field::debug(&ccc) as &dyn tracing::field::Value), | ||
), | ||
( | ||
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"), | ||
Some(&tracing::field::Empty as &dyn tracing::field::Value), | ||
), | ||
]) | ||
}) | ||
} else { | ||
tracing::__macro_support::__disabled_span(__CALLSITE.metadata()) | ||
} | ||
} | ||
|
||
fn emit_event(myspan: tracing::Span) { | ||
let ccc = 333; | ||
let ddd = 444; | ||
|
||
use tracing::__macro_support::Callsite as _; | ||
static __CALLSITE: tracing::callsite::DefaultCallsite = { | ||
static META: tracing::Metadata<'static> = { | ||
tracing_core::metadata::Metadata::new( | ||
"event examples\\remote-tracing\\emit_macro.rs:18", | ||
"remote_tracing::emit_macro", | ||
tracing::Level::INFO, | ||
Some("examples\\remote-tracing\\emit_macro.rs"), | ||
Some(18u32), | ||
Some("remote_tracing::emit_macro"), | ||
tracing_core::field::FieldSet::new( | ||
&["message", "aaa", "bbb", CCC], | ||
tracing_core::callsite::Identifier(&__CALLSITE), | ||
), | ||
tracing::metadata::Kind::EVENT, | ||
) | ||
}; | ||
|
||
tracing::callsite::DefaultCallsite::new(&META) | ||
}; | ||
let enabled = tracing::Level::INFO <= tracing::level_filters::STATIC_MAX_LEVEL | ||
&& tracing::Level::INFO <= tracing::level_filters::LevelFilter::current() | ||
&& { | ||
let interest = __CALLSITE.interest(); | ||
!interest.is_never() | ||
&& tracing::__macro_support::__is_enabled(__CALLSITE.metadata(), interest) | ||
}; | ||
|
||
if enabled { | ||
(|value_set: tracing::field::ValueSet| { | ||
let meta = __CALLSITE.metadata(); | ||
tracing::Event::child_of(&myspan, meta, &value_set); | ||
})({ | ||
let mut iter = __CALLSITE.metadata().fields().iter(); | ||
|
||
__CALLSITE.metadata().fields().value_set(&[ | ||
( | ||
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"), | ||
Some(&format_args!("ddd = {0}", ddd) as &dyn tracing::field::Value), | ||
), | ||
( | ||
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"), | ||
Some(&111 as &dyn tracing::field::Value), | ||
), | ||
( | ||
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"), | ||
Some(&222 as &dyn tracing::field::Value), | ||
), | ||
( | ||
&Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"), | ||
Some(&tracing::field::debug(&ccc) as &dyn tracing::field::Value), | ||
), | ||
]) | ||
}); | ||
} | ||
} | ||
|
||
pub fn emit_tracing() { | ||
let myspan = emit_span(); | ||
myspan.record("ddd", 444); | ||
emit_event(myspan); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
pub fn emit_tracing() { | ||
let ccc = 333; | ||
const CCC: &str = "ccc"; | ||
let ddd = 444; | ||
|
||
let myspan = tracing::span!( | ||
target: "span-target", | ||
tracing::Level::INFO, | ||
"my-span", | ||
aaa = 111, | ||
"bbb" = 222, | ||
{ CCC } = ?ccc, | ||
ddd = tracing::field::Empty | ||
); | ||
|
||
myspan.record("ddd", 444); | ||
|
||
tracing::event!( | ||
parent: &myspan, | ||
tracing::Level::INFO, | ||
aaa = 111, | ||
"bbb" = 222, | ||
{ CCC } = ?ccc, | ||
"ddd = {}", ddd | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use std::fmt::Debug; | ||
use tracing::{ | ||
span::{Attributes, Id, Record}, | ||
Event, Subscriber, | ||
}; | ||
use tracing_subscriber::{ | ||
filter::LevelFilter, | ||
fmt::{self, format::FmtSpan, time::ChronoLocal}, | ||
layer::{Context, SubscriberExt}, | ||
registry::LookupSpan, | ||
util::SubscriberInitExt, | ||
Layer, | ||
}; | ||
|
||
mod emit_macro; | ||
use emit_macro::emit_tracing; | ||
|
||
struct DebugLayer; | ||
|
||
impl<S> Layer<S> for DebugLayer | ||
where | ||
S: Subscriber + for<'a> LookupSpan<'a> + Debug, | ||
{ | ||
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, _ctx: Context<'_, S>) { | ||
println!("on_new_span: {{\r\nattrs: {:#?},\r\nid: {:#?}}}", attrs, id); | ||
} | ||
|
||
fn on_record(&self, span: &Id, values: &Record<'_>, _ctx: Context<'_, S>) { | ||
println!( | ||
"on_record: {{\r\nspan: {:#?},\r\nvalues: {:#?}}}", | ||
span, values | ||
); | ||
} | ||
|
||
fn on_follows_from(&self, span: &Id, follows: &Id, _ctx: Context<'_, S>) { | ||
println!( | ||
"on_follows_from: {{\r\nspan: {:#?},\r\nfollows: {:#?}}}", | ||
span, follows | ||
); | ||
} | ||
|
||
fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) { | ||
println!("on_event: {{\r\nevent: {:#?}}}", event); | ||
} | ||
|
||
fn on_enter(&self, id: &Id, _ctx: Context<'_, S>) { | ||
println!("on_enter: {{\r\nid: {:#?}}}", id); | ||
} | ||
|
||
fn on_exit(&self, id: &Id, _ctx: Context<'_, S>) { | ||
println!("on_exit: {{\r\nid: {:#?}}}", id); | ||
} | ||
|
||
fn on_close(&self, id: Id, _ctx: Context<'_, S>) { | ||
println!("on_close: {{\r\nid: {:#?}}}", id); | ||
} | ||
|
||
fn on_id_change(&self, old: &Id, new: &Id, _ctx: Context<'_, S>) { | ||
println!("on_id_change: {{\r\nold: {:#?},\r\nnew: {:#?}}}", old, new); | ||
} | ||
} | ||
|
||
fn init_subscriber() { | ||
// See: https://github.com/tokio-rs/tracing/issues/3068 | ||
#[cfg(windows)] | ||
nu_ansi_term::enable_ansi_support().ok(); | ||
|
||
tracing_subscriber::registry() | ||
.with( | ||
fmt::layer() | ||
.with_timer(ChronoLocal::new("%m-%d %H:%M:%S".into())) | ||
.with_span_events(FmtSpan::FULL) | ||
.with_thread_names(true) | ||
.with_filter(LevelFilter::INFO), | ||
) | ||
.with(DebugLayer) | ||
.init(); | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
init_subscriber(); | ||
emit_tracing(); | ||
} |