From 1f9225e4d128ca43e6992931e369214bdfa220f7 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 12 Jun 2024 18:52:24 +0000 Subject: [PATCH] add markdown log format for capture Signed-off-by: Jason Volk --- Cargo.lock | 28 +++++++++++++++++++++ Cargo.toml | 1 + src/admin/debug/debug_commands.rs | 10 +++++--- src/core/log/capture/mod.rs | 2 +- src/core/log/capture/util.rs | 42 ++++++++++++++++++++++--------- src/core/log/fmt.rs | 29 +++++++++++++++++++++ 6 files changed, 95 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b2b9b05..283cdb84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2714,6 +2714,24 @@ dependencies = [ "prost", ] +[[package]] +name = "pulldown-cmark" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76979bea66e7875e7509c4ec5300112b316af87fa7a252ca91c448b32dfe3993" +dependencies = [ + "bitflags 2.5.0", + "memchr", + "pulldown-cmark-escape", + "unicase", +] + +[[package]] +name = "pulldown-cmark-escape" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd348ff538bc9caeda7ee8cad2d1d48236a1f443c1fa3913c6a02fe0043b1dd3" + [[package]] name = "quick-error" version = "1.2.3" @@ -2980,6 +2998,7 @@ dependencies = [ "js_int", "js_option", "percent-encoding", + "pulldown-cmark", "regex", "ruma-common", "ruma-identifiers-validation", @@ -4279,6 +4298,15 @@ dependencies = [ "version_check", ] +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.15" diff --git a/Cargo.toml b/Cargo.toml index 217e2434..1fd8bdbc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -280,6 +280,7 @@ features = [ "appservice-api-c", "client-api", "federation-api", + "markdown", "push-gateway-api-c", "state-res", "unstable-exhaustive-types", diff --git a/src/admin/debug/debug_commands.rs b/src/admin/debug/debug_commands.rs index 287c78dd..0316abc2 100644 --- a/src/admin/debug/debug_commands.rs +++ b/src/admin/debug/debug_commands.rs @@ -648,16 +648,18 @@ pub(crate) async fn resolve_true_destination( let state = &services().server.log.capture; let logs = Arc::new(Mutex::new(String::new())); - let capture = Capture::new(state, Some(filter), capture::to_html(&logs)); + let capture = Capture::new(state, Some(filter), capture::fmt_markdown(logs.clone())); let (actual_dest, hostname_uri); { let _capture_scope = capture.start(); (actual_dest, hostname_uri) = resolve_actual_dest(&server_name, !no_cache).await?; }; - let plain = format!("Actual destination: {actual_dest} | Hostname URI: {hostname_uri}"); - let html = format!("{}
{plain}", logs.lock().expect("locked")); - Ok(RoomMessageEventContent::text_html(plain, html)) + let msg = format!( + "{}\nDestination: {actual_dest}\nHostname URI: {hostname_uri}", + logs.lock().expect("locked") + ); + Ok(RoomMessageEventContent::text_markdown(msg)) } #[must_use] diff --git a/src/core/log/capture/mod.rs b/src/core/log/capture/mod.rs index f0495ad9..c0fcd31e 100644 --- a/src/core/log/capture/mod.rs +++ b/src/core/log/capture/mod.rs @@ -10,7 +10,7 @@ pub use data::Data; use guard::Guard; pub use layer::{Layer, Value}; pub use state::State; -pub use util::to_html; +pub use util::*; pub type Filter = dyn Fn(Data<'_>) -> bool + Send + Sync + 'static; pub type Closure = dyn FnMut(Data<'_>) + Send + Sync + 'static; diff --git a/src/core/log/capture/util.rs b/src/core/log/capture/util.rs index 54a19b6a..8bad4ba0 100644 --- a/src/core/log/capture/util.rs +++ b/src/core/log/capture/util.rs @@ -1,19 +1,37 @@ use std::sync::{Arc, Mutex}; -use super::{super::fmt, Closure}; +use super::{ + super::{fmt, Level}, + Closure, Data, +}; +use crate::Result; -pub fn to_html(out: &Arc>) -> Box +pub fn fmt_html(out: Arc>) -> Box where S: std::fmt::Write + Send + 'static, { - let out = out.clone(); - Box::new(move |data| { - fmt::html( - &mut *out.lock().expect("locked"), - &data.level(), - data.span_name(), - data.message(), - ) - .expect("log line appended"); - }) + fmt(fmt::html, out) +} + +pub fn fmt_markdown(out: Arc>) -> Box +where + S: std::fmt::Write + Send + 'static, +{ + fmt(fmt::markdown, out) +} + +pub fn fmt(fun: F, out: Arc>) -> Box +where + F: Fn(&mut S, &Level, &str, &str) -> Result<()> + Send + Sync + Copy + 'static, + S: std::fmt::Write + Send + 'static, +{ + Box::new(move |data| call(fun, &mut *out.lock().expect("locked"), &data)) +} + +fn call(fun: F, out: &mut S, data: &Data<'_>) +where + F: Fn(&mut S, &Level, &str, &str) -> Result<()>, + S: std::fmt::Write, +{ + fun(out, &data.level(), data.span_name(), data.message()).expect("log line appended"); } diff --git a/src/core/log/fmt.rs b/src/core/log/fmt.rs index a2e3a6f7..dbaef890 100644 --- a/src/core/log/fmt.rs +++ b/src/core/log/fmt.rs @@ -16,3 +16,32 @@ where Ok(()) } + +pub fn markdown(out: &mut S, level: &Level, span: &str, msg: &str) -> Result<()> +where + S: Write, +{ + let level = level.as_str().to_uppercase(); + writeln!(out, "`{level:>5}` `{span:^12}` `{msg}`")?; + + Ok(()) +} + +pub fn markdown_table(out: &mut S, level: &Level, span: &str, msg: &str) -> Result<()> +where + S: Write, +{ + let level = level.as_str().to_uppercase(); + writeln!(out, "| `{level:>5}` | `{span:^12}` | `{msg} |")?; + + Ok(()) +} + +pub fn markdown_table_head(out: &mut S) -> Result<()> +where + S: Write, +{ + write!(out, "| level | span | message |\n|------:|:----:|:--------|\n")?; + + Ok(()) +}