Skip to content

Commit 23021c4

Browse files
committed
feat: add store tracer methods
1 parent 337bff5 commit 23021c4

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "trace_types"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::path::PathBuf;
21
mod tracer;
32
mod types;
43
pub use crate::tracer::{Tracer, NONE_TYPE_ID, NONE_VALUE};
@@ -7,6 +6,7 @@ pub use crate::types::*;
76
#[cfg(test)]
87
mod tests {
98
use super::*;
9+
use std::path::PathBuf;
1010

1111
#[test]
1212
fn simple_trace() {
@@ -22,13 +22,16 @@ mod tests {
2222

2323
let int_value = ValueRecord::Int {
2424
i: 1,
25-
type_id: tracer.ensure_type_id(TypeKind::Int, "Int")
25+
type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
2626
};
2727
tracer.register_variable_with_full_value("test_variable", int_value);
2828
tracer.register_return(NONE_VALUE);
2929
assert_eq!(tracer.events.len(), 14);
3030
// visible with
3131
// cargo tets -- --nocapture
32-
println!("{:#?}", tracer.events);
32+
// println!("{:#?}", tracer.events);
33+
34+
// tracer.store_trace_metadata(&PathBuf::from("trace_metadata.json")).unwrap();
35+
// tracer.store_trace_events(&PathBuf::from("trace.json")).unwrap();
3336
}
3437
}

src/tracer.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
use crate::types::{
2-
ArgRecord, CallRecord, EventLogKind, FullValueRecord, FunctionId, FunctionRecord, Line, PathId, RecordEvent, ReturnRecord, StepRecord,
3-
TraceLowLevelEvent, TypeId, ValueRecord, VariableId, TypeSpecificInfo, TypeRecord, TypeKind,
4-
};
51
use std::collections::HashMap;
62
use std::env;
3+
use std::error::Error;
4+
use std::fs;
75
use std::path::{Path, PathBuf};
86

7+
use crate::types::{
8+
ArgRecord, CallRecord, EventLogKind, FullValueRecord, FunctionId, FunctionRecord, Line, PathId, RecordEvent, ReturnRecord, StepRecord,
9+
TraceLowLevelEvent, TraceMetadata, TypeId, TypeKind, TypeRecord, TypeSpecificInfo, ValueRecord, VariableId,
10+
};
11+
912
pub struct Tracer {
1013
// trace metadata:
1114
workdir: PathBuf,
@@ -33,7 +36,7 @@ pub const NONE_VALUE: ValueRecord = ValueRecord::None { type_id: NONE_TYPE_ID };
3336
impl Tracer {
3437
pub fn new(program: &str, args: &[String]) -> Self {
3538
Tracer {
36-
workdir: env::current_dir().unwrap(),
39+
workdir: env::current_dir().expect("can access the current dir"),
3740
program: program.to_string(),
3841
args: args.to_vec(),
3942
events: vec![],
@@ -46,7 +49,6 @@ impl Tracer {
4649
}
4750

4851
pub fn start(&mut self, path: &PathBuf, line: Line) {
49-
let path_id = self.ensure_path_id(path);
5052
let function_id = self.ensure_function_id("<toplevel>", path, line);
5153
self.register_call(function_id, vec![]);
5254

@@ -144,12 +146,24 @@ impl Tracer {
144146
}
145147

146148
pub fn register_full_value(&mut self, variable_id: VariableId, value: ValueRecord) {
147-
self.events.push(
148-
TraceLowLevelEvent::Value(FullValueRecord {
149-
variable_id,
150-
value,
151-
})
152-
);
149+
self.events.push(TraceLowLevelEvent::Value(FullValueRecord { variable_id, value }));
150+
}
151+
152+
pub fn store_trace_metadata(&mut self, path: &PathBuf) -> Result<(), Box<dyn Error>> {
153+
let trace_metadata = TraceMetadata {
154+
program: self.program.clone(),
155+
args: self.args.clone(),
156+
workdir: self.workdir.clone(),
157+
};
158+
let json = serde_json::to_string(&trace_metadata)?;
159+
fs::write(path, json)?;
160+
Ok(())
153161
}
154162

163+
pub fn store_trace_events(&mut self, path: &PathBuf) -> Result<(), Box<dyn Error>> {
164+
// TODO: probably change format
165+
let json = serde_json::to_string(&self.events)?;
166+
fs::write(path, json)?;
167+
Ok(())
168+
}
155169
}

0 commit comments

Comments
 (0)