Skip to content

Commit

Permalink
fix: [unifiedlog_iterator] syntax corrections + parser fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cvandeplas committed Nov 27, 2024
1 parent 0713797 commit c9eb4be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
45 changes: 23 additions & 22 deletions examples/unifiedlog_iterator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ fn main() {
args.format.clone()
};


let mut writer = OutputWriter::new(&args.output, &output_format, args.append).unwrap();

if args.input != "" {
if !args.input.is_empty() {
parse_log_archive(&args.input, &mut writer);
} else if args.live != "false" {
parse_live_system(&mut writer);
Expand Down Expand Up @@ -160,7 +159,7 @@ fn parse_trace_file(
eprintln!("Parsing: {}", full_path);

if data.path().exists() {
let count = iterate_chunks(
let count = iterate_chunks(
&full_path,
&mut missing_data,
string_results,
Expand Down Expand Up @@ -190,7 +189,7 @@ fn parse_trace_file(
eprintln!("Parsing: {}", full_path);

if data.path().exists() {
let count = iterate_chunks(
let count = iterate_chunks(
&full_path,
&mut missing_data,
string_results,
Expand Down Expand Up @@ -220,7 +219,7 @@ fn parse_trace_file(
eprintln!("Parsing: {}", full_path);

if data.path().exists() {
let count = iterate_chunks(
let count = iterate_chunks(
&full_path,
&mut missing_data,
string_results,
Expand Down Expand Up @@ -249,7 +248,7 @@ fn parse_trace_file(
eprintln!("Parsing: {}", full_path);

if data.path().exists() {
let count = iterate_chunks(
let count = iterate_chunks(
&full_path,
&mut missing_data,
string_results,
Expand All @@ -273,7 +272,7 @@ fn parse_trace_file(
if archive_path.exists() {
eprintln!("Parsing: logdata.LiveData.tracev3");

let count = iterate_chunks(
let count = iterate_chunks(
&archive_path.display().to_string(),
&mut missing_data,
string_results,
Expand All @@ -294,8 +293,7 @@ fn parse_trace_file(
// Since we have all Oversize entries now. Go through any log entries that we were not able to build before
for mut leftover_data in missing_data {
// Add all of our previous oversize data to logs for lookups
leftover_data
.oversize = oversize_strings.oversize.clone();
leftover_data.oversize = oversize_strings.oversize.clone();

// Exclude_missing = false
// If we fail to find any missing data its probably due to the logs rolling
Expand Down Expand Up @@ -346,7 +344,10 @@ fn iterate_chunks(
count += results.len();
oversize_strings.oversize = chunk.oversize;
output(&results, writer).unwrap();
if missing_logs.catalog_data.is_empty() && missing_logs.header.is_empty() && missing_logs.oversize.is_empty() {
if missing_logs.catalog_data.is_empty()
&& missing_logs.header.is_empty()
&& missing_logs.oversize.is_empty()
{
continue;
}
// Track possible missing log data due to oversize strings being in another file
Expand All @@ -361,13 +362,17 @@ pub struct OutputWriter {
}

enum OutputWriterEnum {
Csv(Writer<Box<dyn Write>>),
Csv(Box<Writer<Box<dyn Write>>>),
Json(Box<dyn Write>),
}

impl OutputWriter {
pub fn new(output_path: &str, output_format: &str, append: bool) -> Result<Self, Box<dyn Error>> {
let writer: Box<dyn Write> = if output_path != "" {
pub fn new(
output_path: &str,
output_format: &str,
append: bool,
) -> Result<Self, Box<dyn Error>> {
let writer: Box<dyn Write> = if !output_path.is_empty() {
Box::new(
OpenOptions::new()
.write(true)
Expand All @@ -384,7 +389,7 @@ impl OutputWriter {
"csv" => {
let mut csv_writer = Writer::from_writer(writer);
// Write CSV headers
csv_writer.write_record(&[
csv_writer.write_record([
"Timestamp",
"Event Type",
"Log Type",
Expand All @@ -404,13 +409,13 @@ impl OutputWriter {
"System Timezone Name",
])?;
csv_writer.flush()?;
OutputWriterEnum::Csv(csv_writer)
OutputWriterEnum::Csv(Box::new(csv_writer))
}
"jsonl" => OutputWriterEnum::Json(writer),
_ => {
eprintln!("Unsupported output format: {}", output_format);
std::process::exit(1);
},
}
};

Ok(OutputWriter {
Expand Down Expand Up @@ -458,14 +463,10 @@ impl OutputWriter {
}
}


// Append or create csv file
fn output(
results: &Vec<LogData>,
writer: &mut OutputWriter,
) -> Result<(), Box<dyn Error>> {
fn output(results: &Vec<LogData>, writer: &mut OutputWriter) -> Result<(), Box<dyn Error>> {
for data in results {
writer.write_record(&data)?;
writer.write_record(data)?;
}
writer.flush()?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn collect_strings(path: &str) -> Result<Vec<UUIDText>, ParserError> {

let entries = paths
.flat_map(|path| {
path.inspect_err(|err| {
path.map_err(|err| {
error!("[macos-unifiedlogs] Failed to get directory entry: {err:?}",)
})
.ok()
Expand Down

0 comments on commit c9eb4be

Please sign in to comment.