11use super :: { ConsoleLayer , Server } ;
2+ #[ cfg( unix) ]
3+ use std:: path:: Path ;
24use std:: {
3- net:: { SocketAddr , ToSocketAddrs } ,
5+ net:: { IpAddr , SocketAddr , SocketAddrV4 , SocketAddrV6 , ToSocketAddrs } ,
46 path:: PathBuf ,
57 thread,
68 time:: Duration ,
@@ -32,7 +34,7 @@ pub struct Builder {
3234 pub ( crate ) retention : Duration ,
3335
3436 /// The address on which to serve the RPC server.
35- pub ( super ) server_addr : SocketAddr ,
37+ pub ( super ) server_addr : ServerAddr ,
3638
3739 /// If and where to save a recording of the events.
3840 pub ( super ) recording_path : Option < PathBuf > ,
@@ -58,7 +60,7 @@ impl Default for Builder {
5860 publish_interval : ConsoleLayer :: DEFAULT_PUBLISH_INTERVAL ,
5961 retention : ConsoleLayer :: DEFAULT_RETENTION ,
6062 poll_duration_max : ConsoleLayer :: DEFAULT_POLL_DURATION_MAX ,
61- server_addr : SocketAddr :: new ( Server :: DEFAULT_IP , Server :: DEFAULT_PORT ) ,
63+ server_addr : ServerAddr :: Tcp ( SocketAddr :: new ( Server :: DEFAULT_IP , Server :: DEFAULT_PORT ) ) ,
6264 recording_path : None ,
6365 filter_env_var : "RUST_LOG" . to_string ( ) ,
6466 self_trace : false ,
@@ -137,8 +139,38 @@ impl Builder {
137139 /// before falling back on constructing a socket address from those
138140 /// defaults.
139141 ///
142+ /// The socket address can be either a TCP socket address or a
143+ /// [Unix domain socket] (UDS) address. Unix domain sockets are only
144+ /// supported on Unix-compatible operating systems, such as Linux, BSDs,
145+ /// and macOS.
146+ ///
147+ /// Each call to this method will overwrite the previously set value.
148+ ///
149+ /// # Examples
150+ ///
151+ /// Connect to the TCP address `localhost:1234`:
152+ ///
153+ /// ```
154+ /// # use console_subscriber::Builder;
155+ /// use std::net::Ipv4Addr;
156+ /// let builder = Builder::default().server_addr((Ipv4Addr::LOCALHOST, 1234));
157+ /// ```
158+ ///
159+ /// Connect to the UDS address `/tmp/tokio-console`:
160+ ///
161+ /// ```
162+ /// # use console_subscriber::Builder;
163+ /// # #[cfg(unix)]
164+ /// use std::path::Path;
165+ ///
166+ /// // Unix domain sockets are only available on Unix-compatible operating systems.
167+ /// #[cfg(unix)]
168+ /// let builder = Builder::default().server_addr(Path::new("/tmp/tokio-console"));
169+ /// ```
170+ ///
140171 /// [environment variable]: `Builder::with_default_env`
141- pub fn server_addr ( self , server_addr : impl Into < SocketAddr > ) -> Self {
172+ /// [Unix domain socket]: https://en.wikipedia.org/wiki/Unix_domain_socket
173+ pub fn server_addr ( self , server_addr : impl Into < ServerAddr > ) -> Self {
142174 Self {
143175 server_addr : server_addr. into ( ) ,
144176 ..self
@@ -231,11 +263,14 @@ impl Builder {
231263 }
232264
233265 if let Ok ( bind) = std:: env:: var ( "TOKIO_CONSOLE_BIND" ) {
234- self . server_addr = bind
235- . to_socket_addrs ( )
236- . expect ( "TOKIO_CONSOLE_BIND must be formatted as HOST:PORT, such as localhost:4321" )
237- . next ( )
238- . expect ( "tokio console could not resolve TOKIO_CONSOLE_BIND" ) ;
266+ self . server_addr = ServerAddr :: Tcp (
267+ bind. to_socket_addrs ( )
268+ . expect (
269+ "TOKIO_CONSOLE_BIND must be formatted as HOST:PORT, such as localhost:4321" ,
270+ )
271+ . next ( )
272+ . expect ( "tokio console could not resolve TOKIO_CONSOLE_BIND" ) ,
273+ ) ;
239274 }
240275
241276 if let Some ( interval) = duration_from_env ( "TOKIO_CONSOLE_PUBLISH_INTERVAL" ) {
@@ -456,6 +491,66 @@ impl Builder {
456491 }
457492}
458493
494+ /// Specifies the address on which a [`Server`] should listen.
495+ ///
496+ /// This type is passed as an argument to the [`Builder::server_addr`]
497+ /// method, and may be either a TCP socket address, or a [Unix domain socket]
498+ /// (UDS) address. Unix domain sockets are only supported on Unix-compatible
499+ /// operating systems, such as Linux, BSDs, and macOS.
500+ ///
501+ /// [`Server`]: crate::Server
502+ /// [Unix domain socket]: https://en.wikipedia.org/wiki/Unix_domain_socket
503+ #[ derive( Clone , Debug ) ]
504+ #[ non_exhaustive]
505+ pub enum ServerAddr {
506+ /// A TCP address.
507+ Tcp ( SocketAddr ) ,
508+ /// A Unix socket address.
509+ #[ cfg( unix) ]
510+ Unix ( PathBuf ) ,
511+ }
512+
513+ impl From < SocketAddr > for ServerAddr {
514+ fn from ( addr : SocketAddr ) -> ServerAddr {
515+ ServerAddr :: Tcp ( addr)
516+ }
517+ }
518+
519+ impl From < SocketAddrV4 > for ServerAddr {
520+ fn from ( addr : SocketAddrV4 ) -> ServerAddr {
521+ ServerAddr :: Tcp ( addr. into ( ) )
522+ }
523+ }
524+
525+ impl From < SocketAddrV6 > for ServerAddr {
526+ fn from ( addr : SocketAddrV6 ) -> ServerAddr {
527+ ServerAddr :: Tcp ( addr. into ( ) )
528+ }
529+ }
530+
531+ impl < I > From < ( I , u16 ) > for ServerAddr
532+ where
533+ I : Into < IpAddr > ,
534+ {
535+ fn from ( pieces : ( I , u16 ) ) -> ServerAddr {
536+ ServerAddr :: Tcp ( pieces. into ( ) )
537+ }
538+ }
539+
540+ #[ cfg( unix) ]
541+ impl From < PathBuf > for ServerAddr {
542+ fn from ( path : PathBuf ) -> ServerAddr {
543+ ServerAddr :: Unix ( path)
544+ }
545+ }
546+
547+ #[ cfg( unix) ]
548+ impl < ' a > From < & ' a Path > for ServerAddr {
549+ fn from ( path : & ' a Path ) -> ServerAddr {
550+ ServerAddr :: Unix ( path. to_path_buf ( ) )
551+ }
552+ }
553+
459554/// Initializes the console [tracing `Subscriber`][sub] and starts the console
460555/// subscriber [`Server`] on its own background thread.
461556///
0 commit comments