import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'remote_control_diagnostics.dart'; /// Passive tracing only. Identifiers stay local and never enter HTTP/TCP data. class ControlPermissionDiagnostics { static final Object _scopeKey = Object(); static Map? get context => Zone.current[_scopeKey] as Map?; static T scope(Map fields, T Function() action) => runZoned( action, zoneValues: {_scopeKey: Map.of(fields)}, ); static Object? scalar(Object? value) => value == null || value is String || value is num || value is bool ? value : '<${value.runtimeType}>'; /// Callers supply whitelisted fields, never tokens, headers, or raw packets. static void event(String step, [Map fields = const {}]) { try { final trace = RemoteControlDiagnostics.instance; final record = { 'time': DateTime.now().toIso8601String(), ...?context, ...fields, 'traceVersion': 1, 'permissionStep': step, }; final startedUs = record['startedUs']; if (startedUs is int) { record['elapsedMs'] = (trace.elapsedUs - startedUs) ~/ 1000; } trace.event('permission_trace', record); debugPrint('[权限链][$step] ${jsonEncode(record)}'); } catch (_) { // Logging cannot interrupt permission, motion, or safety handling. } } }