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#![allow(non_snake_case)]
36
37use serde::Serialize;
38use tauri::Emitter;
39
40use crate::dev_log;
41
42/// Emit a tagged log line around any `ApplicationHandle::emit`. Returns
43/// the same `Result` as the underlying emit so callers using
44/// `let _ = …` / `?` / `if let Err(e) = …` keep their existing shape.
45pub fn LogSkyEmit<R:tauri::Runtime, P:Serialize + Clone>(
46	Handle:&impl Emitter<R>,
47	Channel:&str,
48	Payload:P,
49) -> tauri::Result<()> {
50	// Measure the serialized payload size for traffic-volume diagnostics.
51	// Silently falls back to 0 on (very unusual) serialize failures -
52	// never blocks the emit itself.
53	let Bytes = serde_json::to_vec(&Payload).map(|V| V.len()).unwrap_or(0);
54	match Handle.emit(Channel, Payload) {
55		Ok(()) => {
56			dev_log!("sky-emit", "[SkyEmit] ok channel={} bytes={}", Channel, Bytes);
57			Ok(())
58		},
59		Err(Error) => {
60			dev_log!("sky-emit", "[SkyEmit] fail channel={} bytes={} error={}", Channel, Bytes, Error);
61			Err(Error)
62		},
63	}
64}