-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: log significant events with
Display
This commit refines our logging strategy by utilizing the `Display` trait instead of `Debug` for significant events. This change is aimed at producing logs that are easier to read and understand. Three kinds of significant events are logged at DEBUG level: - `input`: the `RaftMsg`s received by `RaftCore`, such as client-write or AppendEntries request from the Leader. - `cmd`: the `Command` outputted by `Engine` to execute by storage or network layer, such as `AppendInputEntries` or `ReplicateCommitted`. - `notify`: the `Notification`s received by `RaftCore` from storage or network. Example significant event logs: ``` RAFT_event id=0 cmd: Commit: seq: 5, (T1-N0.4, T1-N0.5] RAFT_event id=1 input: AppendEntries: vote=<T1-N0:Q>, prev_log_id=T1-N0.5, leader_commit=T1-N0.5, entries=[] RAFT_event id=0 notify: sm::Result(command_seq:5, Ok(ApplyResult([5, 6), last_applied=T1-N0.5, entries=[T1-N0.5]))) RAFT_event id=0 input: ClientWriteRequest ```
- Loading branch information
1 parent
3ae6b4b
commit 53abbfc
Showing
21 changed files
with
311 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::fmt; | ||
|
||
/// Implement `Display` for `Result<T,E>` if T and E are `Display`. | ||
/// | ||
/// It outputs `"Ok(...)"` or `"Err(...)"`. | ||
pub(crate) struct DisplayResult<'a, T: fmt::Display, E: fmt::Display>(pub &'a Result<T, E>); | ||
|
||
impl<'a, T, E> fmt::Display for DisplayResult<'a, T, E> | ||
where | ||
T: fmt::Display, | ||
E: fmt::Display, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match &self.0 { | ||
Ok(ok) => { | ||
write!(f, "Ok({})", ok) | ||
} | ||
Err(err) => { | ||
write!(f, "Err({})", err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub(crate) trait DisplayResultExt<'a, T: fmt::Display, E: fmt::Display> { | ||
fn display(&'a self) -> DisplayResult<'a, T, E>; | ||
} | ||
|
||
impl<T, E> DisplayResultExt<'_, T, E> for Result<T, E> | ||
where | ||
T: fmt::Display, | ||
E: fmt::Display, | ||
{ | ||
fn display(&self) -> DisplayResult<T, E> { | ||
DisplayResult(self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_display_result() { | ||
let result: Result<i32, &str> = Ok(42); | ||
let display_result = DisplayResult(&result); | ||
assert_eq!(format!("{}", display_result), "Ok(42)"); | ||
|
||
let result: Result<i32, &str> = Err("error"); | ||
let display_result = DisplayResult(&result); | ||
assert_eq!(format!("{}", display_result), "Err(error)"); | ||
} | ||
} |
Oops, something went wrong.