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;
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 {
+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<()> {
LOGGER.info("Loading plugins");
vfs::dir(path)?;
if path.is_dir() {
for entry in std::fs::read_dir(path)? {
+5 -5
View File
@@ -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) => {
+2 -2
View File
@@ -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()?;
+19 -15
View File
@@ -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<String> {
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<String> {
pub fn read_bytes<'a>(path: &Path, default_content: Vec<u8>) -> crate::Result<Vec<u8>> {
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<T: Default + Serialize + for<'de> Deserialize<'de>>(
path: &Path,
) -> crate::Result<T> {
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);
}