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
use chrono::NaiveDateTime as DateTime;
use enum_as_inner::EnumAsInner;
pub mod log;
pub mod world;
pub use log::Log;
pub use world::{Instance, InstanceLog, InstanceLogList};
#[derive(Debug, EnumAsInner)]
pub enum LogEnum {
Log(Log),
Warning { date: DateTime, msg: Vec<String> },
Error { date: DateTime, msg: Vec<String> },
Exception { date: DateTime, msg: Vec<String> },
Unknown(String),
}
pub fn from_str(s: &str) -> Result<Vec<LogEnum>, ()> {
let mut ret = Vec::<LogEnum>::new();
for log in s.split("\n\r\n") {
let log = LogEnum::from_str(log);
if let Ok(log) = log {
ret.push(log);
}
}
Ok(ret)
}
impl LogEnum {
pub fn from_str(s: &str) -> Result<Self, ()> {
if s.chars().nth(31) != Some('-') {
return Err(());
}
let date = s[0..19].to_string();
let date = DateTime::parse_from_str(&date, "%Y.%m.%d %H:%M:%S").unwrap();
let typ = &s[20..31].split_whitespace().next().unwrap();
let content = &s[34..];
let (mtyp, msg) = if content.chars().nth(0) == Some('[') {
let n = content.find("]").unwrap();
(Some(&content[1..n]), &content[n + 2..])
} else {
(None, content)
};
let mut msg: Vec<String> = msg
.lines()
.map(|s| s.trim_start_matches(' ').to_string())
.collect();
msg.retain(|s| !s.is_empty());
let msg = msg;
let log = match typ {
&"Log" => LogEnum::Log(Log::new(date, mtyp, msg)),
&"Warning" => LogEnum::Warning { date, msg },
&"Error" => LogEnum::Error { date, msg },
&"Exception" => LogEnum::Exception { date, msg },
_ => LogEnum::Unknown(typ.to_string()),
};
Ok(log)
}
}