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
use failure;
use libflate::gzip;
use libflate::zlib;
use std::io;
use errors::{Error, Result};
use message::WireMessage;
#[derive(PartialEq, Clone, Copy)]
pub enum MessageCompression {
None,
Gzip,
Zlib,
}
impl MessageCompression {
pub fn default() -> MessageCompression {
MessageCompression::Gzip
}
pub fn compress(&self, message: &WireMessage) -> Result<Vec<u8>> {
let json = message.to_gelf()?;
Ok(match *self {
MessageCompression::None => json.into_bytes(),
MessageCompression::Gzip => {
let mut cursor = io::Cursor::new(json);
gzip::Encoder::new(Vec::new())
.and_then(|mut encoder| {
io::copy(&mut cursor, &mut encoder)
.and_then(|_| encoder.finish().into_result())
})
.map_err(|e| {
failure::Error::from(e).context(Error::CompressMessageFailed {
compression_method: "gzip",
})
})?
}
MessageCompression::Zlib => {
let mut cursor = io::Cursor::new(json);
zlib::Encoder::new(Vec::new())
.and_then(|mut encoder| {
io::copy(&mut cursor, &mut encoder)
.and_then(|_| encoder.finish().into_result())
})
.map_err(|e| {
failure::Error::from(e).context(Error::CompressMessageFailed {
compression_method: "zlib",
})
})?
}
})
}
}