Skip to content

Commit

Permalink
fix(decode): fix decode crash
Browse files Browse the repository at this point in the history
  • Loading branch information
s1rius committed May 25, 2024
1 parent e46ae7f commit 6cdba76
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 68 deletions.
128 changes: 64 additions & 64 deletions ezlog-core/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{
self,
BufRead,
Cursor,
Read,
Seek,
Write,
},
str::FromStr,
Expand All @@ -16,8 +18,6 @@ use byteorder::{
};
use integer_encoding::VarIntReader;
use log::error;
use once_cell::sync::OnceCell;
use regex::Regex;
use time::{
format_description::well_known::Rfc3339,
OffsetDateTime,
Expand All @@ -37,68 +37,68 @@ use crate::{
};

pub fn decode_record(vec: &[u8]) -> Result<crate::EZRecord> {
static RE: OnceCell<std::result::Result<Regex, regex::Error>> = OnceCell::new();
let regex = RE.get_or_init(|| Regex::new(r"\[(.*?)\]"));
let record_str =
String::from_utf8(vec.to_vec()).map_err(|e| LogError::Parse(format!("{}", e)))?;
let mut record_builder = EZRecord::builder();
if let Ok(regex) = regex {
// Search for the first match
if let Some(caps) = regex.captures(&record_str) {
if let Some(matched) = caps.get(1) {
let header = matched.as_str();
let split: Vec<&str> = header.split_whitespace().collect();

let time = split
.first()
.map(|x| {
OffsetDateTime::parse(x, &Rfc3339).unwrap_or(OffsetDateTime::now_utc())
})
.unwrap_or(OffsetDateTime::now_utc());
record_builder.time(time);

let level = split
.get(1)
.and_then(|x| Level::from_str(x).ok())
.unwrap_or(Level::Trace);
record_builder.level(level);

let target = split.get(2).unwrap_or(&"").to_string();
record_builder.target(target);

let thread_str = split.get(3).unwrap_or(&"");
let mut thread_info: Vec<&str> = thread_str.split(':').collect();
if !thread_info.is_empty() {
let thread_id = thread_info.last().unwrap_or(&"").to_string();
record_builder.thread_id(thread_id.parse::<usize>().unwrap_or(0));
thread_info.pop();
let thread_name = thread_info.join(":");
record_builder.thread_name(thread_name);
}
let mut cursor = Cursor::new(vec);
let mut buf = vec![];
cursor
.read_until(b'[', &mut buf)
.map_err(|_| LogError::Parse("not found [".to_string()))?;
buf.clear();
cursor
.read_until(b']', &mut buf)
.map_err(|_| LogError::Parse("not found ]".to_string()))?;
buf.pop();

let header_buf = String::from_utf8_lossy(&buf);
let mut header_split = header_buf.split_whitespace();
let time = header_split
.next()
.map(|x| OffsetDateTime::parse(x, &Rfc3339).unwrap_or(OffsetDateTime::now_utc()))
.unwrap_or(OffsetDateTime::now_utc());
record_builder.time(time);

let level = header_split
.next()
.and_then(|x| Level::from_str(x).ok())
.unwrap_or(Level::Trace);
record_builder.level(level);

let target = header_split.next().unwrap_or("");
record_builder.target(target.to_string());

if let Some(thread_str) = header_split.next() {
let mut thread_info: Vec<&str> = thread_str.split(':').collect();
if !thread_info.is_empty() {
let thread_id = thread_info.pop().unwrap_or("");
let thread_name: String = thread_info.join(":");
record_builder.thread_id(thread_id.parse::<usize>().unwrap_or(0));
record_builder.thread_name(thread_name);
}
}

#[cfg(feature = "log")]
{
if split.len() == 5 {
let file_info: Vec<&str> = split.last().unwrap_or(&"").split(':').collect();
if !file_info.is_empty() {
let file_name = file_info.first().unwrap_or(&"");
let line = file_info.get(1).unwrap_or(&"");

record_builder.file(file_name);
record_builder.line(line.parse::<u32>().unwrap_or(0));
}
};
}
// header not contains square brackets
// two square brackets and one space
if record_str.len() > header.len() + 3 {
let content = &record_str[header.len() + 3..];
record_builder.content(content.to_string());
}
#[cfg(feature = "log")]
{
let file_str = header_split.next().unwrap_or("");
if !file_str.is_empty() {
let file_info: Vec<&str> = file_str.split(':').collect();
if !file_info.is_empty() {
let file_name = file_info.first().unwrap_or(&"");
let line = file_info.get(1).unwrap_or(&"");

record_builder.file(file_name);
record_builder.line(line.parse::<u32>().unwrap_or(0));
}
}
}
};

let mut content_buf = vec![];
// skip whitespace
cursor.seek(io::SeekFrom::Current(1)).unwrap_or(0);
cursor
.read_to_end(&mut content_buf)
.map_err(|e| LogError::Parse(format!("parse content: {}", e)))?;
let content = String::from_utf8_lossy(&content_buf);
record_builder.content(content.to_string());
Ok(record_builder.build())
}

Expand Down Expand Up @@ -293,7 +293,7 @@ mod tests {
use time::OffsetDateTime;

use super::decode_record;
use crate::thread_name;
use crate::{thread_name, LogError};
use crate::{
decode,
EZLogger,
Expand Down Expand Up @@ -364,7 +364,7 @@ mod tests {
count += 1;
}
if is_end {
tx.send(()).expect("Could not send signal on channel.");
tx.send(()).unwrap_or_else(|_| { LogError::Parse("Could not send signal on channel.".to_string()); });
return None;
}
Some(0)
Expand All @@ -376,7 +376,7 @@ mod tests {
header,
my_closure,
);
rx.recv().expect("Could not receive from channel.");
rx.recv().unwrap_or_else(|_| { LogError::Parse("Could not receive from channel.".to_string()); });
Ok(count)
}

Expand Down Expand Up @@ -443,7 +443,7 @@ mod tests {
}
}
if is_end {
tx.send(()).expect("Could not send signal on channel.");
tx.send(()).unwrap_or_else(|_| { LogError::Parse("Could not send signal on channel.".to_string()); });
return None;
}
Some(0)
Expand All @@ -455,7 +455,7 @@ mod tests {
header,
my_closure,
);
rx.recv().expect("Could not receive from channel.");
rx.recv().unwrap_or_else(|_| { LogError::Parse("Could not receive from channel.".to_string()); });
Ok(array)
}

Expand Down
10 changes: 6 additions & 4 deletions ezlog-core/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl EZLogger {

record.trunks(&self.config).iter().for_each(|record| {
match self
.encode_as_block(record)
.encode_as_vec(record)
.map(|buf| self.appender.write_all(&buf))
{
Ok(_) => {}
Expand All @@ -153,7 +153,7 @@ impl EZLogger {
});
e.map_or(Ok(()), Err)
} else {
let buf = self.encode_as_block(record)?;
let buf = self.encode_as_vec(record)?;
self.appender.write_all(&buf).map_err(|e| e.into())
}
}
Expand Down Expand Up @@ -224,13 +224,13 @@ impl EZLogger {
Box::new(move |input| xor_slice(input, &combine))
}

///
#[inline]
pub fn encode_as_block(&mut self, record: &EZRecord) -> Result<Vec<u8>> {
pub fn encode_as_vec(&mut self, record: &EZRecord) -> Result<Vec<u8>> {
let buf = self.encode(record)?;
encode_content(buf)
}

#[inline]
fn format(&self, record: &EZRecord) -> Result<Vec<u8>> {
crate::formatter().format(record)
}
Expand Down Expand Up @@ -278,6 +278,7 @@ impl EZLogger {
self.config.query_log_files_for_date(date)
}

#[inline]
pub(crate) fn rotate_if_not_empty(&mut self) -> Result<()> {
if self
.appender
Expand All @@ -300,6 +301,7 @@ pub(crate) fn combine_time_position(timestamp: i64, position: u64) -> Vec<u8> {
vec
}

#[inline]
pub(crate) fn xor_slice<'a>(slice: &'a [u8], vec: &'a [u8]) -> Vec<u8> {
let mut result = Vec::with_capacity(slice.len());
for (i, byte) in slice.iter().enumerate() {
Expand Down

0 comments on commit 6cdba76

Please sign in to comment.