Improve internal default logging behavior with std

Previously, any use of our internal debug_log!() macro would require a
platform backend to be initialized. This was confusing when debugging
something in the (headless) wasm lsp implementation and nothing showed
up on the console.

Now if we can, we will always log. If a platform backend exists, we
route through it, otherwise we can do the fallback ourselves.
This commit is contained in:
Simon Hausmann 2022-09-30 09:49:31 +02:00 committed by Simon Hausmann
parent f66a2a5775
commit a875c532ba
4 changed files with 26 additions and 35 deletions

View file

@ -74,21 +74,7 @@ pub trait Platform {
/// should direct the output to some developer visible terminal. The default implementation
/// uses stderr if available, or `console.log` when targeting wasm.
fn debug_log(&self, _arguments: core::fmt::Arguments) {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
}
log(&_arguments.to_string());
} else if #[cfg(feature = "std")] {
eprintln!("{}", _arguments);
}
}
crate::tests::default_debug_log(_arguments);
}
}

View file

@ -88,7 +88,29 @@ pub extern "C" fn send_keyboard_string_sequence(
/// implementation details for debug_log()
#[doc(hidden)]
pub fn debug_log_impl(args: core::fmt::Arguments) {
crate::platform::PLATFORM_INSTANCE.with(|p| p.get().map(|p| p.debug_log(args)));
crate::platform::PLATFORM_INSTANCE.with(|p| match p.get() {
Some(platform) => platform.debug_log(args),
None => default_debug_log(args),
});
}
#[doc(hidden)]
pub fn default_debug_log(_arguments: core::fmt::Arguments) {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
}
log(&_arguments.to_string());
} else if #[cfg(feature = "std")] {
eprintln!("{}", _arguments);
}
}
}
#[macro_export]

View file

@ -49,7 +49,7 @@ backend-gl-all = ["backend-winit", "renderer-winit-femtovg"]
backend-gl-wayland = ["backend-winit-wayland", "renderer-winit-femtovg"]
backend-gl-x11 = ["backend-winit-x11", "renderer-winit-femtovg"]
preview = ["slint-interpreter", "i-slint-backend-selector"]
preview = ["slint-interpreter", "i-slint-core", "i-slint-backend-selector"]
default = ["backend-qt", "backend-winit", "renderer-winit-femtovg", "preview"]
@ -60,10 +60,10 @@ euclid = "0.22"
lsp-types = { version = "0.93.0", features = ["proposed"] }
serde = "1.0.118"
serde_json = "1.0.60"
i-slint-core = { version = "=0.3.1", path = "../../internal/core" }
# for the preview
i-slint-core = { version = "=0.3.1", path = "../../internal/core", optional = true }
slint-interpreter = { version = "=0.3.1", path = "../../internal/interpreter", default-features = false, features = ["compat-0-3-0"], optional = true }
i-slint-backend-selector = { version = "=0.3.1", path="../../internal/backends/selector", optional = true }

View file

@ -12,8 +12,6 @@ mod server_loop;
mod util;
use i_slint_compiler::CompilerConfiguration;
#[cfg(not(feature = "preview"))]
use i_slint_core::window::WindowAdapter;
use js_sys::Function;
use lsp_types::InitializeParams;
use serde::Serialize;
@ -145,11 +143,6 @@ pub fn create(
) -> Result<SlintServer, JsError> {
console_error_panic_hook::set_once();
// make sure the backend is initialized for logging
#[cfg(not(feature = "preview"))]
i_slint_core::platform::set_platform(Box::new(DummyBackend {}))
.expect("platform already initialized");
let init_param = init_param.into_serde()?;
let mut compiler_config =
@ -239,13 +232,3 @@ async fn load_file(path: String, load_file: &Function) -> std::io::Result<String
String::from_utf8(js_sys::Uint8Array::from(array).to_vec())
.map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e.to_string()))
}
#[cfg(not(feature = "preview"))]
struct DummyBackend {}
#[cfg(not(feature = "preview"))]
impl i_slint_core::platform::Platform for DummyBackend {
fn create_window_adapter(&self) -> Rc<dyn WindowAdapter> {
unimplemented!()
}
}