From 003d2cfd2608f06922a208b0e287c2e44b400e32 Mon Sep 17 00:00:00 2001 From: Bobby Powers Date: Wed, 23 Nov 2022 11:59:57 -0800 Subject: [PATCH] use is_terminal instead of atty for RUSTSEC-2021-0145 --- spdlog/Cargo.toml | 2 +- spdlog/src/sink/std_stream_sink.rs | 31 +++++++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/spdlog/Cargo.toml b/spdlog/Cargo.toml index 8007cc8..db2c267 100644 --- a/spdlog/Cargo.toml +++ b/spdlog/Cargo.toml @@ -40,12 +40,12 @@ multi-thread = ["crossbeam"] [dependencies] arc-swap = "1.5.1" atomic = "0.5.1" -atty = "0.2.14" cfg-if = "1.0.0" chrono = "0.4.22" crossbeam = { version = "0.8.2", optional = true } flexible-string = { version = "0.1.0", optional = true } if_chain = "1.0.2" +is-terminal = "0.4" log = { version = "0.4", optional = true } once_cell = "1.16.0" spdlog-macros = { version = "0.1.0", path = "../spdlog-macros" } diff --git a/spdlog/src/sink/std_stream_sink.rs b/spdlog/src/sink/std_stream_sink.rs index 0179bc7..8e398a5 100644 --- a/spdlog/src/sink/std_stream_sink.rs +++ b/spdlog/src/sink/std_stream_sink.rs @@ -47,6 +47,13 @@ impl StdStreamDest { StdStreamDest::Stderr(stream) => StdStreamDest::Stderr(stream.lock()), } } + + fn stream_type(&self) -> StdStream { + match self { + StdStreamDest::Stdout(_) => StdStream::Stdout, + StdStreamDest::Stderr(_) => StdStream::Stderr, + } + } } macro_rules! impl_write_for_dest { @@ -79,7 +86,6 @@ impl_write_for_dest!(StdStreamDest, io::StderrLock<'_>>); pub struct StdStreamSink { common_impl: helper::CommonImpl, dest: StdStreamDest, - atty_stream: atty::Stream, should_render_style: bool, level_style_codes: LevelStyleCodes, } @@ -116,14 +122,20 @@ impl StdStreamSink { /// Sets the style mode. pub fn set_style_mode(&mut self, style_mode: StyleMode) { - self.should_render_style = Self::should_render_style(style_mode, self.atty_stream); + self.should_render_style = Self::should_render_style(style_mode, self.dest.stream_type()); } #[must_use] - fn should_render_style(style_mode: StyleMode, atty_stream: atty::Stream) -> bool { + fn should_render_style(style_mode: StyleMode, stream: StdStream) -> bool { + use is_terminal::IsTerminal; + let is_terminal = match stream { + StdStream::Stdout => io::stdout().is_terminal(), + StdStream::Stderr => io::stderr().is_terminal(), + }; + match style_mode { StyleMode::Always => true, - StyleMode::Auto => atty::is(atty_stream) && enable_ansi_escape_sequences(), + StyleMode::Auto => is_terminal && enable_ansi_escape_sequences(), StyleMode::Never => false, } } @@ -263,16 +275,13 @@ impl StdStreamSinkBuilder<()> { impl StdStreamSinkBuilder { /// Builds a [`StdStreamSink`]. pub fn build(self) -> Result { - let atty_stream = match self.std_stream { - StdStream::Stdout => atty::Stream::Stdout, - StdStream::Stderr => atty::Stream::Stderr, - }; - Ok(StdStreamSink { common_impl: helper::CommonImpl::from_builder(self.common_builder_impl), dest: StdStreamDest::new(self.std_stream), - atty_stream, - should_render_style: StdStreamSink::should_render_style(self.style_mode, atty_stream), + should_render_style: StdStreamSink::should_render_style( + self.style_mode, + self.std_stream, + ), level_style_codes: LevelStyleCodes::default(), }) }