Skip to content

Commit e993cf4

Browse files
committed
feat: configurable logging destination
1 parent 02cc45c commit e993cf4

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

crates/cli/src/utils.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Utility functions used by the Solar CLI.
22
3+
use std::io::{self};
4+
35
use solar_interface::diagnostics::DiagCtxt;
46

57
#[cfg(feature = "mimalloc")]
@@ -43,9 +45,28 @@ pub const fn new_allocator() -> Allocator {
4345
new_wrapped_allocator()
4446
}
4547

48+
#[derive(Default)]
49+
pub enum LogDestination {
50+
#[default]
51+
Stdout,
52+
Stderr,
53+
}
54+
55+
#[cfg(feature = "tracing")]
56+
impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for LogDestination {
57+
type Writer = Box<dyn io::Write>;
58+
59+
fn make_writer(&'a self) -> Self::Writer {
60+
match self {
61+
Self::Stdout => Box::new(std::io::stdout().lock()),
62+
Self::Stderr => Box::new(std::io::stderr().lock()),
63+
}
64+
}
65+
}
66+
4667
/// Initialize the tracing logger.
4768
#[must_use]
48-
pub fn init_logger() -> impl Sized {
69+
pub fn init_logger(dst: LogDestination) -> impl Sized {
4970
#[cfg(not(feature = "tracing"))]
5071
{
5172
if std::env::var_os("RUST_LOG").is_some() {
@@ -60,14 +81,14 @@ pub fn init_logger() -> impl Sized {
6081
}
6182

6283
#[cfg(feature = "tracing")]
63-
match try_init_logger() {
84+
match try_init_logger(dst) {
6485
Ok(guard) => guard,
6586
Err(e) => DiagCtxt::new_early().fatal(e).emit(),
6687
}
6788
}
6889

6990
#[cfg(feature = "tracing")]
70-
fn try_init_logger() -> Result<impl Sized, String> {
91+
fn try_init_logger(dst: LogDestination) -> Result<impl Sized, String> {
7192
use tracing_subscriber::prelude::*;
7293

7394
let (profile_layer, guard) = match std::env::var("SOLAR_PROFILE").as_deref() {
@@ -90,7 +111,7 @@ fn try_init_logger() -> Result<impl Sized, String> {
90111
tracing_subscriber::Registry::default()
91112
.with(tracing_subscriber::EnvFilter::from_default_env())
92113
.with(profile_layer)
93-
.with(tracing_subscriber::fmt::layer())
114+
.with(tracing_subscriber::fmt::layer().with_writer(dst))
94115
.try_init()
95116
.map(|()| guard)
96117
.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)