11//! Utility functions used by the Solar CLI.
22
33use solar_interface:: diagnostics:: DiagCtxt ;
4+ use std:: io;
5+
6+ #[ cfg( feature = "tracing" ) ]
7+ use solar_sema:: ast:: Either ;
48
59#[ cfg( feature = "mimalloc" ) ]
610use 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 ( ) )
0 commit comments