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; +