trying to eat the spaghetti (make it less)

This commit is contained in:
LunarAkai 2025-07-29 11:59:34 +02:00
commit 4ad7d2ebd0
42 changed files with 189 additions and 668 deletions

View file

@ -0,0 +1,10 @@
[package]
name = "moonhare_log"
edition = "2024"
version.workspace = true
authors.workspace = true
[dependencies]
log = "*"
fern = { version = "0.7", features = ["colored"] }
humantime = "*"

View file

@ -0,0 +1,2 @@
# MoonhareLog
This crate provides a Wrapper around the log and fern crates for logging in the Moonhare Game Engine.

View file

@ -0,0 +1,86 @@
/// Wrapper around `log` and `fern` crates
///
use std::{fmt::Display, io, time::SystemTime};
/// Configures the Log Output Settings
pub fn configere_logger() -> Result<(), fern::InitError>{
let base_config = fern::Dispatch::new();
// configure colors for the whole line
let colors_line = fern::colors::ColoredLevelConfig::new()
.error(fern::colors::Color::Red)
.warn(fern::colors::Color::Yellow)
// we actually don't need to specify the color for debug and info, they are white by default
.info(fern::colors::Color::TrueColor { r: 85, g: 85, b: 85 })
.debug(fern::colors::Color::White)
// depending on the terminals color scheme, this is the same as the background color
.trace(fern::colors::Color::Black);
// Separate file config so we can include year, month and day in file logs
let file_config = fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"[{} {} {}] {}",
humantime::format_rfc3339_seconds(SystemTime::now()),
record.level(),
record.target(),
message
))
})
.chain(fern::log_file("moonhare_engine.log")?);
let stdout_config = fern::Dispatch::new()
.format(move |out, message, record| {
// special format for debug messages coming from our own crate.
if record.level() > log::LevelFilter::Info && record.target() == "cmd_program" {
out.finish(format_args!(
"DEBUG @ {}: {}",
humantime::format_rfc3339_seconds(SystemTime::now()),
message
))
} else {
out.finish(format_args!(
"{color_line}[{date} {level} {target}] {message}\x1B[0m",
color_line = format_args!(
"\x1B[{}m",
colors_line.get_color(&record.level()).to_fg_str()
),
date = humantime::format_rfc3339_seconds(SystemTime::now()),
level = record.level(),
target = record.target(),
message = message
))
}
})
.chain(io::stdout());
base_config
.chain(file_config)
.chain(stdout_config)
.apply()?;
Ok(())
}
pub fn info<T: Display>(arg: T) {
log::info!("{}", arg);
}
pub fn warn<T: Display>(arg: T) {
log::warn!("{}", arg);
}
pub fn debug<T: Display>(arg: T) {
log::debug!("{}", arg);
}
pub fn trace<T: Display>(arg: T) {
log::trace!("{}", arg);
}
pub fn error<T: Display>(arg: T) {
log::error!("{}", arg);
}