Skip to main content

soil_cli/params/
shared_params.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use crate::arg_enums::TracingReceiver;
8use clap::Args;
9use soil_service::config::BasePath;
10use std::path::PathBuf;
11
12/// Shared parameters used by all `CoreParams`.
13#[derive(Debug, Clone, Args)]
14pub struct SharedParams {
15	/// Specify the chain specification.
16	///
17	/// It can be one of the predefined ones (dev, local, or staging) or it can be a path to
18	/// a file with the chainspec (such as one exported by the `build-spec` subcommand).
19	#[arg(long, value_name = "CHAIN_SPEC")]
20	pub chain: Option<String>,
21
22	/// Specify the development chain.
23	///
24	/// This flag sets `--chain=dev`, `--force-authoring`, `--rpc-cors=all`, `--alice`, and `--tmp`
25	/// flags, unless explicitly overridden. It also disables local peer discovery (see `--no-mdns`
26	/// and `--discover-local`). With this flag some nodes might start with manual seal, producing
27	/// blocks at certain events (e.g. `polkadot-omni-node`, which produces blocks at certain
28	/// intervals dictated by `--dev-block-time`).
29	#[arg(long)]
30	pub dev: bool,
31
32	/// Specify custom base path.
33	#[arg(long, short = 'd', value_name = "PATH")]
34	pub base_path: Option<PathBuf>,
35
36	/// Sets a custom logging filter (syntax: `<target>=<level>`).
37	///
38	/// Log levels (least to most verbose) are `error`, `warn`, `info`, `debug`, and `trace`.
39	///
40	/// By default, all targets log `info`. The global log level can be set with `-l<level>`.
41	///
42	/// Multiple `<target>=<level>` entries can be specified and separated by a comma.
43	///
44	/// *Example*: `--log error,sync=debug,grandpa=warn`.
45	/// Sets Global log level to `error`, sets `sync` target to debug and grandpa target to `warn`.
46	#[arg(short = 'l', long, value_name = "LOG_PATTERN", num_args = 1..)]
47	pub log: Vec<String>,
48
49	/// Enable detailed log output.
50	///
51	/// Includes displaying the log target, log level and thread name.
52	///
53	/// This is automatically enabled when something is logged with any higher level than `info`.
54	#[arg(long)]
55	pub detailed_log_output: bool,
56
57	/// Disable log color output.
58	#[arg(long)]
59	pub disable_log_color: bool,
60
61	/// Enable feature to dynamically update and reload the log filter.
62	///
63	/// Be aware that enabling this feature can lead to a performance decrease up to factor six or
64	/// more. Depending on the global logging level the performance decrease changes.
65	///
66	/// The `system_addLogFilter` and `system_resetLogFilter` RPCs will have no effect with this
67	/// option not being set.
68	#[arg(long)]
69	pub enable_log_reloading: bool,
70
71	/// Sets a custom profiling filter.
72	///
73	/// Syntax is the same as for logging (`--log`).
74	#[arg(long, value_name = "TARGETS")]
75	pub tracing_targets: Option<String>,
76
77	/// Receiver to process tracing messages.
78	#[arg(long, value_name = "RECEIVER", value_enum, ignore_case = true, default_value_t = TracingReceiver::Log)]
79	pub tracing_receiver: TracingReceiver,
80}
81
82impl SharedParams {
83	/// Specify custom base path.
84	pub fn base_path(&self) -> Result<Option<BasePath>, crate::Error> {
85		match &self.base_path {
86			Some(r) => Ok(Some(r.clone().into())),
87			// If `dev` is enabled, we use the temp base path.
88			None if self.is_dev() => Ok(Some(BasePath::new_temp_dir()?)),
89			None => Ok(None),
90		}
91	}
92
93	/// Specify the development chain.
94	pub fn is_dev(&self) -> bool {
95		self.dev
96	}
97
98	/// Get the chain spec for the parameters provided
99	pub fn chain_id(&self, is_dev: bool) -> String {
100		match self.chain {
101			Some(ref chain) => chain.clone(),
102			None if is_dev => "dev".into(),
103			_ => "".into(),
104		}
105	}
106
107	/// Get the filters for the logging
108	pub fn log_filters(&self) -> &[String] {
109		&self.log
110	}
111
112	/// Should the detailed log output be enabled.
113	pub fn detailed_log_output(&self) -> bool {
114		self.detailed_log_output
115	}
116
117	/// Should the log color output be disabled?
118	pub fn disable_log_color(&self) -> bool {
119		self.disable_log_color
120	}
121
122	/// Is log reloading enabled
123	pub fn enable_log_reloading(&self) -> bool {
124		self.enable_log_reloading
125	}
126
127	/// Receiver to process tracing messages.
128	pub fn tracing_receiver(&self) -> soil_service::TracingReceiver {
129		self.tracing_receiver.into()
130	}
131
132	/// Comma separated list of targets for tracing.
133	pub fn tracing_targets(&self) -> Option<String> {
134		self.tracing_targets.clone()
135	}
136}