Skip to content

Commit

Permalink
Fix rustc & clippy lints
Browse files Browse the repository at this point in the history
Co-authored-by: Sven <SirWindfield@users.noreply.github.com>
  • Loading branch information
jplatte and Sven committed Oct 16, 2020
1 parent b8c3754 commit 2b33c97
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 81 deletions.
42 changes: 15 additions & 27 deletions src/filter/mod.rs
Expand Up @@ -52,7 +52,6 @@
//!
//! fn flush(&self) {}
//! }
//! # fn main() {}
//! ```
//!
//! [Enabling Logging]: ../index.html#enabling-logging
Expand Down Expand Up @@ -92,24 +91,18 @@ pub struct Filter {
/// ## Example
///
/// ```
/// #[macro_use]
/// extern crate log;
/// extern crate env_logger;
///
/// use std::env;
/// use std::io;
/// # #[macro_use] extern crate log;
/// # use std::env;
/// use env_logger::filter::Builder;
///
/// fn main() {
/// let mut builder = Builder::new();
///
/// // Parse a logging filter from an environment variable.
/// if let Ok(rust_log) = env::var("RUST_LOG") {
/// builder.parse(&rust_log);
/// }
/// let mut builder = Builder::new();
///
/// let filter = builder.build();
/// // Parse a logging filter from an environment variable.
/// if let Ok(rust_log) = env::var("RUST_LOG") {
/// builder.parse(&rust_log);
/// }
///
/// let filter = builder.build();
/// ```
///
/// [`Filter`]: struct.Filter.html
Expand All @@ -132,20 +125,15 @@ impl Filter {
/// # Example
///
/// ```rust
/// extern crate log;
/// extern crate env_logger;
///
/// use log::LevelFilter;
/// use env_logger::filter::Builder;
///
/// fn main() {
/// let mut builder = Builder::new();
/// builder.filter(Some("module1"), LevelFilter::Info);
/// builder.filter(Some("module2"), LevelFilter::Error);
/// let mut builder = Builder::new();
/// builder.filter(Some("module1"), LevelFilter::Info);
/// builder.filter(Some("module2"), LevelFilter::Error);
///
/// let filter = builder.build();
/// assert_eq!(filter.filter(), LevelFilter::Info);
/// }
/// let filter = builder.build();
/// assert_eq!(filter.filter(), LevelFilter::Info);
/// ```
pub fn filter(&self) -> LevelFilter {
self.directives
Expand Down Expand Up @@ -310,7 +298,7 @@ fn parse_spec(spec: &str) -> (Vec<Directive>, Option<inner::Filter>) {
);
return (dirs, None);
}
mods.map(|m| {
if let Some(m) = mods {
for s in m.split(',') {
if s.is_empty() {
continue;
Expand Down Expand Up @@ -352,7 +340,7 @@ fn parse_spec(spec: &str) -> (Vec<Directive>, Option<inner::Filter>) {
level: log_level,
});
}
});
}

let filter = filter.and_then(|filter| match inner::Filter::new(filter) {
Ok(re) => Some(re),
Expand Down
9 changes: 5 additions & 4 deletions src/fmt/mod.rs
Expand Up @@ -136,13 +136,14 @@ impl fmt::Debug for Formatter {
}
}

pub(crate) type FormatFn = Box<dyn Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send>;

pub(crate) struct Builder {
pub format_timestamp: Option<TimestampPrecision>,
pub format_module_path: bool,
pub format_level: bool,
pub format_indent: Option<usize>,
#[allow(unknown_lints, bare_trait_objects)]
pub custom_format: Option<Box<Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send>>,
pub custom_format: Option<FormatFn>,
built: bool,
}

Expand All @@ -165,8 +166,7 @@ impl Builder {
/// 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.
#[allow(unknown_lints, bare_trait_objects)]
pub fn build(&mut self) -> Box<Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send> {
pub fn build(&mut self) -> FormatFn {
assert!(!self.built, "attempt to re-use consumed builder");

let built = mem::replace(
Expand Down Expand Up @@ -230,6 +230,7 @@ impl<'a> DefaultFormat<'a> {
.style()
.set_color(Color::Black)
.set_intense(true)
.clone()
.into_value(text)
}
#[cfg(not(feature = "termcolor"))]
Expand Down
8 changes: 3 additions & 5 deletions src/fmt/writer/termcolor/extern_impl.rs
Expand Up @@ -370,9 +370,9 @@ impl Style {
}

/// Wrap a value in the style by taking ownership of it.
pub(crate) fn into_value<T>(&mut self, value: T) -> StyledValue<'static, T> {
pub(crate) fn into_value<T>(self, value: T) -> StyledValue<'static, T> {
StyledValue {
style: Cow::Owned(self.clone()),
style: Cow::Owned(self),
value,
}
}
Expand Down Expand Up @@ -451,6 +451,7 @@ impl_styled_value_fmt!(
///
/// Hexadecimal numbers are written with a `0x` prefix.
#[allow(missing_docs)]
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Color {
Black,
Expand All @@ -463,8 +464,6 @@ pub enum Color {
White,
Ansi256(u8),
Rgb(u8, u8, u8),
#[doc(hidden)]
__Nonexhaustive,
}

impl Color {
Expand All @@ -480,7 +479,6 @@ impl Color {
Color::White => Some(termcolor::Color::White),
Color::Ansi256(value) => Some(termcolor::Color::Ansi256(value)),
Color::Rgb(r, g, b) => Some(termcolor::Color::Rgb(r, g, b)),
_ => None,
}
}
}
72 changes: 27 additions & 45 deletions src/lib.rs
Expand Up @@ -15,20 +15,16 @@
//! ## Example
//!
//! ```
//! #[macro_use] extern crate log;
//! use log::{debug, error, log_enabled, info, Level};
//!
//! use log::Level;
//! env_logger::init();
//!
//! fn main() {
//! env_logger::init();
//! debug!("this is a debug {}", "message");
//! error!("this is printed by default");
//!
//! debug!("this is a debug {}", "message");
//! error!("this is printed by default");
//!
//! if log_enabled!(Level::Info) {
//! let x = 3 * 4; // expensive computation
//! info!("the answer was: {}", x);
//! }
//! if log_enabled!(Level::Info) {
//! let x = 3 * 4; // expensive computation
//! info!("the answer was: {}", x);
//! }
//! ```
//!
Expand Down Expand Up @@ -146,7 +142,6 @@
//!
//! ```
//! # #[macro_use] extern crate log;
//! # fn main() {}
//! #[cfg(test)]
//! mod tests {
//! fn init() {
Expand Down Expand Up @@ -245,7 +240,6 @@
#![cfg_attr(rustbuild, feature(staged_api, rustc_private))]
#![cfg_attr(rustbuild, unstable(feature = "rustc_private", issue = "27812"))]
#![deny(missing_debug_implementations, missing_docs, warnings)]
#![allow(clippy::needless_doctest_main)]

use std::{borrow::Cow, cell::RefCell, env, io};

Expand All @@ -258,7 +252,7 @@ pub use self::fmt::glob::*;

use self::filter::Filter;
use self::fmt::writer::{self, Writer};
use self::fmt::Formatter;
use self::fmt::{FormatFn, Formatter};

/// The default name for the environment variable to read filters from.
pub const DEFAULT_FILTER_ENV: &str = "RUST_LOG";
Expand Down Expand Up @@ -310,8 +304,7 @@ struct Var<'a> {
pub struct Logger {
writer: Writer,
filter: Filter,
#[allow(unknown_lints, bare_trait_objects)]
format: Box<Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send>,
format: FormatFn,
}

/// `Builder` acts as builder for initializing a `Logger`.
Expand All @@ -322,23 +315,20 @@ pub struct Logger {
/// # Examples
///
/// ```
/// #[macro_use] extern crate log;
///
/// use std::env;
/// use std::io::Write;
/// use log::LevelFilter;
/// # #[macro_use] extern crate log;
/// # use std::io::Write;
/// use env_logger::Builder;
/// use log::LevelFilter;
///
/// fn main() {
/// let mut builder = Builder::from_default_env();
/// let mut builder = Builder::from_default_env();
///
/// builder.format(|buf, record| writeln!(buf, "{} - {}", record.level(), record.args()))
/// .filter(None, LevelFilter::Info)
/// .init();
/// builder
/// .format(|buf, record| writeln!(buf, "{} - {}", record.level(), record.args()))
/// .filter(None, LevelFilter::Info)
/// .init();
///
/// error!("error message");
/// info!("info message");
/// }
/// error!("error message");
/// info!("info message");
/// ```
#[derive(Default)]
pub struct Builder {
Expand All @@ -360,16 +350,15 @@ impl Builder {
/// Create a new builder and configure filters and style:
///
/// ```
/// # fn main() {
/// use log::LevelFilter;
/// use env_logger::{Builder, WriteStyle};
///
/// let mut builder = Builder::new();
///
/// builder.filter(None, LevelFilter::Info)
/// .write_style(WriteStyle::Always)
/// .init();
/// # }
/// builder
/// .filter(None, LevelFilter::Info)
/// .write_style(WriteStyle::Always)
/// .init();
/// ```
///
/// [`filter`]: #method.filter
Expand Down Expand Up @@ -612,14 +601,12 @@ impl Builder {
/// Only include messages for info and above for logs in `path::to::module`:
///
/// ```
/// # fn main() {
/// use log::LevelFilter;
/// use env_logger::Builder;
/// use log::LevelFilter;
///
/// let mut builder = Builder::new();
///
/// builder.filter_module("path::to::module", LevelFilter::Info);
/// # }
/// ```
pub fn filter_module(&mut self, module: &str, level: LevelFilter) -> &mut Self {
self.filter.filter_module(module, level);
Expand All @@ -633,14 +620,12 @@ impl Builder {
/// Only include messages for info and above for logs in `path::to::module`:
///
/// ```
/// # fn main() {
/// use log::LevelFilter;
/// use env_logger::Builder;
/// use log::LevelFilter;
///
/// let mut builder = Builder::new();
///
/// builder.filter_level(LevelFilter::Info);
/// # }
/// ```
pub fn filter_level(&mut self, level: LevelFilter) -> &mut Self {
self.filter.filter_level(level);
Expand All @@ -657,14 +642,12 @@ impl Builder {
/// Only include messages for info and above for logs in `path::to::module`:
///
/// ```
/// # fn main() {
/// use log::LevelFilter;
/// use env_logger::Builder;
/// use log::LevelFilter;
///
/// let mut builder = Builder::new();
///
/// builder.filter(Some("path::to::module"), LevelFilter::Info);
/// # }
/// ```
pub fn filter(&mut self, module: Option<&str>, level: LevelFilter) -> &mut Self {
self.filter.filter(module, level);
Expand Down Expand Up @@ -1125,7 +1108,6 @@ pub fn init() {
/// and `MY_LOG_STYLE` for writing colors:
///
/// ```
/// # extern crate env_logger;
/// use env_logger::{Builder, Env};
///
/// # fn run() -> Result<(), Box<::std::error::Error>> {
Expand All @@ -1135,7 +1117,7 @@ pub fn init() {
///
/// Ok(())
/// # }
/// # fn main() { run().unwrap(); }
/// # run().unwrap();
/// ```
///
/// # Errors
Expand Down

0 comments on commit 2b33c97

Please sign in to comment.