Skip to content

Commit

Permalink
Merge pull request #105 from paxproject/ss/new-log-impl
Browse files Browse the repository at this point in the history
Introduce "log" crate
  • Loading branch information
samuelselleck authored Feb 17, 2024
2 parents 9f6326d + bba0821 commit 923d655
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 90 deletions.
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 pax_runtime::math::Point2;
Expand Down Expand Up @@ -35,7 +34,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 @@ -47,7 +47,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
38 changes: 9 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 @@ -34,33 +35,6 @@ use pax_designtime::DesigntimeManager;
#[cfg(feature = "designtime")]
const USERLAND_PROJECT_ID: &str = "userland_project";

// 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 @@ -84,6 +58,14 @@ impl PaxChassisWeb {
pub fn new() -> Self {
#[cfg(feature = "console_error_panic_hook")]
std::panic::set_hook(Box::new(console_error_panic_hook::hook));

#[cfg(debug_assertions)]
console_log::init_with_level(Level::Debug)
.expect("console_log::init_with_level initialized correctly");
#[cfg(not(debug_assertions))]
console_log::init_with_level(Level::Error)
.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 @@ -101,7 +83,6 @@ impl PaxChassisWeb {
let engine = pax_runtime::PaxEngine::new_with_designtime(
main_component_instance,
expression_table,
pax_runtime::api::PlatformSpecificLogger::Web(log_wrapper),
(width, height),
designtime_manager.clone(),
);
Expand All @@ -119,7 +100,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,12 +1,12 @@
pub extern crate pax_macro;
pub use pax_macro::*;

pub use log;
pub use pax_runtime::api;
pub use pax_runtime::engine::node_interface::*;
pub use pax_runtime::math;
pub use pax_runtime::rendering;

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;

use crate::math::{Point2, Space};
Expand Down Expand Up @@ -721,36 +717,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 @@ -964,7 +930,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
7 changes: 0 additions & 7 deletions pax-runtime/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,10 @@ impl PaxEngine {
pub fn new(
main_component_instance: Rc<ComponentInstance>,
expression_table: ExpressionTable,
logger: crate::api::PlatformSpecificLogger,
viewport_size: (f64, f64),
) -> Self {
use crate::math::Transform2;

crate::api::register_logger(logger);

let globals = Globals {
frames_elapsed: 0,
viewport: TransformAndBounds {
Expand All @@ -282,14 +279,10 @@ 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 {
use crate::math::Transform2;

crate::api::register_logger(logger);

let globals = Globals {
frames_elapsed: 0,
viewport: TransformAndBounds {
Expand Down

0 comments on commit 923d655

Please sign in to comment.