From ced7714c549e4eea0499b71c5d2dc4f49b02cce5 Mon Sep 17 00:00:00 2001 From: andylokandy Date: Fri, 2 Aug 2024 14:04:19 +0800 Subject: [PATCH 1/2] chore: reduce one allocation in stdio append --- src/append/stdio.rs | 6 ++---- src/filter/{min_level.rs => level.rs} | 22 +++++++++++----------- src/filter/mod.rs | 8 ++++---- 3 files changed, 17 insertions(+), 19 deletions(-) rename src/filter/{min_level.rs => level.rs} (77%) diff --git a/src/append/stdio.rs b/src/append/stdio.rs index 59d4510..06101fa 100644 --- a/src/append/stdio.rs +++ b/src/append/stdio.rs @@ -21,8 +21,7 @@ pub struct Stdout; impl Append for Stdout { fn append(&self, record: &log::Record) -> anyhow::Result<()> { - let bytes = format!("{}\n", record.args()).into_bytes(); - std::io::stdout().write_all(&bytes)?; + writeln!(std::io::stdout(), "{}", record.args())?; Ok(()) } @@ -36,8 +35,7 @@ pub struct Stderr; impl Append for Stderr { fn append(&self, record: &log::Record) -> anyhow::Result<()> { - let bytes = format!("{}\n", record.args()).into_bytes(); - std::io::stderr().write_all(&bytes)?; + writeln!(std::io::stderr(), "{}", record.args())?; Ok(()) } diff --git a/src/filter/min_level.rs b/src/filter/level.rs similarity index 77% rename from src/filter/min_level.rs rename to src/filter/level.rs index 8a8fe19..dd4b04d 100644 --- a/src/filter/min_level.rs +++ b/src/filter/level.rs @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use log::LevelFilter; use log::Metadata; use crate::filter::Filter; @@ -28,13 +27,13 @@ use crate::filter::FilterResult; /// - `Debug` /// - `Trace` /// -/// If MaxLevel is set to `Info`, it will allow `Error`, `Warn`, and `Info` logs. +/// If LevelFilter is set to `Info`, it will allow `Error`, `Warn`, and `Info` logs. /// -/// If MaxLevel is set to `Off`, it will reject all logs. +/// If LevelFilter is set to `Off`, it will reject all logs. #[derive(Debug, Clone)] -pub struct MaxLevel(pub LevelFilter); +pub struct LevelFilter(pub log::LevelFilter); -impl MaxLevel { +impl LevelFilter { pub(crate) fn filter(&self, metadata: &Metadata) -> FilterResult { let level = metadata.level(); if level <= self.0 { @@ -44,14 +43,15 @@ impl MaxLevel { } } } -impl From for Filter { - fn from(filter: MaxLevel) -> Self { - Filter::MaxLevel(filter) - } -} impl From for Filter { fn from(filter: LevelFilter) -> Self { - Filter::MaxLevel(MaxLevel(filter)) + Filter::Level(filter) + } +} + +impl From for Filter { + fn from(filter: log::LevelFilter) -> Self { + Filter::Level(LevelFilter(filter)) } } diff --git a/src/filter/mod.rs b/src/filter/mod.rs index ff1870c..3617713 100644 --- a/src/filter/mod.rs +++ b/src/filter/mod.rs @@ -13,10 +13,10 @@ // limitations under the License. pub use self::custom::CustomFilter; -pub use self::min_level::MaxLevel; +pub use self::level::LevelFilter; mod custom; -mod min_level; +mod level; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum FilterResult { @@ -30,14 +30,14 @@ pub enum FilterResult { #[derive(Debug)] pub enum Filter { - MaxLevel(MaxLevel), + Level(LevelFilter), Custom(CustomFilter), } impl Filter { pub(crate) fn filter(&self, metadata: &log::Metadata) -> FilterResult { match self { - Filter::MaxLevel(filter) => filter.filter(metadata), + Filter::Level(filter) => filter.filter(metadata), Filter::Custom(filter) => filter.filter(metadata), } } From babb1fc21de58823668f7ba7cd5512546abab88e Mon Sep 17 00:00:00 2001 From: andylokandy Date: Fri, 2 Aug 2024 14:21:25 +0800 Subject: [PATCH 2/2] fix --- src/append/stdio.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/append/stdio.rs b/src/append/stdio.rs index 06101fa..59d4510 100644 --- a/src/append/stdio.rs +++ b/src/append/stdio.rs @@ -21,7 +21,8 @@ pub struct Stdout; impl Append for Stdout { fn append(&self, record: &log::Record) -> anyhow::Result<()> { - writeln!(std::io::stdout(), "{}", record.args())?; + let bytes = format!("{}\n", record.args()).into_bytes(); + std::io::stdout().write_all(&bytes)?; Ok(()) } @@ -35,7 +36,8 @@ pub struct Stderr; impl Append for Stderr { fn append(&self, record: &log::Record) -> anyhow::Result<()> { - writeln!(std::io::stderr(), "{}", record.args())?; + let bytes = format!("{}\n", record.args()).into_bytes(); + std::io::stderr().write_all(&bytes)?; Ok(()) }