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- } ;
51use std:: collections:: HashMap ;
62use std:: env;
3+ use std:: error:: Error ;
4+ use std:: fs;
75use 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+
912pub struct Tracer {
1013 // trace metadata:
1114 workdir : PathBuf ,
@@ -33,7 +36,7 @@ pub const NONE_VALUE: ValueRecord = ValueRecord::None { type_id: NONE_TYPE_ID };
3336impl 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