1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
34// 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.
89// 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.
1314// 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/>.
1617//! Relayer initialization functions.
1819use 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;
2829/// 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);
3233async_std::task_local! {
34pub(crate) static LOOP_NAME: RefCell<String> = RefCell::new(String::default());
35}
3637/// Initialize relay environment.
38pub fn initialize_relay() {
39 initialize_logger(true);
40}
4142/// Initialize Relay logger instance.
43pub fn initialize_logger(with_timestamp: bool) {
44let 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");
4950let local_time = OffsetTime::new(
51 time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC),
52 format,
53 );
5455let 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();
5960let builder = SubscriberBuilder::default().with_env_filter(env_filter);
6162if with_timestamp {
63 builder.with_timer(local_time).init();
64 } else {
65 builder.without_time().init();
66 }
67}
6869/// 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}