11use std:: cell:: RefCell ;
22use std:: fs:: { File , OpenOptions } ;
33use std:: io:: Write ;
4+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
45use std:: sync:: { Arc , Once } ;
56
67use time:: macros:: format_description;
@@ -10,6 +11,33 @@ use tracing_subscriber::fmt::time::UtcTime;
1011const LOG_TIMESTAMP_FORMAT : & [ time:: format_description:: FormatItem < ' static > ] =
1112 format_description ! ( "[year]-[month]-[day]T[hour]:[minute]:[second]Z" ) ;
1213const DEBUG_ENV : & str = "TUNMUX_DEBUG" ;
14+ const GOTATUN_UAPI_CONNECTION_TARGET : & str = "gotatun::device::uapi" ;
15+ const GOTATUN_UAPI_CONNECTION_MESSAGE : & str = "New UAPI connection on unix socket" ;
16+
17+ static SUPPRESS_GOTATUN_UAPI_CONNECTION_LOGS : AtomicUsize = AtomicUsize :: new ( 0 ) ;
18+
19+ pub struct GotatunUapiConnectionLogSuppression ;
20+
21+ impl Drop for GotatunUapiConnectionLogSuppression {
22+ fn drop ( & mut self ) {
23+ SUPPRESS_GOTATUN_UAPI_CONNECTION_LOGS . fetch_sub ( 1 , Ordering :: Relaxed ) ;
24+ }
25+ }
26+
27+ pub fn suppress_gotatun_uapi_connection_logs ( ) -> GotatunUapiConnectionLogSuppression {
28+ SUPPRESS_GOTATUN_UAPI_CONNECTION_LOGS . fetch_add ( 1 , Ordering :: Relaxed ) ;
29+ GotatunUapiConnectionLogSuppression
30+ }
31+
32+ fn should_suppress_log_write ( buf : & [ u8 ] ) -> bool {
33+ if SUPPRESS_GOTATUN_UAPI_CONNECTION_LOGS . load ( Ordering :: Relaxed ) == 0 {
34+ return false ;
35+ }
36+ let Ok ( line) = std:: str:: from_utf8 ( buf) else {
37+ return false ;
38+ } ;
39+ line. contains ( GOTATUN_UAPI_CONNECTION_TARGET ) && line. contains ( GOTATUN_UAPI_CONNECTION_MESSAGE )
40+ }
1341
1442fn level_from_env_or_default ( default : LevelFilter ) -> LevelFilter {
1543 if debug_enabled ( ) {
@@ -141,6 +169,9 @@ struct ServiceWriter;
141169
142170impl Write for ServiceWriter {
143171 fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
172+ if should_suppress_log_write ( buf) {
173+ return Ok ( buf. len ( ) ) ;
174+ }
144175 let _ = std:: io:: stderr ( ) . write_all ( buf) ;
145176 LOG_CAPTURE . with ( |cell| {
146177 if let Some ( capture) = cell. borrow_mut ( ) . as_mut ( ) {
@@ -187,6 +218,9 @@ struct SharedFileWriter(Arc<File>);
187218
188219impl Write for SharedFileWriter {
189220 fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
221+ if should_suppress_log_write ( buf) {
222+ return Ok ( buf. len ( ) ) ;
223+ }
190224 ( & * self . 0 ) . write ( buf)
191225 }
192226
0 commit comments