small_logger/
data_format.rs

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
#[allow(dead_code)]

extern crate time;

use std::collections::HashMap;
use rustc_serialize::{Encoder, Encodable, Decoder, Decodable};
use rustc_serialize::json::decode;
use std::fs::File;
use std::io::Error;
use std::io::BufReader;
use std::io::BufRead;
use std::convert::AsRef;

trait Serializable {
  fn serialized(&self) -> String;
}

impl Serializable for time::Tm {
  fn serialized(&self) -> String {
    let format = "%Y-%m-%d %T.%f";
    let mut ts = time::strftime(format, &self).ok().unwrap();
    let l = ts.len();
    ts.truncate(l-6);
    ts
  }
}

#[derive(Debug)]
struct JsonTime {
  time: time::Tm
}

impl JsonTime {
  fn from_string(time_to_ms: String) -> Result<JsonTime, String> {
    let format = "%Y-%m-%d %T.%f";
    // Add ns to time
    let time = time_to_ms + "000000";
    println!("{}", time);
    match time::strptime(time.as_ref(), format) {
      Ok(ts) => Ok(JsonTime{time: ts}),
      Err(_) => Err(format!("Unable to parse {} as {}", time, format))
    }
  }

  fn to_string(&self) -> String {
    let format = "%Y-%m-%d %T.%f";
    let mut ts = time::strftime(format, &self.time).ok().unwrap();
    let l = ts.len();
    ts.truncate(l-6);
    ts
  }
}

impl Decodable for JsonTime {
  fn decode<D: Decoder>(d: &mut D) -> Result<JsonTime, D::Error> {
    let cropped_time = try!(d.read_str());
    let format = "%Y-%m-%d %T.%f";

    let time = cropped_time + "000000";
    match time::strptime(time.as_ref(), format) {
      Ok(ts) => Ok(JsonTime{time: ts}),
      Err(_) => Err(d.error(format!("Unable to parse {} as {}", time, format).as_ref()))
    }
  }
}

impl Encodable for JsonTime {
  fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
    let format = "%Y-%m-%d %T.%f";
    let mut ts = time::strftime(format, &self.time).ok().unwrap();
    let l = ts.len();
    ts.truncate(l-6);
    s.emit_str(ts.as_ref())
  }
}

#[test]
fn encodeDecodeJsonTime() {
  let t = time::at(time::Timespec::new(236928791, 113000000));
  let jt = JsonTime{time: t};
  let witness = "1977-07-05 07:33:11.113";
  assert_eq!(jt.to_string(), witness);
  assert_eq!(JsonTime::from_string(witness.to_string()).unwrap().time, jt.time);
}


#[derive(Debug)]
#[derive(Clone)]
#[derive(RustcDecodable)]
#[derive(RustcEncodable)]
pub enum LogSource {
  ControlSystem, // Used to control the writer process. Should not appear in the logs
  StdOut,
  StdErr,
  BuildSystem, // Used for system logs in the log files
}

impl LogSource {
  pub fn to_string(&self) -> String {
    match *self {
    LogSource::ControlSystem => "Control System",
    LogSource::StdOut => "stdout",
    LogSource::StdErr => "stderr",
    LogSource::BuildSystem => "Log System",
  }.to_string()
  }
}

#[derive(Debug)]
pub struct TimestampedLine {
  source: LogSource,
  time: time::Tm,
  content: String,
}

#[derive(Debug)]
#[derive(RustcDecodable)]
#[derive(RustcEncodable)]
pub struct DeserializableTimestampedLine {
  source: LogSource,
  time: JsonTime,
  content: String,
}


impl TimestampedLine {
  pub fn tsl(source: LogSource, time: time::Tm, content: String) -> HashMap<String, String> {
    let mut line = HashMap::new();
    line.insert("source".to_string(), source.to_string());
    line.insert("time".to_string(), time.to_utc().serialized());
    line.insert("content".to_string(), content);

    line
  }

  pub fn msg(reason: String) -> HashMap<String, String> {
    TimestampedLine::tsl(LogSource::BuildSystem, time::now(), reason)
  }

  pub fn stop_writer() -> HashMap<String, String> {
    TimestampedLine::tsl(LogSource::ControlSystem, time::now(), "stop".to_string())
  }
}

#[derive(Debug)]
pub struct FileMeta {
  source: LogSource,
  start_time: time::Tm,
  end_time: Option<time::Tm>,
}

impl FileMeta {
  pub fn fast_meta(source: String) -> Result<FileMeta, Error> {
    println!("{}", source);
    let f = try!(File::open(source.clone()));
    let mut t = BufReader::new(f);
    let mut l = String::new();
    match t.read_line(&mut l) {
      Ok(_) => {
        match decode::<DeserializableTimestampedLine>(l.as_ref()) {
          Ok(line) => println!("{:?}", line),
          Err(e) => panic!("Failed reading and decoding line {}: {}", l, e)
        }
      }
      Err(e) => panic!("{}", e)
    }

    Ok(FileMeta{source: LogSource::BuildSystem, start_time: time::now(), end_time: None})
  }
}