Skip to content

Commit

Permalink
init logger callback
Browse files Browse the repository at this point in the history
  • Loading branch information
atenjin committed Jun 12, 2019
1 parent 9c34540 commit 68f4365
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
15 changes: 11 additions & 4 deletions core/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ fn is_node_name_valid(_name: &str) -> Result<(), &str> {
/// `RP` is are custom parameters for the run command. This needs to be a `struct`! The custom
/// parameters are visible to the user as if they were normal run command parameters. If no custom
/// parameters are required, `NoCustom` can be used as type here.
pub fn parse_and_execute<'a, F, CC, RP, S, RS, E, I, T>(
pub fn parse_and_execute<'a, F, CC, RP, S, RS, E, I, T, L>(
spec_factory: S,
version: &VersionInfo,
impl_name: &'static str,
args: I,
exit: E,
init_logger_f: L,
run_service: RS,
) -> error::Result<Option<CC>>
where
Expand All @@ -201,6 +202,7 @@ where
CC: StructOpt + Clone + GetLogFilter,
RP: StructOpt + Clone + AugmentClap,
E: IntoExit,
L: FnOnce(&str, MergeParameters<RunCmd, RP>) -> Result<(), String>,
RS: FnOnce(E, RunCmd, RP, FactoryFullConfiguration<F>) -> Result<(), String>,
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
Expand All @@ -223,7 +225,12 @@ where
.get_matches_from(args);
let cli_args = CoreParams::<CC, RP>::from_clap(&matches);

init_logger(cli_args.get_log_filter().as_ref().map(|v| v.as_ref()).unwrap_or(""));
let pattern = cli_args.get_log_filter().clone().unwrap_or("".to_string());
match cli_args.clone() {
params::CoreParams::Run(params) => init_logger_f(&pattern, params)?,
_ => init_logger(&pattern),
}

fdlimit::raise_fd_limit();

match cli_args {
Expand Down Expand Up @@ -779,7 +786,7 @@ fn network_path(base_path: &Path, chain_id: &str) -> PathBuf {
path
}

fn init_logger(pattern: &str) {
pub fn init_logger(pattern: &str) {
use ansi_term::Colour;

let mut builder = env_logger::Builder::new();
Expand Down Expand Up @@ -837,7 +844,7 @@ fn init_logger(pattern: &str) {
builder.init();
}

fn kill_color(s: &str) -> String {
pub fn kill_color(s: &str) -> String {
lazy_static! {
static ref RE: Regex = Regex::new("\x1b\\[[^m]+m").expect("Error initializing color regex");
}
Expand Down
8 changes: 6 additions & 2 deletions node-template/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use futures::{future, Future, sync::oneshot};
use std::cell::RefCell;
use tokio::runtime::Runtime;
pub use substrate_cli::{VersionInfo, IntoExit, error};
use substrate_cli::{informant, parse_and_execute, NoCustom};
use substrate_cli::{informant, parse_and_execute, init_logger, NoCustom};
use substrate_service::{ServiceFactory, Roles as ServiceRoles};
use crate::chain_spec;
use std::ops::Deref;
Expand All @@ -15,8 +15,12 @@ pub fn run<I, T, E>(args: I, exit: E, version: VersionInfo) -> error::Result<()>
T: Into<std::ffi::OsString> + Clone,
E: IntoExit,
{
parse_and_execute::<service::Factory, NoCustom, NoCustom, _, _, _, _, _>(
parse_and_execute::<service::Factory, NoCustom, NoCustom, _, _, _, _, _, _>(
load_spec, &version, "substrate-node", args, exit,
|s, _| {
init_logger(s);
Ok(())
},
|exit, _cli_args, _custom_args, config| {
info!("{}", version.name);
info!(" version {}", config.full_version());
Expand Down
6 changes: 5 additions & 1 deletion node/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ pub fn run<I, T, E>(args: I, exit: E, version: cli::VersionInfo) -> error::Resul
T: Into<std::ffi::OsString> + Clone,
E: IntoExit,
{
cli::parse_and_execute::<service::Factory, NoCustom, NoCustom, _, _, _, _, _>(
cli::parse_and_execute::<service::Factory, NoCustom, NoCustom, _, _, _, _, _, _>(
load_spec, &version, "substrate-node", args, exit,
|s, _| {
cli::init_logger(s);
Ok(())
},
|exit, _cli_args, _custom_args, config| {
info!("{}", version.name);
info!(" version {}", config.full_version());
Expand Down

0 comments on commit 68f4365

Please sign in to comment.