sc_cli/params/
shared_params.rs

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