Add the Flutter video editing application with project management, media import, timeline editing, preview, AI assistance, diagnostics and settings. Include shared UI and native host packages, platform runners, application tests and release tooling. Provide usage and contribution guides, the MIT license and third-party notices. Exclude local credentials, build outputs and validation data.
216 lines
6.2 KiB
Dart
216 lines
6.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:cutdex_sdk/runtime.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;
|
|
};
|
|
}
|
|
}
|