//! Formatting for log records. //! //! This module contains a [`Formatter`] that can be used to format log records //! into without needing temporary allocations. Usually you won't need to worry //! about the contents of this module and can use the `Formatter` like an ordinary //! [`Write`]. //! //! # Formatting log records //! //! The format used to print log records can be customised using the [`Builder::format`] //! method. //! Custom formats can apply different color and weight to printed values using //! [`Style`] builders. //! //! ``` //! use std::io::Write; //! //! let mut builder = env_logger::Builder::new(); //! //! builder.format(|buf, record| { //! writeln!(buf, "{}: {}", //! record.level(), //! record.args()) //! }); //! ``` //! //! [`Formatter`]: struct.Formatter.html //! [`Style`]: struct.Style.html //! [`Builder::format`]: ../struct.Builder.html#method.format //! [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html
use std::cell::RefCell; use std::fmt::Display; use std::io::prelude::*; use std::rc::Rc; use std::{fmt, io, mem};
pub(crate) mod glob { pubusesuper::{Target, TimestampPrecision, WriteStyle};
}
/// Formatting precision of timestamps. /// /// Seconds give precision of full seconds, milliseconds give thousands of a /// second (3 decimal digits), microseconds are millionth of a second (6 decimal /// digits) and nanoseconds are billionth of a second (9 decimal digits). #[derive(Copy, Clone, Debug)] pubenum TimestampPrecision { /// Full second precision (0 decimal digits)
Seconds, /// Millisecond precision (3 decimal digits)
Millis, /// Microsecond precision (6 decimal digits)
Micros, /// Nanosecond precision (9 decimal digits)
Nanos,
}
/// The default timestamp precision is seconds. impl Default for TimestampPrecision { fn default() -> Self {
TimestampPrecision::Seconds
}
}
/// A formatter to write logs into. /// /// `Formatter` implements the standard [`Write`] trait for writing log records. /// It also supports terminal colors, through the [`style`] method. /// /// # Examples /// /// Use the [`writeln`] macro to format a log record. /// An instance of a `Formatter` is passed to an `env_logger` format as `buf`: /// /// ``` /// use std::io::Write; /// /// let mut builder = env_logger::Builder::new(); /// /// builder.format(|buf, record| writeln!(buf, "{}: {}", record.level(), record.args())); /// ``` /// /// [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html /// [`writeln`]: https://doc.rust-lang.org/stable/std/macro.writeln.html /// [`style`]: #method.style pubstruct Formatter {
buf: Rc<RefCell<Buffer>>,
write_style: WriteStyle,
}
impl Builder { /// Convert the format into a callable function. /// /// If the `custom_format` is `Some`, then any `default_format` switches are ignored. /// If the `custom_format` is `None`, then a default format is returned. /// Any `default_format` switches set to `false` won't be written by the format. pubfn build(&mutself) -> FormatFn {
assert!(!self.built, "attempt to re-use consumed builder");
let built = mem::replace( self,
Builder {
built: true,
..Default::default()
},
);
#[cfg(feature = "color")] type SubtleStyle = StyledValue<'static, &'static str>; #[cfg(not(feature = "color"))] type SubtleStyle = &'static str;
/// The default format. /// /// This format needs to work with any combination of crate features. struct DefaultFormat<'a> {
timestamp: Option<TimestampPrecision>,
module_path: bool,
target: bool,
level: bool,
written_header_value: bool,
indent: Option<usize>,
buf: &'a mut Formatter,
suffix: &'a str,
}
self.write_header_value(ts)
} #[cfg(not(feature = "humantime"))]
{ // Trick the compiler to think we have used self.timestamp // Workaround for "field is never used: `timestamp`" compiler nag. let _ = self.timestamp;
Ok(())
}
}
// The explicit scope here is just to make older versions of Rust happy
{ letmut wrapper = IndentWrapper {
fmt: self,
indent_count,
};
write!(wrapper, "{}", record.args())?;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.