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 log::Record;
#[cfg(feature = "file")]
use serde::de;
#[cfg(feature = "file")]
use serde_value::Value;
#[cfg(feature = "file")]
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;
use std::io;
#[cfg(feature = "file")]
use file::Deserializable;
#[cfg(feature = "json_encoder")]
pub mod json;
#[cfg(feature = "pattern_encoder")]
pub mod pattern;
pub mod writer;
#[allow(dead_code)]
#[cfg(windows)]
const NEWLINE: &'static str = "\r\n";
#[allow(dead_code)]
#[cfg(not(windows))]
const NEWLINE: &'static str = "\n";
pub trait Encode: fmt::Debug + Send + Sync + 'static {
fn encode(&self, w: &mut Write, record: &Record) -> Result<(), Box<Error + Sync + Send>>;
}
#[cfg(feature = "file")]
impl Deserializable for Encode {
fn name() -> &'static str {
"encoder"
}
}
#[cfg(feature = "file")]
pub struct EncoderConfig {
pub kind: String,
pub config: Value,
}
#[cfg(feature = "file")]
impl<'de> de::Deserialize<'de> for EncoderConfig {
fn deserialize<D>(d: D) -> Result<EncoderConfig, D::Error>
where
D: de::Deserializer<'de>,
{
let mut map = BTreeMap::<Value, Value>::deserialize(d)?;
let kind = match map.remove(&Value::String("kind".to_owned())) {
Some(kind) => kind.deserialize_into().map_err(|e| e.to_error())?,
None => "pattern".to_owned(),
};
Ok(EncoderConfig {
kind: kind,
config: Value::Map(map),
})
}
}
#[derive(Copy, Clone, Debug)]
#[allow(missing_docs)]
pub enum Color {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
}
#[derive(Clone, Default)]
pub struct Style {
pub text: Option<Color>,
pub background: Option<Color>,
pub intense: Option<bool>,
_p: (),
}
impl fmt::Debug for Style {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Style")
.field("text", &self.text)
.field("background", &self.background)
.field("intense", &self.intense)
.finish()
}
}
impl Style {
pub fn new() -> Style {
Style::default()
}
pub fn text(&mut self, text: Color) -> &mut Style {
self.text = Some(text);
self
}
pub fn background(&mut self, background: Color) -> &mut Style {
self.background = Some(background);
self
}
pub fn intense(&mut self, intense: bool) -> &mut Style {
self.intense = Some(intense);
self
}
}
pub trait Write: io::Write {
#[allow(unused_variables)]
fn set_style(&mut self, style: &Style) -> io::Result<()> {
Ok(())
}
}
impl<'a, W: Write + ?Sized> Write for &'a mut W {
fn set_style(&mut self, style: &Style) -> io::Result<()> {
<W as Write>::set_style(*self, style)
}
}