@@ -147,16 +147,25 @@ fn cookie_from_record(cookie: WebViewCookie) -> Result<Cookie<'static>, WebViewE
147147 Ok ( builder. build ( ) )
148148}
149149
150- fn log_enabled ( ) -> bool {
151- static LOG_ENABLED : OnceLock < bool > = OnceLock :: new ( ) ;
152- * LOG_ENABLED . get_or_init ( || {
153- std:: env:: var ( "WRYWEBVIEW_LOG" )
154- . map ( |value| {
155- let value = value. trim ( ) . to_ascii_lowercase ( ) ;
156- matches ! ( value. as_str( ) , "1" | "true" | "yes" | "debug" )
157- } )
158- . unwrap_or ( false )
159- } )
150+ use std:: sync:: atomic:: AtomicBool ;
151+
152+ static LOG_ENABLED : AtomicBool = AtomicBool :: new ( false ) ;
153+
154+ pub ( crate ) fn log_enabled ( ) -> bool {
155+ LOG_ENABLED . load ( Ordering :: Relaxed )
156+ }
157+
158+ #[ uniffi:: export]
159+ pub fn set_log_enabled ( enabled : bool ) {
160+ LOG_ENABLED . store ( enabled, Ordering :: Relaxed ) ;
161+ }
162+
163+ macro_rules! wry_log {
164+ ( $( $arg: tt) * ) => {
165+ if log_enabled( ) {
166+ eprintln!( $( $arg) * ) ;
167+ }
168+ } ;
160169}
161170
162171// ============================================================================
@@ -176,7 +185,7 @@ fn create_webview_inner(
176185 if trimmed. is_empty ( ) { None } else { Some ( trimmed) }
177186 } ) ;
178187
179- eprintln ! (
188+ wry_log ! (
180189 "[wrywebview] create_webview handle=0x{:x} size={}x{} url={} user_agent={}" ,
181190 parent_handle,
182191 width,
@@ -207,40 +216,40 @@ fn create_webview_inner(
207216
208217 let webview = builder
209218 . with_navigation_handler ( move |new_url| {
210- eprintln ! ( "[wrywebview] navigation_handler url={}" , new_url) ;
219+ wry_log ! ( "[wrywebview] navigation_handler url={}" , new_url) ;
211220 state_for_nav. is_loading . store ( true , Ordering :: SeqCst ) ;
212221 if let Err ( e) = state_for_nav. update_current_url ( new_url. clone ( ) ) {
213- eprintln ! ( "[wrywebview] navigation_handler state update failed: {}" , e) ;
222+ wry_log ! ( "[wrywebview] navigation_handler state update failed: {}" , e) ;
214223 }
215224 true
216225 } )
217226 . with_on_page_load_handler ( move |event, url| {
218227 match event {
219228 wry:: PageLoadEvent :: Started => {
220- eprintln ! ( "[wrywebview] page_load_handler event=Started url={}" , url) ;
229+ wry_log ! ( "[wrywebview] page_load_handler event=Started url={}" , url) ;
221230 state_for_load. is_loading . store ( true , Ordering :: SeqCst ) ;
222231 }
223232 wry:: PageLoadEvent :: Finished => {
224- eprintln ! ( "[wrywebview] page_load_handler event=Finished url={}" , url) ;
233+ wry_log ! ( "[wrywebview] page_load_handler event=Finished url={}" , url) ;
225234 state_for_load. is_loading . store ( false , Ordering :: SeqCst ) ;
226235 if let Err ( e) = state_for_load. update_current_url ( url. clone ( ) ) {
227- eprintln ! ( "[wrywebview] page_load_handler state update failed: {}" , e) ;
236+ wry_log ! ( "[wrywebview] page_load_handler state update failed: {}" , e) ;
228237 }
229238 }
230239 }
231240 } )
232241 . with_document_title_changed_handler ( move |title| {
233- eprintln ! ( "[wrywebview] title_changed title={}" , title) ;
242+ wry_log ! ( "[wrywebview] title_changed title={}" , title) ;
234243 if let Err ( e) = state_for_title. update_page_title ( title) {
235- eprintln ! ( "[wrywebview] title_changed state update failed: {}" , e) ;
244+ wry_log ! ( "[wrywebview] title_changed state update failed: {}" , e) ;
236245 }
237246 } )
238247 . with_ipc_handler ( move |request| {
239248 let url = request. uri ( ) . to_string ( ) ;
240249 let message = request. into_body ( ) ;
241- eprintln ! ( "[wrywebview] ipc url={} body_len={}" , url, message. len( ) ) ;
250+ wry_log ! ( "[wrywebview] ipc url={} body_len={}" , url, message. len( ) ) ;
242251 if let Err ( e) = state_for_ipc. push_ipc_message ( message) {
243- eprintln ! ( "[wrywebview] ipc queue push failed: {}" , e) ;
252+ wry_log ! ( "[wrywebview] ipc queue push failed: {}" , e) ;
244253 }
245254 } )
246255 . build_as_child ( & window) ?;
@@ -258,7 +267,7 @@ fn create_webview_inner(
258267
259268 // Connect to button-press-event to grab focus when clicked using X11
260269 gtk_widget. connect_button_press_event ( |widget, _event| {
261- eprintln ! ( "[wrywebview] button_press_event -> grab_focus" ) ;
270+ wry_log ! ( "[wrywebview] button_press_event -> grab_focus" ) ;
262271
263272 // Use X11 focus directly for proper keyboard input
264273 if let Some ( gdk_window) = widget. window ( ) {
@@ -281,7 +290,7 @@ fn create_webview_inner(
281290 x11:: xlib:: RevertToParent ,
282291 x11:: xlib:: CurrentTime ,
283292 ) ;
284- eprintln ! ( "[wrywebview] button_press XSetInputFocus xid=0x{:x}" , xid) ;
293+ wry_log ! ( "[wrywebview] button_press XSetInputFocus xid=0x{:x}" , xid) ;
285294 }
286295 }
287296 }
@@ -292,11 +301,11 @@ fn create_webview_inner(
292301 widget. grab_focus ( ) ;
293302 gtk:: glib:: Propagation :: Proceed
294303 } ) ;
295- eprintln ! ( "[wrywebview] gtk focus handling configured with X11 support" ) ;
304+ wry_log ! ( "[wrywebview] gtk focus handling configured with X11 support" ) ;
296305 }
297306
298307 let id = register ( webview, state) ?;
299- eprintln ! ( "[wrywebview] create_webview success id={}" , id) ;
308+ wry_log ! ( "[wrywebview] create_webview success id={}" , id) ;
300309 Ok ( id)
301310}
302311
@@ -343,7 +352,7 @@ pub fn create_webview_with_user_agent(
343352
344353fn set_bounds_inner ( id : u64 , x : i32 , y : i32 , width : i32 , height : i32 ) -> Result < ( ) , WebViewError > {
345354 if log_enabled ( ) {
346- eprintln ! (
355+ wry_log ! (
347356 "[wrywebview] set_bounds id={} pos=({}, {}) size={}x{}" ,
348357 id, x, y, width, height
349358 ) ;
@@ -381,7 +390,7 @@ pub fn set_bounds(id: u64, x: i32, y: i32, width: i32, height: i32) -> Result<()
381390// ============================================================================
382391
383392fn load_url_inner ( id : u64 , url : String ) -> Result < ( ) , WebViewError > {
384- eprintln ! ( "[wrywebview] load_url id={} url={}" , id, url) ;
393+ wry_log ! ( "[wrywebview] load_url id={} url={}" , id, url) ;
385394 if let Ok ( state) = get_state ( id) {
386395 state. is_loading . store ( true , Ordering :: SeqCst ) ;
387396 }
@@ -404,7 +413,7 @@ fn load_url_with_headers_inner(
404413 url : String ,
405414 headers : Vec < HttpHeader > ,
406415) -> Result < ( ) , WebViewError > {
407- eprintln ! (
416+ wry_log ! (
408417 "[wrywebview] load_url_with_headers id={} url={} headers={}" ,
409418 id,
410419 url,
@@ -437,7 +446,7 @@ pub fn load_url_with_headers(
437446}
438447
439448fn load_html_inner ( id : u64 , html : String ) -> Result < ( ) , WebViewError > {
440- eprintln ! ( "[wrywebview] load_html id={} bytes={}" , id, html. len( ) ) ;
449+ wry_log ! ( "[wrywebview] load_html id={} bytes={}" , id, html. len( ) ) ;
441450 if let Ok ( state) = get_state ( id) {
442451 state. is_loading . store ( true , Ordering :: SeqCst ) ;
443452 }
@@ -456,7 +465,7 @@ pub fn load_html(id: u64, html: String) -> Result<(), WebViewError> {
456465}
457466
458467fn stop_loading_inner ( id : u64 ) -> Result < ( ) , WebViewError > {
459- eprintln ! ( "[wrywebview] stop_loading id={}" , id) ;
468+ wry_log ! ( "[wrywebview] stop_loading id={}" , id) ;
460469 if let Ok ( state) = get_state ( id) {
461470 state. is_loading . store ( false , Ordering :: SeqCst ) ;
462471 }
@@ -512,7 +521,7 @@ pub fn evaluate_javascript(
512521}
513522
514523fn go_back_inner ( id : u64 ) -> Result < ( ) , WebViewError > {
515- eprintln ! ( "[wrywebview] go_back id={}" , id) ;
524+ wry_log ! ( "[wrywebview] go_back id={}" , id) ;
516525 if let Ok ( state) = get_state ( id) {
517526 state. is_loading . store ( true , Ordering :: SeqCst ) ;
518527 }
@@ -535,7 +544,7 @@ pub fn go_back(id: u64) -> Result<(), WebViewError> {
535544}
536545
537546fn go_forward_inner ( id : u64 ) -> Result < ( ) , WebViewError > {
538- eprintln ! ( "[wrywebview] go_forward id={}" , id) ;
547+ wry_log ! ( "[wrywebview] go_forward id={}" , id) ;
539548 if let Ok ( state) = get_state ( id) {
540549 state. is_loading . store ( true , Ordering :: SeqCst ) ;
541550 }
@@ -558,7 +567,7 @@ pub fn go_forward(id: u64) -> Result<(), WebViewError> {
558567}
559568
560569fn reload_inner ( id : u64 ) -> Result < ( ) , WebViewError > {
561- eprintln ! ( "[wrywebview] reload id={}" , id) ;
570+ wry_log ! ( "[wrywebview] reload id={}" , id) ;
562571 if let Ok ( state) = get_state ( id) {
563572 state. is_loading . store ( true , Ordering :: SeqCst ) ;
564573 }
@@ -585,7 +594,7 @@ pub fn reload(id: u64) -> Result<(), WebViewError> {
585594// ============================================================================
586595
587596fn focus_inner ( id : u64 ) -> Result < ( ) , WebViewError > {
588- eprintln ! ( "[wrywebview] focus id={}" , id) ;
597+ wry_log ! ( "[wrywebview] focus id={}" , id) ;
589598 with_webview ( id, |webview| {
590599 // On Linux, we need to use X11 focus directly since the GTK widget
591600 // is embedded in a foreign (AWT/Swing) window hierarchy
@@ -629,7 +638,7 @@ fn focus_inner(id: u64) -> Result<(), WebViewError> {
629638 x11:: xlib:: RevertToParent ,
630639 x11:: xlib:: CurrentTime ,
631640 ) ;
632- eprintln ! ( "[wrywebview] XSetInputFocus xid=0x{:x}" , xid) ;
641+ wry_log ! ( "[wrywebview] XSetInputFocus xid=0x{:x}" , xid) ;
633642 }
634643 }
635644 }
@@ -639,7 +648,7 @@ fn focus_inner(id: u64) -> Result<(), WebViewError> {
639648
640649 // Also call GTK grab_focus as a fallback
641650 gtk_widget. grab_focus ( ) ;
642- eprintln ! ( "[wrywebview] gtk grab_focus called" ) ;
651+ wry_log ! ( "[wrywebview] gtk grab_focus called" ) ;
643652 }
644653
645654 webview
@@ -712,7 +721,7 @@ pub fn drain_ipc_messages(id: u64) -> Result<Vec<String>, WebViewError> {
712721// ============================================================================
713722
714723fn get_cookies_for_url_inner ( id : u64 , url : String ) -> Result < Vec < WebViewCookie > , WebViewError > {
715- eprintln ! ( "[wrywebview] get_cookies_for_url id={} url={}" , id, url) ;
724+ wry_log ! ( "[wrywebview] get_cookies_for_url id={} url={}" , id, url) ;
716725 with_webview ( id, |webview| {
717726 let cookies = webview. cookies_for_url ( & url) . map_err ( WebViewError :: from) ?;
718727 Ok ( cookies. iter ( ) . map ( cookie_record_from) . collect ( ) )
@@ -731,7 +740,7 @@ pub fn get_cookies_for_url(id: u64, url: String) -> Result<Vec<WebViewCookie>, W
731740}
732741
733742fn clear_cookies_for_url_inner ( id : u64 , url : String ) -> Result < ( ) , WebViewError > {
734- eprintln ! ( "[wrywebview] clear_cookies_for_url id={} url={}" , id, url) ;
743+ wry_log ! ( "[wrywebview] clear_cookies_for_url id={} url={}" , id, url) ;
735744 with_webview ( id, |webview| {
736745 let cookies = webview. cookies_for_url ( & url) . map_err ( WebViewError :: from) ?;
737746 for cookie in cookies {
@@ -755,7 +764,7 @@ pub fn clear_cookies_for_url(id: u64, url: String) -> Result<(), WebViewError> {
755764}
756765
757766fn clear_all_cookies_inner ( id : u64 ) -> Result < ( ) , WebViewError > {
758- eprintln ! ( "[wrywebview] clear_all_cookies id={}" , id) ;
767+ wry_log ! ( "[wrywebview] clear_all_cookies id={}" , id) ;
759768 with_webview ( id, |webview| {
760769 let cookies = webview. cookies ( ) . map_err ( WebViewError :: from) ?;
761770 for cookie in cookies {
@@ -779,7 +788,7 @@ pub fn clear_all_cookies(id: u64) -> Result<(), WebViewError> {
779788}
780789
781790fn set_cookie_inner ( id : u64 , cookie : WebViewCookie ) -> Result < ( ) , WebViewError > {
782- eprintln ! ( "[wrywebview] set_cookie id={} name={}" , id, & cookie. name) ;
791+ wry_log ! ( "[wrywebview] set_cookie id={} name={}" , id, & cookie. name) ;
783792 let native = cookie_from_record ( cookie) ?;
784793 with_webview ( id, |webview| webview. set_cookie ( & native) . map_err ( WebViewError :: from) )
785794}
@@ -800,7 +809,7 @@ pub fn set_cookie(id: u64, cookie: WebViewCookie) -> Result<(), WebViewError> {
800809// ============================================================================
801810
802811fn destroy_webview_inner ( id : u64 ) -> Result < ( ) , WebViewError > {
803- eprintln ! ( "[wrywebview] destroy_webview id={}" , id) ;
812+ wry_log ! ( "[wrywebview] destroy_webview id={}" , id) ;
804813 unregister ( id)
805814}
806815
0 commit comments