Skip to content

Commit 0bd3f78

Browse files
authored
Merge pull request #12 from kdroidFilter/disable-logs
Introduce configurable logging and refined severity levels
2 parents ec8acdd + 39c7491 commit 0bd3f78

File tree

5 files changed

+76
-54
lines changed

5 files changed

+76
-54
lines changed

webview-compose/src/commonMain/kotlin/io/github/kdroidfilter/webview/setting/WebSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class WebSettings {
2727

2828
var allowUniversalAccessFromFileURLs: Boolean by mutableStateOf(false)
2929

30-
private var logSeverityState: KLogSeverity by mutableStateOf(KLogSeverity.Info)
30+
private var logSeverityState: KLogSeverity by mutableStateOf(KLogSeverity.None)
3131

3232
var logSeverity: KLogSeverity
3333
get() = logSeverityState

webview-compose/src/commonMain/kotlin/io/github/kdroidfilter/webview/util/KLogger.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package io.github.kdroidfilter.webview.util
66
* Keep logging simple and dependency-free across platforms.
77
*/
88
internal object KLogger {
9-
private var minSeverity: KLogSeverity = KLogSeverity.Info
9+
private var minSeverity: KLogSeverity = KLogSeverity.None
1010

1111
fun setMinSeverity(severity: KLogSeverity) {
1212
minSeverity = severity
@@ -39,4 +39,5 @@ enum class KLogSeverity {
3939
Warn,
4040
Error,
4141
Assert,
42+
None,
4243
}

wrywebview/src/main/rust/handle.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::num::NonZeroIsize;
1818
use wry::raw_window_handle::Win32WindowHandle;
1919

2020
use crate::error::WebViewError;
21+
use crate::log_enabled;
2122

2223
/// Wrapper around a raw window handle for WebView creation.
2324
pub struct RawWindow {
@@ -51,26 +52,32 @@ pub fn raw_window_handle_from(parent_handle: u64) -> Result<RawWindowHandle, Web
5152
let hwnd = NonZeroIsize::new(parent_handle as isize)
5253
.ok_or(WebViewError::InvalidWindowHandle)?;
5354
let handle = RawWindowHandle::Win32(Win32WindowHandle::new(hwnd));
54-
eprintln!("[wrywebview] raw_window_handle Win32=0x{:x}", parent_handle);
55+
if log_enabled() {
56+
eprintln!("[wrywebview] raw_window_handle Win32=0x{:x}", parent_handle);
57+
}
5558
return Ok(handle);
5659
}
5760

5861
#[cfg(target_os = "macos")]
5962
{
6063
let ns_view = crate::platform::macos::appkit_ns_view_from_handle(parent_handle)?;
6164
let handle = RawWindowHandle::AppKit(AppKitWindowHandle::new(ns_view));
62-
eprintln!(
63-
"[wrywebview] raw_window_handle AppKit=0x{:x} ns_view=0x{:x}",
64-
parent_handle,
65-
ns_view.as_ptr() as usize
66-
);
65+
if log_enabled() {
66+
eprintln!(
67+
"[wrywebview] raw_window_handle AppKit=0x{:x} ns_view=0x{:x}",
68+
parent_handle,
69+
ns_view.as_ptr() as usize
70+
);
71+
}
6772
return Ok(handle);
6873
}
6974

7075
#[cfg(target_os = "linux")]
7176
{
7277
let handle = RawWindowHandle::Xlib(XlibWindowHandle::new(parent_handle as c_ulong));
73-
eprintln!("[wrywebview] raw_window_handle Xlib=0x{:x}", parent_handle);
78+
if log_enabled() {
79+
eprintln!("[wrywebview] raw_window_handle Xlib=0x{:x}", parent_handle);
80+
}
7481
return Ok(handle);
7582
}
7683

wrywebview/src/main/rust/lib.rs

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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

344353
fn 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

383392
fn 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

439448
fn 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

458467
fn 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

514523
fn 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

537546
fn 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

560569
fn 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

587596
fn 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

714723
fn 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

733742
fn 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

757766
fn 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

781790
fn 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

802811
fn 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

wrywebview/src/main/rust/platform/macos.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub use objc2::MainThreadMarker;
1111
pub use dispatch2::DispatchQueue;
1212

1313
use crate::error::WebViewError;
14+
use crate::log_enabled;
1415

1516
/// Runs a closure on the main thread using GCD.
1617
pub fn run_on_main_thread<F, R>(f: F) -> Result<R, WebViewError>
@@ -29,7 +30,9 @@ pub fn appkit_ns_view_from_handle(parent_handle: u64) -> Result<NonNull<c_void>,
2930
.ok_or(WebViewError::InvalidWindowHandle)?;
3031
let obj = unsafe { &*(ptr.as_ptr() as *mut AnyObject) };
3132
let class_name = obj.class().name().to_string_lossy();
32-
eprintln!("[wrywebview] appkit handle class={}", class_name);
33+
if log_enabled() {
34+
eprintln!("[wrywebview] appkit handle class={}", class_name);
35+
}
3336

3437
let nswindow_name = unsafe { CStr::from_bytes_with_nul_unchecked(b"NSWindow\0") };
3538
let nsview_name = unsafe { CStr::from_bytes_with_nul_unchecked(b"NSView\0") };
@@ -40,10 +43,12 @@ pub fn appkit_ns_view_from_handle(parent_handle: u64) -> Result<NonNull<c_void>,
4043
if msg_send![obj, isKindOfClass: nswindow_cls] {
4144
let view: *mut AnyObject = msg_send![obj, contentView];
4245
let view = NonNull::new(view).ok_or(WebViewError::InvalidWindowHandle)?;
43-
eprintln!(
44-
"[wrywebview] appkit handle is NSWindow, contentView=0x{:x}",
45-
view.as_ptr() as usize
46-
);
46+
if log_enabled() {
47+
eprintln!(
48+
"[wrywebview] appkit handle is NSWindow, contentView=0x{:x}",
49+
view.as_ptr() as usize
50+
);
51+
}
4752
return Ok(view.cast());
4853
}
4954
if msg_send![obj, isKindOfClass: nsview_cls] {

0 commit comments

Comments
 (0)