use super::{
callsite::{self, Callsite},
field,
};
use std::fmt;
pub struct Metadata<'a> {
#[doc(hidden)]
pub name: &'static str,
#[doc(hidden)]
pub target: &'a str,
#[doc(hidden)]
pub level: Level,
#[doc(hidden)]
pub module_path: Option<&'a str>,
#[doc(hidden)]
pub file: Option<&'a str>,
#[doc(hidden)]
pub line: Option<u32>,
#[doc(hidden)]
pub fields: field::FieldSet,
}
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Level(LevelInner);
impl<'a> Metadata<'a> {
pub fn new(
name: &'static str,
target: &'a str,
level: Level,
module_path: Option<&'a str>,
file: Option<&'a str>,
line: Option<u32>,
field_names: &'static [&'static str],
callsite: &'static Callsite,
) -> Self {
Metadata {
name,
target,
level,
module_path,
file,
line,
fields: field::FieldSet {
names: field_names,
callsite: callsite::Identifier(callsite),
},
}
}
pub fn fields(&self) -> &field::FieldSet {
&self.fields
}
pub fn level(&self) -> &Level {
&self.level
}
pub fn name(&self) -> &'static str {
self.name
}
pub fn target(&self) -> &'a str {
self.target
}
pub fn module_path(&self) -> Option<&'a str> {
self.module_path
}
pub fn file(&self) -> Option<&'a str> {
self.file
}
pub fn line(&self) -> Option<u32> {
self.line
}
#[inline]
pub fn callsite(&self) -> callsite::Identifier {
self.fields.callsite()
}
}
impl<'a> fmt::Debug for Metadata<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Metadata")
.field("name", &self.name)
.field("target", &self.target)
.field("level", &self.level)
.field("module_path", &self.module_path)
.field("file", &self.file)
.field("line", &self.line)
.field("field_names", &self.fields)
.finish()
}
}
impl Level {
pub const ERROR: Level = Level(LevelInner::Error);
pub const WARN: Level = Level(LevelInner::Warn);
pub const INFO: Level = Level(LevelInner::Info);
pub const DEBUG: Level = Level(LevelInner::Debug);
pub const TRACE: Level = Level(LevelInner::Trace);
}
#[repr(usize)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
enum LevelInner {
Error = 1,
Warn,
Info,
Debug,
Trace,
}