33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- use std:: path:: PathBuf ;
6+ use std:: path:: { Path , PathBuf } ;
77
88use async_trait:: async_trait;
99use tokio:: sync:: mpsc;
1010
1111use crate :: commands:: tunnels:: ShutdownSignal ;
1212use crate :: log;
1313use crate :: state:: LauncherPaths ;
14- use crate :: util:: errors:: AnyError ;
14+ use crate :: util:: errors:: { wrap, AnyError } ;
15+ use crate :: util:: io:: { tailf, TailEvent } ;
1516
1617pub const SERVICE_LOG_FILE_NAME : & str = "tunnel-service.log" ;
1718
@@ -21,7 +22,7 @@ pub trait ServiceContainer: Send {
2122 & mut self ,
2223 log : log:: Logger ,
2324 launcher_paths : LauncherPaths ,
24- shutdown_rx : mpsc:: Receiver < ShutdownSignal > ,
25+ shutdown_rx : mpsc:: UnboundedReceiver < ShutdownSignal > ,
2526 ) -> Result < ( ) , AnyError > ;
2627}
2728
@@ -40,6 +41,9 @@ pub trait ServiceManager {
4041 handle : impl ' static + ServiceContainer ,
4142 ) -> Result < ( ) , AnyError > ;
4243
44+ /// Show logs from the running service to standard out.
45+ async fn show_logs ( & self ) -> Result < ( ) , AnyError > ;
46+
4347 /// Unregisters the current executable as a service.
4448 async fn unregister ( & self ) -> Result < ( ) , AnyError > ;
4549}
@@ -50,50 +54,42 @@ pub type ServiceManagerImpl = super::service_windows::WindowsService;
5054#[ cfg( target_os = "linux" ) ]
5155pub type ServiceManagerImpl = super :: service_linux:: SystemdService ;
5256
53- #[ cfg( not ( any ( target_os = "windows" , target_os = "linux" ) ) ) ]
54- pub type ServiceManagerImpl = UnimplementedServiceManager ;
57+ #[ cfg( target_os = "macos" ) ]
58+ pub type ServiceManagerImpl = super :: service_macos :: LaunchdService ;
5559
5660#[ allow( unreachable_code) ]
5761#[ allow( unused_variables) ]
5862pub fn create_service_manager ( log : log:: Logger , paths : & LauncherPaths ) -> ServiceManagerImpl {
63+ #[ cfg( target_os = "macos" ) ]
64+ {
65+ super :: service_macos:: LaunchdService :: new ( log, paths)
66+ }
5967 #[ cfg( target_os = "windows" ) ]
6068 {
61- super :: service_windows:: WindowsService :: new ( log)
69+ super :: service_windows:: WindowsService :: new ( log, paths )
6270 }
6371 #[ cfg( target_os = "linux" ) ]
6472 {
6573 super :: service_linux:: SystemdService :: new ( log, paths. clone ( ) )
6674 }
67- #[ cfg( not( any( target_os = "windows" , target_os = "linux" ) ) ) ]
68- {
69- UnimplementedServiceManager :: new ( )
70- }
7175}
7276
73- pub struct UnimplementedServiceManager ( ) ;
74-
75- #[ allow( dead_code) ]
76- impl UnimplementedServiceManager {
77- fn new ( ) -> Self {
78- Self ( )
77+ #[ allow( dead_code) ] // unused on Linux
78+ pub ( crate ) async fn tail_log_file ( log_file : & Path ) -> Result < ( ) , AnyError > {
79+ if !log_file. exists ( ) {
80+ println ! ( "The tunnel service has not started yet." ) ;
81+ return Ok ( ( ) ) ;
7982 }
80- }
8183
82- #[ async_trait]
83- impl ServiceManager for UnimplementedServiceManager {
84- async fn register ( & self , _exe : PathBuf , _args : & [ & str ] ) -> Result < ( ) , AnyError > {
85- unimplemented ! ( "Service management is not supported on this platform" ) ;
86- }
87-
88- async fn run (
89- self ,
90- _launcher_paths : LauncherPaths ,
91- _handle : impl ' static + ServiceContainer ,
92- ) -> Result < ( ) , AnyError > {
93- unimplemented ! ( "Service management is not supported on this platform" ) ;
84+ let file = std:: fs:: File :: open ( log_file) . map_err ( |e| wrap ( e, "error opening log file" ) ) ?;
85+ let mut rx = tailf ( file, 20 ) ;
86+ while let Some ( line) = rx. recv ( ) . await {
87+ match line {
88+ TailEvent :: Line ( l) => print ! ( "{}" , l) ,
89+ TailEvent :: Reset => println ! ( "== Tunnel service restarted ==" ) ,
90+ TailEvent :: Err ( e) => return Err ( wrap ( e, "error reading log file" ) . into ( ) ) ,
91+ }
9492 }
9593
96- async fn unregister ( & self ) -> Result < ( ) , AnyError > {
97- unimplemented ! ( "Service management is not supported on this platform" ) ;
98- }
94+ Ok ( ( ) )
9995}
0 commit comments