From c65bcefb3236abd430cd43d7973da979bec82aef Mon Sep 17 00:00:00 2001 From: Adi Seredinschi Date: Tue, 12 Jan 2021 11:55:28 +0200 Subject: [PATCH] Complete (but messy) example of switching all tracing output to JSON. Overrriding default tracing cfg [WIP] Added missing Tracing component --- relayer-cli/src/application.rs | 21 ++++++- relayer-cli/src/commands/query/connection.rs | 3 + relayer-cli/src/components.rs | 59 ++++++++++++++++++++ relayer-cli/src/lib.rs | 1 + 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 relayer-cli/src/components.rs diff --git a/relayer-cli/src/application.rs b/relayer-cli/src/application.rs index 25cbc278e8..933316aea3 100644 --- a/relayer-cli/src/application.rs +++ b/relayer-cli/src/application.rs @@ -1,11 +1,13 @@ //! Cli Abscissa Application -use crate::{commands::CliCmd, config::Config}; use abscissa_core::{ application::{self, AppCell}, - config, trace, Application, EntryPoint, FrameworkError, StandardPaths, + component::Component, + Application, config, EntryPoint, FrameworkError, StandardPaths, trace, }; +use crate::{commands::CliCmd, config::Config, components::Tracing}; + /// Application state pub static APPLICATION: AppCell = AppCell::new(); @@ -76,13 +78,26 @@ impl Application for CliApp { &mut self.state } + fn framework_components( + &mut self, + command: &Self::Cmd, + ) -> Result>>, FrameworkError> { + Ok(vec![]) + } + /// Register all components used by this application. /// /// If you would like to add additional components to your application /// beyond the default ones provided by the framework, this is the place /// to do so. fn register_components(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> { - let components = self.framework_components(command)?; + let mut components = self.framework_components(command)?; + + tracing::info!("pre-push"); + let c = Tracing::new()?; + components.push(Box::new(c)); + tracing::info!("post-push"); + self.state.components.register(components) } diff --git a/relayer-cli/src/commands/query/connection.rs b/relayer-cli/src/commands/query/connection.rs index b0425d73a9..e0b9166d4a 100644 --- a/relayer-cli/src/commands/query/connection.rs +++ b/relayer-cli/src/commands/query/connection.rs @@ -150,6 +150,9 @@ impl QueryConnectionChannelsCmd { impl Runnable for QueryConnectionChannelsCmd { fn run(&self) { let config = app_config(); + tracing::debug!("post-push ^2"); + tracing::info!("post-push ^2"); + tracing::error!("ERROROROOROR"); let (chain_config, opts) = match self.validate_options(&config) { Err(err) => { diff --git a/relayer-cli/src/components.rs b/relayer-cli/src/components.rs new file mode 100644 index 0000000000..69459ac075 --- /dev/null +++ b/relayer-cli/src/components.rs @@ -0,0 +1,59 @@ +use abscissa_core::{Component, FrameworkError, Shutdown}; +use tracing_subscriber::{EnvFilter, FmtSubscriber, reload::Handle}; +use tracing_subscriber::fmt::{ + format::{Format, Json, JsonFields}, + Formatter, + time::SystemTime +}; +use tracing_subscriber::util::SubscriberInitExt; + +/// Abscissa component for initializing the `tracing` subsystem +#[derive(Component, Debug)] +pub struct Tracing { + filter_handle: Handle>>, +} + +impl Tracing { + /// Creates a new [`Tracing`] component with the given `filter`. + pub fn new() -> Result { + // TODO(adi) put these into a section of the relayer configuration + let filter = "debug".to_string(); + let use_color = false; + + // Construct a tracing subscriber with the supplied filter and enable reloading. + let builder = FmtSubscriber::builder() + .with_env_filter(filter) + .with_ansi(use_color) + .json() + .with_filter_reloading(); + let filter_handle = builder.reload_handle(); + + let subscriber = builder.finish(); + subscriber.init(); + + Ok(Self { + filter_handle, + }) + } +} +// +// impl std::fmt::Debug for Tracing { +// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +// f.debug_struct("Tracing").finish() +// } +// } + +// impl Component for Tracing { +// fn id(&self) -> abscissa_core::component::Id { +// abscissa_core::component::Id::new("relayer-cli::components::Tracing") +// } +// +// fn version(&self) -> abscissa_core::Version { +// abscissa_core::Version::parse("0.1.0").unwrap() +// } +// +// fn before_shutdown(&self, _kind: Shutdown) -> Result<(), FrameworkError> { +// tracing::info!("shutting down"); +// Ok(()) +// } +// } \ No newline at end of file diff --git a/relayer-cli/src/lib.rs b/relayer-cli/src/lib.rs index 0e13022e2b..42a5d9bbce 100644 --- a/relayer-cli/src/lib.rs +++ b/relayer-cli/src/lib.rs @@ -22,3 +22,4 @@ mod conclude; pub mod config; pub mod error; pub mod prelude; +mod components;