Skip to content

Commit e421050

Browse files
committed
Log errors by default
Remove imperative `clap::Error::exit`.
1 parent f54e2ca commit e421050

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

src/bin/bpf-linker.rs

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ extern crate aya_rustc_llvm_proxy;
66
use std::{env, fs, io, path::PathBuf, str::FromStr};
77

88
use bpf_linker::{Cpu, Linker, LinkerOptions, OptLevel, OutputType};
9-
use clap::Parser;
9+
use clap::{CommandFactory as _, Parser};
1010
use thiserror::Error;
11-
use tracing::{info, Level, Subscriber};
12-
use tracing_appender::non_blocking::WorkerGuard;
11+
use tracing::{info, Level};
1312
use tracing_subscriber::{fmt::MakeWriter, prelude::*, EnvFilter};
1413
use tracing_tree::HierarchicalLayer;
1514

@@ -163,6 +162,12 @@ where
163162
}
164163

165164
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<()> {
166171
let args = env::args().map(|arg| {
167172
if arg == "-flavor" {
168173
"--flavor".to_string()
@@ -192,47 +197,48 @@ fn main() {
192197
export,
193198
fatal_errors,
194199
_debug,
195-
} = Parser::parse_from(args);
200+
} = Parser::try_parse_from(args)?;
201+
let mut cmd = CommandLine::command();
196202

197203
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"));
199205
}
200206

201207
// 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 {
209216
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);
219230
let subscriber = subscriber_registry
220231
.with(tracing_layer(io::stdout))
221232
.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))
225234
}
226235
None => {
227236
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)
230238
}
231-
};
232-
if let Err(e) = tracing::subscriber::set_global_default(subscriber) {
233-
error(&e.to_string(), clap::error::ErrorKind::Format);
234239
}
235240
}
241+
.map_err(|e| cmd.error(clap::error::ErrorKind::Format, e.to_string()))?;
236242

237243
info!(
238244
"command line: {:?}",
@@ -242,9 +248,7 @@ fn main() {
242248
let export_symbols = export_symbols
243249
.map(fs::read_to_string)
244250
.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()))?;
248252

249253
// TODO: the data is owned by this call frame; we could make this zero-alloc.
250254
let export_symbols = export_symbols
@@ -284,23 +288,18 @@ fn main() {
284288
btf,
285289
});
286290

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()))?;
293294

294295
if fatal_errors && linker.has_errors() {
295-
error(
296-
"LLVM issued diagnostic with error severity",
296+
return Err(cmd.error(
297297
clap::error::ErrorKind::Io,
298-
);
298+
"LLVM issued diagnostic with error severity",
299+
));
299300
}
300-
}
301301

302-
fn error(desc: &str, kind: clap::error::ErrorKind) -> ! {
303-
clap::Error::raw(kind, desc.to_string()).exit();
302+
Ok(())
304303
}
305304

306305
#[cfg(test)]

src/linker.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,6 @@ impl llvm::LLVMDiagnosticHandler for Linker {
569569
match severity {
570570
llvm_sys::LLVMDiagnosticSeverity::LLVMDSError => {
571571
if MATCHERS.iter().any(|matcher| message.ends_with(matcher)) {
572-
warn!("llvm: {}", message);
573572
return;
574573
}
575574
self.has_errors = true;

0 commit comments

Comments
 (0)