Start a listener that receives incoming log events, and then writes them out to `stdout`, after wrapping them in a valid JSON-RPC notification object.
(out: Arc<Mutex<FramedWrite<O, JsonCodec>>>)
| 41 | /// writes them out to `stdout`, after wrapping them in a valid |
| 42 | /// JSON-RPC notification object. |
| 43 | fn start_writer<O>(out: Arc<Mutex<FramedWrite<O, JsonCodec>>>) -> mpsc::UnboundedSender<LogEntry> |
| 44 | where |
| 45 | O: AsyncWrite + Send + Unpin + 'static, |
| 46 | { |
| 47 | let (sender, mut receiver) = mpsc::unbounded_channel::<LogEntry>(); |
| 48 | tokio::spawn(async move { |
| 49 | while let Some(i) = receiver.recv().await { |
| 50 | // We continue draining the queue, even if we get some |
| 51 | // errors when forwarding. Forwarding could break due to |
| 52 | // an interrupted connection or stdout being closed, but |
| 53 | // keeping the messages in the queue is a memory leak. |
| 54 | let payload = json!({ |
| 55 | "jsonrpc": "2.0", |
| 56 | "method": "log", |
| 57 | "params": i |
| 58 | }); |
| 59 | |
| 60 | let _ = out.lock().await.send(payload).await; |
| 61 | } |
| 62 | }); |
| 63 | sender |
| 64 | } |
| 65 | |
| 66 | /// Initialize the logger starting a flusher to the passed in sink. |
| 67 | pub async fn init<O>(out: Arc<Mutex<FramedWrite<O, JsonCodec>>>) -> Result<(), anyhow::Error> |