Register logging module and use in existing script

This commit is contained in:
Jarrod Doyle 2024-03-08 11:47:15 +00:00
parent 6abe5b467e
commit 58064c4fc1
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 46 additions and 2 deletions

View File

@ -9,6 +9,6 @@ impl FrameCounter {
pub fn update(self) {
self.frame += 1;
println(`Frame: ${self.frame}`);
log::info(`Frame: ${self.frame}`);
}
}

41
src/api.rs Normal file
View File

@ -0,0 +1,41 @@
pub mod log {
use anyhow::Result;
use rune::Module;
#[rune::module(::log)]
pub fn module() -> Result<Module> {
let mut module = Module::from_meta(self::module_meta)?.with_unique("log");
module.function_meta(info)?;
module.function_meta(warn)?;
module.function_meta(error)?;
module.function_meta(trace)?;
module.function_meta(debug)?;
Ok(module)
}
#[rune::function]
pub fn info(message: &str) {
log::info!("{message}");
}
#[rune::function]
pub fn warn(message: &str) {
log::warn!("{message}");
}
#[rune::function]
pub fn error(message: &str) {
log::error!("{message}");
}
#[rune::function]
pub fn trace(message: &str) {
log::trace!("{message}");
}
#[rune::function]
pub fn debug(message: &str) {
log::debug!("{message}");
}
}

View File

@ -1,3 +1,4 @@
mod api;
mod gfx;
mod input;
@ -33,7 +34,9 @@ pub fn main() -> Result<()> {
pub fn run(event_loop: EventLoop<()>, mut context: Context) -> Result<()> {
// !HACK: Temporrary scripting engine setup
let source_dir = format!("{}/scripts", env!("CARGO_MANIFEST_DIR"));
let rune_context = rune::Context::with_default_modules()?;
let mut rune_context = rune::Context::with_default_modules()?;
rune_context.install(api::log::module()?)?;
let runtime = Arc::new(rune_context.runtime()?);
let mut sources = Sources::new();
sources.insert(Source::from_path(format!("{source_dir}/frame_counter.rn"))?)?;