use tracing_subscriber::registry::{LookupSpan, SpanRef};
use crate::layer::{KeyValue, Tree, TreeAttrs, TreeEvent};
use crate::tag::TagData;
use crate::{cfg_json, cfg_uuid};
use std::fmt::{self, Write};
use std::io;
cfg_uuid! {
use crate::fail;
use crate::layer::TreeSpanOpened;
}
pub mod pretty;
pub use pretty::Pretty;
cfg_json! {
pub mod json;
pub use json::Json;
}
pub trait Formatter {
fn fmt(&self, tree: Tree, writer: &mut Vec<u8>) -> io::Result<()>;
}
pub(crate) fn format_immediate<S>(
attrs: &TreeAttrs,
event: &TreeEvent,
span: Option<SpanRef<S>>,
) -> fmt::Result
where
S: for<'a> LookupSpan<'a>,
{
let tag = TagData::from(attrs.level);
let mut writer = format!("{icon} IMMEDIATE {icon} ", icon = tag.icon);
#[cfg(feature = "uuid")]
if let Some(span) = &span {
let uuid = span
.extensions()
.get::<TreeSpanOpened>()
.unwrap_or_else(fail::tree_span_opened_not_in_extensions)
.uuid();
write!(writer, "{} ", uuid)?;
}
#[cfg(feature = "chrono")]
write!(writer, "{} ", attrs.timestamp.to_rfc3339())?;
write!(writer, "{:<8} ", attrs.level)?;
if let Some(span) = &span {
for ancestor in span.scope().from_root() {
write!(writer, "{} > ", ancestor.name())?;
}
}
write!(writer, "{}", event.message)?;
for KeyValue { key, value } in event.fields.iter() {
write!(writer, " | {}: {}", key, value)?;
}
eprintln!("{}", writer);
Ok(())
}