Skip to content

Commit

Permalink
Merge pull request #20 from AstroTechies/logging
Browse files Browse the repository at this point in the history
Fix and improve logging.
  • Loading branch information
konsti219 authored Oct 3, 2024
2 parents b0b288d + a599b19 commit 87145c9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
90 changes: 56 additions & 34 deletions astro_modloader/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
use std::fs;
use std::io::prelude::*;
use std::io::{prelude::*, BufWriter};
use std::sync::{Mutex, OnceLock};

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

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

impl SimpleLogger {
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 {
// Ignore log mutex poison
let mut guard = match self.file.lock() {
Ok(guard) => guard,
Err(err) => err.into_inner(),
};

impl log::Log for SimpleLogger {
f(&mut guard)
}
}

impl Log for SimpleLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Trace
}
Expand All @@ -34,58 +56,58 @@ impl log::Log for SimpleLogger {
};

println!(
"{}{:<5} {}:{}{} {}",
"{}{level:<5} {file_path}:{}{} {}",
"[".truecolor(100, 100, 100),
level,
file_path,
record.line().unwrap_or(0),
"]".truecolor(100, 100, 100),
record.args()
);
}

// we need unsafe to write to a global variable
unsafe {
let level = match record.level() {
Level::Error => "ERROR",
Level::Warn => "WARN",
Level::Info => "INFO",
Level::Debug => "DEBUG",
Level::Trace => "TRACE",
};
let level = match record.level() {
Level::Error => "ERROR",
Level::Warn => "WARN",
Level::Info => "INFO",
Level::Debug => "DEBUG",
Level::Trace => "TRACE",
};

self.lock(|file| {
writeln!(
LOG_FILE.as_ref().unwrap(),
"[{:<5} {}:{}] {}",
level,
file_path,
file,
"[{level:<5} {file_path}:{}] {}",
record.line().unwrap_or(0),
record.args()
)
.unwrap();
}
})
.unwrap();
}
}

fn flush(&self) {}
fn flush(&self) {
self.lock(|file| file.flush()).unwrap()
}
}

static LOGGER: SimpleLogger = SimpleLogger;
static mut LOG_FILE: Option<fs::File> = None;

pub fn init() -> Result<(), SetLoggerError> {
// open file
// unsafe because I'm too lazy to properly handle the file
unsafe {
LOG_FILE = Some(
fn get_logger() -> &'static SimpleLogger {
static LOGGER: OnceLock<SimpleLogger> = OnceLock::new();
LOGGER.get_or_init(|| {
SimpleLogger::new(
// Open file
fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open("modloader_log.txt")
.unwrap(),
);
}
)
})
}

pub fn init() -> Result<(), SetLoggerError> {
log::set_logger(get_logger()).map(|()| log::set_max_level(LevelFilter::Trace))
}

log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Trace))
pub fn flush() {
get_logger().flush()
}
2 changes: 2 additions & 0 deletions astro_modloader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,6 @@ fn main() {
let config = AstroGameConfig;

unreal_mod_manager::run(config);

logging::flush();
}

0 comments on commit 87145c9

Please sign in to comment.