Skip to content

Commit

Permalink
update: log api plugin
Browse files Browse the repository at this point in the history
- Rename `LogApiPlugin` to `AllLogPlugins`
- Provides a way to use `bevy_log`
from webview
  • Loading branch information
not-elm committed Feb 15, 2025
1 parent c85cae1 commit 6a949af
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
3 changes: 3 additions & 0 deletions crates/bevy_flurx_api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
### Breaking Changes

- Rename `ClipboardPlugins` to `AllClipboardPlugins`
- Rename `LogApiPlugin` to `AllLogPlugins`

### New Features

- Add `AllAppPlugins`
- Provides a way to use `bevy_log`
from webview

## v0.2.0

Expand Down
47 changes: 41 additions & 6 deletions crates/bevy_flurx_api/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Provides mechanism to output the logs.
use bevy::prelude::{App, EventReader, Plugin, PostUpdate};
use bevy::app::Update;
use bevy::log;
use bevy::prelude::{App, EventReader, Plugin};
use bevy_flurx_ipc::ipc_events::IpcEventExt;
use bevy_flurx_ipc::prelude::IpcEvent;
use serde::Deserialize;
Expand All @@ -12,13 +14,17 @@ use serde::Deserialize;
/// ```ts
/// window.__FLURX__.log.println("message")
/// ```
pub struct LogPrintlnApiPlugin;
pub struct AllLogPlugins;

impl Plugin for LogPrintlnApiPlugin {
impl Plugin for AllLogPlugins {
fn build(&self, app: &mut App) {
app
.add_ipc_event::<RequestPrintln>("FLURX|log::println")
.add_systems(PostUpdate, println_event);
.add_ipc_event::<RequestLog>("FLURX|log::log")
.add_systems(Update, (
println_event,
log_event,
));
}
}

Expand All @@ -27,8 +33,37 @@ struct RequestPrintln {
message: String,
}

#[derive(Deserialize)]
struct RequestLog {
message: String,
level: RequestLogLevel,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
enum RequestLogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}

fn println_event(mut er: EventReader<IpcEvent<RequestPrintln>>) {
for e in er.read() {
println!("{}", e.payload.message);
for event in er.read() {
println!("{}", event.payload.message);
}
}

fn log_event(mut er: EventReader<IpcEvent<RequestLog>>) {
for event in er.read() {
let message = &event.payload.message;
match event.payload.level {
RequestLogLevel::Trace => log::trace!(message),
RequestLogLevel::Debug => log::debug!(message),
RequestLogLevel::Info => log::info!(message),
RequestLogLevel::Warn => log::warn!(message),
RequestLogLevel::Error => log::error!(message),
}
}
}
2 changes: 1 addition & 1 deletion crates/bevy_webview_wry/scripts/bevy_flurx_api.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 50 additions & 3 deletions tool/api/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,60 @@ import {emit} from "./core";

export namespace log {
/**
* Requests to execute `println!` to aa process.
* Requests to execute `println!` on main process.
*
* @example
* import {log} from "bevy_flurx_api";
* log.println("message");
*/
export const println = (message: string) => emit("FLURX|log::println", {
message
export const println = (message: any) => emit("FLURX|log::println", {
message: convertToString(message)
});

/**
* Requests to execute `log!` on main process.
*
* @example
* import {log} from "bevy_flurx_api";
* log.trace("message");
*/
export const trace = (message: any) => emitLog(message, "trace");

/**
* Requests to execute `log!` on main process.
*
* @example
* import {log} from "bevy_flurx_api";
* log.info("message");
*/
export const info = (message: any) => emitLog(message, "info");

/**
* Requests to execute `log!` on main process.
*
* @example
* import {log} from "bevy_flurx_api";
* log.warn("message");
*/
export const warn = (message: any) => emitLog(message, "warn");

/**
* Requests to execute `log!` on main process.
*
* @example
* import {log} from "bevy_flurx_api";
* log.error("message");
*/
export const error = (message: any) => emitLog(message, "error");

type LogLevel = "trace" | "debug" | "info" | "warn" | "error";

const emitLog = (message: any, level: LogLevel) => {
emit("FLURX|log::log", {
message: convertToString(message),
level
})
};

const convertToString = (message: any) => typeof message === "object" ? JSON.stringify(message, null, 2) : message.toString()
}

0 comments on commit 6a949af

Please sign in to comment.