Gitea CI / Assemble and validate CI artifacts (push) Successful in 1m47s
Gitea CI / Check code and tests (push) Successful in 7m15s
Gitea CI / Build Android APK (push) Successful in 4m6s
Gitea CI / Build Windows x64 app (push) Successful in 3m34s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m29s
Gitea CI / Gitea CI passed (push) Successful in 1s
Gitea Release / Prepare draft Release (push) Failing after 8s
Gitea Release / Upload Android to draft Release (push) Skipped
Gitea Release / Upload Windows to draft Release (push) Skipped
Gitea Release / Verify and publish Release (push) Skipped
216 lines
6.3 KiB
Dart
216 lines
6.3 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:cutdex_native_flutter/cutdex_native_flutter.dart';
|
|
import 'package:cutdex_native/cutdex_native.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import 'diagnostic_store.dart';
|
|
import 'request_report.dart';
|
|
|
|
/// Application-owned storage and SDK adapter. Inactive unless explicitly installed.
|
|
final class AppDiagnostics {
|
|
AppDiagnostics(
|
|
this.store, {
|
|
String? sessionId,
|
|
Map<String, Object?> metadata = const {},
|
|
}) : sessionId = sessionId ?? const Uuid().v4(),
|
|
metadata = Map.of(metadata);
|
|
static AppDiagnostics? instance;
|
|
final DiagnosticStore store;
|
|
final String sessionId;
|
|
final Map<String, Object?> metadata;
|
|
|
|
static Future<AppDiagnostics> initialize() async {
|
|
Directory directory;
|
|
Object? initializationFailure;
|
|
StackTrace? initializationStack;
|
|
try {
|
|
directory = Directory(
|
|
p.join(
|
|
(await getApplicationSupportDirectory()).path,
|
|
'Cutdex',
|
|
'Diagnostics',
|
|
),
|
|
);
|
|
} catch (error, stack) {
|
|
initializationFailure = error;
|
|
initializationStack = stack;
|
|
// Use temporary storage when support-directory discovery is unavailable.
|
|
directory = Directory(
|
|
p.join(Directory.systemTemp.path, 'Cutdex', 'Diagnostics'),
|
|
);
|
|
}
|
|
final diagnostics = AppDiagnostics(
|
|
DiagnosticStore(directory),
|
|
metadata: {
|
|
'platform': Platform.operatingSystem,
|
|
'osVersion': Platform.operatingSystemVersion,
|
|
'dartVersion': Platform.version,
|
|
'buildMode': kReleaseMode
|
|
? 'release'
|
|
: kProfileMode
|
|
? 'profile'
|
|
: 'debug',
|
|
'appVersion': const String.fromEnvironment(
|
|
'CUTDEX_APP_VERSION',
|
|
defaultValue: 'unknown',
|
|
),
|
|
'appBuild': const String.fromEnvironment(
|
|
'CUTDEX_APP_BUILD',
|
|
defaultValue: 'unknown',
|
|
),
|
|
},
|
|
);
|
|
instance = diagnostics;
|
|
diagnostics.event(
|
|
const DiagnosticEvent(
|
|
component: 'app',
|
|
operation: 'start',
|
|
level: DiagnosticSeverity.info,
|
|
),
|
|
);
|
|
if (initializationFailure != null) {
|
|
diagnostics.event(
|
|
DiagnosticEvent(
|
|
component: 'diagnostics',
|
|
operation: 'initialize',
|
|
error: initializationFailure,
|
|
stackTrace: initializationStack,
|
|
),
|
|
);
|
|
}
|
|
return diagnostics;
|
|
}
|
|
|
|
static void report(DiagnosticEvent event) => instance?.event(event);
|
|
|
|
static void failure(
|
|
String component,
|
|
String operation,
|
|
Object error,
|
|
StackTrace stack, {
|
|
Map<String, Object?> context = const {},
|
|
}) => reportDiagnostic(
|
|
report,
|
|
DiagnosticEvent(
|
|
component: component,
|
|
operation: operation,
|
|
error: error,
|
|
stackTrace: stack,
|
|
context: context,
|
|
),
|
|
);
|
|
|
|
void event(DiagnosticEvent event, {bool immediate = false}) {
|
|
try {
|
|
final error = event.error;
|
|
store.record({
|
|
'version': 1,
|
|
'at': DateTime.now().toUtc().toIso8601String(),
|
|
'sessionId': sessionId,
|
|
'environment': metadata,
|
|
'level': event.level.name,
|
|
'component': event.component,
|
|
'operation': event.operation,
|
|
if (error != null)
|
|
'error': {
|
|
'type': error.runtimeType.toString(),
|
|
'message': switch (error) {
|
|
FormatException() => 'FormatException: ${error.message}',
|
|
ArgumentError() =>
|
|
'ArgumentError(${error.name}): ${error.message}',
|
|
ProcessException() =>
|
|
'ProcessException: ${error.message} (${error.errorCode})',
|
|
_ => error.toString(),
|
|
},
|
|
if (error is RuntimeException) ...{
|
|
'code': error.code,
|
|
'domain': error.domain,
|
|
'retryable': error.retryable,
|
|
'nativeDetails': error.details,
|
|
},
|
|
if (error is ProcessingException) ...{
|
|
'code': error.code,
|
|
'backend': error.backend,
|
|
},
|
|
},
|
|
if (event.stackTrace != null) 'stackTrace': event.stackTrace.toString(),
|
|
'context': event.context,
|
|
}, immediate: immediate);
|
|
} catch (_) {
|
|
// Diagnostics cannot change the application failure path.
|
|
}
|
|
}
|
|
|
|
Future<void> request(RequestReport report, String requestId) =>
|
|
store.saveRequest({
|
|
...report.copyWith(at: report.at ?? DateTime.now().toUtc()).toJson(),
|
|
'requestId': requestId,
|
|
'sessionId': sessionId,
|
|
}, requestId);
|
|
|
|
Future<File> export(Directory destination) =>
|
|
store.export(destination, {...metadata, 'sessionId': sessionId});
|
|
|
|
Future<void> readApplicationInfo() async {
|
|
try {
|
|
metadata.addAll(
|
|
await NativeDeviceInfo.readApplicationInfo().timeout(
|
|
const Duration(seconds: 2),
|
|
),
|
|
);
|
|
event(
|
|
const DiagnosticEvent(
|
|
component: 'app',
|
|
operation: 'applicationInfo',
|
|
level: DiagnosticSeverity.info,
|
|
),
|
|
);
|
|
} catch (error, stack) {
|
|
event(
|
|
DiagnosticEvent(
|
|
component: 'diagnostics',
|
|
operation: 'applicationInfo',
|
|
error: error,
|
|
stackTrace: stack,
|
|
level: DiagnosticSeverity.warning,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Preserves framework/engine behavior while observing both error routes.
|
|
void installErrorHandlers() {
|
|
final previousFlutter = FlutterError.onError;
|
|
FlutterError.onError = (details) {
|
|
event(
|
|
DiagnosticEvent(
|
|
component: 'flutter',
|
|
operation: 'frameworkError',
|
|
error: details.exception,
|
|
stackTrace: details.stack,
|
|
context: {'library': details.library},
|
|
),
|
|
);
|
|
if (previousFlutter != null) previousFlutter(details);
|
|
};
|
|
final previousPlatform = PlatformDispatcher.instance.onError;
|
|
PlatformDispatcher.instance.onError = (error, stack) {
|
|
event(
|
|
DiagnosticEvent(
|
|
component: 'dart',
|
|
operation: 'uncaughtAsyncError',
|
|
error: error,
|
|
stackTrace: stack,
|
|
),
|
|
immediate: true,
|
|
);
|
|
return previousPlatform?.call(error, stack) ?? false;
|
|
};
|
|
}
|
|
}
|