-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
299c5ee
commit 8452f2d
Showing
14 changed files
with
247 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
This log event doesn't have a timestamp | ||
2015-01-31T15:50:45.392 Id: 3190; This is a | ||
TIMESTAMP Id: 3190; This is a | ||
multi-line log event with unicode: 这是一个有多行的日志 | ||
2015-01-31T15:50:45.393 Id: 0; This is a multi-line log event. I will pay | ||
TIMESTAMP Id: 0; This is a multi-line log event. I will pay | ||
you 1000 dollars to test this file. | ||
2015-01-31T15:50:45.392 Id: 0; This is a variable=0 | ||
2015-01-31T15:50:45.392 Id: 0; But this is:0 | ||
2015-01-31T15:50:45.392 Variable with delimiter: a b a b a a a a | ||
TIMESTAMP Id: 0; This is a variable=0 | ||
TIMESTAMP Id: 0; But this is:0 | ||
TIMESTAMP Variable with delimiter: a b a b a a a a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod dfa; | ||
pub mod error_handling; | ||
pub mod lexer; | ||
pub mod log_parser; | ||
mod nfa; | ||
pub mod parser; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
use std::fmt::Debug; | ||
use crate::error_handling::Error::LogParserInternalErr; | ||
use crate::error_handling::Result; | ||
use crate::lexer::BufferedFileStream; | ||
use crate::lexer::LexerStream; | ||
use crate::lexer::{Lexer, Token, TokenType}; | ||
use crate::parser::SchemaConfig; | ||
use std::rc::Rc; | ||
|
||
pub struct LogParser { | ||
lexer: Lexer, | ||
schema_config: Rc<SchemaConfig>, | ||
tokens: Option<Vec<Token>>, | ||
} | ||
|
||
pub struct LogEvent { | ||
tokens: Vec<Token>, | ||
line_range: (usize, usize), | ||
has_timestamp: bool, | ||
schema_config: Rc<SchemaConfig>, | ||
} | ||
|
||
impl LogParser { | ||
pub fn new(schema_config: Rc<SchemaConfig>) -> Result<Self> { | ||
let lexer = Lexer::new(schema_config.clone())?; | ||
Ok((Self { | ||
lexer, | ||
schema_config, | ||
tokens: Some(Vec::new()), | ||
})) | ||
} | ||
|
||
pub fn set_input_file(&mut self, path: &str) -> Result<()> { | ||
self.tokens = Some(Vec::new()); | ||
let buffered_file_stream = Box::new(BufferedFileStream::new(path)?); | ||
self.set_input_stream(buffered_file_stream) | ||
} | ||
|
||
pub fn set_input_stream(&mut self, input_stream: Box<dyn LexerStream>) -> Result<()> { | ||
self.lexer.set_input_stream(input_stream); | ||
Ok(()) | ||
} | ||
|
||
pub fn parse_next_log_event(&mut self) -> Result<Option<LogEvent>> { | ||
loop { | ||
match self.lexer.get_next_token()? { | ||
Some(token) => match token.get_token_type() { | ||
TokenType::Timestamp(_) => { | ||
if self.tokens.is_none() { | ||
self.buffer_token(token); | ||
continue; | ||
} | ||
let log_event = self.emit_buffered_tokens_as_log_event()?; | ||
self.buffer_token(token); | ||
return Ok(log_event); | ||
} | ||
_ => self.buffer_token(token), | ||
}, | ||
None => break, | ||
} | ||
} | ||
self.emit_buffered_tokens_as_log_event() | ||
} | ||
|
||
fn buffer_token(&mut self, token: Token) { | ||
if self.tokens.is_none() { | ||
self.tokens = Some(Vec::new()); | ||
} | ||
self.tokens.as_mut().unwrap().push(token); | ||
} | ||
|
||
fn emit_buffered_tokens_as_log_event(&mut self) -> Result<Option<LogEvent>> { | ||
match &self.tokens { | ||
Some(_) => { | ||
let tokens = self.tokens.take().unwrap(); | ||
LogEvent::new(self.schema_config.clone(), tokens) | ||
} | ||
None => Ok(None), | ||
} | ||
} | ||
} | ||
|
||
impl LogEvent { | ||
fn new(schema_config: Rc<SchemaConfig>, tokens: Vec<Token>) -> Result<Option<Self>> { | ||
if tokens.is_empty() { | ||
return Err(LogParserInternalErr("The given token vector is empty")); | ||
} | ||
let has_timestamp = match tokens.first().unwrap().get_token_type() { | ||
TokenType::Timestamp(_) => true, | ||
_ => false, | ||
}; | ||
let line_range = ( | ||
tokens.first().unwrap().get_line_num(), | ||
tokens.last().unwrap().get_line_num(), | ||
); | ||
Ok(Some( | ||
(Self { | ||
tokens, | ||
line_range, | ||
has_timestamp, | ||
schema_config, | ||
}), | ||
)) | ||
} | ||
|
||
pub fn get_timestamp_token(&self) -> Option<&Token> { | ||
match self.has_timestamp { | ||
true => Some(&self.tokens[0]), | ||
false => None, | ||
} | ||
} | ||
|
||
pub fn get_line_range(&self) -> (usize, usize) { | ||
self.line_range | ||
} | ||
|
||
pub fn get_log_message_tokens(&self) -> &[Token] { | ||
match self.has_timestamp { | ||
true => &self.tokens[1..], | ||
false => &self.tokens[..], | ||
} | ||
} | ||
} | ||
|
||
impl Debug for LogEvent { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
let mut result = String::new(); | ||
match self.get_timestamp_token() { | ||
Some(ts_token) => result += format!("Timestamp:\n\t{:?}\n", ts_token).as_str(), | ||
None => result += "Timestamp:\n\tNONE\n", | ||
} | ||
|
||
let (mut curr_line_num, _) = self.get_line_range(); | ||
result += format!("Line {}:\n", curr_line_num).as_str(); | ||
for token in self.get_log_message_tokens() { | ||
if token.get_line_num() != curr_line_num { | ||
curr_line_num = token.get_line_num(); | ||
result += format!("Line {}:\n", curr_line_num).as_str(); | ||
} | ||
result += format!("\t{:?}\n", token).as_str(); | ||
} | ||
|
||
write!(f, "{}", result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod log_parser; | ||
|
||
pub use log_parser::LogParser; | ||
pub use log_parser::LogEvent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub(crate) mod regex_parser; | ||
|
||
mod schema_parser; | ||
|
||
pub use schema_parser::parser::SchemaConfig; | ||
pub use schema_parser::parser::TimestampSchema; | ||
pub use schema_parser::parser::VarSchema; |
Oops, something went wrong.