Merge pull request #265 from matthiasbeyer/libimagstorestdhook/init

Libimagstorestdhook/init
This commit is contained in:
Matthias Beyer 2016-03-26 19:51:19 +01:00
commit 144e2db92d
6 changed files with 173 additions and 7 deletions

View file

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

View file

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

View file

@ -1,6 +1,8 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use std::env; use std::env;
use std::io::stderr;
use std::io::Write;
pub use clap::App; pub use clap::App;
@ -36,14 +38,14 @@ impl<'a> Runtime<'a> {
use std::env; use std::env;
use std::error::Error; use std::error::Error;
use libimagstore::hook::position::HookPosition;
use libimagstorestdhook::debug::DebugHook;
use libimagutil::trace::trace_error;
use configuration::error::ConfigErrorKind; use configuration::error::ConfigErrorKind;
let matches = cli_spec.get_matches(); let matches = cli_spec.get_matches();
let is_debugging = matches.is_present("debugging"); 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") let rtp : PathBuf = matches.value_of("runtimepath")
.map(PathBuf::from) .map(PathBuf::from)
@ -70,6 +72,7 @@ impl<'a> Runtime<'a> {
let cause : Option<Box<Error>> = Some(Box::new(e)); let cause : Option<Box<Error>> = Some(Box::new(e));
return Err(RuntimeError::new(RuntimeErrorKind::Instantiate, cause)); return Err(RuntimeError::new(RuntimeErrorKind::Instantiate, cause));
} else { } else {
trace_error(&e);
None None
} }
} else { } else {
@ -79,11 +82,39 @@ impl<'a> Runtime<'a> {
let store_config = { let store_config = {
match &cfg { match &cfg {
&Some(ref c) => c.store_config().map(|c| c.clone()), &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 { Runtime {
cli_matches: matches, cli_matches: matches,
configuration: cfg, configuration: cfg,
@ -229,4 +260,3 @@ impl<'a> Runtime<'a> {
} }
} }

View file

@ -0,0 +1,12 @@
[package]
name = "libimagstorestdhook"
version = "0.1.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies]
toml = "0.1.25"
log = "0.3.5"
[dependencies.libimagstore]
path = "../libimagstore"

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

@ -0,0 +1,7 @@
#[macro_use] extern crate log;
extern crate toml;
extern crate libimagstore;
pub mod debug;