-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from tinythings/isbm-constraint-stdout-handler
Add constraint stdout handler
- Loading branch information
Showing
6 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
Outcome Logger | ||
============== | ||
|
||
.. note:: | ||
|
||
This document explains how to use **outcome logger** event handler. | ||
|
||
Synopsis | ||
-------- | ||
|
||
.. code-block:: text | ||
outcome-logger: null | ||
Outcome logger is one of the simplest event handlers, used to summarise the outcome | ||
of all constraints. | ||
|
||
Options | ||
------- | ||
|
||
``prefix`` | ||
^^^^^^^^^^ | ||
|
||
Prefix to the logging message text. Example: | ||
|
||
.. code-block:: yaml | ||
:caption: Prefix example | ||
prefix: "Hello" | ||
Example | ||
------- | ||
|
||
.. code-block:: yaml | ||
:caption: Setup example | ||
events: | ||
# Capture all events | ||
$/$/$/$: | ||
handlers: | ||
outcome-logger | ||
outcome-logger: | ||
prefix: "My constraints" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ Event Handlers | |
:maxdepth: 1 | ||
|
||
console_logger | ||
outcome_logger | ||
|
||
Overview | ||
-------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Constraint result handler for STDOUT | ||
*/ | ||
|
||
use super::evthandler::EventHandler; | ||
use crate::intp::{ | ||
actproc::response::ActionResponse, | ||
conf::{EventConfig, EventConfigOption}, | ||
}; | ||
use colored::Colorize; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct ConstraintHandler { | ||
eid: String, | ||
config: EventConfig, | ||
} | ||
|
||
impl ConstraintHandler { | ||
/// Get prefix from the configuration | ||
fn get_prefix(&self) -> String { | ||
let mut prefix = "".to_string(); | ||
if let Some(config) = self.config() { | ||
if let Some(p) = config.as_string("prefix") { | ||
prefix = format!("{} - ", p.cyan()); | ||
} | ||
} | ||
|
||
prefix | ||
} | ||
} | ||
|
||
/// STDOUT event handler. It just outputs the action response to a log. | ||
impl EventHandler for ConstraintHandler { | ||
fn new(eid: String, cfg: EventConfig) -> Self | ||
where | ||
Self: Sized, | ||
{ | ||
ConstraintHandler { eid, config: cfg } | ||
} | ||
|
||
fn id() -> String | ||
where | ||
Self: Sized, | ||
{ | ||
"outcome-logger".to_string() | ||
} | ||
|
||
fn handle(&self, evt: &ActionResponse) { | ||
if !&evt.match_eid(&self.eid) { | ||
return; | ||
} | ||
|
||
let prefix = self.get_prefix(); | ||
|
||
if !evt.errors.has_errors() { | ||
log::info!("{}All constraints {}", prefix, "passed".bright_green().bold()); | ||
return; | ||
} | ||
|
||
log::info!("{}", evt.errors.descr()); | ||
for f in evt.errors.failures() { | ||
log::error!("{}{}: {}", prefix, f.title.yellow(), f.msg); | ||
} | ||
} | ||
|
||
fn config(&self) -> Option<EventConfigOption> { | ||
self.config.cfg(&ConstraintHandler::id()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters