自主任务接口接入和bug修复和工作记录页面
This commit is contained in:
53
lib/core/logging/work_trace_diagnostics.dart
Normal file
53
lib/core/logging/work_trace_diagnostics.dart
Normal file
@@ -0,0 +1,53 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// 只观察轨迹链路,不参与任务状态、订阅和控制指令。
|
||||
/// 高频日志采样;首条和不同原因各自打印,避免定位推送刷满控制台。
|
||||
class WorkTraceDiagnostics {
|
||||
WorkTraceDiagnostics({void Function(String)? write, DateTime Function()? now})
|
||||
: _write = write ?? debugPrint,
|
||||
_now = now ?? DateTime.now;
|
||||
final void Function(String) _write;
|
||||
final DateTime Function() _now;
|
||||
final _last = <String, DateTime>{};
|
||||
final _suppressed = <String, int>{};
|
||||
|
||||
void log(
|
||||
String stage,
|
||||
Map<String, Object?> fields, {
|
||||
String? sampleKey,
|
||||
Duration interval = const Duration(seconds: 1),
|
||||
}) {
|
||||
try {
|
||||
final now = _now();
|
||||
if (sampleKey != null) {
|
||||
final last = _last[sampleKey];
|
||||
if (last != null && now.difference(last) < interval) {
|
||||
_suppressed.update(sampleKey, (v) => v + 1, ifAbsent: () => 1);
|
||||
return;
|
||||
}
|
||||
_last[sampleKey] = now;
|
||||
}
|
||||
final skipped = sampleKey == null
|
||||
? 0
|
||||
: _suppressed.remove(sampleKey) ?? 0;
|
||||
final details = <String, Object?>{'time': now.toIso8601String()};
|
||||
for (final field in fields.entries) {
|
||||
var value = field.value;
|
||||
if (value is String && value.length > 1600) {
|
||||
value = '${value.substring(0, 1600)}…(共${value.length}字符)';
|
||||
}
|
||||
details[field.key] = value;
|
||||
}
|
||||
if (skipped > 0) details['sampledOut'] = skipped;
|
||||
_write('[作业轨迹][$stage] ${jsonEncode(details)}');
|
||||
} catch (_) {
|
||||
// 日志或序列化失败不能影响点处理、状态发布和画线。
|
||||
}
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_last.clear();
|
||||
_suppressed.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user