Created
September 9, 2020 17:49
-
-
Save gnunicorn/75d7c6b59cc75e64e69058447e042a94 to your computer and use it in GitHub Desktop.
a short `cargo play` example of building `'static tracing_core::Metadata<'static>` from non-static strings
This file contains hidden or 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
//# lazy_static = "1.4.0" | |
//# tracing = "0.1.19" | |
//# tracing-core = "0.1.16" | |
//# parking_lot = "0.10" | |
use tracing; | |
use tracing_core; | |
use parking_lot::Mutex; | |
#[macro_use] | |
extern crate lazy_static; | |
use std::collections::hash_map::{HashMap, Entry}; | |
type MetadataId = (String, String, String, String); | |
/// Static entry use for wasm-originated metadata. | |
pub struct WasmCallsite; | |
impl tracing_core::callsite::Callsite for WasmCallsite { | |
fn set_interest(&self, _: tracing_core::Interest) { unimplemented!() } | |
fn metadata(&self) -> &tracing_core::Metadata { unimplemented!() } | |
} | |
static CALLSITE: WasmCallsite = WasmCallsite; | |
lazy_static! { | |
static ref METADATA: Mutex<HashMap<MetadataId, tracing_core::Metadata<'static>>> = Default::default(); | |
} | |
fn make_metadata<'a>( | |
target: String, | |
name: String, | |
level: tracing::Level, | |
kind: tracing_core::metadata::Kind | |
) -> &'a tracing_core::Metadata<'static> { | |
let n : &'static str = unsafe { | |
std::mem::transmute(name.as_str()) | |
}; | |
let t : &'static str = unsafe { | |
std::mem::transmute(target.as_str()) | |
}; | |
let mut l = METADATA.lock(); | |
let r = l | |
.entry((target, name, format!("{:?}", level), format!("{:?}", kind))) | |
.or_insert_with(|| { | |
println!("creating"); | |
tracing_core::Metadata::new( | |
n, t, level, None, None, None, | |
tracing_core::field::FieldSet::new(&[], tracing_core::identify_callsite!(&CALLSITE)), | |
kind | |
) | |
}); | |
let m : &'static tracing_core::Metadata<'static> = unsafe { | |
std::mem::transmute(r) | |
}; | |
m | |
} | |
fn main() { | |
{ | |
let _ = make_metadata( | |
"test".to_owned(), | |
"target".to_owned(), | |
tracing::Level::ERROR, | |
tracing_core::metadata::Kind::SPAN | |
); | |
let _ = make_metadata( | |
"name".to_owned(), | |
"target".to_owned(), | |
tracing::Level::ERROR, | |
tracing_core::metadata::Kind::EVENT | |
); | |
let _ = make_metadata( | |
"name".to_owned(), | |
"target".to_owned(), | |
tracing::Level::WARN, | |
tracing_core::metadata::Kind::SPAN | |
); | |
} | |
println!("{:?}", make_metadata( | |
"test".to_owned(), | |
"target".to_owned(), | |
tracing::Level::ERROR, | |
tracing_core::metadata::Kind::SPAN | |
)); | |
println!("{:?}", METADATA.lock().len()); | |
println!("{:?}", make_metadata( | |
"name".to_owned(), | |
"target".to_owned(), | |
tracing::Level::ERROR, | |
tracing_core::metadata::Kind::EVENT | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment