diff --git a/Cargo.toml b/Cargo.toml index bacb23d..aadb442 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.75" cargo-husky = "1.5.0" chrono = { version = "0.4.31", default-features = false, features = [ "clock", diff --git a/README.md b/README.md index fbaf562..c6491b2 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,39 @@ This is the main repository for the raytracing project. -## `log` crate +## `log` + +https://crates.io/crates/log > Usage: > The basic use of the log crate is through the five logging macros: `error!`, `warn!`, `info!`, `debug!` and `trace!` where `error!` represents the highest-priority log messages and trace! the lowest. The log messages are filtered by configuring the log level to exclude messages with a lower priority. Each of these macros accept format strings similarly to println!. + +## `anyhow` + +https://crates.io/crates/anyhow + +> Usage: +> This library provides anyhow::Error, a trait object based error type for easy idiomatic error handling in Rust applications. + +Example: +```rust +use anyhow::Context; + +fn test() -> anyhow::Result<()> { + // watch out for the question mark operator + + let file = std::fs::File::create("test.txt").context("Failed to create test file")?; + + file.set_len(1234).context("Failed to set file length")?; + + Ok(()) +} +``` + +If this fails the result is a fancy error message: +``` +Error: Failed to create test file + +Caused by: + File exists (os error 17) +``` \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 615e027..8eccc07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::borrow::Cow; +use anyhow::Context; use glium::{ glutin::{ dpi::PhysicalSize, @@ -20,12 +21,8 @@ mod scene; pub type Color = Vector3; -pub fn main() { - std::fs::create_dir_all("logs").expect("Failed to create logs directory"); - let log_file_name = format!( - "logs/trayracer_{}.log", - chrono::Local::now().format("%Y-%m-%d_%H-%M-%S") - ); +pub fn main() -> anyhow::Result<()> { + std::fs::create_dir_all("logs").context("Failed to create logs directory")?; let log_level = if cfg!(debug_assertions) { LevelFilter::Trace @@ -42,10 +39,14 @@ pub fn main() { WriteLogger::new( log_level, Config::default(), - File::create(log_file_name).unwrap(), + File::create(format!( + "logs/trayracer_{}.log", + chrono::Local::now().format("%Y-%m-%d_%H-%M-%S") + )) + .context("Failed to create log file")?, ), ]) - .expect("Failed to initialize logger"); + .context("Failed to initialize logger")?; let window_builder = WindowBuilder::new() .with_title("TrayRacer!") @@ -54,7 +55,8 @@ pub fn main() { let context_builder = ContextBuilder::new(); let event_loop = EventLoop::new(); - let display = glium::Display::new(window_builder, context_builder, &event_loop).unwrap(); + let display = glium::Display::new(window_builder, context_builder, &event_loop) + .context("Failed to create display")?; event_loop.run(move |e, _, c| match e { Event::WindowEvent { @@ -86,6 +88,7 @@ pub fn main() { UncompressedFloatFormat::F32F32F32F32, MipmapsOption::NoMipmap, ) + .context("Failed to create texture") .unwrap(); let mut frame = display.draw(); @@ -106,7 +109,7 @@ pub fn main() { MagnifySamplerFilter::Linear, ); - frame.finish().unwrap(); + frame.finish().context("Failed to finish frame").unwrap(); } Event::WindowEvent { .. } => {} Event::RedrawRequested(_) => {}