Skip to content

Commit ee0a235

Browse files
committed
feat: implemented the convert command in runtime_tracing_cli - capable of conversion between binary and json trace formats
1 parent 93cafff commit ee0a235

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

runtime_tracing/src/tracer.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashMap;
44
use std::env;
55
use std::error::Error;
66
use std::fs;
7-
use std::io::stdout;
7+
use std::io::{stdout, BufReader};
88
use std::path::{Path, PathBuf};
99

1010
use crate::types::{
@@ -300,6 +300,21 @@ impl Tracer {
300300
Ok(())
301301
}
302302

303+
pub fn load_trace_events(&mut self, path: &Path, format: TraceEventsFileFormat) -> Result<(), Box<dyn Error>> {
304+
match format {
305+
TraceEventsFileFormat::Json => {
306+
let json = std::fs::read_to_string(path)?;
307+
self.events = serde_json::from_str(&json)?;
308+
}
309+
TraceEventsFileFormat::Binary => {
310+
let file = fs::File::open(path)?;
311+
let mut buf_reader = BufReader::new(file);
312+
self.events = crate::capnptrace::read_trace(&mut buf_reader)?;
313+
}
314+
}
315+
Ok(())
316+
}
317+
303318
pub fn store_trace_events(&self, path: &Path, format: TraceEventsFileFormat) -> Result<(), Box<dyn Error>> {
304319
match format {
305320
TraceEventsFileFormat::Json => {

runtime_tracing_cli/src/main.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
1+
use std::path::Path;
2+
3+
use clap::{Args, Parser, Subcommand};
4+
use runtime_tracing::{TraceEventsFileFormat, Tracer};
5+
6+
#[derive(Debug, Clone, Args)]
7+
struct ConvertCommand {
8+
input_file: String,
9+
output_file: String,
10+
}
11+
12+
#[non_exhaustive]
13+
#[derive(Subcommand, Clone, Debug)]
14+
enum RuntimeTracingCliCommand {
15+
/// Convert from one trace file format to another
16+
Convert(ConvertCommand),
17+
}
18+
19+
#[derive(Parser, Debug)]
20+
#[command(version, about, long_about = None)]
21+
struct RuntimeTracingCli {
22+
#[command(subcommand)]
23+
command: RuntimeTracingCliCommand,
24+
}
25+
26+
fn determine_file_format_from_name(s: &str) -> Option<TraceEventsFileFormat> {
27+
if s.ends_with(".json") {
28+
Some(TraceEventsFileFormat::Json)
29+
} else {
30+
if s.ends_with(".bin") {
31+
Some(TraceEventsFileFormat::Binary)
32+
} else {
33+
None
34+
}
35+
}
36+
}
37+
138
fn main() {
2-
println!("Hello, world!");
39+
let args = RuntimeTracingCli::parse();
40+
41+
match args.command {
42+
RuntimeTracingCliCommand::Convert(convert_command) => {
43+
let input_file_format = determine_file_format_from_name(&convert_command.input_file).unwrap();
44+
let output_file_format = determine_file_format_from_name(&convert_command.output_file).unwrap();
45+
let mut trace = Tracer::new("", &[]);
46+
trace.load_trace_events(Path::new(&convert_command.input_file), input_file_format).unwrap();
47+
trace.store_trace_events(Path::new(&convert_command.output_file), output_file_format).unwrap();
48+
}
49+
}
350
}

0 commit comments

Comments
 (0)