add string stream convenience util; add ?Sized bounds on log fmt functors

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-08-03 02:16:52 +00:00
parent e9ab548445
commit f98bfbbdcd
2 changed files with 14 additions and 4 deletions

View file

@ -5,7 +5,7 @@ use crate::Result;
pub fn html<S>(out: &mut S, level: &Level, span: &str, msg: &str) -> Result<()>
where
S: Write,
S: Write + ?Sized,
{
let color = color::code_tag(level);
let level = level.as_str().to_uppercase();
@ -19,7 +19,7 @@ where
pub fn markdown<S>(out: &mut S, level: &Level, span: &str, msg: &str) -> Result<()>
where
S: Write,
S: Write + ?Sized,
{
let level = level.as_str().to_uppercase();
writeln!(out, "`{level:>5}` `{span:^12}` `{msg}`")?;
@ -29,7 +29,7 @@ where
pub fn markdown_table<S>(out: &mut S, level: &Level, span: &str, msg: &str) -> Result<()>
where
S: Write,
S: Write + ?Sized,
{
let level = level.as_str().to_uppercase();
writeln!(out, "| {level:>5} | {span:^12} | {msg} |")?;
@ -39,7 +39,7 @@ where
pub fn markdown_table_head<S>(out: &mut S) -> Result<()>
where
S: Write,
S: Write + ?Sized,
{
write!(out, "| level | span | message |\n| ------: | :-----: | :------- |\n")?;

View file

@ -30,6 +30,16 @@ macro_rules! is_format {
};
}
#[inline]
pub fn collect_stream<F>(func: F) -> Result<String>
where
F: FnOnce(&mut dyn std::fmt::Write) -> Result<()>,
{
let mut out = String::new();
func(&mut out)?;
Ok(out)
}
#[inline]
#[must_use]
pub fn camel_to_snake_string(s: &str) -> String {