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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright (C) 2017-2019 Guillaume Desmottes <guillaume@desmottes.be>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Lines;
use std::io::Read;
use std::str;
extern crate itertools;
use itertools::join;

extern crate gstreamer as gst;
use gst::{ClockTime, DebugLevel, Structure};

#[macro_use]
extern crate lazy_static;
extern crate regex;
#[macro_use]
extern crate failure;

use regex::Regex;

#[derive(Debug, PartialEq)]
pub enum TimestampField {
    Hour,
    Minute,
    Second,
    SubSecond,
}

#[derive(Debug, PartialEq)]
pub enum Token {
    Timestamp { field: Option<TimestampField> },
    PID,
    Thread,
    Level,
    Category,
    File,
    LineNumber,
    Function,
    Message,
    Object,
}

#[derive(Debug, Fail, PartialEq)]
pub enum ParsingError {
    #[fail(display = "invalid debug level: {}", name)]
    InvalidDebugLevel { name: String },
    #[fail(display = "invalid timestamp: {} : {:?}", ts, field)]
    InvalidTimestamp { ts: String, field: TimestampField },
    #[fail(display = "missing token: {:?}", t)]
    MissingToken { t: Token },
    #[fail(display = "invalid PID: {}", pid)]
    InvalidPID { pid: String },
    #[fail(display = "missing location")]
    MissingLocation,
    #[fail(display = "invalid line number: {}", line)]
    InvalidLineNumber { line: String },
}

#[derive(Debug)]
pub struct Entry {
    pub ts: ClockTime,
    pub pid: u32,
    pub thread: String,
    pub level: DebugLevel,
    pub category: String,
    pub file: String,
    pub line: u32,
    pub function: String,
    pub message: String,
    pub object: Option<String>,
}

fn parse_debug_level(s: &str) -> Result<DebugLevel, ParsingError> {
    match s {
        "ERROR" => Ok(DebugLevel::Error),
        "WARN" => Ok(DebugLevel::Warning),
        "FIXME" => Ok(DebugLevel::Fixme),
        "INFO" => Ok(DebugLevel::Info),
        "DEBUG" => Ok(DebugLevel::Debug),
        "LOG" => Ok(DebugLevel::Log),
        "TRACE" => Ok(DebugLevel::Trace),
        "MEMDUMP" => Ok(DebugLevel::Memdump),
        _ => Err(ParsingError::InvalidDebugLevel {
            name: s.to_string(),
        }),
    }
}

fn parse_time(ts: &str) -> Result<ClockTime, ParsingError> {
    let mut split = ts.splitn(3, ':');
    let h: u64 = split
        .next()
        .ok_or_else(|| ParsingError::MissingToken {
            t: Token::Timestamp {
                field: Some(TimestampField::Hour),
            },
        })?
        .parse()
        .map_err(|_e| ParsingError::InvalidTimestamp {
            ts: ts.to_string(),
            field: TimestampField::Hour,
        })?;

    let m: u64 = split
        .next()
        .ok_or_else(|| ParsingError::MissingToken {
            t: Token::Timestamp {
                field: Some(TimestampField::Minute),
            },
        })?
        .parse()
        .map_err(|_e| ParsingError::InvalidTimestamp {
            ts: ts.to_string(),
            field: TimestampField::Minute,
        })?;

    split = split
        .next()
        .ok_or_else(|| ParsingError::MissingToken {
            t: Token::Timestamp {
                field: Some(TimestampField::Second),
            },
        })?
        .splitn(2, '.');
    let secs: u64 = split
        .next()
        .ok_or_else(|| ParsingError::MissingToken {
            t: Token::Timestamp {
                field: Some(TimestampField::Second),
            },
        })?
        .parse()
        .map_err(|_e| ParsingError::InvalidTimestamp {
            ts: ts.to_string(),
            field: TimestampField::Second,
        })?;

    let subsecs: u64 = split
        .next()
        .ok_or_else(|| ParsingError::MissingToken {
            t: Token::Timestamp {
                field: Some(TimestampField::SubSecond),
            },
        })?
        .parse()
        .map_err(|_e| ParsingError::InvalidTimestamp {
            ts: ts.to_string(),
            field: TimestampField::SubSecond,
        })?;

    Ok(ClockTime::from_seconds(h * 60 * 60 + m * 60 + secs) + ClockTime::from_nseconds(subsecs))
}

fn split_location(location: &str) -> Result<(String, u32, String, Option<String>), ParsingError> {
    let mut split = location.splitn(4, ':');
    let file = split
        .next()
        .ok_or_else(|| ParsingError::MissingToken { t: Token::File })?;
    let line_str = split.next().ok_or_else(|| ParsingError::MissingToken {
        t: Token::LineNumber,
    })?;
    let line = line_str
        .parse()
        .map_err(|_e| ParsingError::InvalidLineNumber {
            line: line_str.to_string(),
        })?;

    let function = split
        .next()
        .ok_or_else(|| ParsingError::MissingToken { t: Token::Function })?;

    let object = split
        .next()
        .ok_or_else(|| ParsingError::MissingToken { t: Token::Object })?;

    let object_name = {
        if !object.is_empty() {
            let object = object
                .to_string()
                .trim_start_matches('<')
                .trim_end_matches('>')
                .to_string();

            Some(object)
        } else {
            None
        }
    };

    Ok((file.to_string(), line, function.to_string(), object_name))
}

impl Entry {
    fn new(line: &str) -> Result<Entry, ParsingError> {
        // Strip color codes
        lazy_static! {
            static ref RE: Regex = Regex::new("\x1b\\[[0-9;]*m").unwrap();
        }
        let line = RE.replace_all(&line, "");

        let mut it = line.split(' ');
        let ts_str = it.next().ok_or_else(|| ParsingError::MissingToken {
            t: Token::Timestamp { field: None },
        })?;
        let ts = parse_time(ts_str)?;

        let mut it = it.skip_while(|x| x.is_empty());
        let pid_str = it
            .next()
            .ok_or_else(|| ParsingError::MissingToken { t: Token::PID })?;
        let pid = pid_str.parse().map_err(|_e| ParsingError::InvalidPID {
            pid: pid_str.to_string(),
        })?;

        let mut it = it.skip_while(|x| x.is_empty());
        let thread = it
            .next()
            .ok_or_else(|| ParsingError::MissingToken { t: Token::Thread })?
            .to_string();

        let mut it = it.skip_while(|x| x.is_empty());
        let level_str = it
            .next()
            .ok_or_else(|| ParsingError::MissingToken { t: Token::Level })?;
        let level = parse_debug_level(level_str)?;

        let mut it = it.skip_while(|x| x.is_empty());
        let category = it
            .next()
            .ok_or_else(|| ParsingError::MissingToken { t: Token::Category })?
            .to_string();

        let mut it = it.skip_while(|x| x.is_empty());
        let location_str = it.next().ok_or_else(|| ParsingError::MissingLocation)?;
        let (file, line, function, object) = split_location(location_str)?;
        let message: String = join(it, " ");

        Ok(Entry {
            ts,
            pid,
            thread,
            level,
            category,
            file,
            line,
            function,
            object,
            message,
        })
    }

    pub fn message_to_struct(&self) -> Option<Structure> {
        Structure::from_string(&self.message)
    }
}

impl fmt::Display for Entry {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}  {} {} {:?} {} {}:{}:{}:<{}> {}",
            self.ts,
            self.pid,
            self.thread,
            self.level,
            self.category,
            self.file,
            self.line,
            self.function,
            self.object.clone().unwrap_or_else(|| "".to_string()),
            self.message
        )
    }
}

pub struct ParserIterator<R: Read> {
    lines: Lines<BufReader<R>>,
}

impl<R: Read> ParserIterator<R> {
    fn new(lines: Lines<BufReader<R>>) -> Self {
        Self { lines }
    }
}

impl<R: Read> Iterator for ParserIterator<R> {
    type Item = Entry;

    fn next(&mut self) -> Option<Entry> {
        match self.lines.next() {
            None => None,
            Some(line) => match Entry::new(&line.unwrap()) {
                Ok(entry) => Some(entry),
                Err(_err) => None,
            },
        }
    }
}

pub fn parse<R: Read>(r: R) -> ParserIterator<R> {
    gst::init().expect("Failed to initialize gst");

    let file = BufReader::new(r);

    ParserIterator::new(file.lines())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;

    #[test]
    fn no_color() {
        let f = File::open("test-logs/nocolor.log").expect("Failed to open log file");
        let mut parsed = parse(f);

        let entry = parsed.next().expect("First entry missing");
        assert_eq!(entry.ts.nanoseconds().unwrap(), 7773544);
        assert_eq!(format!("{}", entry.ts), "00:00:00.007773544");
        assert_eq!(entry.pid, 8874);
        assert_eq!(entry.thread, "0x558951015c00");
        assert_eq!(entry.level, DebugLevel::Info);
        assert_eq!(entry.category, "GST_INIT");
        assert_eq!(entry.file, "gst.c");
        assert_eq!(entry.line, 510);
        assert_eq!(entry.function, "init_pre");
        assert_eq!(
            entry.message,
            "Initializing GStreamer Core Library version 1.10.4"
        );

        let entry = parsed.nth(3).expect("3th entry missing");
        assert_eq!(entry.message, "0x55895101d040 ref 1->2");
        assert_eq!(entry.object, Some("allocatorsysmem0".to_string()));
    }

    #[test]
    fn color() {
        let f = File::open("test-logs/color.log").expect("Failed to open log file");
        let mut parsed = parse(f);

        let entry = parsed.next().expect("First entry missing");
        assert_eq!(entry.ts.nanoseconds().unwrap(), 208614);
        assert_eq!(format!("{}", entry.ts), "00:00:00.000208614");
        assert_eq!(entry.pid, 17267);
        assert_eq!(entry.thread, "0x2192200");
        assert_eq!(entry.level, DebugLevel::Info);
        assert_eq!(entry.category, "GST_INIT");
        assert_eq!(entry.file, "gst.c");
        assert_eq!(entry.line, 584);
        assert_eq!(entry.function, "init_pre");
        assert_eq!(
            entry.message,
            "Initializing GStreamer Core Library version 1.13.0.1"
        );

        assert_eq!(parsed.count(), 14);
    }

    #[test]
    fn corrupted() {
        let f = File::open("test-logs/corrupted-nocolor.log").expect("Failed to open log file");
        let mut parsed = parse(f);

        assert!(parsed.next().is_none());
        let entry = parsed.next().expect("First entry missing");
        assert_eq!(entry.ts.nanoseconds().unwrap(), 7773544);
        assert_eq!(format!("{}", entry.ts), "00:00:00.007773544");
        assert_eq!(entry.pid, 8874);
        assert_eq!(entry.thread, "0x558951015c00");
        assert_eq!(entry.level, DebugLevel::Info);
        assert_eq!(entry.category, "GST_INIT");
        assert_eq!(entry.file, "gst.c");
        assert_eq!(entry.line, 510);
        assert_eq!(entry.function, "init_pre");
        assert_eq!(
            entry.message,
            "Initializing GStreamer Core Library version 1.10.4"
        );

        let entry = parsed.nth(3).expect("3th entry missing");
        assert_eq!(entry.message, "0x55895101d040 ref 1->2");
        assert_eq!(entry.object, Some("allocatorsysmem0".to_string()));
    }

    #[test]
    fn timestamps() {
        assert!(Entry::new("foo").is_err());

        let e1 = "e:00:00.007773544  8874 0x558951015c00 INFO                GST_INIT gst.c:510:init_pre: Init";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(
                e,
                ParsingError::InvalidTimestamp {
                    ts: "e:00:00.007773544".to_string(),
                    field: TimestampField::Hour,
                }
            ),
        };

        let e1 = ":00:00.007773544  8874 0x558951015c00 INFO                GST_INIT gst.c:510:init_pre: Init";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(
                e,
                ParsingError::InvalidTimestamp {
                    ts: ":00:00.007773544".to_string(),
                    field: TimestampField::Hour,
                }
            ),
        };

        let e1 = "8874 0x558951015c00 INFO                GST_INIT gst.c:510:init_pre: Init";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(
                e,
                ParsingError::MissingToken {
                    t: Token::Timestamp {
                        field: Some(TimestampField::Minute)
                    },
                }
            ),
        };
    }

    #[test]
    fn pid() {
        let e1 = "00:00:00.007773544 ";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(e, ParsingError::MissingToken { t: Token::PID }),
        };

        let e1 = "00:00:00.007773544  8fuz874 0x558951015c00 INFO                GST_INIT gst.c:510:init_pre: Init";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(
                e,
                ParsingError::InvalidPID {
                    pid: "8fuz874".to_string(),
                }
            ),
        };
    }

    #[test]
    fn thread() {
        let e1 = "00:00:00.007773544  8874 ";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(e, ParsingError::MissingToken { t: Token::Thread }),
        };
    }

    #[test]
    fn debug_level() {
        let e1 = "00:00:00.007773544  8874 0x558951015c00 ";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(e, ParsingError::MissingToken { t: Token::Level }),
        };

        let e1 = "00:00:00.007773544  8874 0x558951015c00 FUZZLEVEL                GST_INIT gst.c:510:init_pre: Init";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(
                e,
                ParsingError::InvalidDebugLevel {
                    name: "FUZZLEVEL".to_string(),
                }
            ),
        };
    }

    #[test]
    fn category() {
        let e1 = "00:00:00.007773544  8874 0x558951015c00 INFO";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(e, ParsingError::MissingToken { t: Token::Category }),
        };
    }

    #[test]
    fn location() {
        let e1 = "00:00:00.007773544  8874 0x558951015c00 INFO GST_INIT";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(e, ParsingError::MissingLocation {}),
        };

        let e1 = "00:00:00.007773544  8874 0x558951015c00 INFO GST_INIT gst.c";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(
                e,
                ParsingError::MissingToken {
                    t: Token::LineNumber
                }
            ),
        };

        let e1 = "00:00:00.007773544  8874 0x558951015c00 INFO GST_INIT gst.c:fuzz";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(
                e,
                ParsingError::InvalidLineNumber {
                    line: "fuzz".to_string()
                }
            ),
        };

        let e1 = "00:00:00.007773544  8874 0x558951015c00 INFO GST_INIT gst.c:510";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(e, ParsingError::MissingToken { t: Token::Function }),
        };

        let e1 = "00:00:00.007773544  8874 0x558951015c00 INFO GST_INIT gst.c:510:";
        match Entry::new(e1) {
            Ok(_) => assert!(false),
            Err(e) => assert_eq!(e, ParsingError::MissingToken { t: Token::Object }),
        };
    }
}