1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! Native console consumer implementation.

use crate::entry::Entry;
use crate::entry;
use crate::processor::consumer;



// ===============================
// === Native Console Consumer ===
// ===============================

/// A simple consumer which uses `println!` to simulate hierarchical logging.
#[derive(Clone,Copy,Debug,Default)]
pub struct NativeConsole {
    depth           : usize,
    collapsed_depth : usize,
}

impl NativeConsole {
    fn print(&self, msg:String) {
        if self.collapsed_depth == 0 {
            if self.depth == 0 {
                println!("{}",msg)
            } else {
                let pfx = " ".repeat(4 * self.depth);
                println!("{}{}",pfx,msg)
            }
        }
    }
}

impl<Levels> consumer::Definition<Levels,String> for NativeConsole {
    fn consume(&mut self, event:Entry<Levels>, message:Option<String>) {
        match &event.content {
            entry::Content::Message(_) => {
                if let Some(msg) = message {
                    self.print(msg);
                }
            },
            entry::Content::GroupBegin(group) => {
                if let Some(msg) = message {
                    self.print(msg);
                }
                if group.collapsed {
                    self.collapsed_depth += 1
                } else {
                    self.depth += 1
                }
            },
            entry::Content::GroupEnd => {
                if self.collapsed_depth > 0 {
                    self.collapsed_depth -= 1
                } else {
                    self.depth -= 1
                }
            }
        }
    }
}