@@ -29,6 +29,8 @@ fn main() {
2929}
3030
3131fn run ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
32+ #[ cfg( feature = "flame-it" ) ]
33+ let main_guard = flame:: start_guard ( "RustPython main" ) ;
3234 env_logger:: init ( ) ;
3335 let app = App :: new ( "RustPython" )
3436 . version ( crate_version ! ( ) )
@@ -55,15 +57,25 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
5557 )
5658 . arg ( Arg :: from_usage ( "[pyargs] 'args for python'" ) . multiple ( true ) ) ;
5759 #[ cfg( feature = "flame-it" ) ]
58- let app = app. arg (
59- Arg :: with_name ( "profile_output" )
60- . long ( "profile-output" )
61- . takes_value ( true )
62- . help (
63- "the file to output the profile graph to. present due to being \
64- built with feature 'flame-it'",
65- ) ,
66- ) ;
60+ let app = app
61+ . arg (
62+ Arg :: with_name ( "profile_output" )
63+ . long ( "profile-output" )
64+ . takes_value ( true )
65+ . help (
66+ "the file to output the profiling information to. present due to being \
67+ built with feature 'flame-it'",
68+ ) ,
69+ )
70+ . arg (
71+ Arg :: with_name ( "profile_format" )
72+ . long ( "profile-format" )
73+ . takes_value ( true )
74+ . help (
75+ "the profile format to output the profiling information in. present due to \
76+ being built with feature 'flame-it'",
77+ ) ,
78+ ) ;
6779 let matches = app. get_matches ( ) ;
6880
6981 // Construct vm:
@@ -92,13 +104,43 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
92104 {
93105 use std:: fs:: File ;
94106
95- let profile_output = matches
96- . value_of_os ( "profile_output" )
97- . unwrap_or_else ( || "flame-graph.html" . as_ref ( ) ) ;
98- if profile_output == "-" {
99- flame:: dump_stdout ( ) ;
107+ main_guard. end ( ) ;
108+
109+ enum ProfileFormat {
110+ Html ,
111+ Text ,
112+ Speedscope ,
113+ }
114+
115+ let profile_output = matches. value_of_os ( "profile_output" ) ;
116+
117+ let profile_format = match matches. value_of ( "profile_format" ) {
118+ Some ( "html" ) => ProfileFormat :: Html ,
119+ Some ( "text" ) => ProfileFormat :: Text ,
120+ None if profile_output == Some ( "-" . as_ref ( ) ) => ProfileFormat :: Text ,
121+ Some ( "speedscope" ) | None => ProfileFormat :: Speedscope ,
122+ Some ( other) => {
123+ error ! ( "Unknown profile format {}" , other) ;
124+ process:: exit ( 1 ) ;
125+ }
126+ } ;
127+
128+ let profile_output = profile_output. unwrap_or_else ( || match profile_format {
129+ ProfileFormat :: Html => "flame-graph.html" . as_ref ( ) ,
130+ ProfileFormat :: Text => "flame.txt" . as_ref ( ) ,
131+ ProfileFormat :: Speedscope => "flamescope.json" . as_ref ( ) ,
132+ } ) ;
133+
134+ let profile_output: Box < dyn std:: io:: Write > = if profile_output == "-" {
135+ Box :: new ( std:: io:: stdout ( ) )
100136 } else {
101- flame:: dump_html ( & mut File :: create ( profile_output) ?) ?;
137+ Box :: new ( File :: create ( profile_output) ?)
138+ } ;
139+
140+ match profile_format {
141+ ProfileFormat :: Html => flame:: dump_html ( profile_output) ?,
142+ ProfileFormat :: Text => flame:: dump_text_to_writer ( profile_output) ?,
143+ ProfileFormat :: Speedscope => flamescope:: dump ( profile_output) ?,
102144 }
103145 }
104146
0 commit comments