1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#![warn(missing_docs)]
#![warn(unused_extern_crates)]
pub mod arg_enums;
mod commands;
mod config;
mod error;
mod params;
mod runner;
pub use arg_enums::*;
pub use commands::*;
pub use config::*;
pub use error::*;
pub use params::*;
pub use runner::*;
use sc_service::{Configuration, TaskExecutor};
pub use sc_service::{ChainSpec, Role};
pub use sp_version::RuntimeVersion;
use std::io::Write;
pub use structopt;
use structopt::{
clap::{self, AppSettings},
StructOpt,
};
use tracing_subscriber::layer::SubscriberExt;
pub trait SubstrateCli: Sized {
fn impl_name() -> String;
fn impl_version() -> String;
fn executable_name() -> String {
std::env::current_exe().ok()
.and_then(|e| e.file_name().map(|s| s.to_os_string()))
.and_then(|w| w.into_string().ok())
.unwrap_or_else(|| env!("CARGO_PKG_NAME").into())
}
fn description() -> String;
fn author() -> String;
fn support_url() -> String;
fn copyright_start_year() -> i32;
fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn ChainSpec>, String>;
fn from_args() -> Self
where
Self: StructOpt + Sized,
{
<Self as SubstrateCli>::from_iter(&mut std::env::args_os())
}
fn from_iter<I>(iter: I) -> Self
where
Self: StructOpt + Sized,
I: IntoIterator,
I::Item: Into<std::ffi::OsString> + Clone,
{
let app = <Self as StructOpt>::clap();
let mut full_version = Self::impl_version();
full_version.push_str("\n");
let name = Self::executable_name();
let author = Self::author();
let about = Self::description();
let app = app
.name(name)
.author(author.as_str())
.about(about.as_str())
.version(full_version.as_str())
.settings(&[
AppSettings::GlobalVersion,
AppSettings::ArgsNegateSubcommands,
AppSettings::SubcommandsNegateReqs,
]);
let matches = match app.get_matches_from_safe(iter) {
Ok(matches) => matches,
Err(mut e) => {
e.message.extend("\n".chars());
if e.use_stderr() {
let _ = std::io::stderr().write_all(e.message.as_bytes());
std::process::exit(1);
} else {
let _ = std::io::stdout().write_all(e.message.as_bytes());
std::process::exit(0);
}
},
};
<Self as StructOpt>::from_clap(&matches)
}
fn try_from_iter<I>(iter: I) -> clap::Result<Self>
where
Self: StructOpt + Sized,
I: IntoIterator,
I::Item: Into<std::ffi::OsString> + Clone,
{
let app = <Self as StructOpt>::clap();
let mut full_version = Self::impl_version();
full_version.push_str("\n");
let name = Self::executable_name();
let author = Self::author();
let about = Self::description();
let app = app
.name(name)
.author(author.as_str())
.about(about.as_str())
.version(full_version.as_str());
let matches = app.get_matches_from_safe(iter)?;
Ok(<Self as StructOpt>::from_clap(&matches))
}
fn client_id() -> String {
format!("{}/v{}", Self::impl_name(), Self::impl_version())
}
fn create_configuration<T: CliConfiguration<DVC>, DVC: DefaultConfigurationValues>(
&self,
command: &T,
task_executor: TaskExecutor,
) -> error::Result<Configuration> {
command.create_configuration(self, task_executor)
}
fn create_runner<T: CliConfiguration>(&self, command: &T) -> error::Result<Runner<Self>> {
command.init::<Self>()?;
Runner::new(self, command)
}
fn native_runtime_version(chain_spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion;
}
pub fn init_logger(
pattern: &str,
tracing_receiver: sc_tracing::TracingReceiver,
tracing_targets: Option<String>,
) -> std::result::Result<(), String> {
if let Err(e) = tracing_log::LogTracer::init() {
return Err(format!(
"Registering Substrate logger failed: {:}!", e
))
}
let mut env_filter = tracing_subscriber::EnvFilter::default()
.add_directive("ws=off".parse().expect("provided directive is valid"))
.add_directive("yamux=off".parse().expect("provided directive is valid"))
.add_directive("cranelift_codegen=off".parse().expect("provided directive is valid"))
.add_directive("cranelife_wasm=warn".parse().expect("provided directive is valid"))
.add_directive("hyper=warn".parse().expect("provided directive is valid"))
.add_directive("sc_tracing=trace".parse().expect("provided directive is valid"))
.add_directive(tracing_subscriber::filter::LevelFilter::INFO.into());
if let Ok(lvl) = std::env::var("RUST_LOG") {
if lvl != "" {
if let Ok(directive) = lvl.parse() {
env_filter = env_filter.add_directive(directive);
}
}
}
if pattern != "" {
if let Ok(directive) = pattern.parse() {
env_filter = env_filter.add_directive(directive);
}
}
let isatty = atty::is(atty::Stream::Stderr);
let enable_color = isatty;
let subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_env_filter(env_filter)
.with_target(false)
.with_ansi(enable_color)
.with_writer(std::io::stderr)
.compact()
.finish();
if let Some(tracing_targets) = tracing_targets {
let profiling = sc_tracing::ProfilingLayer::new(tracing_receiver, &tracing_targets);
if let Err(e) = tracing::subscriber::set_global_default(subscriber.with(profiling)) {
return Err(format!(
"Registering Substrate tracing subscriber failed: {:}!", e
))
}
} else {
if let Err(e) = tracing::subscriber::set_global_default(subscriber) {
return Err(format!(
"Registering Substrate tracing subscriber failed: {:}!", e
))
}
}
Ok(())
}