Better logging with plugin fix

This commit is contained in:
2025-08-30 03:00:18 +02:00
parent 19c102a7e8
commit 9b07d1281f
9 changed files with 53 additions and 33 deletions
+1 -4
View File
@@ -13,6 +13,7 @@ pub mod vfs;
pub use anyhow::Result; pub use anyhow::Result;
use crate::plugin::{Plugin, PluginInstance}; use crate::plugin::{Plugin, PluginInstance};
pub use once_cell;
#[derive(serde::Serialize, serde::Deserialize)] #[derive(serde::Serialize, serde::Deserialize)]
pub struct ServerConfig { pub struct ServerConfig {
@@ -71,10 +72,6 @@ impl Server {
let listener = TcpListener::bind(format!("0.0.0.0:{}", self.config.port)); let listener = TcpListener::bind(format!("0.0.0.0:{}", self.config.port));
self.logger self.logger
.info(format!("Server listening 0.0.0.0:{}", self.config.port)); .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() { for stream in listener?.incoming() {
match stream { match stream {
+1
View File
@@ -28,6 +28,7 @@ pub fn load_plugin(wasm_path: &Path) -> anyhow::Result<PluginInstance> {
} }
pub fn load_plugins(arr: &mut Vec<PluginInstance>, path: &Path) -> crate::Result<()> { pub fn load_plugins(arr: &mut Vec<PluginInstance>, path: &Path) -> crate::Result<()> {
LOGGER.info("Loading plugins");
vfs::dir(path)?; vfs::dir(path)?;
if path.is_dir() { if path.is_dir() {
for entry in std::fs::read_dir(path)? { for entry in std::fs::read_dir(path)? {
+5 -5
View File
@@ -17,17 +17,17 @@ macro_rules! export_plugin {
#[macro_export] #[macro_export]
macro_rules! logger { macro_rules! logger {
(const $i:ident $name:expr) => { (const $i:ident $name:expr) => {
const $i: once_cell::sync::Lazy<$crate::logger::Logger> = const $i: $crate::once_cell::sync::Lazy<$crate::logger::Logger> =
once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name)); $crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name));
}; };
($i:ident $name:expr) => { ($i:ident $name:expr) => {
const $i: once_cell::sync::Lazy<$crate::logger::Logger> = const $i: $crate::once_cell::sync::Lazy<$crate::logger::Logger> =
once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name)); $crate::once_cell::sync::Lazy::new(|| $crate::logger::Logger::new($name));
}; };
(const $name:expr) => { (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) => { ($name:expr) => {
+2 -2
View File
@@ -2,8 +2,8 @@ use std::path::PathBuf;
use voxa_server::{ServerConfig, vfs}; use voxa_server::{ServerConfig, vfs};
fn main() -> anyhow::Result<()> { fn main() -> voxa_server::Result<()> {
let root = PathBuf::new(); let root = PathBuf::from("./");
let config: ServerConfig = vfs::read_config(&root.join("config.json"))?; let config: ServerConfig = vfs::read_config(&root.join("config.json"))?;
let mut server = config.build(&root); let mut server = config.build(&root);
server.run()?; server.run()?;
+19 -15
View File
@@ -1,9 +1,16 @@
use std::{fs, path::Path}; use std::{fs, path::Path};
logger! {
const LOGGER "File"
}
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::logger;
pub fn dir(path: &Path) -> crate::Result<()> { pub fn dir(path: &Path) -> crate::Result<()> {
if !path.exists() { if !path.exists() {
LOGGER.info(format!("Directory {path:?} does not exist, creating it"));
fs::create_dir_all(path)?; fs::create_dir_all(path)?;
} }
Ok(()) Ok(())
@@ -11,11 +18,10 @@ pub fn dir(path: &Path) -> crate::Result<()> {
pub fn read(path: &Path, default_content: &str) -> crate::Result<String> { pub fn read(path: &Path, default_content: &str) -> crate::Result<String> {
if !path.exists() { if !path.exists() {
dir(path.parent().ok_or(std::io::Error::new( LOGGER.info(format!(
std::io::ErrorKind::InvalidFilename, "File {path:?} does not exist, creating it with default contents"
"File doesn't have a parent assigned, example: `config/config.json`", ));
))?)?; write(path, default_content)?;
fs::write(path, default_content)?;
return Ok(default_content.to_string()); return Ok(default_content.to_string());
} }
@@ -24,11 +30,10 @@ pub fn read(path: &Path, default_content: &str) -> crate::Result<String> {
pub fn read_bytes<'a>(path: &Path, default_content: Vec<u8>) -> crate::Result<Vec<u8>> { pub fn read_bytes<'a>(path: &Path, default_content: Vec<u8>) -> crate::Result<Vec<u8>> {
if !path.exists() { if !path.exists() {
dir(path.parent().ok_or(std::io::Error::new( LOGGER.info(format!(
std::io::ErrorKind::InvalidFilename, "File {path:?} does not exist, creating it with default contents"
"File doesn't have a parent assigned, example: `config/config.json`", ));
))?)?; write_bytes(path, &default_content)?;
fs::write(path, &default_content)?;
return Ok(default_content); return Ok(default_content);
} }
@@ -39,12 +44,11 @@ pub fn read_config<T: Default + Serialize + for<'de> Deserialize<'de>>(
path: &Path, path: &Path,
) -> crate::Result<T> { ) -> crate::Result<T> {
if !path.exists() { if !path.exists() {
dir(path.parent().ok_or(std::io::Error::new( LOGGER.info(format!(
std::io::ErrorKind::InvalidFilename, "File {path:?} does not exist, creating it with default contents"
"File doesn't have a parent assigned, example: `config/config.json`", ));
))?)?;
let default = T::default(); let default = T::default();
fs::write(path, &serde_json::to_string_pretty(&default)?)?; write_config(path, &default)?;
return Ok(default); return Ok(default);
} }
+2
View File
@@ -1 +1,3 @@
/target /target
/plugins
config.json
+7
View File
@@ -20,6 +20,12 @@ version = "2.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.101" version = "1.0.101"
@@ -105,6 +111,7 @@ name = "voxa-server"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"once_cell",
"serde", "serde",
"serde_json", "serde_json",
] ]
+6 -2
View File
@@ -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)] #[derive(Default)]
pub struct MyPlugin; pub struct MyPlugin;
impl Plugin for MyPlugin { impl Plugin for MyPlugin {
fn init(&mut self) { fn init(&mut self) {
println!("MyPlugin initialized!"); LOGGER.info("MyPlugin initialized!");
} }
} }
+10 -5
View File
@@ -1,9 +1,14 @@
use std::path::PathBuf; use std::path::PathBuf;
use voxa_server::plugin::PluginInstance;
use voxa_server::{ServerConfig, plugin::PluginInstance, vfs};
fn main() -> voxa_server::Result<()> { fn main() -> voxa_server::Result<()> {
let root = PathBuf::from("../"); let root = PathBuf::from("./");
let config = voxa_server::ServerConfig::default(); let config: ServerConfig = vfs::read_config(&root.join("config.json"))?;
let plugin = test_plugin::MyPlugin::default(); let mut server = config.build(&root);
config.start_with(vec![PluginInstance::Static(Box::new(plugin))], &root) server.add_plugin(PluginInstance::Static(Box::new(
test_plugin::MyPlugin::default(),
)));
server.run()?;
Ok(())
} }