added log wrapper

This commit is contained in:
LunarAkai 2025-07-28 19:50:49 +02:00
commit 237dd614a2
15 changed files with 213 additions and 59 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target
*.log

53
Cargo.lock generated
View file

@ -221,6 +221,16 @@ dependencies = [
"libc",
]
[[package]]
name = "colored"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
dependencies = [
"lazy_static",
"windows-sys 0.52.0",
]
[[package]]
name = "combine"
version = "4.6.7"
@ -345,6 +355,16 @@ dependencies = [
"windows-sys 0.60.2",
]
[[package]]
name = "fern"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4316185f709b23713e41e3195f90edef7fb00c3ed4adc79769cf09cc762a3b29"
dependencies = [
"colored",
"log",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -512,6 +532,12 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
[[package]]
name = "humantime"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b112acc8b3adf4b107a8ec20977da0273a8c386765a3ec0229bd500a1443f9f"
[[package]]
name = "indexmap"
version = "2.10.0"
@ -570,6 +596,12 @@ version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.174"
@ -652,7 +684,23 @@ dependencies = [
name = "moonhare_engine"
version = "0.1.0"
dependencies = [
"fern",
"glium",
"log",
"winit",
]
[[package]]
name = "moonhare_graphics"
version = "0.1.0"
[[package]]
name = "moonhare_log"
version = "0.1.0"
dependencies = [
"fern",
"humantime",
"log",
]
[[package]]
@ -1030,6 +1078,7 @@ version = "0.1.0"
dependencies = [
"glium",
"moonhare_engine",
"moonhare_log",
]
[[package]]
@ -1877,9 +1926,9 @@ checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486"
[[package]]
name = "winit"
version = "0.30.11"
version = "0.30.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4409c10174df8779dc29a4788cac85ed84024ccbc1743b776b21a520ee1aaf4"
checksum = "c66d4b9ed69c4009f6321f762d6e61ad8a2389cd431b97cb1e146812e9e6c732"
dependencies = [
"ahash",
"android-activity",

View file

@ -2,6 +2,7 @@
resolver = "3"
members = [
"moonhare_engine",
"moonhare_graphics", "moonhare_log",
"playground"
]

View file

@ -4,4 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
glium = "0.36.0"
glium = "0.36.0"
log = "0.4"
fern = "0.7"
winit = "0.30.12"

View file

@ -1,30 +1,28 @@
use std::{ops::{ControlFlow, DerefMut}, sync::Mutex};
use glium::{glutin::surface::WindowSurface, winit::{self, event::{self, WindowEvent}, event_loop::{self, EventLoop}, window::Window}};
use glium::{glutin::{api::egl::surface::Surface, surface::WindowSurface}, winit::{self, event::{self, WindowEvent}, event_loop::{self, EventLoop}, window::Window}, Display};
use winit::application::ApplicationHandler;
use crate::{game_plugin::GamePlugin, window::winit_window::WinitWindow, ENGINE_NAME};
use crate::{core::game, game_plugin::GamePlugin, window::winit_window::WinitWindow, ENGINE_NAME};
pub struct Game {
pub running: bool,
pub game_plugin: Option<Box<dyn GamePlugin>>,
pub window: Window,
pub display: glium::Display<WindowSurface>,
pub event_loop: EventLoop<()>
pub window: WinitWindow,
}
impl Game {
pub fn new() -> Self {
let _event_loop: EventLoop<()> = EventLoop::new().unwrap();
let _window = WinitWindow::construct_window(&_event_loop);
let mut game_window = WinitWindow::default();
_event_loop.run_app(&mut game_window);
Game {
running: true,
game_plugin: None,
window: _window.0,
display: _window.1,
event_loop: _event_loop,
window: game_window,
}
}
@ -33,8 +31,6 @@ impl Game {
}
pub fn init(&mut self) {
self.window.set_fullscreen(None);
self.window.set_decorations(true);
if let Some(ref mut game_plugin) = self.game_plugin {
game_plugin.init();
} else {
@ -49,13 +45,13 @@ impl Game {
}
pub fn render(&mut self) {
let mut target = self.display.draw();
//let mut target = display.draw();
if let Some(ref mut game_plugin) = self.game_plugin {
game_plugin.render(&mut target);
}
//if let Some(ref mut game_plugin) = self.game_plugin {
// game_plugin.render(&mut target);
//}
target.finish().unwrap();
//target.finish().unwrap();
}
pub fn cleanup(&mut self) {
@ -72,36 +68,4 @@ impl Game {
self.cleanup();
}
pub fn get_display(&self) -> &glium::Display<WindowSurface>{
&self.display
}
pub fn get_window(self) -> Window {
self.window
}
pub fn handle_events(&mut self, event: winit::event::Event<()>) {
if let Some(ref mut game_plugin) = self.game_plugin {
game_plugin.handle_events();
}
match event {
glium::winit::event::Event::WindowEvent { event, .. } => match event {
glium::winit::event::WindowEvent::CloseRequested => {
},
glium::winit::event::WindowEvent::Resized(window_size) => {
},
glium::winit::event::WindowEvent::RedrawRequested => {
},
_ => (),
}
glium::winit::event::Event::AboutToWait => {
},
_ => (),
}
}
}

View file

@ -1,15 +1,19 @@
use glium::{backend::glutin::SimpleWindowBuilder, glutin::{display::GetGlDisplay, surface::WindowSurface}, winit::{self, dpi::LogicalSize, event_loop::{ActiveEventLoop, EventLoop}, raw_window_handle::HasDisplayHandle, window::{Window, WindowAttributes}}, Display};
use glium::{backend::{glutin::SimpleWindowBuilder, Context}, glutin::{display::GetGlDisplay, prelude::GlContext, surface::WindowSurface}, winit::{self, dpi::LogicalSize, event_loop::{ActiveEventLoop, EventLoop}, raw_window_handle::HasDisplayHandle, window::{Window, WindowAttributes}}, Display};
use winit::application::ApplicationHandler;
use crate::window::window_config::WindowConfig;
use crate::ENGINE_NAME;
pub struct WinitWindow {}
#[derive(Default)]
pub struct WinitWindow {
pub window: Option<Window>,
}
impl WinitWindow {
/// constructs a new winit window
pub fn construct_window(event_loop: &EventLoop<()>) -> (Window, Display<WindowSurface>) {
impl ApplicationHandler for WinitWindow {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let config = WindowConfig::default();
let mut window_attributes = WindowAttributes::default();
@ -25,8 +29,18 @@ impl WinitWindow {
window_attributes = window_attributes.with_visible(true);
window_attributes = winit::platform::wayland::WindowAttributesExtWayland::with_name(window_attributes, ENGINE_NAME, "");
SimpleWindowBuilder::new().set_window_builder(window_attributes).build(event_loop)
self.window = Some(event_loop.create_window(window_attributes).unwrap());
}
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
window_id: winit::window::WindowId,
event: winit::event::WindowEvent,
) {
todo!()
}
}

View file

@ -0,0 +1,7 @@
[package]
name = "moonhare_graphics"
edition = "2024"
version.workspace = true
authors.workspace = true
[dependencies]

View file

View file

@ -0,0 +1,4 @@
/// Crate for providing an abstraction layer over different graphics APIs
pub mod graphics_server;
pub mod shader;

View file

@ -0,0 +1,4 @@
pub enum ShaderType {
Vertex,
Fragment,
}

10
moonhare_log/Cargo.toml Normal file
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 = "*"

86
moonhare_log/src/lib.rs Normal file
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 mh_info<T: Display>(arg: T) {
log::info!("{}", arg);
}
pub fn mh_warn<T: Display>(arg: T) {
log::warn!("{}", arg);
}
pub fn mh_debug<T: Display>(arg: T) {
log::debug!("{}", arg);
}
pub fn mh_trace<T: Display>(arg: T) {
log::trace!("{}", arg);
}
pub fn mh_error<T: Display>(arg: T) {
log::error!("{}", arg);
}

View file

@ -5,4 +5,5 @@ edition = "2024"
[dependencies]
moonhare_engine = { path = "../moonhare_engine" }
moonhare_log = { path = "../moonhare_log" }
glium = "0.36.0"

View file

@ -3,6 +3,7 @@ use std::fs::read_to_string;
use glium::{index::NoIndices, Frame, Program, VertexBuffer};
use glium::{uniform, Surface};
use moonhare_engine::{core::game::Game, game_plugin::GamePlugin, vertex::Vertex};
use moonhare_log::{mh_debug, mh_error, mh_info, mh_trace, mh_warn};
struct PlaygroundGame {
t: f32,
@ -65,7 +66,14 @@ impl GamePlugin for PlaygroundGame {
fn main() {
let _ = moonhare_log::configere_logger();
mh_info("Blub");
mh_debug("blub");
mh_warn("blug");
mh_trace("trace");
mh_error("error");
println!("Blaa");
/*
let mut game = Game::new();
game.register_plugin(Box::new(PlaygroundGame{ t: 0.0, shape: Default::default(), vertex_buffer: None, indices: None, program: None }));
game.init();
@ -131,4 +139,6 @@ fn main() {
game.register_plugin(Box::new(pg_game));
game.run();
*/
}