Skip to content

Commit 80834c8

Browse files
committed
log: mw_log_fmt_macro implementation
- Replacement for `format_args!` macros. - Replacement for `Debug` derive macro. - Unit tests.
1 parent cd10951 commit 80834c8

File tree

20 files changed

+1671
-26
lines changed

20 files changed

+1671
-26
lines changed

Cargo.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
resolver = "2"
33
# Split to default members without tests and examples.
44
# Used when executing cargo from project root.
5-
default-members = ["src/containers", "src/log/mw_log_fmt"]
5+
default-members = [
6+
"src/containers",
7+
"src/log/mw_log_fmt",
8+
"src/log/mw_log_fmt_macro",
9+
]
610
# Include tests and examples as a member for IDE support and Bazel builds.
7-
members = ["src/containers", "src/log/mw_log_fmt"]
11+
members = ["src/containers", "src/log/mw_log_fmt", "src/log/mw_log_fmt_macro"]
812

913

1014
[workspace.package]
@@ -16,6 +20,7 @@ authors = ["S-CORE Contributors"]
1620

1721
[workspace.dependencies]
1822
mw_log_fmt = { path = "src/log/mw_log_fmt" }
23+
mw_log_fmt_macro = { path = "src/log/mw_log_fmt_macro" }
1924

2025

2126
[workspace.lints.clippy]

docs/module/log/architecture/_assets/interface.puml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ package log <<component>> {
3737
+trace!(...) : ()
3838
}
3939

40-
class mw_log_macro <<module>> {
40+
class mw_log_fmt_macro <<module>> {
4141
+mw_log_format_args!(format_string: &str, args...) : Arguments<'_>
4242
+mw_log_format_args_nl!(format_string: &str, args...) : Arguments<'_>
4343
}
@@ -57,7 +57,7 @@ package log <<component>> {
5757

5858
mw_log -- Level
5959
mw_log -- LevelFilter
60-
mw_log -- mw_log_macro
60+
mw_log -- mw_log_fmt_macro
6161
mw_log -right- mw_log_fmt
6262
}
6363

docs/module/log/architecture/_assets/static_view.puml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ package "log" <<component>> {
55

66
component "mw_log_fmt"
77

8-
component "mw_log_macro"
8+
component "mw_log_fmt_macro"
99

1010
mw_log ..> mw_log_fmt : use
11-
mw_log ..> mw_log_macro : use
11+
mw_log ..> mw_log_fmt_macro : use
1212
}
1313

1414
component "mw_log_subscriber" <<component>>

docs/module/log/detailed_design/_assets/class_diagram.puml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ package "mw_log_fmt crate" {
217217
mw_log_fmt -- Arguments
218218
}
219219

220-
package "mw_log_macro crate" {
221-
+class mw_log_macro <<module>> {
220+
package "mw_log_fmt_macro crate" {
221+
+class mw_log_fmt_macro <<module>> {
222222
+mw_log_format_args!(format_string: &str, args...) : Arguments<'_>
223223
+mw_log_format_args_nl!(format_string: &str, args...) : Arguments<'_>
224224
}
@@ -262,7 +262,7 @@ package "mw_log_subscriber crate" {
262262
}
263263

264264
"mw_log crate" -[hidden]down-> "mw_log_fmt crate"
265-
"mw_log crate" -[hidden]up-> "mw_log_macro crate"
265+
"mw_log crate" -[hidden]up-> "mw_log_fmt_macro crate"
266266

267267
"mw_log crate" -[hidden]down------> "mw_log_subscriber crate"
268268
"mw_log_fmt crate" -[hidden]down------> "mw_log_subscriber crate"

docs/module/log/detailed_design/_assets/log_op.puml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ participant "mw_log <<module>>" as mw_log
77
end box
88

99
box #LightGreen
10-
participant "mw_log_macro <<module>>" as mw_log_macro
10+
participant "mw_log_fmt_macro <<module>>" as mw_log_fmt_macro
1111
end box
1212

1313
box #LightPink
@@ -36,8 +36,8 @@ alt log-level-check-failed
3636
mw_log --> actor
3737

3838
else log-level-check-passed
39-
mw_log -> mw_log_macro : mw_log_format_args!()
40-
mw_log_macro --> mw_log
39+
mw_log -> mw_log_fmt_macro : mw_log_format_args!()
40+
mw_log_fmt_macro --> mw_log
4141

4242
mw_log -> logger : log()
4343

docs/module/log/detailed_design/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ Log component consists of three units:
3636

3737
- `mw_log` - modelled after `log` Rust library.
3838
- `mw_log_fmt` - replacement for `core::fmt` provided by Rust core library.
39-
- `mw_log_macro` - replacement for `format_args` macro provided by Rust compiler.
39+
- `mw_log_fmt_macro` - replacement for macros provided by Rust compiler:
40+
- `mw_log_format_args!` - replacement for `format_args!`
41+
- `ScoreDebug` - replacement for `Debug`
4042

4143
Most common approach in Rust is that formatting always results in a string.
4244
This means that the `log` library always receives a pre-formatted string.

src/log/mw_log_fmt/builders.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ impl<'a> DebugStruct<'a> {
8080
pub fn finish(&mut self) -> Result {
8181
if self.has_fields {
8282
let empty_spec = FormatSpec::new();
83-
self.result = self.result.and_then(|_| self.writer.write_str(" }", &empty_spec));
83+
self.result = self
84+
.result
85+
.and_then(|_| self.writer.write_str(" }", &empty_spec));
8486
}
8587
self.result
8688
}
@@ -253,7 +255,10 @@ impl<'a> DebugSet<'a> {
253255

254256
/// Finishes output and returns any error encountered.
255257
pub fn finish(&mut self) -> Result {
256-
self.inner.result = self.inner.result.and_then(|_| self.inner.writer.write_str("}", &FormatSpec::new()));
258+
self.inner.result = self
259+
.inner
260+
.result
261+
.and_then(|_| self.inner.writer.write_str("}", &FormatSpec::new()));
257262
self.inner.result
258263
}
259264
}
@@ -323,7 +328,10 @@ impl<'a> DebugList<'a> {
323328

324329
/// Finishes output and returns any error encountered.
325330
pub fn finish(&mut self) -> Result {
326-
self.inner.result = self.inner.result.and_then(|_| self.inner.writer.write_str("]", &FormatSpec::new()));
331+
self.inner.result = self
332+
.inner
333+
.result
334+
.and_then(|_| self.inner.writer.write_str("]", &FormatSpec::new()));
327335
self.inner.result
328336
}
329337
}
@@ -420,7 +428,10 @@ impl<'a> DebugMap<'a> {
420428
F: FnOnce(Writer) -> Result,
421429
{
422430
self.result = self.result.and_then(|_| {
423-
assert!(self.has_key, "attempted to format a map value before its key");
431+
assert!(
432+
self.has_key,
433+
"attempted to format a map value before its key"
434+
);
424435
value_fmt(self.writer)?;
425436
self.has_key = false;
426437
Ok(())
@@ -446,7 +457,10 @@ impl<'a> DebugMap<'a> {
446457
/// Marks the map as non-exhaustive, indicating to the reader that there are some other entries that are not shown in the debug representation.
447458
pub fn finish_non_exhaustive(&mut self) -> Result {
448459
self.result = self.result.and_then(|_| {
449-
assert!(!self.has_key, "attempted to finish a map with a partial entry");
460+
assert!(
461+
!self.has_key,
462+
"attempted to finish a map with a partial entry"
463+
);
450464

451465
let empty_spec = FormatSpec::new();
452466
if self.has_fields {
@@ -466,7 +480,10 @@ impl<'a> DebugMap<'a> {
466480
/// Otherwise this method will panic.
467481
pub fn finish(&mut self) -> Result {
468482
self.result = self.result.and_then(|_| {
469-
assert!(!self.has_key, "attempted to finish a map with a partial entry");
483+
assert!(
484+
!self.has_key,
485+
"attempted to finish a map with a partial entry"
486+
);
470487
let empty_spec = FormatSpec::new();
471488
self.writer.write_str("}", &empty_spec)
472489
});
@@ -542,7 +559,9 @@ mod tests {
542559

543560
let mut writer = StringWriter::new();
544561
let spec = FormatSpec::new();
545-
let _ = DebugStruct::new(&mut writer, &spec, "X").finish().map_err(|_| panic!("failed to finish"));
562+
let _ = DebugStruct::new(&mut writer, &spec, "X")
563+
.finish()
564+
.map_err(|_| panic!("failed to finish"));
546565

547566
assert_eq!(writer.get(), format!("{:?}", v));
548567
}
@@ -594,7 +613,9 @@ mod tests {
594613
fn test_tuple_empty_finish() {
595614
let mut writer = StringWriter::new();
596615
let spec = FormatSpec::new();
597-
let _ = DebugTuple::new(&mut writer, &spec, "").finish().map_err(|_| panic!("failed to finish"));
616+
let _ = DebugTuple::new(&mut writer, &spec, "")
617+
.finish()
618+
.map_err(|_| panic!("failed to finish"));
598619

599620
assert_eq!(writer.get(), "");
600621
}
@@ -730,7 +751,10 @@ mod tests {
730751
.finish_non_exhaustive()
731752
.map_err(|_| panic!("failed to finish"));
732753

733-
assert_eq!(writer.get(), "{\"first\": 123, \"second\": 456, \"third\": 789, ..}");
754+
assert_eq!(
755+
writer.get(),
756+
"{\"first\": 123, \"second\": 456, \"third\": 789, ..}"
757+
);
734758
}
735759

736760
#[test]

src/log/mw_log_fmt/fmt.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ mod tests {
153153
Fragment::Placeholder(Placeholder::new(&-100i8, FormatSpec::new())),
154154
Fragment::Placeholder(Placeholder::new(&-1234i16, FormatSpec::new())),
155155
Fragment::Placeholder(Placeholder::new(&-123456i32, FormatSpec::new())),
156-
Fragment::Placeholder(Placeholder::new(&-1200000000000000000i64, FormatSpec::new())),
156+
Fragment::Placeholder(Placeholder::new(
157+
&-1200000000000000000i64,
158+
FormatSpec::new(),
159+
)),
157160
Fragment::Placeholder(Placeholder::new(&123u8, FormatSpec::new())),
158161
Fragment::Placeholder(Placeholder::new(&1234u16, FormatSpec::new())),
159162
Fragment::Placeholder(Placeholder::new(&123456u32, FormatSpec::new())),
@@ -192,7 +195,10 @@ mod tests {
192195
Fragment::Placeholder(Placeholder::new(&-100i8, FormatSpec::new())),
193196
Fragment::Placeholder(Placeholder::new(&-1234i16, FormatSpec::new())),
194197
Fragment::Placeholder(Placeholder::new(&-123456i32, FormatSpec::new())),
195-
Fragment::Placeholder(Placeholder::new(&-1200000000000000000i64, FormatSpec::new())),
198+
Fragment::Placeholder(Placeholder::new(
199+
&-1200000000000000000i64,
200+
FormatSpec::new(),
201+
)),
196202
Fragment::Placeholder(Placeholder::new(&123u8, FormatSpec::new())),
197203
Fragment::Placeholder(Placeholder::new(&1234u16, FormatSpec::new())),
198204
Fragment::Placeholder(Placeholder::new(&123456u32, FormatSpec::new())),

src/log/mw_log_fmt/fmt_impl_qm.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ impl ScoreDebug for Path {
3535
}
3636

3737
f.write_str(valid, spec)?;
38-
f.write_str(core::char::REPLACEMENT_CHARACTER.encode_utf8(&mut [0; MAX_LEN_UTF8]), spec)?;
38+
f.write_str(
39+
core::char::REPLACEMENT_CHARACTER.encode_utf8(&mut [0; MAX_LEN_UTF8]),
40+
spec,
41+
)?;
3942
}
4043

4144
Ok(())

0 commit comments

Comments
 (0)