MCPcopy Create free account
hub / github.com/ElementsProject/lightning / init

Function init

plugins/src/logging.rs:50–77  ·  view source on GitHub ↗

Initialize the logger starting a flusher to the passed in sink.

(out: Arc<Mutex<FramedWrite<O, JsonCodec>>>)

Source from the content-addressed store, hash-verified

48
49/// Initialize the logger starting a flusher to the passed in sink.
50pub async fn init<O>(out: Arc<Mutex<FramedWrite<O, JsonCodec>>>) -> Result<(), log::SetLoggerError>
51where
52 O: AsyncWrite + Send + Unpin + 'static,
53{
54 let out = out.clone();
55
56 let filter_str = std::env::var("CLN_PLUGIN_LOG").unwrap_or("info".to_string());
57 let filter = filter::Builder::new().parse(&filter_str).build();
58
59 let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::<LogEntry>();
60 tokio::spawn(async move {
61 while let Some(i) = receiver.recv().await {
62 // We continue draining the queue, even if we get some
63 // errors when forwarding. Forwarding could break due to
64 // an interrupted connection or stdout being closed, but
65 // keeping the messages in the queue is a memory leak.
66 let payload = json!({
67 "jsonrpc": "2.0",
68 "method": "log",
69 "params": i
70 });
71
72 let _ = out.lock().await.send(payload).await;
73 }
74 });
75 log::set_boxed_logger(Box::new(PluginLogger { sender, filter }))
76 .map(|()| log::set_max_level(log::LevelFilter::Debug))
77}
78
79impl log::Log for PluginLogger {
80 fn enabled(&self, metadata: &Metadata) -> bool {

Callers

nothing calls this directly

Calls 3

to_stringMethod · 0.80
sendMethod · 0.80
recvMethod · 0.45

Tested by

no test coverage detected