Skip to main content

Mountain/IPC/
SkyEmit.rs

1//! # SkyEmit
2//!
3//! Wrapper over `tauri::Emitter::emit(channel, payload)` that logs every
4//! Mountain → Wind/Sky emit under the `sky-emit` DevLog tag. Drop-in
5//! replacement for `handle.emit(channel, payload)` at any call site that
6//! benefits from per-channel traffic instrumentation.
7//!
8//! ## When to use
9//!
10//! Prefer `LogSkyEmit` over bare `emit` for any `sky://...` channel the
11//! user might want to audit. The existing ~50+ emit sites remain
12//! compatible - convert them incrementally as you touch nearby code.
13//!
14//! ## Output shape
15//!
16//! Each successful emit produces:
17//!
18//! ```
19//! [DEV:SKY-EMIT] [SkyEmit] ok channel=sky://tree-view/create bytes=64
20//! ```
21//!
22//! Failures produce:
23//!
24//! ```
25//! [DEV:SKY-EMIT] [SkyEmit] fail channel=sky://… bytes=64 error=<reason>
26//! ```
27//!
28//! ## Tag filtering
29//!
30//! `Trace=sky-emit tail -f Mountain.dev.log` shows the stream on
31//! its own so you can audit exactly which channels are being emitted,
32//! in what order, and with what payload size - without re-running or
33//! adding ad-hoc prints.
34
35
36use serde::Serialize;
37use tauri::Emitter;
38
39use crate::dev_log;
40
41/// Emit a tagged log line around any `ApplicationHandle::emit`. Returns
42/// the same `Result` as the underlying emit so callers using
43/// `let _ = …` / `?` / `if let Err(e) = …` keep their existing shape.
44pub fn LogSkyEmit<R:tauri::Runtime, P:Serialize + Clone>(
45	Handle:&impl Emitter<R>,
46
47	Channel:&str,
48
49	Payload:P,
50) -> tauri::Result<()> {
51	// Measure the serialized payload size for traffic-volume diagnostics.
52	// Silently falls back to 0 on (very unusual) serialize failures -
53	// never blocks the emit itself.
54	let Bytes = serde_json::to_vec(&Payload).map(|V| V.len()).unwrap_or(0);
55
56	match Handle.emit(Channel, Payload) {
57		Ok(()) => {
58			dev_log!("sky-emit", "[SkyEmit] ok channel={} bytes={}", Channel, Bytes);
59
60			Ok(())
61		},
62
63		Err(Error) => {
64			dev_log!("sky-emit", "[SkyEmit] fail channel={} bytes={} error={}", Channel, Bytes, Error);
65
66			Err(Error)
67		},
68	}
69}