From fd709bc56dae91fdd183486360dd08ac535c5c5b Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Thu, 12 Aug 2021 16:45:01 +0300 Subject: [PATCH] dap: logging using fern --- Cargo.lock | 1 + helix-dap/Cargo.toml | 3 +++ helix-dap/examples/dap-basic.rs | 11 +++++++++++ helix-dap/src/transport.rs | 20 +++++++------------- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 19e9ad4a4..bf01504ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -327,6 +327,7 @@ name = "helix-dap" version = "0.3.0" dependencies = [ "anyhow", + "fern", "log", "serde", "serde_json", diff --git a/helix-dap/Cargo.toml b/helix-dap/Cargo.toml index 6adaaeddf..0ba1a8f38 100644 --- a/helix-dap/Cargo.toml +++ b/helix-dap/Cargo.toml @@ -18,3 +18,6 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" thiserror = "1.0" tokio = { version = "1.9", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] } + +[dev-dependencies] +fern = "0.6" diff --git a/helix-dap/examples/dap-basic.rs b/helix-dap/examples/dap-basic.rs index 45522516a..7056f77ae 100644 --- a/helix-dap/examples/dap-basic.rs +++ b/helix-dap/examples/dap-basic.rs @@ -2,6 +2,17 @@ use helix_dap::{Client, Result, SourceBreakpoint}; #[tokio::main] pub async fn main() -> Result<()> { + let base_config = fern::Dispatch::new().level(log::LevelFilter::Info); + + let stderr_config = fern::Dispatch::new() + .format(|out, message, record| out.finish(format_args!("[{}] {}", record.level(), message))) + .chain(std::io::stderr()); + + base_config + .chain(stderr_config) + .apply() + .expect("Failed to set up logging"); + let mut client = Client::start("nc", vec!["127.0.0.1", "7777"], 0)?; println!("init: {:?}", client.initialize().await); diff --git a/helix-dap/src/transport.rs b/helix-dap/src/transport.rs index 1c004cfea..19171f0da 100644 --- a/helix-dap/src/transport.rs +++ b/helix-dap/src/transport.rs @@ -1,6 +1,6 @@ use crate::{Error, Result}; use anyhow::Context; -use log::error; +use log::{error, info}; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::HashMap; @@ -123,8 +123,7 @@ impl Transport { reader.read_exact(&mut content).await?; let msg = std::str::from_utf8(&content).context("invalid utf8 from server")?; - // TODO: `info!` here - println!("<- DAP {}", msg); + info!("<- DAP {}", msg); // try parsing as output (server response) or call (server request) let output: serde_json::Result = serde_json::from_str(msg); @@ -153,8 +152,7 @@ impl Transport { server_stdin: &mut BufWriter, request: String, ) -> Result<()> { - // TODO: `info!` here - println!("-> DAP {}", request); + info!("-> DAP {}", request); // send the headers server_stdin @@ -181,8 +179,7 @@ impl Transport { request_seq, .. }) => { - // TODO: `info!` here - println!("<- DAP success ({}, in response to {})", seq, request_seq); + info!("<- DAP success ({}, in response to {})", seq, request_seq); if let Payload::Response(val) = msg { (request_seq, Ok(val)) } else { @@ -197,8 +194,7 @@ impl Transport { command, .. }) => { - // TODO: `error!` here - println!( + error!( "<- DAP error {:?} ({:?}) for command #{} {}", message, body, request_seq, command ); @@ -212,16 +208,14 @@ impl Transport { ref seq, .. }) => { - // TODO: `info!` here - println!("<- DAP request {} #{}", command, seq); + info!("<- DAP request {} #{}", command, seq); client_tx.send(msg).expect("Failed to send"); return Ok(()); } Payload::Event(Event { ref event, ref seq, .. }) => { - // TODO: `info!` here - println!("<- DAP event {} #{}", event, seq); + info!("<- DAP event {} #{}", event, seq); client_tx.send(msg).expect("Failed to send"); return Ok(()); }