capdebug 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -168,7 +168,45 @@ CapDebug.unobserveEvent('online');
168
168
 
169
169
  ---
170
170
 
171
- ## Native Device Diagnostics
171
+ ## Native Telemetry & Diagnostics
172
+
173
+ ### Emitting Telemetry from Swift & Kotlin
174
+
175
+ `capdebug` includes static native helpers so native plugins, background workers, or services can emit debug events directly into the WebView's CapDebug event bus:
176
+
177
+ #### iOS (Swift)
178
+ ```swift
179
+ import CapDebug
180
+
181
+ CapDebugPlugin.emit(
182
+ type: "native.video.compress",
183
+ message: "Video compression completed",
184
+ data: ["outputBitrate": 1500000, "codec": "h264"],
185
+ level: "info",
186
+ duration: 342.5 // ms
187
+ )
188
+ ```
189
+
190
+ #### Android (Kotlin)
191
+ ```kotlin
192
+ import com.capdebug.plugin.CapDebugPlugin
193
+ import com.getcapacitor.JSObject
194
+
195
+ val data = JSObject().apply {
196
+ put("file", "recording.m4a")
197
+ put("format", "aac")
198
+ }
199
+
200
+ CapDebugPlugin.emit(
201
+ type: "native.audio.transcode",
202
+ message: "Audio transcode finished",
203
+ data: data,
204
+ level: "info",
205
+ duration: 180L // ms
206
+ )
207
+ ```
208
+
209
+ ### Querying Native Device Diagnostics in TypeScript
172
210
 
173
211
  When running in Capacitor iOS or Android (or web fallback), diagnostics are fetched on-demand when opening the **Device** tab or via programmatic calls:
174
212
 
@@ -20,6 +20,60 @@ import java.util.Locale
20
20
  @CapacitorPlugin(name = "CapDebug")
21
21
  class CapDebugPlugin : Plugin() {
22
22
 
23
+ override fun load() {
24
+ super.load()
25
+ activeBridge = bridge
26
+ }
27
+
28
+ companion object {
29
+ private var activeBridge: com.getcapacitor.Bridge? = null
30
+
31
+ /**
32
+ * Emits a telemetry or debug event directly from Android native code into CapDebug.
33
+ */
34
+ @JvmStatic
35
+ @JvmOverloads
36
+ fun emit(
37
+ type: String,
38
+ message: String? = null,
39
+ data: JSObject? = null,
40
+ level: String = "info",
41
+ duration: Long? = null,
42
+ source: String = "native",
43
+ bridge: com.getcapacitor.Bridge? = null
44
+ ) {
45
+ val targetBridge = bridge ?: activeBridge ?: return
46
+
47
+ // Zero-overhead guard: No-op in production release builds
48
+ val isDebuggable = try {
49
+ val context = targetBridge.context
50
+ (context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0
51
+ } catch (e: Exception) {
52
+ false
53
+ }
54
+
55
+ if (!isDebuggable) {
56
+ return
57
+ }
58
+
59
+ val payload = JSObject().apply {
60
+ put("source", source)
61
+ put("platform", "android")
62
+ put("type", type)
63
+ put("level", level)
64
+ message?.let { put("message", it) }
65
+ put("timestamp", System.currentTimeMillis())
66
+ data?.let { put("data", it) }
67
+ duration?.let { put("duration", it) }
68
+ }
69
+
70
+ val js = "window.dispatchEvent(new CustomEvent('capdebug', { detail: $payload }));"
71
+ targetBridge.webView?.post {
72
+ targetBridge.webView?.evaluateJavascript(js, null)
73
+ }
74
+ }
75
+ }
76
+
23
77
  @PluginMethod
24
78
  fun getDeviceInfo(call: PluginCall) {
25
79
  try {