11use std:: fs;
2- use std:: path:: { PathBuf , Path } ;
2+ use std:: path:: { Path , PathBuf } ;
33use std:: sync:: atomic:: { AtomicBool , Ordering } ;
44use std:: sync:: Once ;
55
@@ -8,8 +8,8 @@ use pyo3::prelude::*;
88use std:: fmt;
99
1010pub mod code_object;
11- pub mod tracer;
1211mod runtime_tracer;
12+ pub mod tracer;
1313pub use crate :: code_object:: { CodeObjectRegistry , CodeObjectWrapper } ;
1414pub use crate :: tracer:: { install_tracer, uninstall_tracer, EventSet , Tracer } ;
1515
@@ -25,20 +25,14 @@ fn init_rust_logging_with_default(default_filter: &str) {
2525 let env = env_logger:: Env :: default ( ) . default_filter_or ( default_filter) ;
2626 // Use a compact format with timestamps and targets to aid debugging.
2727 let mut builder = env_logger:: Builder :: from_env ( env) ;
28- builder
29- . format_timestamp_micros ( )
30- . format_target ( true ) ;
28+ builder. format_timestamp_micros ( ) . format_target ( true ) ;
3129 let _ = builder. try_init ( ) ;
3230 } ) ;
3331}
3432
3533/// Start tracing using sys.monitoring and runtime_tracing writer.
3634#[ pyfunction]
37- fn start_tracing (
38- path : & str ,
39- format : & str ,
40- activation_path : Option < & str > ,
41- ) -> PyResult < ( ) > {
35+ fn start_tracing ( path : & str , format : & str , activation_path : Option < & str > ) -> PyResult < ( ) > {
4236 // Ensure logging is ready before any tracer logs might be emitted.
4337 // Default only our crate to debug to avoid excessive verbosity from deps.
4438 init_rust_logging_with_default ( "codetracer_python_recorder=debug" ) ;
@@ -49,26 +43,31 @@ fn start_tracing(
4943 // Interpret `path` as a directory where trace files will be written.
5044 let out_dir = Path :: new ( path) ;
5145 if out_dir. exists ( ) && !out_dir. is_dir ( ) {
52- return Err ( PyRuntimeError :: new_err ( "trace path exists and is not a directory" ) ) ;
46+ return Err ( PyRuntimeError :: new_err (
47+ "trace path exists and is not a directory" ,
48+ ) ) ;
5349 }
5450 if !out_dir. exists ( ) {
5551 // Best-effort create the directory tree
56- fs:: create_dir_all ( & out_dir)
57- . map_err ( |e| PyRuntimeError :: new_err ( format ! ( "failed to create trace directory: {}" , e) ) ) ?;
52+ fs:: create_dir_all ( & out_dir) . map_err ( |e| {
53+ PyRuntimeError :: new_err ( format ! ( "failed to create trace directory: {}" , e) )
54+ } ) ?;
5855 }
5956
6057 // Map format string to enum
6158 let fmt = match format. to_lowercase ( ) . as_str ( ) {
6259 "json" => runtime_tracing:: TraceEventsFileFormat :: Json ,
6360 // Use BinaryV0 for "binary" to avoid streaming writer here.
64- "binary" | "binaryv0" | "binary_v0" | "b0" => runtime_tracing:: TraceEventsFileFormat :: BinaryV0 ,
61+ "binary" | "binaryv0" | "binary_v0" | "b0" => {
62+ runtime_tracing:: TraceEventsFileFormat :: BinaryV0
63+ }
6564 //TODO AI! We need to assert! that the format is among the known values.
6665 other => {
6766 eprintln ! ( "Unknown format '{}', defaulting to binary (v0)" , other) ;
6867 runtime_tracing:: TraceEventsFileFormat :: BinaryV0
6968 }
7069 } ;
71-
70+
7271 // Build output file paths inside the directory.
7372 let ( events_path, meta_path, paths_path) = match fmt {
7473 runtime_tracing:: TraceEventsFileFormat :: Json => (
@@ -90,17 +89,10 @@ fn start_tracing(
9089 // Program and args: keep minimal; Python-side API stores full session info if needed
9190 let sys = py. import ( "sys" ) ?;
9291 let argv = sys. getattr ( "argv" ) ?;
93- let program: String = argv
94- . get_item ( 0 ) ?
95- . extract :: < String > ( ) ?;
92+ let program: String = argv. get_item ( 0 ) ?. extract :: < String > ( ) ?;
9693 //TODO: Error-handling. What to do if argv is empty? Does this ever happen?
9794
98- let mut tracer = runtime_tracer:: RuntimeTracer :: new (
99- & program,
100- & [ ] ,
101- fmt,
102- activation_path,
103- ) ;
95+ let mut tracer = runtime_tracer:: RuntimeTracer :: new ( & program, & [ ] , fmt, activation_path) ;
10496
10597 // Start location: prefer activation path, otherwise best-effort argv[0]
10698 let start_path: & Path = activation_path. unwrap_or ( Path :: new ( & program) ) ;
0 commit comments