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
//! Serde serialization adapter for slog-rs

#![warn(missing_docs)]

#[macro_use]
extern crate slog;
extern crate serde;

use std::{io, fmt};
use slog::ser;
use std::cell::RefCell;
use std::fmt::Write;

thread_local! {
    static TL_BUF: RefCell<String> = RefCell::new(String::with_capacity(128))
}

/// slog-rs's `Serializer` adapter for `serde::Serializer`
///
/// Newtype to wrap serde Serializer, so that `Serialize` can be implemented
/// for it
pub struct SerdeSerializer<'a, S: 'a + serde::Serializer>(pub &'a mut S);

impl<'a, S> slog::ser::Serializer for SerdeSerializer<'a, S>
    where S: 'a + serde::Serializer
{
    fn emit_bool(&mut self, key: &str, val: bool) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }

    fn emit_unit(&mut self, key: &str) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, ())
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }

    fn emit_char(&mut self, key: &str, val: char) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }

    fn emit_none(&mut self, key: &str) -> ser::Result {
        let none: Option<()> = None;
        serde::Serializer::serialize_map_elt(self.0, key, none)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }

    fn emit_u8(&mut self, key: &str, val: u8) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_i8(&mut self, key: &str, val: i8) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_u16(&mut self, key: &str, val: u16) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_i16(&mut self, key: &str, val: i16) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_usize(&mut self, key: &str, val: usize) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_isize(&mut self, key: &str, val: isize) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_u32(&mut self, key: &str, val: u32) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_i32(&mut self, key: &str, val: i32) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_f32(&mut self, key: &str, val: f32) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_u64(&mut self, key: &str, val: u64) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_i64(&mut self, key: &str, val: i64) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_f64(&mut self, key: &str, val: f64) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_str(&mut self, key: &str, val: &str) -> ser::Result {
        serde::Serializer::serialize_map_elt(self.0, key, val)
            .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
    }
    fn emit_arguments(&mut self, key: &str, val: &fmt::Arguments) -> ser::Result {

        TL_BUF.with(|buf| {
            let mut buf = buf.borrow_mut();

            buf.write_fmt(*val).unwrap();

            let res = {
                || {
                    serde::Serializer::serialize_map_elt(self.0, key, &*buf)
                        .map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())

                }
            }();
            buf.clear();
            res
        })
    }
}