mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-12-22 13:45:20 +00:00
event_monitor: Append double newlines after each event
This allows to consume and parse the output event output file. Signed-off-by: Bo Chen <chen.bo@intel.com>
This commit is contained in:
parent
33e2a7121e
commit
32353fa389
@ -9,6 +9,7 @@ extern crate serde_derive;
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
@ -50,6 +51,9 @@ pub fn event_log(source: &str, event: &str, properties: Option<&HashMap<Cow<str>
|
|||||||
properties,
|
properties,
|
||||||
};
|
};
|
||||||
serde_json::to_writer_pretty(file, &e).ok();
|
serde_json::to_writer_pretty(file, &e).ok();
|
||||||
|
|
||||||
|
let mut file = file;
|
||||||
|
file.write_all(b"\n\n").ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -503,26 +503,16 @@ impl MetaEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse the event_monitor file based on the format that each event
|
// Parse the event_monitor file based on the format that each event
|
||||||
// is surrounded by '{' and '}'
|
// is followed by a double newline
|
||||||
fn parse_event_file(event_file: &str) -> Vec<serde_json::Value> {
|
fn parse_event_file(event_file: &str) -> Vec<serde_json::Value> {
|
||||||
let content = fs::read(event_file).unwrap();
|
let content = fs::read(event_file).unwrap();
|
||||||
|
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
let mut entry_start = 0;
|
for entry in String::from_utf8_lossy(&content)
|
||||||
let mut count = 0;
|
.trim()
|
||||||
for (idx, &c) in content.iter().enumerate() {
|
.split("\n\n")
|
||||||
if c as char == '{' {
|
.collect::<Vec<&str>>()
|
||||||
count += 1;
|
{
|
||||||
} else if c as char == '}' {
|
ret.push(serde_json::from_str(entry).unwrap());
|
||||||
assert!(count > 0);
|
|
||||||
count -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if count == 0 {
|
|
||||||
let entry = String::from_utf8_lossy(&content[entry_start..idx + 1]);
|
|
||||||
ret.push(serde_json::from_str(&entry).unwrap());
|
|
||||||
entry_start = idx + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret
|
ret
|
||||||
|
Loading…
Reference in New Issue
Block a user