Skip to content

Commit

Permalink
JSON-aware output in conclude.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
adizere committed Apr 7, 2021
1 parent 2e2dc0b commit 573a5a9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
1 change: 0 additions & 1 deletion ci/simple_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
timeout = '10s'
strategy = 'naive'
log_level = 'info'
log_json = false

[[chains]]
id = 'ibc-0'
Expand Down
1 change: 0 additions & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
timeout = '10s'
strategy = 'naive'
log_level = 'error'
log_json = false

[[chains]]
id = 'ibc-0'
Expand Down
1 change: 0 additions & 1 deletion config_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
timeout = '10s'
strategy = 'naive'
log_level = 'error'
log_json = false

[[chains]]
id = 'ibc-0'
Expand Down
3 changes: 3 additions & 0 deletions relayer-cli/src/components.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io;

use abscissa_core::component::Id;
use abscissa_core::{Component, FrameworkError};
use tracing_subscriber::fmt::{
format::{Format, Json, JsonFields},
Expand All @@ -11,6 +12,8 @@ use tracing_subscriber::{reload::Handle, EnvFilter, FmtSubscriber};

use ibc_relayer::config::GlobalConfig;

pub const TRACING_COMPONENT_ID: Id = Id::new("ibc_relayer_cli::components::Tracing");

/// Custom types to simplify the `Tracing` definition below
type Formatter = TracingFormatter<JsonFields, Format<Json, SystemTime>, StdWriter>;
type StdWriter = fn() -> io::Stderr;
Expand Down
19 changes: 18 additions & 1 deletion relayer-cli/src/conclude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,18 @@ use std::fmt;
use serde::Serialize;
use tracing::error;

use crate::components::TRACING_COMPONENT_ID;
use crate::prelude::{app_reader, Application};

/// Functional-style method to exit a program.
///
/// ## Note: See `Output::exit()` for the preferred method of exiting a relayer command.
pub fn exit_with(out: Output) {
// Handle the output message
println!("{}", serde_json::to_string(&out).unwrap());
match json() {
true => println!("{}", serde_json::to_string(&out).unwrap()),
false => println!("{}: {}", out.status, out.result),
}

// The return code
if out.status == Status::Error {
Expand All @@ -75,6 +81,17 @@ pub fn exit_with(out: Output) {
}
}

/// Returns true if the application global json flag `-j` or `--json` is enabled.
/// Returns false otherwise.
pub fn json() -> bool {
let a = app_reader();
// if the custom Tracing component is loaded, then JSON is enabled
a.state()
.components
.get_by_id(TRACING_COMPONENT_ID)
.is_some()
}

/// Exits the program. Useful when a type produces an error which can no longer be propagated, and
/// the program must exit instead.
///
Expand Down
1 change: 0 additions & 1 deletion relayer/tests/config/fixtures/relayer_conf_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
timeout = '10s'
strategy = 'naive'
log_level = 'error'
log_json = false

[[chains]]
id = 'chain_A'
Expand Down

0 comments on commit 573a5a9

Please sign in to comment.