From 2fd692c24d298f79a6ecf6a0d1cbea07e9b01859 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 15 Jan 2023 01:41:29 +0100 Subject: [PATCH] logging --- Cargo.toml | 2 ++ src/game.rs | 6 ++++- src/game/assets/textures.rs | 2 ++ src/logging.rs | 45 +++++++++++++++++++++++++++++++++++++ src/main.rs | 2 ++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/logging.rs diff --git a/Cargo.toml b/Cargo.toml index 55895b7..023098b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" glium = "0.32" glutin = "*" image = { version = "0.24", default_features = false, features = ["png"] } +log = "0.4" +env_logger = "0.10" diff --git a/src/game.rs b/src/game.rs index 5afd196..0325673 100644 --- a/src/game.rs +++ b/src/game.rs @@ -11,10 +11,14 @@ use assets::Assets; use display::init_display; pub fn run() { + log::info!("starting up"); let event_loop = EventLoop::new(); + log::info!("initializing display"); let display = init_display(&event_loop); + log::info!("loading assets"); let assets = Assets::load_all_sync(&display); - + log::info!("game loaded"); + event_loop.run(move |ev, _, control_flow| { match ev { Event::WindowEvent { event, .. } => match event { diff --git a/src/game/assets/textures.rs b/src/game/assets/textures.rs index b1e846c..c07e5c5 100644 --- a/src/game/assets/textures.rs +++ b/src/game/assets/textures.rs @@ -2,6 +2,8 @@ use std::{fs, io}; use glium::texture::{RawImage2d, SrgbTexture2d}; fn load_png(file_path: &str, display: &glium::Display) -> SrgbTexture2d { + log::info!("loading texture {}", file_path); + //Load file let data = fs::read(file_path) .expect(&format!("Failed to load texture: {}", file_path)); diff --git a/src/logging.rs b/src/logging.rs new file mode 100644 index 0000000..29ff50f --- /dev/null +++ b/src/logging.rs @@ -0,0 +1,45 @@ +//! Custom env_logger options and styling + +use env_logger::{fmt::Color, Builder, Env}; +use log::Level; +use std::io::Write; + +pub fn init() { + let mut env = Env::default(); + if cfg!(debug_assertions) { + env = env.filter_or("RUST_LOG", "info"); + } + Builder::from_env(env) + .format(|buf, record| { + let mut level_style = buf.style(); + level_style.set_color(match record.level() { + Level::Error => Color::Red, + Level::Warn => Color::Yellow, + _ => Color::Blue + }).set_bold(true); + + let mut location_style = buf.style(); + location_style.set_bold(true); + location_style.set_dimmed(true); + + let mut location_line_style = buf.style(); + location_line_style.set_dimmed(true); + + writeln!( + buf, + "{} {:<50}\t{}{}{}", + level_style.value(match record.level() { + Level::Error => "[e]", + Level::Warn => "[w]", + Level::Info => "[i]", + Level::Debug => "[d]", + Level::Trace => "[t]", + }), + format!("{}", record.args()), + location_style.value(record.module_path().unwrap_or("")), + location_line_style.value(" :"), + location_line_style.value(record.line().unwrap_or(0)) + ) + }) + .init(); +} diff --git a/src/main.rs b/src/main.rs index 0680567..7f25654 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ mod game; +mod logging; fn main() { + logging::init(); game::run(); }