Skip to content

Commit

Permalink
chore: reduce one allocation in stdio append
Browse files Browse the repository at this point in the history
  • Loading branch information
andylokandy committed Aug 2, 2024
1 parent eebc2fe commit ced7714
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
6 changes: 2 additions & 4 deletions src/append/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand All @@ -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(())
}

Expand Down
22 changes: 11 additions & 11 deletions src/filter/min_level.rs → src/filter/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -44,14 +43,15 @@ impl MaxLevel {
}
}
}
impl From<MaxLevel> for Filter {
fn from(filter: MaxLevel) -> Self {
Filter::MaxLevel(filter)
}
}

impl From<LevelFilter> for Filter {
fn from(filter: LevelFilter) -> Self {
Filter::MaxLevel(MaxLevel(filter))
Filter::Level(filter)
}
}

impl From<log::LevelFilter> for Filter {
fn from(filter: log::LevelFilter) -> Self {
Filter::Level(LevelFilter(filter))
}
}
8 changes: 4 additions & 4 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
}
}
Expand Down

0 comments on commit ced7714

Please sign in to comment.