From 21d9b5f697e1ea7c8f31cd3c8aac50b51dbd2a7c Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Fri, 21 Jun 2024 18:49:27 +0900 Subject: [PATCH] Switch logging target when the response to capabilities is a bundle --- src/hg_connect_http.rs | 25 +++++++++++++++++++++++++ src/logging.rs | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/src/hg_connect_http.rs b/src/hg_connect_http.rs index 859918a9e..ed1daad18 100644 --- a/src/hg_connect_http.rs +++ b/src/hg_connect_http.rs @@ -603,6 +603,31 @@ fn http_send_info(data: &mut HttpThreadData) { None } }; + if data.logger.as_ref().map(LoggingWriter::log_target) == Some("raw-wire::capabilities") + { + match content_type.as_deref() { + Some("application/mercurial-0.1" | "application/mercurial-0.2") => {} + _ => { + // If the response to the capabilities request is a bundle, log in + // a different category. + // Ideally we'd log the headers too with the switched logger, but + // it's too late for that. + trace!( + target: "raw-wire::capabilities", + "Not a capabilities response; switching to clonebundle.", + ); + if log_enabled!(target: "raw-wire::clonebundle", log::Level::Trace) { + let mut writer = LoggingWriter::new_hex( + "raw-wire::clonebundle".to_string(), + log::Level::Trace, + std::io::sink(), + ); + writer.set_direction(logging::Direction::Receive); + data.logger = Some(writer); + } + } + } + } data.sender .send(Either::Left(HttpResponseInfo { http_status: http_status as usize, diff --git a/src/logging.rs b/src/logging.rs index 458ba9354..45feed6c9 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -477,6 +477,10 @@ impl<'a, W: Write> LoggingWriter<'a, W> { pub fn set_direction(&mut self, direction: Direction) { self.log.direction = direction; } + + pub fn log_target(&self) -> &str { + &self.log.target + } } impl<'a, W: Write> Write for LoggingWriter<'a, W> {