mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-22 00:24:19 +01:00
cln-plugin: Implement logging facade adapter for cln plugins
We wrap emitted messages into a JSON-RPC notification envelope and write them to stdout. We use an indirection over an mpsc channel in order to avoid deadlocks if we emit logs while holding the writer lock on stdout.
This commit is contained in:
committed by
Rusty Russell
parent
9ae1f33992
commit
f5e1829117
@@ -1,16 +1,19 @@
|
||||
use crate::codec::{JsonCodec, JsonRpcCodec};
|
||||
use futures::sink::SinkExt;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio_util::codec::FramedWrite;
|
||||
pub mod codec;
|
||||
mod messages;
|
||||
pub use anyhow::Error;
|
||||
use futures::sink::SinkExt;
|
||||
extern crate log;
|
||||
use log::{trace, warn};
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio_stream::StreamExt;
|
||||
use tokio_util::codec::FramedRead;
|
||||
use tokio_util::codec::FramedWrite;
|
||||
|
||||
pub mod codec;
|
||||
pub mod logging;
|
||||
mod messages;
|
||||
|
||||
#[macro_use]
|
||||
extern crate serde_json;
|
||||
@@ -50,19 +53,19 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(self) -> Result<(), Error> {
|
||||
let (plugin, input) = self.build();
|
||||
plugin.run(input).await
|
||||
}
|
||||
|
||||
pub fn build(self) -> (Plugin<S, I, O>, I) {
|
||||
let output = Arc::new(Mutex::new(FramedWrite::new(
|
||||
self.output,
|
||||
JsonCodec::default(),
|
||||
)));
|
||||
|
||||
// Now configure the logging, so any `log` call is wrapped
|
||||
// in a JSON-RPC notification and sent to c-lightning
|
||||
tokio::spawn(async move {});
|
||||
(
|
||||
Plugin {
|
||||
state: Arc::new(Mutex::new(self.state)),
|
||||
output: Arc::new(Mutex::new(FramedWrite::new(
|
||||
self.output,
|
||||
JsonCodec::default(),
|
||||
))),
|
||||
output,
|
||||
input_type: PhantomData,
|
||||
},
|
||||
self.input,
|
||||
@@ -74,7 +77,7 @@ pub struct Plugin<S, I, O>
|
||||
where
|
||||
S: Clone + Send,
|
||||
I: AsyncRead,
|
||||
O: Send + AsyncWrite,
|
||||
O: Send + AsyncWrite + 'static,
|
||||
{
|
||||
//input: FramedRead<Stdin, JsonCodec>,
|
||||
output: Arc<Mutex<FramedWrite<O, JsonCodec>>>,
|
||||
@@ -87,11 +90,14 @@ impl<S, I, O> Plugin<S, I, O>
|
||||
where
|
||||
S: Clone + Send,
|
||||
I: AsyncRead + Send + Unpin,
|
||||
O: Send + AsyncWrite + Unpin,
|
||||
O: Send + AsyncWrite + Unpin + 'static,
|
||||
{
|
||||
/// Read incoming requests from `c-lightning and dispatch their handling.
|
||||
#[allow(unused_mut)]
|
||||
pub async fn run(mut self, input: I) -> Result<(), Error> {
|
||||
crate::logging::init(self.output.clone()).await?;
|
||||
trace!("Plugin logging initialized");
|
||||
|
||||
let mut input = FramedRead::new(input, JsonRpcCodec::default());
|
||||
loop {
|
||||
match input.next().await {
|
||||
|
||||
Reference in New Issue
Block a user