Skip to content

Commit

Permalink
Merge branch 'Oakchris1955-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
emabee committed Jun 21, 2024
2 parents b9396e5 + 43f1370 commit a306c2b
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 47 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<LevelFilter>` 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)
Expand Down
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "flexi_logger"
version = "0.28.4"
version = "0.28.5"
authors = ["emabee <[email protected]>"]
categories = ["development-tools::debugging"]
description = """
Expand Down Expand Up @@ -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"]
Expand All @@ -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 }
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,15 @@ 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.

**<span style="color:red">C</span><span style="color:blue">o</span><span
style="color:green">l</span><span style="color:orange">o</span><span
style="color:magenta">r</span><span style="color:darkturquoise">s</span>**,
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.

Expand Down
2 changes: 1 addition & 1 deletion examples/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ fn main() {

#[cfg(feature = "colors")]
{
use is_terminal::IsTerminal;
use nu_ansi_term::Color;
use std::io::IsTerminal;

for i in 0..=255 {
println!("{}: {}", i, Color::Fixed(i).paint(i.to_string()));
Expand Down
2 changes: 1 addition & 1 deletion examples/colors2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {

// #[cfg(feature = "colors")]
// {
// use is_terminal::IsTerminal;;
// use std::io::IsTerminal;;

// colored::control::set_override(true);

Expand Down
5 changes: 0 additions & 5 deletions scripts/qualify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,24 @@ 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 --no-default-features --features=is-terminal");
run_command!("cargo build --all-features");
run_command!("cargo build --release");
run_command!("cargo build --release --all-features");

// 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
run_command!("cargo +1.70.0 test --all-features");
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
Expand Down
13 changes: 5 additions & 8 deletions src/code_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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).
Expand Down Expand Up @@ -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<dyn std::error::Error>> {
# #[cfg(feature = "is-terminal")]
# {
flexi_logger::Logger::try_with_str("info")?
.adaptive_format_for_stderr(AdaptiveFormat::Detailed);
Expand All @@ -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};
Expand Down
3 changes: 0 additions & 3 deletions src/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,6 @@ fn parse_style(input: &str) -> Result<Style, std::num::ParseIntError> {
///
/// 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)
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions src/log_specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,19 +449,34 @@ impl std::fmt::Display for LogSpecification {
Ok(())
}
}

impl std::convert::TryFrom<&str> for LogSpecification {
type Error = FlexiLoggerError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
LogSpecification::parse(value)
}
}

impl std::convert::TryFrom<&String> for LogSpecification {
type Error = FlexiLoggerError;
fn try_from(value: &String) -> Result<Self, Self::Error> {
LogSpecification::parse(value)
}
}

impl From<LevelFilter> 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("; ");
Expand Down
46 changes: 23 additions & 23 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(feature = "is-terminal")]
use crate::formats::AdaptiveFormat;
use crate::{
filter::LogLineFilter,
Expand All @@ -11,13 +10,13 @@ use crate::{
Cleanup, Criterion, DeferredNow, FileSpec, FlexiLoggerError, FormatFunction, LogSpecification,
LoggerHandle, Naming, WriteMode,
};
#[cfg(feature = "is-terminal")]
use is_terminal::IsTerminal;

use log::LevelFilter;
#[cfg(feature = "specfile")]
use std::sync::Mutex;
use std::{
collections::HashMap,
io::IsTerminal,
path::PathBuf,
sync::{Arc, RwLock},
time::Duration,
Expand Down Expand Up @@ -81,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<LogSpecification>) -> Self {
Self::from_spec_and_errs(logspec.into())
}

/// Creates a Logger that reads the [`LogSpecification`] from a `String` or `&str`.
Expand Down Expand Up @@ -135,21 +149,11 @@ impl Logger {
format_for_file: default_format,

#[cfg(feature = "colors")]
format_for_stdout: AdaptiveFormat::Default.format_function(
if cfg!(feature = "is-terminal") {
std::io::stdout().is_terminal()
} else {
false
},
),
format_for_stdout: AdaptiveFormat::Default
.format_function(std::io::stdout().is_terminal()),
#[cfg(feature = "colors")]
format_for_stderr: AdaptiveFormat::Default.format_function(
if cfg!(feature = "is-terminal") {
std::io::stderr().is_terminal()
} else {
false
},
),
format_for_stderr: AdaptiveFormat::Default
.format_function(std::io::stderr().is_terminal()),

#[cfg(not(feature = "colors"))]
format_for_stdout: default_format,
Expand Down Expand Up @@ -311,8 +315,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());
Expand All @@ -333,8 +335,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());
Expand Down

0 comments on commit a306c2b

Please sign in to comment.