Add debug hook

This commit is contained in:
Matthias Beyer 2016-03-24 12:09:45 +01:00
parent 6e74145739
commit 7f1a671e4d
5 changed files with 156 additions and 7 deletions

View file

@ -14,6 +14,9 @@ tempfile = "2.1.1"
[dependencies.libimagstore]
path = "../libimagstore"
[dependencies.libimagstorestdhook]
path = "../libimagstorestdhook"
[dependencies.libimagutil]
path = "../libimagutil"

View file

@ -21,6 +21,7 @@ extern crate clap;
extern crate toml;
extern crate libimagstore;
extern crate libimagstorestdhook;
extern crate libimagutil;
mod configuration;

View file

@ -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<Box<Error>> = 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> {
}
}

View file

@ -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(())
}
}
}

View file

@ -3,3 +3,5 @@ extern crate toml;
extern crate libimagstore;
pub mod debug;