From cfe9093fd8bee1ec7fda68f8aa6500620e68c88e Mon Sep 17 00:00:00 2001 From: Oakchris1955 <80592203+Oakchris1955@users.noreply.github.com> Date: Wed, 19 Jun 2024 12:47:29 +0300 Subject: [PATCH 1/3] Remove is-terminal dependency The unstable feature "is_terminal" of the Standard Library has been stabilized since 1.70.0, which is also the MSRV of this crate --- Cargo.toml | 3 +-- README.md | 5 ++--- examples/colors.rs | 2 +- examples/colors2.rs | 2 +- scripts/qualify.rs | 1 - src/code_examples.md | 3 --- src/formats.rs | 3 --- src/logger.rs | 17 ++--------------- 8 files changed, 7 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dcf0654..31649ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = ["colors", "textfilter"] async = ["dep:crossbeam-channel", "dep:crossbeam-queue"] -colors = ["dep:nu-ansi-term", "is-terminal"] +colors = ["dep:nu-ansi-term"] compress = ["dep:flate2"] dont_minimize_extra_stacks = [] json = ["dep:serde_json", "dep:serde", "dep:serde_derive"] @@ -42,7 +42,6 @@ textfilter = ["dep:regex"] trc = ["async", "specfile", "dep:tracing", "dep:tracing-subscriber"] [dependencies] -is-terminal = { version = "0.4", optional = true } nu-ansi-term = { version = "0.50", optional = true } chrono = { version = "0.4.22", default-features = false, features = ["clock"] } crossbeam-channel = { version = "0.5", optional = true } diff --git a/README.md b/README.md index 02df4ab..f1d7e44 100644 --- a/README.md +++ b/README.md @@ -107,8 +107,7 @@ The default feature `colors` simplifies this by doing three things: * provides additional colored pendants to the existing uncolored format functions * it uses `colored_default_format()` for the output to stderr, and the non-colored `default_format()` for the output to files -* it activates the optional dependency to `is-terminal` to being able to switch off - coloring if the output is not sent to a terminal but e.g. piped to another program. +* it switches off coloring if the output is not sent to a terminal but e.g. piped to another program. **Colors**, or styles in general, are a matter of taste, and no choice will fit every need. So you can override the default formatting and coloring in various ways. -With switching off the default features and choosing feature `is-terminal` explicitly +With switching off the default features (see [usage](#usage)) you can remove the `nu_ansi_term`-based coloring but keep the capability to switch off your own coloring. diff --git a/examples/colors.rs b/examples/colors.rs index dacf95e..794de2e 100644 --- a/examples/colors.rs +++ b/examples/colors.rs @@ -4,7 +4,7 @@ fn main() { #[cfg(feature = "colors")] { - use is_terminal::IsTerminal; + use std::io::IsTerminal; use nu_ansi_term::Color; for i in 0..=255 { diff --git a/examples/colors2.rs b/examples/colors2.rs index adfa531..90eed79 100644 --- a/examples/colors2.rs +++ b/examples/colors2.rs @@ -9,7 +9,7 @@ fn main() { // #[cfg(feature = "colors")] // { - // use is_terminal::IsTerminal;; + // use std::io::IsTerminal;; // colored::control::set_override(true); diff --git a/scripts/qualify.rs b/scripts/qualify.rs index 37a9f77..d2fafba 100755 --- a/scripts/qualify.rs +++ b/scripts/qualify.rs @@ -55,7 +55,6 @@ fn main() { run_command!("cargo build"); run_command!("cargo build --no-default-features"); #[rustfmt::skip] - run_command!("cargo build --no-default-features --features=is-terminal"); run_command!("cargo build --all-features"); run_command!("cargo build --release"); run_command!("cargo build --release --all-features"); diff --git a/src/code_examples.md b/src/code_examples.md index 4098a29..c0f8c2f 100644 --- a/src/code_examples.md +++ b/src/code_examples.md @@ -259,10 +259,8 @@ by providing one of the variants of [`AdaptiveFormat`](crate::AdaptiveFormat) to format method, e.g. ```rust -# #[cfg(feature = "is-terminal")] # use flexi_logger::AdaptiveFormat; # fn main() -> Result<(), Box> { -# #[cfg(feature = "is-terminal")] # { flexi_logger::Logger::try_with_str("info")? .adaptive_format_for_stderr(AdaptiveFormat::Detailed); @@ -276,7 +274,6 @@ format method, e.g. `flexi_logger` initializes by default equivalently to this: ```rust -# #[cfg(feature = "is-terminal")] # mod example { # use flexi_logger::{Logger,AdaptiveFormat,default_format, FileSpec}; # use log::{debug, error, info, trace, warn}; diff --git a/src/formats.rs b/src/formats.rs index 7d6e665..d5f20b3 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -455,8 +455,6 @@ fn parse_style(input: &str) -> Result { /// /// This is helpful if the output is sometimes piped into other programs, which usually /// do not expect color control byte sequences. -#[cfg_attr(docsrs, doc(cfg(feature = "is-terminal")))] -#[cfg(feature = "is-terminal")] #[derive(Clone, Copy)] pub enum AdaptiveFormat { /// Chooses between [`default_format`](crate::default_format) @@ -487,7 +485,6 @@ pub enum AdaptiveFormat { Custom(FormatFunction, FormatFunction), } -#[cfg(feature = "is-terminal")] impl AdaptiveFormat { #[must_use] pub(crate) fn format_function(self, is_tty: bool) -> FormatFunction { diff --git a/src/logger.rs b/src/logger.rs index 0f7f696..795d71d 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -1,4 +1,3 @@ -#[cfg(feature = "is-terminal")] use crate::formats::AdaptiveFormat; use crate::{ filter::LogLineFilter, @@ -11,8 +10,8 @@ use crate::{ Cleanup, Criterion, DeferredNow, FileSpec, FlexiLoggerError, FormatFunction, LogSpecification, LoggerHandle, Naming, WriteMode, }; -#[cfg(feature = "is-terminal")] -use is_terminal::IsTerminal; + +use std::io::IsTerminal; use log::LevelFilter; #[cfg(feature = "specfile")] use std::sync::Mutex; @@ -136,19 +135,11 @@ impl Logger { #[cfg(feature = "colors")] format_for_stdout: AdaptiveFormat::Default.format_function( - if cfg!(feature = "is-terminal") { std::io::stdout().is_terminal() - } else { - false - }, ), #[cfg(feature = "colors")] format_for_stderr: AdaptiveFormat::Default.format_function( - if cfg!(feature = "is-terminal") { std::io::stderr().is_terminal() - } else { - false - }, ), #[cfg(not(feature = "colors"))] @@ -311,8 +302,6 @@ impl Logger { /// Coloring is used if `stderr` is a tty. /// /// Regarding the default, see [`Logger::format`]. - #[cfg_attr(docsrs, doc(cfg(feature = "is-terminal")))] - #[cfg(feature = "is-terminal")] #[must_use] pub fn adaptive_format_for_stderr(mut self, adaptive_format: AdaptiveFormat) -> Self { self.format_for_stderr = adaptive_format.format_function(std::io::stderr().is_terminal()); @@ -333,8 +322,6 @@ impl Logger { /// Coloring is used if `stdout` is a tty. /// /// Regarding the default, see [`Logger::format`]. - #[cfg_attr(docsrs, doc(cfg(feature = "is-terminal")))] - #[cfg(feature = "is-terminal")] #[must_use] pub fn adaptive_format_for_stdout(mut self, adaptive_format: AdaptiveFormat) -> Self { self.format_for_stdout = adaptive_format.format_function(std::io::stdout().is_terminal()); From 9b745fd4b44ffb1212e9ca58ecd92e7ef8f0a5e9 Mon Sep 17 00:00:00 2001 From: Oakchris1955 <80592203+Oakchris1955@users.noreply.github.com> Date: Wed, 19 Jun 2024 12:52:31 +0300 Subject: [PATCH 2/3] Implement From for LogSpecification --- src/log_specification.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/log_specification.rs b/src/log_specification.rs index 66644e7..8cc071d 100644 --- a/src/log_specification.rs +++ b/src/log_specification.rs @@ -462,6 +462,19 @@ impl std::convert::TryFrom<&String> for LogSpecification { } } +impl From for LogSpecification { + fn from(value: LevelFilter) -> Self { + match value { + LevelFilter::Error => LogSpecification::error(), + LevelFilter::Warn => LogSpecification::warn(), + LevelFilter::Info => LogSpecification::info(), + LevelFilter::Debug => LogSpecification::debug(), + LevelFilter::Trace => LogSpecification::trace(), + LevelFilter::Off => LogSpecification::off(), + } + } +} + fn push_err(s: &str, parse_errs: &mut String) { if !parse_errs.is_empty() { parse_errs.push_str("; "); From 43f1370441f5a3f37f2d58042522667056101251 Mon Sep 17 00:00:00 2001 From: Meinolf Block Date: Fri, 21 Jun 2024 12:17:59 +0200 Subject: [PATCH 3/3] [0.28.5] --- CHANGELOG.md | 8 ++++++++ Cargo.toml | 2 +- examples/colors.rs | 2 +- scripts/qualify.rs | 4 ---- src/code_examples.md | 10 +++++----- src/log_specification.rs | 2 ++ src/logger.rs | 31 ++++++++++++++++++++++--------- 7 files changed, 39 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f4f025..5018fa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.28.5] - 2024-06-21 + +Remove unnecessary dependency to `is-terminal`. + +Add impl `From` for `LogSpecification`. + +Kudos to [Oakchris1955](https://github.com/Oakchris1955). + ## [0.28.4] - 2024-06-14 Fix [issue #162](https://github.com/emabee/flexi_logger/issues/162) diff --git a/Cargo.toml b/Cargo.toml index 31649ab..42e5f40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "flexi_logger" -version = "0.28.4" +version = "0.28.5" authors = ["emabee "] categories = ["development-tools::debugging"] description = """ diff --git a/examples/colors.rs b/examples/colors.rs index 794de2e..35daabe 100644 --- a/examples/colors.rs +++ b/examples/colors.rs @@ -4,8 +4,8 @@ fn main() { #[cfg(feature = "colors")] { - use std::io::IsTerminal; use nu_ansi_term::Color; + use std::io::IsTerminal; for i in 0..=255 { println!("{}: {}", i, Color::Fixed(i).paint(i.to_string())); diff --git a/scripts/qualify.rs b/scripts/qualify.rs index d2fafba..e3ea8e1 100755 --- a/scripts/qualify.rs +++ b/scripts/qualify.rs @@ -54,7 +54,6 @@ fn main() { std::fs::remove_file("Cargo.lock").ok(); run_command!("cargo build"); run_command!("cargo build --no-default-features"); - #[rustfmt::skip] run_command!("cargo build --all-features"); run_command!("cargo build --release"); run_command!("cargo build --release --all-features"); @@ -62,7 +61,6 @@ fn main() { // Clippy in important variants run_command!("cargo clippy -- -D warnings"); run_command!("cargo clippy --all-features -- -D warnings"); - #[rustfmt::skip] run_command!("cargo +nightly clippy --all-targets --all-features -- -D warnings"); // Run tests in important variants @@ -70,12 +68,10 @@ fn main() { run_command!("cargo test --release --all-features"); run_command!("cargo test --no-default-features"); run_command!("cargo test --release"); - #[rustfmt::skip] run_command!("cargo test --release --features specfile_without_notification"); // doc run_command!("cargo +nightly test --all-features --doc"); - #[rustfmt::skip] run_command!("cargo +nightly doc --all-features --no-deps --open"); // check version consistency diff --git a/src/code_examples.md b/src/code_examples.md index c0f8c2f..6bac39b 100644 --- a/src/code_examples.md +++ b/src/code_examples.md @@ -36,7 +36,7 @@ and call `start()` immediately: # Ok(())} ``` -- Combine both options: +- Combine both options, with env having precendence over the given parameter value: ```rust # use flexi_logger::{Logger,FlexiLoggerError}; @@ -45,11 +45,11 @@ and call `start()` immediately: # Ok(())} ``` -or, even shorter, use: + or, even shorter, use: -```rust -flexi_logger::init(); -``` + ```rust + flexi_logger::init(); + ``` After that, you just use the log-macros from the log crate. Those log lines that match the log specification are then written to the default output channel (stderr). diff --git a/src/log_specification.rs b/src/log_specification.rs index 8cc071d..d144ccc 100644 --- a/src/log_specification.rs +++ b/src/log_specification.rs @@ -449,12 +449,14 @@ impl std::fmt::Display for LogSpecification { Ok(()) } } + impl std::convert::TryFrom<&str> for LogSpecification { type Error = FlexiLoggerError; fn try_from(value: &str) -> Result { LogSpecification::parse(value) } } + impl std::convert::TryFrom<&String> for LogSpecification { type Error = FlexiLoggerError; fn try_from(value: &String) -> Result { diff --git a/src/logger.rs b/src/logger.rs index 795d71d..576c005 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -11,12 +11,12 @@ use crate::{ LoggerHandle, Naming, WriteMode, }; -use std::io::IsTerminal; use log::LevelFilter; #[cfg(feature = "specfile")] use std::sync::Mutex; use std::{ collections::HashMap, + io::IsTerminal, path::PathBuf, sync::{Arc, RwLock}, time::Duration, @@ -80,9 +80,24 @@ enum LogTarget { /// loglevel-specification. impl Logger { /// Creates a Logger that you provide with an explicit [`LogSpecification`]. + /// + /// ## Examples + /// + /// ```rust + /// use log::LevelFilter; + /// use flexi_logger::Logger; + /// let logger = Logger::with(LevelFilter::Info).start().unwrap(); + /// ``` + /// + /// ```rust + /// use flexi_logger::{Logger, LogSpecification}; + /// let logger = Logger::with( + /// LogSpecification::parse("info, critical_mod = trace").unwrap() + /// ).start().unwrap(); + /// ``` #[must_use] - pub fn with(logspec: LogSpecification) -> Self { - Self::from_spec_and_errs(logspec) + pub fn with(logspec: impl Into) -> Self { + Self::from_spec_and_errs(logspec.into()) } /// Creates a Logger that reads the [`LogSpecification`] from a `String` or `&str`. @@ -134,13 +149,11 @@ impl Logger { format_for_file: default_format, #[cfg(feature = "colors")] - format_for_stdout: AdaptiveFormat::Default.format_function( - std::io::stdout().is_terminal() - ), + format_for_stdout: AdaptiveFormat::Default + .format_function(std::io::stdout().is_terminal()), #[cfg(feature = "colors")] - format_for_stderr: AdaptiveFormat::Default.format_function( - std::io::stderr().is_terminal() - ), + format_for_stderr: AdaptiveFormat::Default + .format_function(std::io::stderr().is_terminal()), #[cfg(not(feature = "colors"))] format_for_stdout: default_format,