@@ -32,13 +32,15 @@ fn print_error_message(error: &Error) {
3232 eprintln ! ( "[{error_tag}] {error}" ) ;
3333}
3434
35+ #[ tracing:: instrument( skip_all, level = "info" ) ]
3536fn read_json ( file_path : PathBuf ) -> Result < Value > {
3637 let sierra_file = File :: open ( file_path) . context ( "Unable to open json file" ) ?;
3738 let sierra_file_reader = BufReader :: new ( sierra_file) ;
3839
3940 serde_json:: from_reader ( sierra_file_reader) . context ( "Unable to read json file" )
4041}
4142
43+ #[ tracing:: instrument( skip_all, level = "info" ) ]
4244fn output_casm ( output_json : & Value , output_file_path : Option < PathBuf > ) -> Result < ( ) > {
4345 match output_file_path {
4446 Some ( output_path) => {
@@ -58,6 +60,7 @@ fn output_casm(output_json: &Value, output_file_path: Option<PathBuf>) -> Result
5860
5961fn main_execution ( ) -> Result < bool > {
6062 let cli = Cli :: parse ( ) ;
63+ let _g = init_logging ( ) ;
6164
6265 match cli. command {
6366 Commands :: CompileContract ( compile_contract) => {
@@ -89,3 +92,96 @@ fn main() {
8992 }
9093 } ;
9194}
95+
96+ #[ cfg( not( feature = "tracing" ) ) ]
97+ fn init_logging ( ) -> Option < impl Drop > {
98+ struct Zst ;
99+ impl Drop for Zst {
100+ fn drop ( & mut self ) { }
101+ }
102+ Option :: < Zst > :: None
103+ }
104+
105+ #[ cfg( feature = "tracing" ) ]
106+ fn init_logging ( ) -> Option < impl Drop > {
107+ use chrono:: Local ;
108+ use std:: fs;
109+
110+ use std:: path:: PathBuf ;
111+ use tracing_chrome:: ChromeLayerBuilder ;
112+ use tracing_subscriber:: filter:: { EnvFilter , LevelFilter , Targets } ;
113+ use tracing_subscriber:: fmt:: time:: Uptime ;
114+ use tracing_subscriber:: fmt:: Layer ;
115+ use tracing_subscriber:: prelude:: * ;
116+
117+ let mut guard = None ;
118+
119+ let fmt_layer = Layer :: new ( )
120+ . with_writer ( std:: io:: stderr)
121+ . with_timer ( Uptime :: default ( ) )
122+ . with_filter (
123+ EnvFilter :: builder ( )
124+ . with_default_directive ( LevelFilter :: TRACE . into ( ) )
125+ . with_env_var ( "USC_LOG" )
126+ . from_env_lossy ( ) ,
127+ ) ;
128+
129+ // Disabled unless explicitly enabled with env var.
130+ let tracing_profile = is_truthy_env ( "USC_TRACING_PROFILE" , false ) ;
131+
132+ let profile_layer = if tracing_profile {
133+ let mut path = PathBuf :: from ( format ! ( "./usc-profile-{}.json" , Local :: now( ) . to_rfc3339( ) ) ) ;
134+
135+ // Create the file now, so that we early panic, and `fs::canonicalize` will work.
136+ let profile_file = fs:: File :: create ( & path) . expect ( "failed to create profile file" ) ;
137+
138+ // Try to canonicalise the path so that it is easier to find the file from logs.
139+ if let Ok ( canonical) = fs:: canonicalize ( & path) {
140+ path = canonical;
141+ }
142+
143+ eprintln ! (
144+ "this USC run will output tracing profile to: {}" ,
145+ path. display( )
146+ ) ;
147+ eprintln ! (
148+ "open that file with https://ui.perfetto.dev (or chrome://tracing) to analyze it"
149+ ) ;
150+
151+ let ( profile_layer, profile_layer_guard) = ChromeLayerBuilder :: new ( )
152+ . writer ( profile_file)
153+ . include_args ( true )
154+ . build ( ) ;
155+
156+ // Filter out less important logs because they're too verbose,
157+ // and with them the profile file quickly grows to several GBs of data.
158+ let profile_layer = profile_layer. with_filter (
159+ Targets :: new ( )
160+ . with_default ( LevelFilter :: TRACE )
161+ . with_target ( "salsa" , LevelFilter :: WARN ) ,
162+ ) ;
163+
164+ guard = Some ( profile_layer_guard) ;
165+ Some ( profile_layer)
166+ } else {
167+ None
168+ } ;
169+
170+ tracing:: subscriber:: set_global_default (
171+ tracing_subscriber:: registry ( )
172+ . with ( fmt_layer)
173+ . with ( profile_layer) ,
174+ )
175+ . expect ( "could not set up global logger" ) ;
176+
177+ guard
178+ }
179+
180+ #[ cfg( feature = "tracing" ) ]
181+ #[ must_use]
182+ pub fn is_truthy_env ( name : & str , default : bool ) -> bool {
183+ std:: env:: var ( name) . ok ( ) . map_or ( default, |var| {
184+ let s = var. as_str ( ) ;
185+ s == "true" || s == "1"
186+ } )
187+ }
0 commit comments