Don't print file paths in fuzz logger as they can be very long
[rust-lightning] / fuzz / src / utils / test_logger.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 use lightning::util::logger::{Logger, Record};
11 use std::sync::{Arc, Mutex};
12 use std::io::Write;
13
14 pub trait Output : Clone  + 'static {
15         fn locked_write(&self, data: &[u8]);
16 }
17
18 #[derive(Clone)]
19 pub struct DevNull {}
20 impl Output for DevNull {
21         fn locked_write(&self, _data: &[u8]) {}
22 }
23 #[derive(Clone)]
24 pub struct StringBuffer(Arc<Mutex<String>>);
25 impl Output for StringBuffer {
26         fn locked_write(&self, data: &[u8]) {
27                 self.0.lock().unwrap().push_str(&String::from_utf8(data.to_vec()).unwrap());
28         }
29 }
30 impl StringBuffer {
31         pub fn new() -> Self {
32                 Self(Arc::new(Mutex::new(String::new())))
33         }
34         pub fn into_string(self) -> String {
35                 Arc::try_unwrap(self.0).unwrap().into_inner().unwrap()
36         }
37 }
38
39 pub struct TestLogger<Out : Output> {
40         id: String,
41         out: Out,
42 }
43 impl<Out: Output> TestLogger<Out> {
44         pub fn new(id: String, out: Out) -> TestLogger<Out> {
45                 TestLogger { id, out }
46         }
47 }
48
49 struct LockedWriteAdapter<'a, Out: Output>(&'a Out);
50 impl<'a, Out: Output> Write for LockedWriteAdapter<'a, Out> {
51         fn write(&mut self, data: &[u8]) -> Result<usize, std::io::Error> {
52                 self.0.locked_write(data);
53                 Ok(data.len())
54         }
55         fn flush(&mut self) -> Result<(), std::io::Error> { Ok(()) }
56 }
57
58 impl<Out: Output> Logger for TestLogger<Out> {
59         fn log(&self, record: &Record) {
60                 write!(LockedWriteAdapter(&self.out),
61                         "{:<5} {} [{} : {}] {}\n", record.level.to_string(), self.id, record.module_path, record.line, record.args)
62                         .unwrap();
63         }
64 }