From 9c561014f3c4278c0991290c898713f8e9c928e8 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Thu, 3 Apr 2025 03:09:35 +0100 Subject: feat: xml logging --- src/writer.rs | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) (limited to 'src/writer.rs') diff --git a/src/writer.rs b/src/writer.rs index e82d674..7ed1775 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -2,12 +2,14 @@ use std::collections::HashSet; use async_recursion::async_recursion; use tokio::io::{AsyncWrite, AsyncWriteExt}; +use tracing::info; use crate::{ declaration::{Declaration, VersionInfo}, element::{escape_str, Content, Element, IntoContent, IntoElement, Name, NamespaceDeclaration}, endable::Endable, error::Error, + loggable::Loggable, xml::{self, composers::Composer, parsers_complete::Parser}, Result, XMLNS_NS, XML_NS, }; @@ -15,7 +17,7 @@ use crate::{ // pub struct Writer { #[derive(Debug)] pub struct Writer { - inner: Endable, + inner: Endable>, depth: Vec, namespace_declarations: Vec>, } @@ -32,14 +34,14 @@ impl Writer { namespace: XMLNS_NS.to_string(), }); Self { - inner: Endable::new(writer), + inner: Endable::new(Loggable::new(writer)), depth: Vec::new(), namespace_declarations: vec![default_declarations], } } pub fn into_inner(self) -> W { - self.inner.into_inner() + self.inner.into_inner().into_inner() } } @@ -65,24 +67,48 @@ impl Writer { pub async fn write_full(&mut self, into_element: &impl IntoElement) -> Result<()> { let element = into_element.into_element(); - Ok(self.write_element(&element).await?) + self.write_element(&element).await?; + let bytes = &self.inner.ignore_end().take_log(); + let log = str::from_utf8(bytes).unwrap_or("failed to convert bytes written to str"); + info!("wrote element: {}", log); + Ok(()) } pub async fn write_start(&mut self, into_element: &impl IntoElement) -> Result<()> { let element = into_element.into_element(); - Ok(self.write_element_start(&element).await?) + self.write_element_start(&element).await?; + let bytes = &self.inner.ignore_end().take_log(); + let log = str::from_utf8(bytes).unwrap_or("failed to convert bytes written to str"); + info!("wrote element start: {}", log); + Ok(()) } pub async fn write_all_content(&mut self, into_element: &impl IntoElement) -> Result<()> { for content in &into_element.get_content() { self.write_content(content).await?; } + let bytes = &self.inner.ignore_end().take_log(); + let log = str::from_utf8(bytes).unwrap_or("failed to convert bytes written to str"); + info!("wrote element content: {}", log); Ok(()) } pub async fn write(&mut self, into_content: &impl IntoContent) -> Result<()> { let content = into_content.into_content(); - Ok(self.write_content(&content).await?) + self.write_content(&content).await?; + let bytes = &self.inner.ignore_end().take_log(); + let log = str::from_utf8(bytes).unwrap_or("failed to convert bytes written to str"); + info!("wrote element: {}", log); + Ok(()) + } + + // pub async fn write_end(&mut self) + pub async fn write_end(&mut self) -> Result<()> { + self.write_end_tag().await?; + let bytes = &self.inner.ignore_end().take_log(); + let log = str::from_utf8(bytes).unwrap_or("failed to convert bytes written to str"); + info!("wrote element end: {}", log); + Ok(()) } #[async_recursion] @@ -94,7 +120,7 @@ impl Writer { for content in &element.content { self.write_content(content).await?; } - self.write_end().await?; + self.write_end_tag().await?; } Ok(()) } @@ -334,7 +360,7 @@ impl Writer { Ok(()) } - pub async fn write_end(&mut self) -> Result<()> { + pub async fn write_end_tag(&mut self) -> Result<()> { let writer = self.inner.try_as_mut()?; if let Some(name) = &self.depth.pop() { let e_tag; -- cgit