@@ -6,10 +6,9 @@ extern crate aya_rustc_llvm_proxy;
6
6
use std:: { env, fs, io, path:: PathBuf , str:: FromStr } ;
7
7
8
8
use bpf_linker:: { Cpu , Linker , LinkerOptions , OptLevel , OutputType } ;
9
- use clap:: Parser ;
9
+ use clap:: { CommandFactory as _ , Parser } ;
10
10
use thiserror:: Error ;
11
- use tracing:: { info, Level , Subscriber } ;
12
- use tracing_appender:: non_blocking:: WorkerGuard ;
11
+ use tracing:: { info, Level } ;
13
12
use tracing_subscriber:: { fmt:: MakeWriter , prelude:: * , EnvFilter } ;
14
13
use tracing_tree:: HierarchicalLayer ;
15
14
@@ -163,6 +162,12 @@ where
163
162
}
164
163
165
164
fn main ( ) {
165
+ if let Err ( e) = try_main ( ) {
166
+ e. exit ( ) // Needed for proper formatting.
167
+ }
168
+ }
169
+
170
+ fn try_main ( ) -> clap:: error:: Result < ( ) > {
166
171
let args = env:: args ( ) . map ( |arg| {
167
172
if arg == "-flavor" {
168
173
"--flavor" . to_string ( )
@@ -192,47 +197,48 @@ fn main() {
192
197
export,
193
198
fatal_errors,
194
199
_debug,
195
- } = Parser :: parse_from ( args) ;
200
+ } = Parser :: try_parse_from ( args) ?;
201
+ let mut cmd = CommandLine :: command ( ) ;
196
202
197
203
if inputs. is_empty ( ) {
198
- error ( "no input files" , clap:: error:: ErrorKind :: TooFewValues ) ;
204
+ return Err ( cmd . error ( clap:: error:: ErrorKind :: TooFewValues , "no input files" ) ) ;
199
205
}
200
206
201
207
// Configure tracing.
202
- if let Some ( log_level) = log_level {
203
- let subscriber_registry = tracing_subscriber:: registry ( )
204
- . with ( EnvFilter :: from_default_env ( ) . add_directive ( log_level. into ( ) ) ) ;
205
- let ( subscriber, _guard) : (
206
- Box < dyn Subscriber + Send + Sync + ' static > ,
207
- Option < WorkerGuard > ,
208
- ) = match log_file {
208
+ let _guard = {
209
+ let filter = EnvFilter :: from_default_env ( ) ;
210
+ let filter = match log_level {
211
+ None => filter,
212
+ Some ( log_level) => filter. add_directive ( log_level. into ( ) ) ,
213
+ } ;
214
+ let subscriber_registry = tracing_subscriber:: registry ( ) . with ( filter) ;
215
+ match log_file {
209
216
Some ( log_file) => {
210
- let file_appender = tracing_appender:: rolling:: never (
211
- log_file. parent ( ) . unwrap_or_else ( || {
212
- error ( "invalid log_file" , clap:: error:: ErrorKind :: InvalidValue )
213
- } ) ,
214
- log_file. file_name ( ) . unwrap_or_else ( || {
215
- error ( "invalid log_file" , clap:: error:: ErrorKind :: InvalidValue )
216
- } ) ,
217
- ) ;
218
- let ( non_blocking, _guard) = tracing_appender:: non_blocking ( file_appender) ;
217
+ let mut comps = log_file. components ( ) ;
218
+ let file_name = comps
219
+ . next_back ( )
220
+ . and_then ( |p| match p {
221
+ std:: path:: Component :: Normal ( p) => Some ( p) ,
222
+ _ => None ,
223
+ } )
224
+ . ok_or_else ( || {
225
+ cmd. error ( clap:: error:: ErrorKind :: InvalidValue , "invalid log_file" )
226
+ } ) ?;
227
+ let parent = comps. as_path ( ) ;
228
+ let file_appender = tracing_appender:: rolling:: never ( parent, file_name) ;
229
+ let ( non_blocking, guard) = tracing_appender:: non_blocking ( file_appender) ;
219
230
let subscriber = subscriber_registry
220
231
. with ( tracing_layer ( io:: stdout) )
221
232
. with ( tracing_layer ( non_blocking) ) ;
222
-
223
- println ! ( "Logging to stdout" ) ;
224
- ( Box :: new ( subscriber) , Some ( _guard) )
233
+ tracing:: subscriber:: set_global_default ( subscriber) . map ( |( ) | Some ( guard) )
225
234
}
226
235
None => {
227
236
let subscriber = subscriber_registry. with ( tracing_layer ( io:: stderr) ) ;
228
- println ! ( "Logging to stderr" ) ;
229
- ( Box :: new ( subscriber) , None )
237
+ tracing:: subscriber:: set_global_default ( subscriber) . map ( |( ) | None )
230
238
}
231
- } ;
232
- if let Err ( e) = tracing:: subscriber:: set_global_default ( subscriber) {
233
- error ( & e. to_string ( ) , clap:: error:: ErrorKind :: Format ) ;
234
239
}
235
240
}
241
+ . map_err ( |e| cmd. error ( clap:: error:: ErrorKind :: Format , e. to_string ( ) ) ) ?;
236
242
237
243
info ! (
238
244
"command line: {:?}" ,
@@ -242,9 +248,7 @@ fn main() {
242
248
let export_symbols = export_symbols
243
249
. map ( fs:: read_to_string)
244
250
. transpose ( )
245
- . unwrap_or_else ( |e| {
246
- error ( & e. to_string ( ) , clap:: error:: ErrorKind :: Io ) ;
247
- } ) ;
251
+ . map_err ( |e| cmd. error ( clap:: error:: ErrorKind :: Io , e. to_string ( ) ) ) ?;
248
252
249
253
// TODO: the data is owned by this call frame; we could make this zero-alloc.
250
254
let export_symbols = export_symbols
@@ -284,23 +288,18 @@ fn main() {
284
288
btf,
285
289
} ) ;
286
290
287
- if let Err ( e) = linker. link ( ) {
288
- error (
289
- & format ! ( "{}; has_errors={}" , e, linker. has_errors( ) ) ,
290
- clap:: error:: ErrorKind :: Io ,
291
- ) ;
292
- }
291
+ linker
292
+ . link ( )
293
+ . map_err ( |e| cmd. error ( clap:: error:: ErrorKind :: Io , e. to_string ( ) ) ) ?;
293
294
294
295
if fatal_errors && linker. has_errors ( ) {
295
- error (
296
- "LLVM issued diagnostic with error severity" ,
296
+ return Err ( cmd. error (
297
297
clap:: error:: ErrorKind :: Io ,
298
- ) ;
298
+ "LLVM issued diagnostic with error severity" ,
299
+ ) ) ;
299
300
}
300
- }
301
301
302
- fn error ( desc : & str , kind : clap:: error:: ErrorKind ) -> ! {
303
- clap:: Error :: raw ( kind, desc. to_string ( ) ) . exit ( ) ;
302
+ Ok ( ( ) )
304
303
}
305
304
306
305
#[ cfg( test) ]
0 commit comments