From 00e1bea43942dd8f16533982f8f68aa0939307ec Mon Sep 17 00:00:00 2001 From: Esper Thomson Date: Mon, 30 Sep 2024 17:51:54 -0400 Subject: [PATCH] Add `BufWriter` wrapper for performance. --- astro_modloader/src/logging.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/astro_modloader/src/logging.rs b/astro_modloader/src/logging.rs index 892269e..f27f288 100644 --- a/astro_modloader/src/logging.rs +++ b/astro_modloader/src/logging.rs @@ -1,5 +1,5 @@ use std::fs; -use std::io::prelude::*; +use std::io::{prelude::*, BufWriter}; use std::sync::{Mutex, OnceLock}; use colored::*; @@ -7,11 +7,17 @@ use log::{Level, LevelFilter, Log, Metadata, Record, SetLoggerError}; #[derive(Debug)] struct SimpleLogger { - file: Mutex, + file: Mutex>, } impl SimpleLogger { - fn lock(&self, f: impl FnOnce(&mut fs::File) -> T) -> T { + fn new(file: fs::File) -> Self { + SimpleLogger { + file: Mutex::new(BufWriter::new(file)), + } + } + + fn lock(&self, f: impl FnOnce(&mut BufWriter) -> T) -> T { let mut guard = match self.file.lock() { Ok(guard) => guard, Err(err) => err.into_inner(), @@ -83,16 +89,15 @@ impl Log for SimpleLogger { fn get_logger() -> &'static SimpleLogger { static LOGGER: OnceLock = OnceLock::new(); - LOGGER.get_or_init(|| SimpleLogger { - // open file - file: Mutex::new( + LOGGER.get_or_init(|| { + SimpleLogger::new( fs::OpenOptions::new() .write(true) .create(true) .truncate(true) .open("modloader_log.txt") .unwrap(), - ), + ) }) }