Skip to content

Commit

Permalink
Add BufWriter wrapper for performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
Esper89 committed Sep 30, 2024
1 parent 3618c2f commit 00e1bea
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions astro_modloader/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use std::fs;
use std::io::prelude::*;
use std::io::{prelude::*, BufWriter};
use std::sync::{Mutex, OnceLock};

use colored::*;
use log::{Level, LevelFilter, Log, Metadata, Record, SetLoggerError};

#[derive(Debug)]
struct SimpleLogger {
file: Mutex<fs::File>,
file: Mutex<BufWriter<fs::File>>,
}

impl SimpleLogger {
fn lock<T>(&self, f: impl FnOnce(&mut fs::File) -> T) -> T {
fn new(file: fs::File) -> Self {
SimpleLogger {
file: Mutex::new(BufWriter::new(file)),
}
}

fn lock<T>(&self, f: impl FnOnce(&mut BufWriter<fs::File>) -> T) -> T {
let mut guard = match self.file.lock() {
Ok(guard) => guard,
Err(err) => err.into_inner(),
Expand Down Expand Up @@ -83,16 +89,15 @@ impl Log for SimpleLogger {

fn get_logger() -> &'static SimpleLogger {
static LOGGER: OnceLock<SimpleLogger> = 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(),
),
)
})
}

Expand Down

0 comments on commit 00e1bea

Please sign in to comment.