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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::collections::HashMap;
use std::collections::hash_map::Iter;

use std::time::SystemTime;


/// Structured logging information to attach to spans.
///
/// Each log has a set of vaules idenfied by strings.
/// Values stored in fields can be of any type convertible to a `LogValue`.
///
/// Logs also have an optional timestamp.
/// If set, the timestamp must be between the start and the end of the span.
///
/// # Examples
///
/// ```
/// extern crate opentracingrust;
///
/// use std::thread;
/// use std::time::Duration;
/// use std::time::SystemTime;
///
/// use opentracingrust::Log;
/// use opentracingrust::tracers::NoopTracer;
///
///
/// fn main() {
///     let (tracer, _) = NoopTracer::new();
///     let mut span = tracer.span("example");
///
///     let time = SystemTime::now();
///     thread::sleep(Duration::from_millis(50));
///
///     let log = Log::new()
///         .log("error", false)
///         .log("event", "some-event")
///         .log("line", 26)
///         .at(time);
///     span.log(log);
/// }
/// ```
#[derive(Debug)]
pub struct Log {
    fields: LogFileds,
    timestamp: Option<SystemTime>,
}

impl Log {
    /// Creates an empty structured log.
    pub fn new() -> Log {
        Log {
            fields: LogFileds::new(),
            timestamp: None,
        }
    }
}

impl Log {
    /// Sets the timestamp associated with the log.
    pub fn at(mut self, timestamp: SystemTime) -> Log {
        self.timestamp = Some(timestamp);
        self
    }

    /// Sets the timestamp to now if not set.
    pub fn at_or_now(&mut self) {
        if let None = self.timestamp {
            self.timestamp = Some(SystemTime::now())
        }
    }

    /// Extend the log fields with the given value.
    ///
    /// If a value with the same key is already in the log the value is replaced.
    pub fn log<LV: Into<LogValue>>(mut self, key: &str, value: LV) -> Log {
        self.fields.log(key.into(), value.into());
        self
    }

    /// Access an iterator over stored fields.
    pub fn iter(&self) -> Iter<String, LogValue> {
        self.fields.iter()
    }

    /// Access the (optional) timestamp for the log.
    pub fn timestamp(&self) -> Option<&SystemTime> {
        self.timestamp.as_ref()
    }
}


/// Structured log fields container.
#[derive(Debug)]
struct LogFileds(HashMap<String, LogValue>);

impl LogFileds {
    /// Creates an empty
    pub fn new() -> LogFileds {
        LogFileds(HashMap::new())
    }

    /// Insert/update a field.
    pub fn log(&mut self, key: String, value: LogValue) {
        self.0.insert(key, value);
    }

    /// Access an iterator over fields.
    pub fn iter(&self) -> Iter<String, LogValue> {
        self.0.iter()
    }
}


/// Enumeration of valid types for log values.
#[derive(Debug, PartialEq)]
pub enum LogValue {
    Boolean(bool),
    Float(f64),
    Integer(i64),
    String(String),
}

impl From<bool> for LogValue {
    fn from(value: bool) -> LogValue {
        LogValue::Boolean(value)
    }
}

impl From<f64> for LogValue {
    fn from(value: f64) -> LogValue {
        LogValue::Float(value)
    }
}

impl From<i64> for LogValue {
    fn from(value: i64) -> LogValue {
        LogValue::Integer(value)
    }
}

impl<'a> From<&'a str> for LogValue {
    fn from(value: &'a str) -> LogValue {
        LogValue::String(String::from(value))
    }
}

impl From<String> for LogValue {
    fn from(value: String) -> LogValue {
        LogValue::String(value)
    }
}


#[cfg(test)]
mod tests {
    use std::time::Duration;
    use std::time::SystemTime;

    use super::Log;
    use super::LogValue;

    #[test]
    fn add_field() {
        let log = Log::new().log("key", "value");
        let entries: Vec<(&String, &LogValue)> = log.iter().collect();
        assert_eq!(entries, [
            (&String::from("key"), &LogValue::String(String::from("value")))
        ]);
    }

    #[test]
    fn defults_to_no_time() {
        match Log::new().timestamp() {
            None => (),
            _ => panic!("Time should not be set")
        }
    }

    #[test]
    fn set_default_timestamp() {
        let start = SystemTime::now();
        let mut log = Log::new();
        log.at_or_now();
        let time = log.timestamp().unwrap();
        let duration = time.duration_since(start).unwrap();
        if duration > Duration::from_millis(100) {
            panic!("Log timestamp too far from expected time");
        }
    }

    #[test]
    fn set_log_timestamp() {
        let time = SystemTime::now();
        let log = Log::new().at(time.clone());
        assert_eq!(&time, log.timestamp().unwrap());
    }
}