Skip to content

Commit d455050

Browse files
onbjergDaniPopes
andauthored
feat: configurable logging destination (#498)
Allow the tracing output to be written to either stdout or stderr, defaulting to stdout. Can be extended with support for files later, if desired. Useful for #401 to get more detailed output --------- Co-authored-by: DaniPopes <[email protected]>
1 parent 2225547 commit d455050

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

crates/cli/src/utils.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Utility functions used by the Solar CLI.
22
33
use solar_interface::diagnostics::DiagCtxt;
4+
use std::io;
5+
6+
#[cfg(feature = "tracing")]
7+
use solar_sema::ast::Either;
48

59
#[cfg(feature = "mimalloc")]
610
use mimalloc as _;
@@ -43,9 +47,31 @@ pub const fn new_allocator() -> Allocator {
4347
new_wrapped_allocator()
4448
}
4549

50+
/// `tracing` logger destination.
51+
#[derive(Default)]
52+
pub enum LogDestination {
53+
/// [`io::stdout`].
54+
#[default]
55+
Stdout,
56+
/// [`io::stderr`].
57+
Stderr,
58+
}
59+
60+
#[cfg(feature = "tracing")]
61+
impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for LogDestination {
62+
type Writer = Either<io::Stdout, io::Stderr>;
63+
64+
fn make_writer(&'a self) -> Self::Writer {
65+
match self {
66+
Self::Stdout => Either::Left(io::stdout()),
67+
Self::Stderr => Either::Right(io::stderr()),
68+
}
69+
}
70+
}
71+
4672
/// Initialize the tracing logger.
4773
#[must_use]
48-
pub fn init_logger() -> impl Sized {
74+
pub fn init_logger(dst: LogDestination) -> impl Sized {
4975
#[cfg(not(feature = "tracing"))]
5076
{
5177
if std::env::var_os("RUST_LOG").is_some() {
@@ -60,14 +86,14 @@ pub fn init_logger() -> impl Sized {
6086
}
6187

6288
#[cfg(feature = "tracing")]
63-
match try_init_logger() {
89+
match try_init_logger(dst) {
6490
Ok(guard) => guard,
6591
Err(e) => DiagCtxt::new_early().fatal(e).emit(),
6692
}
6793
}
6894

6995
#[cfg(feature = "tracing")]
70-
fn try_init_logger() -> Result<impl Sized, String> {
96+
fn try_init_logger(dst: LogDestination) -> Result<impl Sized, String> {
7197
use tracing_subscriber::prelude::*;
7298

7399
let (profile_layer, guard) = match std::env::var("SOLAR_PROFILE").as_deref() {
@@ -90,7 +116,7 @@ fn try_init_logger() -> Result<impl Sized, String> {
90116
tracing_subscriber::Registry::default()
91117
.with(tracing_subscriber::EnvFilter::from_default_env())
92118
.with(profile_layer)
93-
.with(tracing_subscriber::fmt::layer())
119+
.with(tracing_subscriber::fmt::layer().with_writer(dst))
94120
.try_init()
95121
.map(|()| guard)
96122
.map_err(|e| e.to_string())

crates/solar/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static ALLOC: utils::Allocator = utils::new_allocator();
1212
fn main() -> ExitCode {
1313
signal_handler::install();
1414
panic_hook::install();
15-
let _guard = utils::init_logger();
15+
let _guard = utils::init_logger(Default::default());
1616
let args = match parse_args(std::env::args_os()) {
1717
Ok(args) => args,
1818
Err(e) => e.exit(),

0 commit comments

Comments
 (0)