Browse Source

Change log passing

master
Adrian Heine 4 years ago
parent
commit
d7180e327a
  1. 7
      src/loggers.rs
  2. 117
      src/setup/core.rs
  3. 2
      src/setup/mod.rs
  4. 157
      src/setup/setup.rs
  5. 3
      src/setup/util.rs

7
src/loggers.rs

@ -7,7 +7,7 @@ use std::io::Write;
// 1 - Error, 2 - Warn, 3 - Info, 4 - Debug, 5 - Trace // 1 - Error, 2 - Warn, 3 - Info, 4 - Debug, 5 - Trace
pub type Level = usize; pub type Level = usize;
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct Entry<S>(pub Level, pub S); pub struct Entry<S>(pub Level, pub S);
pub trait Logger { pub trait Logger {
@ -67,7 +67,7 @@ impl Logger for StdErrLogger {
} }
impl Drop for StdErrLogger { impl Drop for StdErrLogger {
fn drop(&mut self) { fn drop(&mut self) {
if *self.line_started.borrow() == true {
if *self.line_started.borrow() {
writeln!(&mut stderr()).unwrap(); writeln!(&mut stderr()).unwrap();
} }
} }
@ -98,7 +98,7 @@ impl<'a, L: Logger> Logger for FilteringLogger<'a, L> {
} }
} }
#[derive(Debug, Default)]
#[derive(Clone, Debug, Default)]
pub struct StoringLogger { pub struct StoringLogger {
log: RefCell<(bool, Vec<Entry<String>>)>, log: RefCell<(bool, Vec<Entry<String>>)>,
} }
@ -125,6 +125,7 @@ impl Logger for StoringLogger {
None None
} }
.unwrap_or_else(|| Entry(level, line.into())); .unwrap_or_else(|| Entry(level, line.into()));
log.0 = true;
log.1.push(entry); log.1.push(entry);
} }
fn writeln<S: AsRef<str> + Into<String>>(&self, level: Level, line: S) { fn writeln<S: AsRef<str> + Into<String>>(&self, level: Level, line: S) {

117
src/setup/core.rs

@ -1,6 +1,6 @@
use super::runnable::Runnable; use super::runnable::Runnable;
use super::util::{AddResult, AddableResource};
use super::Setup;
use super::setup::Setup;
use super::util::{AddableResource, InternalAddResult};
use super::SymbolRunner; use super::SymbolRunner;
use crate::async_utils::try_join; use crate::async_utils::try_join;
use crate::loggers::{Logger, StoringLogger}; use crate::loggers::{Logger, StoringLogger};
@ -16,15 +16,15 @@ use std::marker::PhantomData;
#[async_trait(?Send)] #[async_trait(?Send)]
pub trait AddGeneric<X: ToArtifact> { pub trait AddGeneric<X: ToArtifact> {
async fn add_generic(&self, x: X) -> AddResult<X>;
async fn add_generic(&self, x: X) -> InternalAddResult<X>;
} }
macro_rules! add_generic { macro_rules! add_generic {
( $($name:ident)* ) => ( ( $($name:ident)* ) => (
#[async_trait(?Send)] #[async_trait(?Send)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
impl<SR: 'static, _L: 'static, _B: 'static, LOG: 'static + Logger, Rs: 'static + Hash + Eq, As: 'static + Clone, $($name: AddableResource,)*>
AddGeneric<($($name,)*)> for Setup<SR, LOG, _L, _B, Rs, As>
impl<SR: 'static, _L: 'static, _B: 'static, Rs: 'static + Hash + Eq, As: 'static + Clone, $($name: AddableResource,)*>
AddGeneric<($($name,)*)> for Setup<SR, _L, _B, Rs, As>
where where
$( $(
RegularSetupCore<SR, _L, _B>: SetupCore<$name, Self>, RegularSetupCore<SR, _L, _B>: SetupCore<$name, Self>,
@ -34,11 +34,13 @@ macro_rules! add_generic {
),* ),*
{ {
#[allow(unused)] #[allow(unused)]
async fn add_generic(&self, ($($name,)*): ($($name,)*)) -> Result<(($($name::Artifact,)*), bool), Box<dyn Error>>
async fn add_generic(&self, ($($name,)*): ($($name,)*)) -> Result<(StoringLogger, ($($name::Artifact,)*), bool), (StoringLogger, Box<dyn Error>)>
{ {
let x: Result<_, Box<dyn Error>> = try_join!($(self.add_force($name, false),)*);
let x: Result<_, _> = try_join!($(self.add($name, false),)*);
let ($($name,)*) = x?; let ($($name,)*) = x?;
Ok((($($name.0,)*), false $(|| $name.1)*))
let logger = StoringLogger::default();
$(logger.put($name.0.release());)*
Ok((logger, ($($name.1,)*), false $(|| $name.2)*))
} }
} }
); );
@ -51,57 +53,54 @@ for_each_tuple!(add_generic);
#[async_trait(?Send)] #[async_trait(?Send)]
impl< impl<
SR: 'static + SymbolRunner, SR: 'static + SymbolRunner,
LOG: 'static + Logger,
T: AddableResource, T: AddableResource,
Rs: 'static + Hash + Eq + FromResource<T>, Rs: 'static + Hash + Eq + FromResource<T>,
As: 'static + FromArtifact<T> + Clone, As: 'static + FromArtifact<T> + Clone,
L: 'static + ResourceLocator<T, Prerequisites = Option<T>>, L: 'static + ResourceLocator<T, Prerequisites = Option<T>>,
B: 'static + ImplementationBuilder<T>, B: 'static + ImplementationBuilder<T>,
> AddGeneric<Option<T>> for Setup<SR, LOG, L, B, Rs, As>
> AddGeneric<Option<T>> for Setup<SR, L, B, Rs, As>
where where
<B as ImplementationBuilder<T>>::Implementation: Runnable + Debug, <B as ImplementationBuilder<T>>::Implementation: Runnable + Debug,
Self: AddGeneric<B::Prerequisites>, Self: AddGeneric<B::Prerequisites>,
T::Artifact: Clone, T::Artifact: Clone,
{ {
async fn add_generic(&self, r: Option<T>) -> AddResult<Option<T>> {
async fn add_generic(&self, r: Option<T>) -> InternalAddResult<Option<T>> {
Ok(match r { Ok(match r {
Some(r) => { Some(r) => {
let (result, did_run) = self.add_force(r, false).await?;
(Some(result), did_run)
let (logger, result, did_run) = self.add(r, false).await?;
(logger, Some(result), did_run)
} }
None => (None, false),
None => (StoringLogger::default(), None, false),
}) })
} }
} }
#[async_trait(?Send)] #[async_trait(?Send)]
impl< impl<
LOG: 'static + Logger,
T: AddableResource, T: AddableResource,
Rs: 'static + Hash + Eq + FromResource<T>, Rs: 'static + Hash + Eq + FromResource<T>,
As: 'static + FromArtifact<T> + Clone, As: 'static + FromArtifact<T> + Clone,
SR: 'static, SR: 'static,
L: 'static, L: 'static,
B: 'static, B: 'static,
> AddGeneric<T> for Setup<SR, LOG, L, B, Rs, As>
> AddGeneric<T> for Setup<SR, L, B, Rs, As>
where where
T::Artifact: Clone, T::Artifact: Clone,
RegularSetupCore<SR, L, B>: 'static + SetupCore<T, Self>, RegularSetupCore<SR, L, B>: 'static + SetupCore<T, Self>,
{ {
async fn add_generic(&self, r: T) -> AddResult<T> {
self.add_force(r, false).await
async fn add_generic(&self, r: T) -> InternalAddResult<T> {
self.add(r, false).await
} }
} }
#[async_trait(?Send)] #[async_trait(?Send)]
pub trait SetupCore<R: AddableResource, S> { pub trait SetupCore<R: AddableResource, S> {
async fn add<LOG: Logger, RR: AsRef<R>>(
async fn add<RR: AsRef<R>>(
&self, &self,
setup: &S, setup: &S,
parent_logger: &LOG,
resource: RR, resource: RR,
force_run: bool, force_run: bool,
) -> AddResult<R>;
) -> InternalAddResult<R>;
} }
#[derive(Debug)] #[derive(Debug)]
@ -127,49 +126,57 @@ where
L: ResourceLocator<R>, L: ResourceLocator<R>,
S: AddGeneric<B::Prerequisites> + AddGeneric<<L as ResourceLocator<R>>::Prerequisites>, S: AddGeneric<B::Prerequisites> + AddGeneric<<L as ResourceLocator<R>>::Prerequisites>,
{ {
async fn add<LOG: Logger, RR: AsRef<R>>(
async fn add<RR: AsRef<R>>(
&self, &self,
setup: &S, setup: &S,
parent_logger: &LOG,
resource: RR, resource: RR,
force_run: bool, force_run: bool,
) -> AddResult<R> {
) -> InternalAddResult<R> {
let resource = resource.as_ref(); let resource = resource.as_ref();
let logger = StoringLogger::new(); let logger = StoringLogger::new();
logger.write(4, format!("Adding {:?} ... ", resource)); logger.write(4, format!("Adding {:?} ... ", resource));
let result = async {
logger.trace(format!(" (force_run is {})", force_run));
let (location, location_prereqs) = L::locate(resource);
logger.trace(format!("Adding location prereqs for {:?}", resource));
let (_, location_prereqs_did_run) = setup.add_generic(location_prereqs).await?;
logger.trace(format!(
"Location prereqs for {:?} did_run: {}",
resource, location_prereqs_did_run
));
logger.trace(format!("Adding implementation prereqs for {:?}", resource));
let (prereqs, prereqs_did_run) = setup.add_generic(B::prerequisites(resource)).await?;
logger.trace(format!(
"Implementation prereqs for {:?} did_run: {}",
resource, prereqs_did_run
));
logger.trace(format!("Running implementation for {:?}", resource));
let implementation = B::create(resource, &location, prereqs);
let did_run = implementation
.run(
&self.symbol_runner,
&logger,
force_run || location_prereqs_did_run || prereqs_did_run,
)
.await?;
Ok((location, did_run))
logger.write(4, format!("(force_run is {})", force_run));
let (location, location_prereqs) = L::locate(resource);
logger.trace(format!("Adding location prereqs for {:?}", resource));
let result = setup.add_generic(location_prereqs).await;
if let Err((log, e)) = result {
logger.put(log.release());
return Err((logger, e));
} }
.await;
logger.write(4, "done.");
let max_level = if result.is_err() { 5 } else { 3 };
if parent_logger.put(logger.release().into_iter().filter(|e| e.0 <= max_level)) == 0 {
parent_logger.write(3, ".");
let (location_prereq_logger, _, location_prereqs_did_run) = result.unwrap();
logger.put(location_prereq_logger.release());
logger.trace(format!(
"Location prereqs for {:?} did_run: {}",
resource, location_prereqs_did_run
));
logger.trace(format!("Adding implementation prereqs for {:?}", resource));
let result = setup.add_generic(B::prerequisites(resource)).await;
if let Err((log, e)) = result {
logger.put(log.release());
return Err((logger, e));
}
let (impl_prereq_logger, prereqs, prereqs_did_run) = result.unwrap();
logger.put(impl_prereq_logger.release());
logger.trace(format!(
"Implementation prereqs for {:?} did_run: {}",
resource, prereqs_did_run
));
logger.trace(format!("Running implementation for {:?}", resource));
let implementation = B::create(resource, &location, prereqs);
let did_run_result = implementation
.run(
&self.symbol_runner,
&logger,
force_run || location_prereqs_did_run || prereqs_did_run,
)
.await;
match did_run_result {
Ok(did_run) => {
logger.write(4, "done.");
Ok((logger, location, did_run))
}
Err(e) => Err((logger, e)),
} }
result
} }
} }

2
src/setup/mod.rs

@ -8,4 +8,4 @@ pub use symbol_runner::{
}; };
mod runnable; mod runnable;
mod setup; mod setup;
pub use setup::Setup;
pub use setup::SetupFacade as Setup;

157
src/setup/setup.rs

@ -1,8 +1,9 @@
use super::core::{RegularSetupCore, SetupCore}; use super::core::{RegularSetupCore, SetupCore};
use super::runnable::Runnable; use super::runnable::Runnable;
use super::util::{AddResult, AddableResource};
use super::util::{AddResult, AddableResource, InternalAddResult};
use super::SymbolRunner; use super::SymbolRunner;
use crate::loggers::Logger;
use crate::async_utils::sleep;
use crate::loggers::{Logger, StoringLogger};
use crate::resources::{DefaultArtifacts, DefaultResources, FromArtifact, FromResource}; use crate::resources::{DefaultArtifacts, DefaultResources, FromArtifact, FromResource};
use crate::{DefaultBuilder, DefaultLocator}; use crate::{DefaultBuilder, DefaultLocator};
use futures::future::FutureExt; use futures::future::FutureExt;
@ -14,57 +15,27 @@ use std::future::Future;
use std::hash::Hash; use std::hash::Hash;
use std::pin::Pin; use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
use std::time::Duration;
type Cache<Rs, As> = HashMap<Rs, Shared<Pin<Box<dyn Future<Output = (As, bool)>>>>>; type Cache<Rs, As> = HashMap<Rs, Shared<Pin<Box<dyn Future<Output = (As, bool)>>>>>;
#[derive(Debug)] #[derive(Debug)]
struct SetupInner<CORE, LOG, Rs, As> {
struct SetupInner<CORE, Rs, As> {
core: CORE, core: CORE,
logger: LOG,
resources: RefCell<Cache<Rs, As>>, resources: RefCell<Cache<Rs, As>>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Setup<
SR,
LOG,
L = DefaultLocator,
B = DefaultBuilder,
Rs = DefaultResources<'static, &'static str>,
As = DefaultArtifacts<'static, &'static str>,
>(Rc<SetupInner<RegularSetupCore<SR, L, B>, LOG, Rs, As>>);
pub struct Setup<SR, L, B, Rs, As>(Rc<SetupInner<RegularSetupCore<SR, L, B>, Rs, As>>);
// https://github.com/rust-lang/rust/issues/27336
impl<SR, LOG> Setup<SR, LOG> {
pub fn new(symbol_runner: SR, logger: LOG) -> Self {
Self::new_with(symbol_runner, logger)
}
}
impl<L, B, As, SR, LOG, Rs: Hash + Eq> Setup<SR, LOG, L, B, Rs, As> {
pub fn new_with(symbol_runner: SR, logger: LOG) -> Self {
Self(Rc::new(SetupInner {
core: RegularSetupCore::new(symbol_runner),
logger,
resources: RefCell::default(),
}))
}
}
impl<
L: 'static,
B: 'static,
SR: 'static,
LOG: 'static + Logger,
Rs: Hash + Eq + 'static,
As: 'static,
> Setup<SR, LOG, L, B, Rs, As>
impl<L: 'static, B: 'static, SR: 'static, Rs: Hash + Eq + 'static, As: 'static>
Setup<SR, L, B, Rs, As>
{ {
fn borrow_resources(&self) -> RefMut<'_, Cache<Rs, As>> { fn borrow_resources(&self) -> RefMut<'_, Cache<Rs, As>> {
self.0.resources.borrow_mut() self.0.resources.borrow_mut()
} }
pub async fn add_force<R: AddableResource>(&self, resource: R, force_run: bool) -> AddResult<R>
pub async fn add<R: AddableResource>(&self, resource: R, force_run: bool) -> InternalAddResult<R>
where where
Rs: FromResource<R>, Rs: FromResource<R>,
As: FromArtifact<R> + Clone, As: FromArtifact<R> + Clone,
@ -73,51 +44,123 @@ impl<
{ {
let (storable_resource, weak_resource) = Rs::from_resource(resource); let (storable_resource, weak_resource) = Rs::from_resource(resource);
let mut resources = self.borrow_resources(); let mut resources = self.borrow_resources();
if let Some(future) = resources.remove(&storable_resource) {
let result = if let Some(future) = resources.remove(&storable_resource) {
assert!( assert!(
!force_run, !force_run,
"Forcing to run an already-added resource is a logical error" "Forcing to run an already-added resource is a logical error"
); );
resources.insert(storable_resource, future.clone()); resources.insert(storable_resource, future.clone());
drop(resources); drop(resources);
Ok(future.await)
let logger = StoringLogger::default();
logger.trace(format!(
"{:?} already added",
weak_resource.upgrade().expect("Dangling!")
));
let (t, did_run) = future.await;
Ok((logger, t, did_run))
} else { } else {
let inner_weak = Rc::downgrade(&self.0); let inner_weak = Rc::downgrade(&self.0);
let future = Box::pin(async move { let future = Box::pin(async move {
let this = Self(inner_weak.upgrade().expect("Dangling!")); let this = Self(inner_weak.upgrade().expect("Dangling!"));
let resource = weak_resource.upgrade().expect("Dangling!"); let resource = weak_resource.upgrade().expect("Dangling!");
// Need to convert Box<Error> to String for Clone for Shared // Need to convert Box<Error> to String for Clone for Shared
this
.0
.core
.add(&this, &this.0.logger, resource, force_run)
.await
.map(|(t, did_run)| (As::from_artifact(t), did_run))
.map_err(|e| e.to_string())
let result = this.0.core.add(&this, resource, force_run).await;
result
.map(|(logger, t, did_run)| (logger, As::from_artifact(t), did_run))
.map_err(|(logger, e)| (logger, e.to_string()))
}) })
.shared(); .shared();
let future_clone = future.clone(); let future_clone = future.clone();
resources.insert( resources.insert(
storable_resource, storable_resource,
(Box::pin(async move { future_clone.await.unwrap() })
as Pin<Box<dyn Future<Output = (As, bool)>>>)
(Box::pin(async move {
let result = future_clone.await;
if result.is_err() {
// Step back to give the initial caller time to handle the error before unwrapping
sleep(Duration::from_millis(0)).await;
}
result.map(|(_, t, did_run)| (t, did_run)).unwrap()
}) as Pin<Box<dyn Future<Output = (As, bool)>>>)
.shared(), .shared(),
); );
drop(resources); drop(resources);
future.await.map_err(|e| e.into())
}
.map(|(t, did_run)| (t.into_artifact(), did_run))
let result = future.await;
result.map_err(|(logger, e)| (logger, e.into()))
};
result.map(|(logger, t, did_run)| (logger, t.into_artifact(), did_run))
} }
}
//
// Legacy
//
#[derive(Debug)]
pub struct SetupFacade<
SR,
LOG,
L = DefaultLocator,
B = DefaultBuilder,
Rs = DefaultResources<'static, &'static str>,
As = DefaultArtifacts<'static, &'static str>,
>(LOG, Setup<SR, L, B, Rs, As>);
impl<SR, LOG> SetupFacade<SR, LOG> {
pub fn new(symbol_runner: SR, logger: LOG) -> Self {
Self::new_with(symbol_runner, logger)
}
}
impl<L, B, As, SR, LOG, Rs: Hash + Eq> SetupFacade<SR, LOG, L, B, Rs, As> {
pub fn new_with(symbol_runner: SR, logger: LOG) -> Self {
Self(
logger,
Setup(Rc::new(SetupInner {
core: RegularSetupCore::new(symbol_runner),
resources: RefCell::default(),
})),
)
}
}
impl<
SR: 'static,
LOG: 'static + Logger,
L: 'static,
B: 'static,
Rs: 'static + Eq + Hash,
As: 'static,
> SetupFacade<SR, LOG, L, B, Rs, As>
{
pub async fn add_force<R: AddableResource>(&self, resource: R, force_run: bool) -> AddResult<R>
where
Rs: FromResource<R>,
As: FromArtifact<R> + Clone,
R::Artifact: Clone,
RegularSetupCore<SR, L, B>: SetupCore<R, Setup<SR, L, B, Rs, As>>,
{
let result = self.1.add(resource, force_run).await;
match result {
Ok((logger, t, did_run)) => {
if self
.0
.put(logger.release().into_iter().filter(|e| e.0 <= 3))
== 0
{
self.0.write(3, ".");
}
Ok((t, did_run))
}
Err((logger, e)) => {
self.0.put(logger.release());
Err(e)
}
}
}
pub async fn add<R: AddableResource>(&self, resource: R) -> AddResult<R> pub async fn add<R: AddableResource>(&self, resource: R) -> AddResult<R>
where where
RegularSetupCore<SR, L, B>: SetupCore<R, Self>,
RegularSetupCore<SR, L, B>: SetupCore<R, Setup<SR, L, B, Rs, As>>,
Rs: FromResource<R>, Rs: FromResource<R>,
As: FromArtifact<R> + Clone, As: FromArtifact<R> + Clone,
R::Artifact: Clone, R::Artifact: Clone,
SR: SymbolRunner,
{ {
self.add_force(resource, false).await self.add_force(resource, false).await
} }
@ -130,7 +173,7 @@ impl<
where where
RegularSetupCore<SR, L, B>: SymbolRunner, RegularSetupCore<SR, L, B>: SymbolRunner,
{ {
symbol.run(&self.0.core, &self.0.logger, force).await
symbol.run(&(self.1).0.core, &self.0, force).await
} }
} }

3
src/setup/util.rs

@ -1,3 +1,4 @@
use crate::loggers::StoringLogger;
use crate::resources::Resource; use crate::resources::Resource;
use crate::to_artifact::ToArtifact; use crate::to_artifact::ToArtifact;
use std::error::Error; use std::error::Error;
@ -7,3 +8,5 @@ pub trait AddableResource: 'static + Resource + Debug {}
impl<R> AddableResource for R where R: 'static + Resource + Debug {} impl<R> AddableResource for R where R: 'static + Resource + Debug {}
pub type AddResult<R> = Result<(<R as ToArtifact>::Artifact, bool), Box<dyn Error>>; pub type AddResult<R> = Result<(<R as ToArtifact>::Artifact, bool), Box<dyn Error>>;
pub type InternalAddResult<R> =
Result<(StoringLogger, <R as ToArtifact>::Artifact, bool), (StoringLogger, Box<dyn Error>)>;
Loading…
Cancel
Save