@@ -17,9 +17,10 @@ use red_knot::workspace::WorkspaceMetadata;
17
17
use ruff_db:: program:: { ProgramSettings , SearchPathSettings } ;
18
18
use ruff_db:: system:: { OsSystem , System , SystemPathBuf } ;
19
19
20
- use self :: target_version:: TargetVersion ;
20
+ use cli:: target_version:: TargetVersion ;
21
+ use cli:: verbosity:: { Verbosity , VerbosityLevel } ;
21
22
22
- mod target_version ;
23
+ mod cli ;
23
24
24
25
#[ derive( Debug , Parser ) ]
25
26
#[ command(
@@ -43,14 +44,19 @@ struct Args {
43
44
help = "Custom directory to use for stdlib typeshed stubs"
44
45
) ]
45
46
custom_typeshed_dir : Option < SystemPathBuf > ,
47
+
46
48
#[ arg(
47
49
long,
48
50
value_name = "PATH" ,
49
51
help = "Additional path to use as a module-resolution source (can be passed multiple times)"
50
52
) ]
51
53
extra_search_path : Vec < SystemPathBuf > ,
54
+
52
55
#[ arg( long, help = "Python version to assume when resolving types" , default_value_t = TargetVersion :: default ( ) , value_name="VERSION" ) ]
53
56
target_version : TargetVersion ,
57
+
58
+ #[ clap( flatten) ]
59
+ verbosity : Verbosity ,
54
60
}
55
61
56
62
#[ allow(
@@ -60,16 +66,18 @@ struct Args {
60
66
clippy:: dbg_macro
61
67
) ]
62
68
pub fn main ( ) -> anyhow:: Result < ( ) > {
63
- countme:: enable ( true ) ;
64
- setup_tracing ( ) ;
65
-
66
69
let Args {
67
70
current_directory,
68
71
custom_typeshed_dir,
69
72
extra_search_path : extra_paths,
70
73
target_version,
74
+ verbosity,
71
75
} = Args :: parse_from ( std:: env:: args ( ) . collect :: < Vec < _ > > ( ) ) ;
72
76
77
+ let verbosity = verbosity. level ( ) ;
78
+ countme:: enable ( verbosity == Some ( VerbosityLevel :: Trace ) ) ;
79
+ setup_tracing ( verbosity) ;
80
+
73
81
let cwd = if let Some ( cwd) = current_directory {
74
82
let canonicalized = cwd. as_utf8_path ( ) . canonicalize_utf8 ( ) . unwrap ( ) ;
75
83
SystemPathBuf :: from_utf8_path_buf ( canonicalized)
@@ -97,7 +105,7 @@ pub fn main() -> anyhow::Result<()> {
97
105
// cache and load the cache if it exists.
98
106
let mut db = RootDatabase :: new ( workspace_metadata, program_settings, system) ;
99
107
100
- let ( main_loop, main_loop_cancellation_token) = MainLoop :: new ( ) ;
108
+ let ( main_loop, main_loop_cancellation_token) = MainLoop :: new ( verbosity ) ;
101
109
102
110
// Listen to Ctrl+C and abort the watch mode.
103
111
let main_loop_cancellation_token = Mutex :: new ( Some ( main_loop_cancellation_token) ) ;
@@ -126,18 +134,19 @@ pub fn main() -> anyhow::Result<()> {
126
134
}
127
135
128
136
struct MainLoop {
129
- orchestrator_sender : crossbeam_channel:: Sender < OrchestratorMessage > ,
130
- main_loop_receiver : crossbeam_channel:: Receiver < MainLoopMessage > ,
137
+ verbosity : Option < VerbosityLevel > ,
138
+ orchestrator : crossbeam_channel:: Sender < OrchestratorMessage > ,
139
+ receiver : crossbeam_channel:: Receiver < MainLoopMessage > ,
131
140
}
132
141
133
142
impl MainLoop {
134
- fn new ( ) -> ( Self , MainLoopCancellationToken ) {
143
+ fn new ( verbosity : Option < VerbosityLevel > ) -> ( Self , MainLoopCancellationToken ) {
135
144
let ( orchestrator_sender, orchestrator_receiver) = crossbeam_channel:: bounded ( 1 ) ;
136
145
let ( main_loop_sender, main_loop_receiver) = crossbeam_channel:: bounded ( 1 ) ;
137
146
138
147
let mut orchestrator = Orchestrator {
139
148
receiver : orchestrator_receiver,
140
- sender : main_loop_sender. clone ( ) ,
149
+ main_loop : main_loop_sender. clone ( ) ,
141
150
revision : 0 ,
142
151
} ;
143
152
@@ -147,8 +156,9 @@ impl MainLoop {
147
156
148
157
(
149
158
Self {
150
- orchestrator_sender,
151
- main_loop_receiver,
159
+ verbosity,
160
+ orchestrator : orchestrator_sender,
161
+ receiver : main_loop_receiver,
152
162
} ,
153
163
MainLoopCancellationToken {
154
164
sender : main_loop_sender,
@@ -158,29 +168,27 @@ impl MainLoop {
158
168
159
169
fn file_changes_notifier ( & self ) -> FileChangesNotifier {
160
170
FileChangesNotifier {
161
- sender : self . orchestrator_sender . clone ( ) ,
171
+ sender : self . orchestrator . clone ( ) ,
162
172
}
163
173
}
164
174
165
175
#[ allow( clippy:: print_stderr) ]
166
176
fn run ( self , db : & mut RootDatabase ) {
167
- self . orchestrator_sender
168
- . send ( OrchestratorMessage :: Run )
169
- . unwrap ( ) ;
177
+ self . orchestrator . send ( OrchestratorMessage :: Run ) . unwrap ( ) ;
170
178
171
- for message in & self . main_loop_receiver {
179
+ for message in & self . receiver {
172
180
tracing:: trace!( "Main Loop: Tick" ) ;
173
181
174
182
match message {
175
183
MainLoopMessage :: CheckWorkspace { revision } => {
176
184
let db = db. snapshot ( ) ;
177
- let sender = self . orchestrator_sender . clone ( ) ;
185
+ let orchestrator = self . orchestrator . clone ( ) ;
178
186
179
187
// Spawn a new task that checks the workspace. This needs to be done in a separate thread
180
188
// to prevent blocking the main loop here.
181
189
rayon:: spawn ( move || {
182
190
if let Ok ( result) = db. check ( ) {
183
- sender
191
+ orchestrator
184
192
. send ( OrchestratorMessage :: CheckCompleted {
185
193
diagnostics : result,
186
194
revision,
@@ -195,10 +203,14 @@ impl MainLoop {
195
203
}
196
204
MainLoopMessage :: CheckCompleted ( diagnostics) => {
197
205
eprintln ! ( "{}" , diagnostics. join( "\n " ) ) ;
198
- eprintln ! ( "{}" , countme:: get_all( ) ) ;
206
+ if self . verbosity == Some ( VerbosityLevel :: Trace ) {
207
+ eprintln ! ( "{}" , countme:: get_all( ) ) ;
208
+ }
199
209
}
200
210
MainLoopMessage :: Exit => {
201
- eprintln ! ( "{}" , countme:: get_all( ) ) ;
211
+ if self . verbosity == Some ( VerbosityLevel :: Trace ) {
212
+ eprintln ! ( "{}" , countme:: get_all( ) ) ;
213
+ }
202
214
return ;
203
215
}
204
216
}
@@ -208,7 +220,7 @@ impl MainLoop {
208
220
209
221
impl Drop for MainLoop {
210
222
fn drop ( & mut self ) {
211
- self . orchestrator_sender
223
+ self . orchestrator
212
224
. send ( OrchestratorMessage :: Shutdown )
213
225
. unwrap ( ) ;
214
226
}
@@ -240,7 +252,7 @@ impl MainLoopCancellationToken {
240
252
241
253
struct Orchestrator {
242
254
/// Sends messages to the main loop.
243
- sender : crossbeam_channel:: Sender < MainLoopMessage > ,
255
+ main_loop : crossbeam_channel:: Sender < MainLoopMessage > ,
244
256
/// Receives messages from the main loop.
245
257
receiver : crossbeam_channel:: Receiver < OrchestratorMessage > ,
246
258
revision : usize ,
@@ -252,7 +264,7 @@ impl Orchestrator {
252
264
while let Ok ( message) = self . receiver . recv ( ) {
253
265
match message {
254
266
OrchestratorMessage :: Run => {
255
- self . sender
267
+ self . main_loop
256
268
. send ( MainLoopMessage :: CheckWorkspace {
257
269
revision : self . revision ,
258
270
} )
@@ -265,7 +277,7 @@ impl Orchestrator {
265
277
} => {
266
278
// Only take the diagnostics if they are for the latest revision.
267
279
if self . revision == revision {
268
- self . sender
280
+ self . main_loop
269
281
. send ( MainLoopMessage :: CheckCompleted ( diagnostics) )
270
282
. unwrap ( ) ;
271
283
} else {
@@ -313,8 +325,8 @@ impl Orchestrator {
313
325
} ,
314
326
default ( std:: time:: Duration :: from_millis( 10 ) ) => {
315
327
// No more file changes after 10 ms, send the changes and schedule a new analysis
316
- self . sender . send( MainLoopMessage :: ApplyChanges ( changes) ) . unwrap( ) ;
317
- self . sender . send( MainLoopMessage :: CheckWorkspace { revision: self . revision} ) . unwrap( ) ;
328
+ self . main_loop . send( MainLoopMessage :: ApplyChanges ( changes) ) . unwrap( ) ;
329
+ self . main_loop . send( MainLoopMessage :: CheckWorkspace { revision: self . revision} ) . unwrap( ) ;
318
330
return ;
319
331
}
320
332
}
@@ -349,7 +361,14 @@ enum OrchestratorMessage {
349
361
FileChanges ( Vec < FileWatcherChange > ) ,
350
362
}
351
363
352
- fn setup_tracing ( ) {
364
+ fn setup_tracing ( verbosity : Option < VerbosityLevel > ) {
365
+ let trace_level = match verbosity {
366
+ None => Level :: WARN ,
367
+ Some ( VerbosityLevel :: Info ) => Level :: INFO ,
368
+ Some ( VerbosityLevel :: Debug ) => Level :: DEBUG ,
369
+ Some ( VerbosityLevel :: Trace ) => Level :: TRACE ,
370
+ } ;
371
+
353
372
let subscriber = Registry :: default ( ) . with (
354
373
tracing_tree:: HierarchicalLayer :: default ( )
355
374
. with_indent_lines ( true )
@@ -359,9 +378,7 @@ fn setup_tracing() {
359
378
. with_targets ( true )
360
379
. with_writer ( || Box :: new ( std:: io:: stderr ( ) ) )
361
380
. with_timer ( Uptime :: default ( ) )
362
- . with_filter ( LoggingFilter {
363
- trace_level : Level :: TRACE ,
364
- } ) ,
381
+ . with_filter ( LoggingFilter { trace_level } ) ,
365
382
) ;
366
383
367
384
tracing:: subscriber:: set_global_default ( subscriber) . unwrap ( ) ;
0 commit comments