referrerpolicy=no-referrer-when-downgrade

relay_utils/
initialize.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Relayer initialization functions.
18
19use parking_lot::Mutex;
20use sp_tracing::{
21	tracing::Level,
22	tracing_subscriber::{
23		fmt::{time::OffsetTime, SubscriberBuilder},
24		EnvFilter,
25	},
26};
27use std::cell::RefCell;
28
29/// Relayer version that is provided as metric. Must be set by a binary
30/// (get it with `option_env!("CARGO_PKG_VERSION")` from a binary package code).
31pub static RELAYER_VERSION: Mutex<Option<String>> = Mutex::new(None);
32
33async_std::task_local! {
34	pub(crate) static LOOP_NAME: RefCell<String> = RefCell::new(String::default());
35}
36
37/// Initialize relay environment.
38pub fn initialize_relay() {
39	initialize_logger(true);
40}
41
42/// Initialize Relay logger instance.
43pub fn initialize_logger(with_timestamp: bool) {
44	let format = time::format_description::parse(
45		"[year]-[month]-[day] \
46		[hour repr:24]:[minute]:[second] [offset_hour sign:mandatory]",
47	)
48	.expect("static format string is valid");
49
50	let local_time = OffsetTime::new(
51		time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC),
52		format,
53	);
54
55	let env_filter = EnvFilter::builder()
56		.with_default_directive(Level::WARN.into())
57		.with_default_directive("bridge=info".parse().expect("static filter string is valid"))
58		.from_env_lossy();
59
60	let builder = SubscriberBuilder::default().with_env_filter(env_filter);
61
62	if with_timestamp {
63		builder.with_timer(local_time).init();
64	} else {
65		builder.without_time().init();
66	}
67}
68
69/// Initialize relay loop. Must only be called once per every loop task.
70pub(crate) fn initialize_loop(loop_name: String) {
71	LOOP_NAME.with(|g_loop_name| *g_loop_name.borrow_mut() = loop_name);
72}