Refactor logging system to switch between stdout and file logging
* Refactor logging to switch between stdout and file logging * Use "clear" instead of "tput reset" for unix * Remove redundant comments
This commit is contained in:
parent
18e6d3ad0f
commit
0e83a18f9b
16 changed files with 252 additions and 148 deletions
|
@ -11,4 +11,3 @@ pub fn init_renderer() -> Result<()> {
|
|||
let mut app = App::default();
|
||||
Ok(event_loop.run_app(&mut app)?)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use tokio::task::spawn_blocking;
|
||||
use winit::window::Window;
|
||||
|
||||
pub struct WgpuCtx<'window> {
|
||||
|
@ -46,13 +45,14 @@ impl<'window> WgpuCtx<'window> {
|
|||
surface.configure(&device, &surface_config);
|
||||
|
||||
Ok(WgpuCtx {
|
||||
device: device,
|
||||
queue: queue,
|
||||
surface: surface,
|
||||
surface_config: surface_config,
|
||||
adapter: adapter,
|
||||
device,
|
||||
queue,
|
||||
surface,
|
||||
surface_config,
|
||||
adapter,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn new_blocking(window: Arc<Window>) -> Result<WgpuCtx<'window>> {
|
||||
tokio::task::block_in_place(|| {
|
||||
tokio::runtime::Runtime::new()
|
||||
|
@ -60,41 +60,47 @@ impl<'window> WgpuCtx<'window> {
|
|||
.block_on(async { WgpuCtx::new(window).await })
|
||||
})
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, new_size: (u32, u32)) {
|
||||
let (width, height) = new_size;
|
||||
self.surface_config.width = width.max(1);
|
||||
self.surface_config.height = height.max(1);
|
||||
self.surface.configure(&self.device, &self.surface_config);
|
||||
}
|
||||
|
||||
pub fn draw(&mut self) {
|
||||
let surface_texture = self
|
||||
.surface
|
||||
.get_current_texture()
|
||||
.expect("Failed to get surface texture");
|
||||
let view = surface_texture.texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
{
|
||||
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: None,
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view: &view,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
||||
r: 0.1,
|
||||
g: 0.2,
|
||||
b: 0.3,
|
||||
a: 1.0,
|
||||
}),
|
||||
store: wgpu::StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
});
|
||||
}
|
||||
self.queue.submit(Some(encoder.finish()));
|
||||
surface_texture.present();
|
||||
.surface
|
||||
.get_current_texture()
|
||||
.expect("Failed to get surface texture");
|
||||
let view = surface_texture
|
||||
.texture
|
||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let mut encoder = self
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
|
||||
{
|
||||
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: None,
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view: &view,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
||||
r: 0.1,
|
||||
g: 0.2,
|
||||
b: 0.3,
|
||||
a: 1.0,
|
||||
}),
|
||||
store: wgpu::StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
});
|
||||
}
|
||||
self.queue.submit(Some(encoder.finish()));
|
||||
surface_texture.present();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
pub mod ctx;
|
||||
use ctx::WgpuCtx;
|
||||
use log2::{debug, error, trace};
|
||||
|
||||
use log::{debug, trace};
|
||||
use std::sync::Arc;
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::ActiveEventLoop;
|
||||
use winit::window::{self, Window, WindowId};
|
||||
pub mod ctx;
|
||||
use winit::window::{Window, WindowId};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct App<'window> {
|
||||
window: Option<Arc<Window>>,
|
||||
|
@ -17,9 +18,11 @@ impl ApplicationHandler for App<'_> {
|
|||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
if self.window.is_none() {
|
||||
let win_attr = Window::default_attributes().with_title("Zenyx");
|
||||
let window = Arc::new(event_loop
|
||||
.create_window(win_attr)
|
||||
.expect("create window err."));
|
||||
let window = Arc::new(
|
||||
event_loop
|
||||
.create_window(win_attr)
|
||||
.expect("create window err."),
|
||||
);
|
||||
self.window = Some(window.clone());
|
||||
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
|
||||
self.ctx = Some(wgpu_ctx)
|
||||
|
@ -44,15 +47,15 @@ impl ApplicationHandler for App<'_> {
|
|||
}
|
||||
}
|
||||
WindowEvent::Resized(size) => {
|
||||
if let (Some(wgpu_ctx),Some(window)) = (&mut self.ctx, &self.window) {
|
||||
if let (Some(wgpu_ctx), Some(window)) = (&mut self.ctx, &self.window) {
|
||||
wgpu_ctx.resize(size.into());
|
||||
window.request_redraw();
|
||||
let size_str: String = size.height.to_string() + "x" + &size.width.to_string();
|
||||
//self.window.as_ref().unwrap().set_title(&format!("you reszed the window to {size_str}"));
|
||||
debug!("Window resized to {:?}", size_str);
|
||||
|
||||
let size_str: String = size.height.to_string() + "x" + &size.width.to_string();
|
||||
debug!("Window resized to {:?}", size_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => trace!("Unhandled window event"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,28 @@
|
|||
use super::COMMAND_LIST;
|
||||
use std::process::Command;
|
||||
use log2::{debug, info};
|
||||
use log::debug;
|
||||
|
||||
pub(crate) fn say_hello() {
|
||||
println!("Hello, World!");
|
||||
}
|
||||
|
||||
pub(crate) fn echo(args: Vec<String>) {
|
||||
debug!("{}", args.join(" "));
|
||||
println!("{}", args.join(" "))
|
||||
}
|
||||
|
||||
pub(crate) fn exit() {
|
||||
debug!("Exiting...");
|
||||
println!("Exiting...");
|
||||
std::process::exit(0)
|
||||
}
|
||||
|
||||
pub(crate) fn clear() {
|
||||
info!("Clearing screen..., running command");
|
||||
println!("Clearing screen..., running command");
|
||||
let _result = if cfg!(target_os = "windows") {
|
||||
debug!("target_os is windows");
|
||||
Command::new("cmd").args(["/c", "cls"]).spawn()
|
||||
} else {
|
||||
debug!("target_os is unix");
|
||||
// "clear" or "tput reset"
|
||||
Command::new("tput").arg("reset").spawn()
|
||||
Command::new("clear").spawn()
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ pub mod commands;
|
|||
pub mod repl;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use log2::{debug, error, info};
|
||||
use log::{debug, info};
|
||||
use parking_lot::RwLock;
|
||||
use std::{borrow::Borrow, collections::HashMap, sync::Arc};
|
||||
|
||||
|
@ -26,11 +26,10 @@ pub struct Command {
|
|||
|
||||
impl Command {
|
||||
pub fn execute(&self, args: Option<Vec<String>>) {
|
||||
//debug!("Executing command: {}", self.name);
|
||||
match &self.function {
|
||||
Callable::Simple(f) => {
|
||||
if let Some(args) = args {
|
||||
error!(
|
||||
eprintln!(
|
||||
"Command expected 0 arguments but {} args were given. Ignoring..",
|
||||
args.len()
|
||||
);
|
||||
|
@ -39,7 +38,7 @@ impl Command {
|
|||
}
|
||||
Callable::WithArgs(f) => match args {
|
||||
Some(args) => f(args),
|
||||
None => error!("Command expected arguments but received 0"),
|
||||
None => eprintln!("Command expected arguments but received 0"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +75,7 @@ impl CommandList {
|
|||
func: Callable,
|
||||
arg_count: Option<u8>,
|
||||
) {
|
||||
debug!("Adding command: {}", name);
|
||||
info!("Adding command: {}", name);
|
||||
let mut commands = self.commands.write();
|
||||
|
||||
commands.push(Command {
|
||||
|
@ -88,24 +87,22 @@ impl CommandList {
|
|||
}
|
||||
|
||||
fn add_alias(&self, name: String, alias: String) {
|
||||
//println!("Input alias: {}", alias);
|
||||
if self.aliases.read().contains_key(&alias) {
|
||||
error!("Alias: '{}' already exists", alias);
|
||||
eprintln!("Alias: '{}' already exists", alias);
|
||||
return;
|
||||
}
|
||||
let mut commands = self.commands.write();
|
||||
if let Some(command) = commands.iter_mut().find(|cmd| cmd.name == name) {
|
||||
info!("Adding alias: {} for cmd: {}", alias, command.name);
|
||||
debug!("Adding alias: {} for cmd: {}", alias, command.name);
|
||||
self.aliases
|
||||
.write()
|
||||
.insert(alias.to_string(), name.to_string());
|
||||
} else {
|
||||
error!("Command: '{}' was not found", name);
|
||||
eprintln!("Command: '{}' was not found", name);
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_command(&self, mut name: String, args: Option<Vec<String>>) {
|
||||
//info!("received input command: {}", name);
|
||||
let commands = self.commands.borrow();
|
||||
if self.aliases.read().contains_key(&name) {
|
||||
name = self
|
||||
|
@ -116,7 +113,7 @@ impl CommandList {
|
|||
.1
|
||||
.to_string();
|
||||
|
||||
debug!("changed to {}", name);
|
||||
debug!("changed to {}", &name);
|
||||
}
|
||||
if let Some(command) = commands.read().iter().find(|cmd| cmd.name == name) {
|
||||
match (command.arg_count, args.as_ref()) {
|
||||
|
|
|
@ -2,7 +2,6 @@ use super::{commands, Callable, COMMAND_LIST};
|
|||
use chrono::Local;
|
||||
use reedline::{Prompt, Reedline, Signal};
|
||||
use regex::Regex;
|
||||
use std::{borrow::Borrow, collections::HashMap, sync::Arc};
|
||||
|
||||
fn register_commands() {
|
||||
COMMAND_LIST.add_command(
|
||||
|
@ -42,7 +41,7 @@ fn register_commands() {
|
|||
|
||||
// EXAMPLE
|
||||
// Adding aliases for commands
|
||||
COMMAND_LIST.add_alias("cls".to_string(), "clear".to_string()); // Likely unintended; consider removing or renaming.
|
||||
COMMAND_LIST.add_alias("clear".to_string(), "cls".to_string());
|
||||
}
|
||||
|
||||
struct ZPrompt {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue