pezsp_runtime/
runtime_logger.rs

1// This file is part of Bizinikiwi.
2
3// Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! A logger that can be used to log from the runtime.
19//!
20//! See [`RuntimeLogger`] for more docs.
21
22/// Runtime logger implementation - `log` crate backend.
23///
24/// The logger should be initialized if you want to display
25/// logs inside the runtime that is not necessarily running natively.
26pub struct RuntimeLogger;
27
28impl RuntimeLogger {
29	/// Initialize the logger.
30	///
31	/// This is a no-op when running natively (`std`).
32	#[cfg(feature = "std")]
33	pub fn init() {}
34
35	/// Initialize the logger.
36	///
37	/// This is a no-op when running natively (`std`).
38	#[cfg(not(feature = "std"))]
39	pub fn init() {
40		static LOGGER: RuntimeLogger = RuntimeLogger;
41		let _ = log::set_logger(&LOGGER);
42
43		// Use the same max log level as used by the host.
44		log::set_max_level(pezsp_io::logging::max_level().into());
45	}
46}
47
48impl log::Log for RuntimeLogger {
49	fn enabled(&self, _: &log::Metadata) -> bool {
50		// The final filtering is done by the host. This is not perfect, as we would still call into
51		// the host for log lines that will be thrown away.
52		true
53	}
54
55	fn log(&self, record: &log::Record) {
56		use core::fmt::Write;
57		let mut msg = alloc::string::String::default();
58		let _ = ::core::write!(&mut msg, "{}", record.args());
59
60		pezsp_io::logging::log(record.level().into(), record.target(), msg.as_bytes());
61	}
62
63	fn flush(&self) {}
64}