Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/log_custom/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl ScoreWrite for StringWriter {
write!(self.buffer, "{}", v).map_err(|_| Error)
}

fn write_i128(&mut self, v: &i128, _spec: &FormatSpec) -> FmtResult {
write!(self.buffer, "{}", v).map_err(|_| Error)
}

fn write_u8(&mut self, v: &u8, _spec: &FormatSpec) -> FmtResult {
write!(self.buffer, "{}", v).map_err(|_| Error)
}
Expand All @@ -74,6 +78,10 @@ impl ScoreWrite for StringWriter {
write!(self.buffer, "{}", v).map_err(|_| Error)
}

fn write_u128(&mut self, v: &u128, _spec: &FormatSpec) -> FmtResult {
write!(self.buffer, "{}", v).map_err(|_| Error)
}

fn write_str(&mut self, v: &str, _spec: &FormatSpec) -> FmtResult {
write!(self.buffer, "{}", v).map_err(|_| Error)
}
Expand Down
24 changes: 22 additions & 2 deletions src/log/score_log_fmt/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub trait ScoreWrite {
fn write_i32(&mut self, v: &i32, spec: &FormatSpec) -> Result;
/// Write a `i64` into this writer.
fn write_i64(&mut self, v: &i64, spec: &FormatSpec) -> Result;
/// Write a `i128` into this writer.
fn write_i128(&mut self, v: &i128, spec: &FormatSpec) -> Result;
/// Write a `u8` into this writer.
fn write_u8(&mut self, v: &u8, spec: &FormatSpec) -> Result;
/// Write a `u16` into this writer.
Expand All @@ -56,6 +58,8 @@ pub trait ScoreWrite {
fn write_u32(&mut self, v: &u32, spec: &FormatSpec) -> Result;
/// Write a `u64` into this writer.
fn write_u64(&mut self, v: &u64, spec: &FormatSpec) -> Result;
/// Write a `u128` into this writer.
fn write_u128(&mut self, v: &u128, spec: &FormatSpec) -> Result;
/// Write a `&str` into this writer.
fn write_str(&mut self, v: &str, spec: &FormatSpec) -> Result;
}
Expand Down Expand Up @@ -154,18 +158,26 @@ mod tests {
Fragment::Placeholder(Placeholder::new(&-1234i16, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(&-123456i32, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(&-1200000000000000000i64, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(
&-128000000000000000000000000000000000000i128,
FormatSpec::new(),
)),
Fragment::Placeholder(Placeholder::new(&123u8, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(&1234u16, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(&123456u32, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(&1200000000000000000u64, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(
&128000000000000000000000000000000000000u128,
FormatSpec::new(),
)),
Fragment::Literal("_string"),
];
let args = Arguments(&fragments);

let result = ScoreDebug::fmt(&args, &mut w, &FormatSpec::new());
assert!(result == Ok(()));
assert!(
w.get() == "test_true123.4432.2-100-1234-123456-120000000000000000012312341234561200000000000000000_string"
w.get() == "test_true123.4432.2-100-1234-123456-1200000000000000000-12800000000000000000000000000000000000012312341234561200000000000000000128000000000000000000000000000000000000_string"
)
}

Expand Down Expand Up @@ -195,16 +207,24 @@ mod tests {
Fragment::Placeholder(Placeholder::new(&-1234i16, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(&-123456i32, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(&-1200000000000000000i64, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(
&-128000000000000000000000000000000000000i128,
FormatSpec::new(),
)),
Fragment::Placeholder(Placeholder::new(&123u8, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(&1234u16, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(&123456u32, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(&1200000000000000000u64, FormatSpec::new())),
Fragment::Placeholder(Placeholder::new(
&128000000000000000000000000000000000000u128,
FormatSpec::new(),
)),
Fragment::Placeholder(Placeholder::new(&"test", FormatSpec::new())),
];
let args = Arguments(&fragments);
assert!(write(&mut w, args) == Ok(()));

let exp_pattern = "true123.4432.2-100-1234-123456-120000000000000000012312341234561200000000000000000test";
let exp_pattern = "true123.4432.2-100-1234-123456-1200000000000000000-12800000000000000000000000000000000000012312341234561200000000000000000128000000000000000000000000000000000000test";
assert_eq!(w.get(), exp_pattern);
}

Expand Down
24 changes: 24 additions & 0 deletions src/log/score_log_fmt/fmt_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ impl_debug_for_t!(i8, write_i8);
impl_debug_for_t!(i16, write_i16);
impl_debug_for_t!(i32, write_i32);
impl_debug_for_t!(i64, write_i64);
impl_debug_for_t!(i128, write_i128);
impl_debug_for_t!(u8, write_u8);
impl_debug_for_t!(u16, write_u16);
impl_debug_for_t!(u32, write_u32);
impl_debug_for_t!(u64, write_u64);
impl_debug_for_t!(u128, write_u128);

impl ScoreDebug for () {
fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result {
Expand Down Expand Up @@ -86,6 +88,13 @@ impl ScoreDebug for std::string::FromUtf8Error {
}
}

impl ScoreDebug for core::time::Duration {
fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result {
f.write_f64(&self.as_secs_f64(), spec)?;
f.write_str("s", spec)
}
}

macro_rules! impl_debug_for_t_casted {
($ti:ty, $to:ty, $fn:ident) => {
impl ScoreDebug for $ti {
Expand Down Expand Up @@ -272,6 +281,11 @@ mod tests {
common_test_debug(-1200000000000000000i64);
}

#[test]
fn test_i128_debug() {
common_test_debug(-128000000000000000000000000000000000000i128);
}

#[test]
fn test_u8_debug() {
common_test_debug(123u8);
Expand All @@ -292,6 +306,11 @@ mod tests {
common_test_debug(1200000000000000000u64);
}

#[test]
fn test_u128_debug() {
common_test_debug(128000000000000000000000000000000000000u128);
}

#[test]
fn test_unit_debug() {
common_test_debug(());
Expand Down Expand Up @@ -376,6 +395,11 @@ mod tests {
common_test_debug(Box::new(432.1));
}

#[test]
fn test_duration_debug() {
common_test_debug(core::time::Duration::new(123, 456789));
}

#[test]
fn test_hashmap_debug() {
common_test_debug(std::collections::HashMap::from([("x", 123), ("y", 321), ("z", 444)]));
Expand Down
14 changes: 14 additions & 0 deletions src/log/score_log_fmt/fmt_impl_qm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use crate::fmt::{Result, ScoreDebug, Writer};
use crate::fmt_spec::FormatSpec;
use core::net::AddrParseError;
use std::path::{Path, PathBuf};

// TODO: replace with `core::char::MAX_LEN_UTF8` once stable.
Expand Down Expand Up @@ -51,6 +52,12 @@ impl ScoreDebug for PathBuf {
}
}

impl ScoreDebug for AddrParseError {
fn fmt(&self, f: Writer, spec: &FormatSpec) -> Result {
f.write_str(&format!("{:?}", self), spec)
}
}

#[cfg(test)]
mod tests {
use crate::test_utils::common_test_debug;
Expand All @@ -65,4 +72,11 @@ mod tests {
fn test_pathbuf_debug() {
common_test_debug(PathBuf::from("/tmp/test_path"));
}

#[test]
fn test_addr_parse_error_debug() {
let a1 = "invalid address";
let a2: Result<std::net::IpAddr, std::net::AddrParseError> = a1.parse();
common_test_debug(a2.unwrap_err());
}
}
8 changes: 8 additions & 0 deletions src/log/score_log_fmt/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ impl ScoreWrite for StringWriter {
Ok(write!(self.buf, "{}", v)?)
}

fn write_i128(&mut self, v: &i128, _spec: &FormatSpec) -> Result {
Ok(write!(self.buf, "{}", v)?)
}

fn write_u8(&mut self, v: &u8, _spec: &FormatSpec) -> Result {
Ok(write!(self.buf, "{}", v)?)
}
Expand All @@ -81,6 +85,10 @@ impl ScoreWrite for StringWriter {
Ok(write!(self.buf, "{}", v)?)
}

fn write_u128(&mut self, v: &u128, _spec: &FormatSpec) -> Result {
Ok(write!(self.buf, "{}", v)?)
}

fn write_str(&mut self, v: &str, _spec: &FormatSpec) -> Result {
Ok(write!(self.buf, "{}", v)?)
}
Expand Down
8 changes: 8 additions & 0 deletions src/log/score_log_fmt_macro/tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ impl ScoreWrite for StringWriter {
write!(self.buf, "{}", v).map_err(|_| Error)
}

fn write_i128(&mut self, v: &i128, _spec: &FormatSpec) -> Result {
write!(self.buf, "{}", v).map_err(|_| Error)
}

fn write_u8(&mut self, v: &u8, _spec: &FormatSpec) -> Result {
write!(self.buf, "{}", v).map_err(|_| Error)
}
Expand All @@ -75,6 +79,10 @@ impl ScoreWrite for StringWriter {
write!(self.buf, "{}", v).map_err(|_| Error)
}

fn write_u128(&mut self, v: &u128, _spec: &FormatSpec) -> Result {
write!(self.buf, "{}", v).map_err(|_| Error)
}

fn write_str(&mut self, v: &str, _spec: &FormatSpec) -> Result {
write!(self.buf, "{}", v).map_err(|_| Error)
}
Expand Down
8 changes: 8 additions & 0 deletions src/log/stdout_logger/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ impl<const N: usize> ScoreWrite for FixedBufWriter<N> {
write!(self.buf, "{}", v).map_err(|_| Error)
}

fn write_i128(&mut self, v: &i128, _spec: &FormatSpec) -> Result {
write!(self.buf, "{}", v).map_err(|_| Error)
}

fn write_u8(&mut self, v: &u8, _spec: &FormatSpec) -> Result {
write!(self.buf, "{}", v).map_err(|_| Error)
}
Expand All @@ -157,6 +161,10 @@ impl<const N: usize> ScoreWrite for FixedBufWriter<N> {
write!(self.buf, "{}", v).map_err(|_| Error)
}

fn write_u128(&mut self, v: &u128, _spec: &FormatSpec) -> Result {
write!(self.buf, "{}", v).map_err(|_| Error)
}

fn write_str(&mut self, v: &str, _spec: &FormatSpec) -> Result {
write!(self.buf, "{}", v).map_err(|_| Error)
}
Expand Down
Loading