1
1
use super :: { ConsoleLayer , Server } ;
2
+ #[ cfg( unix) ]
3
+ use std:: path:: Path ;
2
4
use std:: {
3
- net:: { SocketAddr , ToSocketAddrs } ,
5
+ net:: { IpAddr , SocketAddr , SocketAddrV4 , SocketAddrV6 , ToSocketAddrs } ,
4
6
path:: PathBuf ,
5
7
thread,
6
8
time:: Duration ,
@@ -32,7 +34,7 @@ pub struct Builder {
32
34
pub ( crate ) retention : Duration ,
33
35
34
36
/// The address on which to serve the RPC server.
35
- pub ( super ) server_addr : SocketAddr ,
37
+ pub ( super ) server_addr : ServerAddr ,
36
38
37
39
/// If and where to save a recording of the events.
38
40
pub ( super ) recording_path : Option < PathBuf > ,
@@ -58,7 +60,7 @@ impl Default for Builder {
58
60
publish_interval : ConsoleLayer :: DEFAULT_PUBLISH_INTERVAL ,
59
61
retention : ConsoleLayer :: DEFAULT_RETENTION ,
60
62
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 ) ) ,
62
64
recording_path : None ,
63
65
filter_env_var : "RUST_LOG" . to_string ( ) ,
64
66
self_trace : false ,
@@ -137,8 +139,38 @@ impl Builder {
137
139
/// before falling back on constructing a socket address from those
138
140
/// defaults.
139
141
///
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
+ ///
140
171
/// [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 {
142
174
Self {
143
175
server_addr : server_addr. into ( ) ,
144
176
..self
@@ -231,11 +263,14 @@ impl Builder {
231
263
}
232
264
233
265
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
+ ) ;
239
274
}
240
275
241
276
if let Some ( interval) = duration_from_env ( "TOKIO_CONSOLE_PUBLISH_INTERVAL" ) {
@@ -456,6 +491,66 @@ impl Builder {
456
491
}
457
492
}
458
493
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
+
459
554
/// Initializes the console [tracing `Subscriber`][sub] and starts the console
460
555
/// subscriber [`Server`] on its own background thread.
461
556
///
0 commit comments