anvil_polkadot/
logging.rs1use parking_lot::RwLock;
4use std::sync::Arc;
5use tracing::{Metadata, subscriber::Interest};
6use tracing_subscriber::{Layer, layer::Context};
7
8pub(crate) const NODE_USER_LOG_TARGET: &str = "node::user";
10
11pub(crate) const EVM_CONSOLE_LOG_TARGET: &str = "node::console";
13
14#[derive(Clone, Debug, Default)]
19pub struct NodeLogLayer {
20 state: LoggingManager,
21}
22
23impl NodeLogLayer {
24 pub fn new(state: LoggingManager) -> Self {
26 Self { state }
27 }
28}
29
30impl<S> Layer<S> for NodeLogLayer
32where
33 S: tracing::Subscriber,
34{
35 fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
36 match metadata.target() {
38 NODE_USER_LOG_TARGET | EVM_CONSOLE_LOG_TARGET => Interest::sometimes(),
39 _ => Interest::always(),
40 }
41 }
42
43 fn enabled(&self, metadata: &Metadata<'_>, _ctx: Context<'_, S>) -> bool {
44 match metadata.target() {
46 NODE_USER_LOG_TARGET | EVM_CONSOLE_LOG_TARGET => self.state.is_enabled(),
47 _ => true,
48 }
49 }
50}
51
52#[derive(Clone, Debug)]
54pub struct LoggingManager {
55 pub enabled: Arc<RwLock<bool>>,
57}
58
59impl LoggingManager {
60 pub fn is_enabled(&self) -> bool {
62 *self.enabled.read()
63 }
64
65 pub fn set_enabled(&self, enabled: bool) {
67 let mut current = self.enabled.write();
68 *current = enabled;
69 }
70}
71
72impl Default for LoggingManager {
73 fn default() -> Self {
74 Self { enabled: Arc::new(RwLock::new(true)) }
75 }
76}