1 use lightning::util::logger::{Logger, Record};
2 use std::sync::{Arc, Mutex};
5 pub trait Output : Clone + Sync + Send + 'static {
6 fn locked_write(&self, data: &[u8]);
11 impl Output for DevNull {
12 fn locked_write(&self, _data: &[u8]) {}
15 pub struct StringBuffer(Arc<Mutex<String>>);
16 impl Output for StringBuffer {
17 fn locked_write(&self, data: &[u8]) {
18 self.0.lock().unwrap().push_str(&String::from_utf8(data.to_vec()).unwrap());
22 pub fn new() -> Self {
23 Self(Arc::new(Mutex::new(String::new())))
25 pub fn into_string(self) -> String {
26 Arc::try_unwrap(self.0).unwrap().into_inner().unwrap()
30 pub struct TestLogger<Out : Output> {
34 impl<Out: Output> TestLogger<Out> {
35 pub fn new(id: String, out: Out) -> TestLogger<Out> {
36 TestLogger { id, out }
40 struct LockedWriteAdapter<'a, Out: Output>(&'a Out);
41 impl<'a, Out: Output> Write for LockedWriteAdapter<'a, Out> {
42 fn write(&mut self, data: &[u8]) -> Result<usize, std::io::Error> {
43 self.0.locked_write(data);
46 fn flush(&mut self) -> Result<(), std::io::Error> { Ok(()) }
49 impl<Out: Output> Logger for TestLogger<Out> {
50 fn log(&self, record: &Record) {
51 write!(LockedWriteAdapter(&self.out),
52 "{:<5} {} [{} : {}, {}] {}\n", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args)