Files
flutterApp/lib/core/logging/control_permission_diagnostics.dart

48 lines
1.5 KiB
Dart
Raw Normal View History

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<String, Object?>? get context =>
Zone.current[_scopeKey] as Map<String, Object?>?;
static T scope<T>(Map<String, Object?> fields, T Function() action) =>
runZoned(
action,
zoneValues: {_scopeKey: Map<String, Object?>.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<String, Object?> fields = const {}]) {
try {
final trace = RemoteControlDiagnostics.instance;
final record = <String, Object?>{
'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.
}
}
}