Skip to content

Commit

Permalink
new log impl working on mac and web
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelselleck committed Feb 10, 2024
1 parent 37cabc9 commit f3017c9
Show file tree
Hide file tree
Showing 14 changed files with 16 additions and 91 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated from 37f467 to 8569b2
1 change: 1 addition & 0 deletions pax-chassis-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include = ["src/**/*","pax-swift-common/**/*"]
[lib]

[dependencies]
env_logger = "0.11.1"
piet = "0.6.0"
piet-coregraphics = "0.6.0"
pax-runtime = { path = "../pax-runtime", version="0.12.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct InterruptBuffer {

typedef struct PaxEngineContainer PaxEngineContainer;

struct PaxEngineContainer *pax_init(void (*logger)(const char*));
struct PaxEngineContainer *pax_init();

void pax_dealloc_engine(struct PaxEngineContainer * container);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct InterruptBuffer {

typedef struct PaxEngineContainer PaxEngineContainer;

struct PaxEngineContainer *pax_init(void (*logger)(const char*));
struct PaxEngineContainer *pax_init();

void pax_dealloc_engine(struct PaxEngineContainer * container);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct InterruptBuffer {

typedef struct PaxEngineContainer PaxEngineContainer;

struct PaxEngineContainer *pax_init(void (*logger)(const char*));
struct PaxEngineContainer *pax_init();

void pax_dealloc_engine(struct PaxEngineContainer * container);

Expand Down
5 changes: 2 additions & 3 deletions pax-chassis-common/src/core_graphics_c_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern crate core;
use std::ffi::c_void;

use std::mem::{transmute, ManuallyDrop};
use std::os::raw::c_char;

use core_graphics::context::CGContext;
use piet_coregraphics::CoreGraphicsContext;
Expand Down Expand Up @@ -34,7 +33,8 @@ pub struct PaxEngineContainer {

/// Allocate an instance of the Pax engine, with a specified root/main component from the loaded `pax_cartridge`.
#[no_mangle] //Exposed to Swift via PaxCartridge.h
pub extern "C" fn pax_init(logger: extern "C" fn(*const c_char)) -> *mut PaxEngineContainer {
pub extern "C" fn pax_init() -> *mut PaxEngineContainer {
env_logger::init();
//Initialize a ManuallyDrop-contained PaxEngine, so that a pointer to that
//engine can be passed back to Swift via the C (FFI) bridge
//This could presumably be cleaned up -- see `pax_dealloc_engine`
Expand All @@ -46,7 +46,6 @@ pub extern "C" fn pax_init(logger: extern "C" fn(*const c_char)) -> *mut PaxEngi
let engine: ManuallyDrop<Box<PaxEngine>> = ManuallyDrop::new(Box::new(PaxEngine::new(
main_component_instance,
expression_table,
pax_runtime::api::PlatformSpecificLogger::MacOS(logger),
(1.0, 1.0),
)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,7 @@ struct PaxViewMacos: View {
var cgContext = context.cgContext

if PaxEngineContainer.paxEngineContainer == nil {
let swiftLoggerCallback : @convention(c) (UnsafePointer<CChar>?) -> () = {
(msg) -> () in
let outputString = String(cString: msg!)
print(outputString)
}

//For manual debugger attachment:
// do {
// sleep(15)
// }

PaxEngineContainer.paxEngineContainer = pax_init(swiftLoggerCallback)
PaxEngineContainer.paxEngineContainer = pax_init()
} else {

let nativeMessageQueue = pax_tick(PaxEngineContainer.paxEngineContainer!, &cgContext, CFloat(dirtyRect.width), CFloat(dirtyRect.height))
Expand Down
2 changes: 2 additions & 0 deletions pax-chassis-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pax-cartridge = {path="../pax-cartridge", version="0.12.0"}
pax-message = {path = "../pax-message", version="0.12.0"}
wasm-bindgen = {version = "0.2.80", features=["serde-serialize"]}
serde_json = "1.0.95"
console_log = "1.0.0"
log = "0.4.20"
console_error_panic_hook = { version = "0.1.6", optional = true }
js-sys = "0.3.63"

Expand Down
32 changes: 3 additions & 29 deletions pax-chassis-web/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Basic example of rendering in the browser
use js_sys::Uint8Array;
use log::Level;
use pax_runtime::api::ArgsButtonClick;
use pax_runtime::api::ArgsCheckboxChange;
use pax_runtime::api::ArgsTextboxChange;
Expand Down Expand Up @@ -29,33 +30,6 @@ use serde_json;
#[cfg(feature = "designtime")]
use pax_designtime::DesigntimeManager;

// Console.log support, piped from `pax_engine::log`
#[wasm_bindgen]
extern "C" {
// Use `js_namespace` here to bind `console.log(..)` instead of just
// `log(..)`
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);

// The `console.log` is quite polymorphic, so we can bind it with multiple
// signatures. Note that we need to use `js_name` to ensure we always call
// `log` in JS.
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_u32(a: u32);

// Multiple arguments too!
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_many(a: &str, b: &str);
}

macro_rules! console_log {
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}

pub fn log_wrapper(msg: &str) {
console_log!("{}", msg);
}

#[wasm_bindgen]
pub fn wasm_memory() -> JsValue {
wasm_bindgen::memory()
Expand All @@ -75,6 +49,8 @@ impl PaxChassisWeb {
pub fn new() -> Self {
#[cfg(feature = "console_error_panic_hook")]
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log::init_with_level(Level::Debug)
.expect("console_log::init_with_level initialized correctly");
let window = window().unwrap();
let width = window.inner_width().unwrap().as_f64().unwrap();
let height = window.inner_height().unwrap().as_f64().unwrap();
Expand All @@ -92,7 +68,6 @@ impl PaxChassisWeb {
let engine = pax_core::PaxEngine::new_with_designtime(
main_component_instance,
expression_table,
pax_runtime::api::PlatformSpecificLogger::Web(log_wrapper),
(width, height),
definition_to_instance_traverser.get_designtime_manager(),
);
Expand All @@ -108,7 +83,6 @@ impl PaxChassisWeb {
let engine = pax_runtime::PaxEngine::new(
main_component_instance,
expression_table,
pax_runtime::api::PlatformSpecificLogger::Web(log_wrapper),
(width, height),
);

Expand Down
1 change: 1 addition & 0 deletions pax-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pax-macro = {path="../pax-macro", version="0.12.0"}
pax-message = {path="../pax-message", version="0.12.0"}
pax-runtime = {path="../pax-runtime", version="0.12.0"}
pax-compiler = {path="../pax-compiler", optional=true, version="0.12.0"}
log = "0.4.20"

[features]
parser = ["dep:pax-compiler"]
Expand Down
2 changes: 1 addition & 1 deletion pax-engine/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub extern crate pax_macro;
pub use pax_macro::*;

pub use log;
pub use pax_runtime::api;

pub use pax_runtime::api::log;
pub use pax_runtime::api::serde;
pub use pax_runtime::api::Property;

Expand Down
35 changes: 0 additions & 35 deletions pax-runtime/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use std::borrow::Borrow;
use std::collections::VecDeque;
use std::ffi::CString;
use std::ops::{Add, Deref, Mul, Neg};

#[cfg(feature = "designtime")]
use std::rc::Rc;

use kurbo::BezPath;
use lazy_static::lazy_static;
use mut_static::MutStatic;
use piet::PaintBrush;

pub use crate::numeric::Numeric;
Expand Down Expand Up @@ -665,36 +661,6 @@ impl Default for Size {
}
}

/// Coproduct for storing various kinds of function pointer,
/// needed to achieve compatibility with various native bridge mechanisms
pub enum PlatformSpecificLogger {
Web(fn(&str)),
MacOS(extern "C" fn(*const std::os::raw::c_char)),
}

pub struct Logger(PlatformSpecificLogger);

lazy_static! {
static ref LOGGER: MutStatic<Logger> = MutStatic::new();
}

pub fn register_logger(logger: PlatformSpecificLogger) {
LOGGER.borrow().set(Logger(logger)).unwrap();
}

/// Log to the appropriate native logging mechanism
/// Most often called as `pax_engine::log("some message")`
pub fn log(msg: &str) {
let logging_variant = &(LOGGER.borrow().read().expect("Logger isn't registered").0);
match logging_variant {
PlatformSpecificLogger::Web(closure) => closure(msg),
PlatformSpecificLogger::MacOS(closure) => {
let msg = CString::new(msg).unwrap();
(closure)(msg.as_ptr());
}
}
}

impl Mul for Size {
type Output = Size;

Expand Down Expand Up @@ -908,7 +874,6 @@ impl<T: Default + Clone> PropertyInstance<T> for PropertyLiteral<T> {
}

fn _get_transition_manager(&mut self) -> Option<&mut TransitionManager<T>> {
// log(&format!("property T not printable"));
if let None = self.transition_manager.value {
None
} else {
Expand Down
6 changes: 0 additions & 6 deletions pax-runtime/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,8 @@ impl PaxEngine {
pub fn new(
main_component_instance: Rc<ComponentInstance>,
expression_table: ExpressionTable,
logger: crate::api::PlatformSpecificLogger,
viewport_size: (f64, f64),
) -> Self {
crate::api::register_logger(logger);

let globals = Globals {
frames_elapsed: 0,
viewport: TransformAndBounds {
Expand All @@ -276,12 +273,9 @@ impl PaxEngine {
pub fn new_with_designtime(
main_component_instance: Rc<ComponentInstance>,
expression_table: ExpressionTable,
logger: crate::api::PlatformSpecificLogger,
viewport_size: (f64, f64),
designtime: Rc<RefCell<DesigntimeManager>>,
) -> Self {
crate::api::register_logger(logger);

let globals = Globals {
frames_elapsed: 0,
viewport: TransformAndBounds {
Expand Down
2 changes: 1 addition & 1 deletion www.pax.dev

0 comments on commit f3017c9

Please sign in to comment.