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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::io::{self, Write, Stdout, StdoutLock};
use std::fmt;
use std::error::Error;
use log::LogRecord;
use serde_value::Value;
use append::Append;
use append::console::serde::ConsoleAppenderConfig;
use encode::{self, Encode, Style};
use encode::pattern::PatternEncoder;
use encode::writer::{SimpleWriter, ConsoleWriter, ConsoleWriterLock};
use file::{Deserialize, Deserializers};
#[cfg_attr(rustfmt, rustfmt_skip)]
mod serde;
enum Writer {
Tty(ConsoleWriter),
Raw(Stdout),
}
impl Writer {
fn lock<'a>(&'a self) -> WriterLock<'a> {
match *self {
Writer::Tty(ref w) => WriterLock::Tty(w.lock()),
Writer::Raw(ref w) => WriterLock::Raw(SimpleWriter(w.lock())),
}
}
}
enum WriterLock<'a> {
Tty(ConsoleWriterLock<'a>),
Raw(SimpleWriter<StdoutLock<'a>>),
}
impl<'a> io::Write for WriterLock<'a> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match *self {
WriterLock::Tty(ref mut w) => w.write(buf),
WriterLock::Raw(ref mut w) => w.write(buf),
}
}
fn flush(&mut self) -> io::Result<()> {
match *self {
WriterLock::Tty(ref mut w) => w.flush(),
WriterLock::Raw(ref mut w) => w.flush(),
}
}
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
match *self {
WriterLock::Tty(ref mut w) => w.write_all(buf),
WriterLock::Raw(ref mut w) => w.write_all(buf),
}
}
fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
match *self {
WriterLock::Tty(ref mut w) => w.write_fmt(fmt),
WriterLock::Raw(ref mut w) => w.write_fmt(fmt),
}
}
}
impl<'a> encode::Write for WriterLock<'a> {
fn set_style(&mut self, style: &Style) -> io::Result<()> {
match *self {
WriterLock::Tty(ref mut w) => w.set_style(style),
WriterLock::Raw(ref mut w) => w.set_style(style),
}
}
}
pub struct ConsoleAppender {
stdout: Writer,
encoder: Box<Encode>,
}
impl fmt::Debug for ConsoleAppender {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("ConsoleAppender")
.field("encoder", &self.encoder)
.finish()
}
}
impl Append for ConsoleAppender {
fn append(&self, record: &LogRecord) -> Result<(), Box<Error>> {
let mut stdout = self.stdout.lock();
try!(self.encoder.encode(&mut stdout, record));
try!(stdout.flush());
Ok(())
}
}
impl ConsoleAppender {
pub fn builder() -> ConsoleAppenderBuilder {
ConsoleAppenderBuilder { encoder: None }
}
}
pub struct ConsoleAppenderBuilder {
encoder: Option<Box<Encode>>,
}
impl ConsoleAppenderBuilder {
pub fn encoder(mut self, encoder: Box<Encode>) -> ConsoleAppenderBuilder {
self.encoder = Some(encoder);
self
}
pub fn build(self) -> ConsoleAppender {
let stdout = match ConsoleWriter::stdout() {
Some(stdout) => Writer::Tty(stdout),
None => Writer::Raw(io::stdout()),
};
ConsoleAppender {
stdout: stdout,
encoder: self.encoder.unwrap_or_else(|| Box::new(PatternEncoder::default())),
}
}
}
pub struct ConsoleAppenderDeserializer;
impl Deserialize for ConsoleAppenderDeserializer {
type Trait = Append;
fn deserialize(&self,
config: Value,
deserializers: &Deserializers)
-> Result<Box<Append>, Box<Error>> {
let config = try!(config.deserialize_into::<ConsoleAppenderConfig>());
let mut appender = ConsoleAppender::builder();
if let Some(encoder) = config.encoder {
appender = appender.encoder(try!(deserializers.deserialize("encoder",
&encoder.kind,
encoder.config)));
}
Ok(Box::new(appender.build()))
}
}