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`, `--alice`, and `--tmp`
37 /// flags, unless explicitly overridden. It also disables local peer discovery (see `--no-mdns`
38 /// and `--discover-local`). With this flag some nodes might start with manual seal, producing
39 /// blocks at certain events (e.g. `polkadot-omni-node`, which produces blocks at certain
40 /// intervals dictated by `--dev-block-time`).
41 #[arg(long)]
42 pub dev: bool,
43
44 /// Specify custom base path.
45 #[arg(long, short = 'd', value_name = "PATH")]
46 pub base_path: Option<PathBuf>,
47
48 /// Sets a custom logging filter (syntax: `<target>=<level>`).
49 ///
50 /// Log levels (least to most verbose) are `error`, `warn`, `info`, `debug`, and `trace`.
51 ///
52 /// By default, all targets log `info`. The global log level can be set with `-l<level>`.
53 ///
54 /// Multiple `<target>=<level>` entries can be specified and separated by a comma.
55 ///
56 /// *Example*: `--log error,sync=debug,grandpa=warn`.
57 /// Sets Global log level to `error`, sets `sync` target to debug and grandpa target to `warn`.
58 #[arg(short = 'l', long, value_name = "LOG_PATTERN", num_args = 1..)]
59 pub log: Vec<String>,
60
61 /// Enable detailed log output.
62 ///
63 /// Includes displaying the log target, log level and thread name.
64 ///
65 /// This is automatically enabled when something is logged with any higher level than `info`.
66 #[arg(long)]
67 pub detailed_log_output: bool,
68
69 /// Disable log color output.
70 #[arg(long)]
71 pub disable_log_color: bool,
72
73 /// Enable feature to dynamically update and reload the log filter.
74 ///
75 /// Be aware that enabling this feature can lead to a performance decrease up to factor six or
76 /// more. Depending on the global logging level the performance decrease changes.
77 ///
78 /// The `system_addLogFilter` and `system_resetLogFilter` RPCs will have no effect with this
79 /// option not being set.
80 #[arg(long)]
81 pub enable_log_reloading: bool,
82
83 /// Sets a custom profiling filter.
84 ///
85 /// Syntax is the same as for logging (`--log`).
86 #[arg(long, value_name = "TARGETS")]
87 pub tracing_targets: Option<String>,
88
89 /// Receiver to process tracing messages.
90 #[arg(long, value_name = "RECEIVER", value_enum, ignore_case = true, default_value_t = TracingReceiver::Log)]
91 pub tracing_receiver: TracingReceiver,
92}
93
94impl SharedParams {
95 /// Specify custom base path.
96 pub fn base_path(&self) -> Result<Option<BasePath>, crate::Error> {
97 match &self.base_path {
98 Some(r) => Ok(Some(r.clone().into())),
99 // If `dev` is enabled, we use the temp base path.
100 None if self.is_dev() => Ok(Some(BasePath::new_temp_dir()?)),
101 None => Ok(None),
102 }
103 }
104
105 /// Specify the development chain.
106 pub fn is_dev(&self) -> bool {
107 self.dev
108 }
109
110 /// Get the chain spec for the parameters provided
111 pub fn chain_id(&self, is_dev: bool) -> String {
112 match self.chain {
113 Some(ref chain) => chain.clone(),
114 None if is_dev => "dev".into(),
115 _ => "".into(),
116 }
117 }
118
119 /// Get the filters for the logging
120 pub fn log_filters(&self) -> &[String] {
121 &self.log
122 }
123
124 /// Should the detailed log output be enabled.
125 pub fn detailed_log_output(&self) -> bool {
126 self.detailed_log_output
127 }
128
129 /// Should the log color output be disabled?
130 pub fn disable_log_color(&self) -> bool {
131 self.disable_log_color
132 }
133
134 /// Is log reloading enabled
135 pub fn enable_log_reloading(&self) -> bool {
136 self.enable_log_reloading
137 }
138
139 /// Receiver to process tracing messages.
140 pub fn tracing_receiver(&self) -> sc_service::TracingReceiver {
141 self.tracing_receiver.into()
142 }
143
144 /// Comma separated list of targets for tracing.
145 pub fn tracing_targets(&self) -> Option<String> {
146 self.tracing_targets.clone()
147 }
148}