Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit c2aedee

Browse files
committed
feat: intro tracing logging system
Signed-off-by: Chojan Shang <[email protected]>
1 parent c033ed5 commit c2aedee

File tree

3 files changed

+26
-57
lines changed

3 files changed

+26
-57
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ edition = "2018"
1414
exclude = ["/.github/*"]
1515

1616
[dependencies]
17-
log = { version = "0.4", features = ["std"] }
1817
lazy_static = "1.4"
1918
parking_lot = "0.11"
2019
rand = "0.8"
20+
tracing = "0.1"
21+
22+
[dev-dependencies]
23+
tracing-test = "0.1"
2124

2225
[features]
2326
failpoints = []

src/lib.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@ impl FailPoint {
498498
None => panic!("failpoint {} panic", name),
499499
},
500500
Task::Print(msg) => match msg {
501-
Some(ref msg) => log::info!("{}", msg),
502-
None => log::info!("failpoint {} executed.", name),
501+
Some(ref msg) => tracing::info!("{}", msg),
502+
None => tracing::info!("failpoint {} executed.", name),
503503
},
504504
Task::Pause => unreachable!(),
505505
Task::Yield => thread::yield_now(),
@@ -661,7 +661,7 @@ pub fn eval<R, F: FnOnce(Option<String>) -> R>(name: &str, f: F) -> Option<R> {
661661
/// defined via the `failpoint!` macro) as a string.
662662
/// - `sleep(milliseconds)`, sleep for the specified time.
663663
/// - `panic(msg)`, panic with the message.
664-
/// - `print(msg)`, log the message, using the `log` crate, at the `info` level.
664+
/// - `print(msg)`, log the message, using the `tracing` crate, at the `info` level.
665665
/// - `pause`, sleep until other action is set to the fail point.
666666
/// - `yield`, yield the CPU.
667667
/// - `delay(milliseconds)`, busy waiting for the specified time.
@@ -868,30 +868,13 @@ mod tests {
868868
point.eval("test_failpoint_panic");
869869
}
870870

871+
#[tracing_test::traced_test]
871872
#[test]
872873
fn test_print() {
873-
struct LogCollector(Arc<Mutex<Vec<String>>>);
874-
impl log::Log for LogCollector {
875-
fn enabled(&self, _: &log::Metadata) -> bool {
876-
true
877-
}
878-
fn log(&self, record: &log::Record) {
879-
let mut buf = self.0.lock();
880-
buf.push(format!("{}", record.args()));
881-
}
882-
fn flush(&self) {}
883-
}
884-
885-
let buffer = Arc::new(Mutex::new(vec![]));
886-
let collector = LogCollector(buffer.clone());
887-
log::set_max_level(log::LevelFilter::Info);
888-
log::set_boxed_logger(Box::new(collector)).unwrap();
889-
890874
let point = FailPoint::new();
891875
point.set_actions("", vec![Action::new(Task::Print(None), 1.0, None)]);
892876
assert!(point.eval("test_failpoint_print").is_none());
893-
let msg = buffer.lock().pop().unwrap();
894-
assert_eq!(msg, "failpoint test_failpoint_print executed.");
877+
assert!(logs_contain("failpoint test_failpoint_print executed."));
895878
}
896879

897880
#[test]
@@ -1031,3 +1014,20 @@ mod tests {
10311014
assert_eq!(f1(), 0);
10321015
}
10331016
}
1017+
1018+
// TODO: move it to test.rs, once tracing-test is fixed
1019+
#[test]
1020+
#[tracing_test::traced_test]
1021+
#[cfg_attr(not(feature = "failpoints"), ignore)]
1022+
fn test_failpoints_print() {
1023+
let f = || {
1024+
failpoint!("print");
1025+
};
1026+
crate::cfg("print", "print(msg)").unwrap();
1027+
f();
1028+
assert!(logs_contain("msg"));
1029+
1030+
crate::cfg("print", "print").unwrap();
1031+
f();
1032+
assert!(logs_contain("failpoint print executed."));
1033+
}

tests/tests.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,40 +63,6 @@ fn test_panic() {
6363
f();
6464
}
6565

66-
#[test]
67-
#[cfg_attr(not(feature = "failpoints"), ignore)]
68-
fn test_print() {
69-
struct LogCollector(Arc<Mutex<Vec<String>>>);
70-
impl log::Log for LogCollector {
71-
fn enabled(&self, _: &log::Metadata) -> bool {
72-
true
73-
}
74-
fn log(&self, record: &log::Record) {
75-
let mut buf = self.0.lock().unwrap();
76-
buf.push(format!("{}", record.args()));
77-
}
78-
fn flush(&self) {}
79-
}
80-
81-
let buffer = Arc::new(Mutex::new(vec![]));
82-
let collector = LogCollector(buffer.clone());
83-
log::set_max_level(log::LevelFilter::Info);
84-
log::set_boxed_logger(Box::new(collector)).unwrap();
85-
86-
let f = || {
87-
failpoint!("print");
88-
};
89-
failpoints::cfg("print", "print(msg)").unwrap();
90-
f();
91-
let msg = buffer.lock().unwrap().pop().unwrap();
92-
assert_eq!(msg, "msg");
93-
94-
failpoints::cfg("print", "print").unwrap();
95-
f();
96-
let msg = buffer.lock().unwrap().pop().unwrap();
97-
assert_eq!(msg, "failpoint print executed.");
98-
}
99-
10066
#[test]
10167
#[cfg_attr(not(feature = "failpoints"), ignore)]
10268
fn test_pause() {

0 commit comments

Comments
 (0)