From 9b07d1281f964e80de388c1bffee29bef18a33f7 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 30 Aug 2025 03:00:18 +0200 Subject: [PATCH] Better logging with plugin fix --- src/lib.rs | 5 +---- src/loader.rs | 1 + src/macros.rs | 10 +++++----- src/main.rs | 4 ++-- src/vfs.rs | 34 +++++++++++++++++++--------------- test-plugin/.gitignore | 2 ++ test-plugin/Cargo.lock | 7 +++++++ test-plugin/src/lib.rs | 8 ++++++-- test-plugin/src/main.rs | 15 ++++++++++----- 9 files changed, 53 insertions(+), 33 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8f554b5..838378f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ pub mod vfs; pub use anyhow::Result; use crate::plugin::{Plugin, PluginInstance}; +pub use once_cell; #[derive(serde::Serialize, serde::Deserialize)] pub struct ServerConfig { @@ -71,10 +72,6 @@ impl Server { let listener = TcpListener::bind(format!("0.0.0.0:{}", self.config.port)); self.logger .info(format!("Server listening 0.0.0.0:{}", self.config.port)); - self.logger - .warn(format!("Server listening 0.0.0.0:{}", self.config.port)); - self.logger - .error(format!("Server listening 0.0.0.0:{}", self.config.port)); for stream in listener?.incoming() { match stream { diff --git a/src/loader.rs b/src/loader.rs index ab3f092..84d2279 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -28,6 +28,7 @@ pub fn load_plugin(wasm_path: &Path) -> anyhow::Result { } pub fn load_plugins(arr: &mut Vec, path: &Path) -> crate::Result<()> { + LOGGER.info("Loading plugins"); vfs::dir(path)?; if path.is_dir() { for entry in std::fs::read_dir(path)? { diff --git a/src/macros.rs b/src/macros.rs index 4fd27ea..205249a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -17,17 +17,17 @@ macro_rules! export_plugin { #[macro_export] macro_rules! logger { (const $i:ident $name:expr) => { - const $i: once_cell::sync::Lazy<$crate::logger::Logger> = - once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name)); + const $i: $crate::once_cell::sync::Lazy<$crate::logger::Logger> = + $crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name)); }; ($i:ident $name:expr) => { - const $i: once_cell::sync::Lazy<$crate::logger::Logger> = - once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name)); + const $i: $crate::once_cell::sync::Lazy<$crate::logger::Logger> = + $crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name)); }; (const $name:expr) => { - once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name)) + $crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name)) }; ($name:expr) => { diff --git a/src/main.rs b/src/main.rs index 028515b..0384ba5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,8 @@ use std::path::PathBuf; use voxa_server::{ServerConfig, vfs}; -fn main() -> anyhow::Result<()> { - let root = PathBuf::new(); +fn main() -> voxa_server::Result<()> { + let root = PathBuf::from("./"); let config: ServerConfig = vfs::read_config(&root.join("config.json"))?; let mut server = config.build(&root); server.run()?; diff --git a/src/vfs.rs b/src/vfs.rs index ebafa88..0b62a96 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -1,9 +1,16 @@ use std::{fs, path::Path}; +logger! { + const LOGGER "File" +} + use serde::{Deserialize, Serialize}; +use crate::logger; + pub fn dir(path: &Path) -> crate::Result<()> { if !path.exists() { + LOGGER.info(format!("Directory {path:?} does not exist, creating it")); fs::create_dir_all(path)?; } Ok(()) @@ -11,11 +18,10 @@ pub fn dir(path: &Path) -> crate::Result<()> { pub fn read(path: &Path, default_content: &str) -> crate::Result { if !path.exists() { - dir(path.parent().ok_or(std::io::Error::new( - std::io::ErrorKind::InvalidFilename, - "File doesn't have a parent assigned, example: `config/config.json`", - ))?)?; - fs::write(path, default_content)?; + LOGGER.info(format!( + "File {path:?} does not exist, creating it with default contents" + )); + write(path, default_content)?; return Ok(default_content.to_string()); } @@ -24,11 +30,10 @@ pub fn read(path: &Path, default_content: &str) -> crate::Result { pub fn read_bytes<'a>(path: &Path, default_content: Vec) -> crate::Result> { if !path.exists() { - dir(path.parent().ok_or(std::io::Error::new( - std::io::ErrorKind::InvalidFilename, - "File doesn't have a parent assigned, example: `config/config.json`", - ))?)?; - fs::write(path, &default_content)?; + LOGGER.info(format!( + "File {path:?} does not exist, creating it with default contents" + )); + write_bytes(path, &default_content)?; return Ok(default_content); } @@ -39,12 +44,11 @@ pub fn read_config Deserialize<'de>>( path: &Path, ) -> crate::Result { if !path.exists() { - dir(path.parent().ok_or(std::io::Error::new( - std::io::ErrorKind::InvalidFilename, - "File doesn't have a parent assigned, example: `config/config.json`", - ))?)?; + LOGGER.info(format!( + "File {path:?} does not exist, creating it with default contents" + )); let default = T::default(); - fs::write(path, &serde_json::to_string_pretty(&default)?)?; + write_config(path, &default)?; return Ok(default); } diff --git a/test-plugin/.gitignore b/test-plugin/.gitignore index ea8c4bf..e25729a 100644 --- a/test-plugin/.gitignore +++ b/test-plugin/.gitignore @@ -1 +1,3 @@ /target +/plugins +config.json \ No newline at end of file diff --git a/test-plugin/Cargo.lock b/test-plugin/Cargo.lock index 7040a56..468a1c0 100644 --- a/test-plugin/Cargo.lock +++ b/test-plugin/Cargo.lock @@ -20,6 +20,12 @@ version = "2.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + [[package]] name = "proc-macro2" version = "1.0.101" @@ -105,6 +111,7 @@ name = "voxa-server" version = "0.1.0" dependencies = [ "anyhow", + "once_cell", "serde", "serde_json", ] diff --git a/test-plugin/src/lib.rs b/test-plugin/src/lib.rs index 0933ab3..0c746d9 100644 --- a/test-plugin/src/lib.rs +++ b/test-plugin/src/lib.rs @@ -1,11 +1,15 @@ -use voxa_server::{export_plugin, plugin::Plugin}; +use voxa_server::{export_plugin, logger, plugin::Plugin}; + +logger! { + const LOGGER "My Plugin" +} #[derive(Default)] pub struct MyPlugin; impl Plugin for MyPlugin { fn init(&mut self) { - println!("MyPlugin initialized!"); + LOGGER.info("MyPlugin initialized!"); } } diff --git a/test-plugin/src/main.rs b/test-plugin/src/main.rs index 34e7f1b..29eb6b1 100644 --- a/test-plugin/src/main.rs +++ b/test-plugin/src/main.rs @@ -1,9 +1,14 @@ use std::path::PathBuf; -use voxa_server::plugin::PluginInstance; + +use voxa_server::{ServerConfig, plugin::PluginInstance, vfs}; fn main() -> voxa_server::Result<()> { - let root = PathBuf::from("../"); - let config = voxa_server::ServerConfig::default(); - let plugin = test_plugin::MyPlugin::default(); - config.start_with(vec![PluginInstance::Static(Box::new(plugin))], &root) + let root = PathBuf::from("./"); + let config: ServerConfig = vfs::read_config(&root.join("config.json"))?; + let mut server = config.build(&root); + server.add_plugin(PluginInstance::Static(Box::new( + test_plugin::MyPlugin::default(), + ))); + server.run()?; + Ok(()) }