From d20220b57e03e991309d49bd3c7a6f29abc8d68d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 24 Mar 2016 11:52:05 +0100 Subject: [PATCH 1/3] Initial import --- libimagstorestdhook/Cargo.toml | 6 ++++++ libimagstorestdhook/src/lib.rs | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 libimagstorestdhook/Cargo.toml create mode 100644 libimagstorestdhook/src/lib.rs diff --git a/libimagstorestdhook/Cargo.toml b/libimagstorestdhook/Cargo.toml new file mode 100644 index 00000000..887f5baa --- /dev/null +++ b/libimagstorestdhook/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "libimagstorestdhook" +version = "0.1.0" +authors = ["Matthias Beyer "] + +[dependencies] diff --git a/libimagstorestdhook/src/lib.rs b/libimagstorestdhook/src/lib.rs new file mode 100644 index 00000000..cf4da5ad --- /dev/null +++ b/libimagstorestdhook/src/lib.rs @@ -0,0 +1,6 @@ +#[cfg(test)] +mod test { + #[test] + fn it_works() { + } +} From 6e74145739d167ac381616f00c2b06b1ff3c9849 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 24 Mar 2016 11:52:43 +0100 Subject: [PATCH 2/3] Add dependencies --- libimagstorestdhook/Cargo.toml | 6 ++++++ libimagstorestdhook/src/lib.rs | 11 +++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/libimagstorestdhook/Cargo.toml b/libimagstorestdhook/Cargo.toml index 887f5baa..35c57363 100644 --- a/libimagstorestdhook/Cargo.toml +++ b/libimagstorestdhook/Cargo.toml @@ -4,3 +4,9 @@ version = "0.1.0" authors = ["Matthias Beyer "] [dependencies] +toml = "0.1.25" +log = "0.3.5" + +[dependencies.libimagstore] +path = "../libimagstore" + diff --git a/libimagstorestdhook/src/lib.rs b/libimagstorestdhook/src/lib.rs index cf4da5ad..880c1231 100644 --- a/libimagstorestdhook/src/lib.rs +++ b/libimagstorestdhook/src/lib.rs @@ -1,6 +1,5 @@ -#[cfg(test)] -mod test { - #[test] - fn it_works() { - } -} +#[macro_use] extern crate log; +extern crate toml; + +extern crate libimagstore; + From 7f1a671e4da102c9b6a28945f60deb389b1ba10f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 24 Mar 2016 12:09:45 +0100 Subject: [PATCH 3/3] Add debug hook --- libimagrt/Cargo.toml | 3 + libimagrt/src/lib.rs | 1 + libimagrt/src/runtime.rs | 44 ++++++++++-- libimagstorestdhook/src/debug.rs | 113 +++++++++++++++++++++++++++++++ libimagstorestdhook/src/lib.rs | 2 + 5 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 libimagstorestdhook/src/debug.rs diff --git a/libimagrt/Cargo.toml b/libimagrt/Cargo.toml index 2e47aa34..eb626e55 100644 --- a/libimagrt/Cargo.toml +++ b/libimagrt/Cargo.toml @@ -14,6 +14,9 @@ tempfile = "2.1.1" [dependencies.libimagstore] path = "../libimagstore" +[dependencies.libimagstorestdhook] +path = "../libimagstorestdhook" + [dependencies.libimagutil] path = "../libimagutil" diff --git a/libimagrt/src/lib.rs b/libimagrt/src/lib.rs index fdcd3b40..d2660808 100644 --- a/libimagrt/src/lib.rs +++ b/libimagrt/src/lib.rs @@ -21,6 +21,7 @@ extern crate clap; extern crate toml; extern crate libimagstore; +extern crate libimagstorestdhook; extern crate libimagutil; mod configuration; diff --git a/libimagrt/src/runtime.rs b/libimagrt/src/runtime.rs index 746bf100..ecc5c227 100644 --- a/libimagrt/src/runtime.rs +++ b/libimagrt/src/runtime.rs @@ -1,6 +1,8 @@ use std::path::PathBuf; use std::process::Command; use std::env; +use std::io::stderr; +use std::io::Write; pub use clap::App; @@ -36,14 +38,14 @@ impl<'a> Runtime<'a> { use std::env; use std::error::Error; + use libimagstore::hook::position::HookPosition; + use libimagstorestdhook::debug::DebugHook; + use libimagutil::trace::trace_error; + use configuration::error::ConfigErrorKind; let matches = cli_spec.get_matches(); - let is_debugging = matches.is_present("debugging"); - let is_verbose = matches.is_present("verbosity"); - - Runtime::init_logger(is_debugging, is_verbose); let rtp : PathBuf = matches.value_of("runtimepath") .map(PathBuf::from) @@ -70,6 +72,7 @@ impl<'a> Runtime<'a> { let cause : Option> = Some(Box::new(e)); return Err(RuntimeError::new(RuntimeErrorKind::Instantiate, cause)); } else { + trace_error(&e); None } } else { @@ -79,11 +82,39 @@ impl<'a> Runtime<'a> { let store_config = { match &cfg { &Some(ref c) => c.store_config().map(|c| c.clone()), - _ => None + &None => None, } }; - Store::new(storepath, store_config).map(|store| { + if is_debugging { + write!(stderr(), "Config: {:?}\n", cfg); + write!(stderr(), "Store-config: {:?}\n", store_config); + } + + Store::new(storepath, store_config).map(|mut store| { + // If we are debugging, generate hooks for all positions + if is_debugging { + let hooks = vec![ + (DebugHook::new(HookPosition::PreCreate), HookPosition::PreCreate), + (DebugHook::new(HookPosition::PostCreate), HookPosition::PostCreate), + (DebugHook::new(HookPosition::PreRetrieve), HookPosition::PreRetrieve), + (DebugHook::new(HookPosition::PostRetrieve), HookPosition::PostRetrieve), + (DebugHook::new(HookPosition::PreUpdate), HookPosition::PreUpdate), + (DebugHook::new(HookPosition::PostUpdate), HookPosition::PostUpdate), + (DebugHook::new(HookPosition::PreDelete), HookPosition::PreDelete), + (DebugHook::new(HookPosition::PostDelete), HookPosition::PostDelete), + ]; + + // Put all debug hooks into the aspect "debug". + // If it fails, trace the error and warn, but continue. + for (hook, position) in hooks { + if let Err(e) = store.register_hook(position, &String::from("debug"), Box::new(hook)) { + trace_error(&e); + warn!("Registering debug hook with store failed"); + } + } + } + Runtime { cli_matches: matches, configuration: cfg, @@ -229,4 +260,3 @@ impl<'a> Runtime<'a> { } } - diff --git a/libimagstorestdhook/src/debug.rs b/libimagstorestdhook/src/debug.rs new file mode 100644 index 00000000..2a68529e --- /dev/null +++ b/libimagstorestdhook/src/debug.rs @@ -0,0 +1,113 @@ +use toml::Value; + +use libimagstore::hook::Hook; +use libimagstore::hook::accessor::HookDataAccessor; +use libimagstore::hook::accessor::HookDataAccessorProvider; +use libimagstore::hook::position::HookPosition; + +use self::accessor::DebugHookAccessor as DHA; + +#[derive(Debug)] +pub struct DebugHook { + position: HookPosition, + accessor: DHA, +} + +impl DebugHook { + + pub fn new(pos: HookPosition) -> DebugHook { + DebugHook { + position: pos.clone(), + accessor: DHA::new(pos), + } + } + +} + +impl Hook for DebugHook { + + fn name(&self) -> &'static str { + "stdhook_debug" + } + + fn set_config(&mut self, _: &Value) { + () // We are not configurable here. + } + +} + +impl HookDataAccessorProvider for DebugHook { + + fn accessor(&self) -> HookDataAccessor { + use libimagstore::hook::position::HookPosition as HP; + use libimagstore::hook::accessor::HookDataAccessor as HDA; + + match self.position { + HP::PreCreate => HDA::StoreIdAccess(&self.accessor), + HP::PostCreate => HDA::MutableAccess(&self.accessor), + HP::PreRetrieve => HDA::StoreIdAccess(&self.accessor), + HP::PostRetrieve => HDA::MutableAccess(&self.accessor), + HP::PreUpdate => HDA::MutableAccess(&self.accessor), + HP::PostUpdate => HDA::MutableAccess(&self.accessor), + HP::PreDelete => HDA::StoreIdAccess(&self.accessor), + HP::PostDelete => HDA::StoreIdAccess(&self.accessor), + } + } + +} + +pub mod accessor { + use std::ops::Deref; + + use libimagstore::storeid::StoreId; + use libimagstore::store::FileLockEntry; + use libimagstore::hook::result::HookResult; + use libimagstore::hook::accessor::MutableHookDataAccessor; + use libimagstore::hook::accessor::NonMutableHookDataAccessor; + use libimagstore::hook::accessor::StoreIdAccessor; + use libimagstore::hook::position::HookPosition; + + #[derive(Debug)] + pub struct DebugHookAccessor { + position: HookPosition, + } + + impl DebugHookAccessor { + + pub fn new(position: HookPosition) -> DebugHookAccessor { + DebugHookAccessor { + position: position, + } + } + + } + + impl StoreIdAccessor for DebugHookAccessor { + + fn access(&self, id: &StoreId) -> HookResult<()> { + debug!("[DEBUG HOOK]: {:?}", id); + Ok(()) + } + + } + + impl MutableHookDataAccessor for DebugHookAccessor { + + fn access_mut(&self, fle: &mut FileLockEntry) -> HookResult<()> { + debug!("[DEBUG HOOK] {:?}", fle.deref().deref()); + Ok(()) + } + + } + + impl NonMutableHookDataAccessor for DebugHookAccessor { + + fn access(&self, fle: &FileLockEntry) -> HookResult<()> { + debug!("[DEBUG HOOK] {:?}", fle.deref().deref()); + Ok(()) + } + + } + +} + diff --git a/libimagstorestdhook/src/lib.rs b/libimagstorestdhook/src/lib.rs index 880c1231..e9394024 100644 --- a/libimagstorestdhook/src/lib.rs +++ b/libimagstorestdhook/src/lib.rs @@ -3,3 +3,5 @@ extern crate toml; extern crate libimagstore; +pub mod debug; +