feat(logging): add logging to home voice recorder

This commit is contained in:
qzl
2026-04-01 14:34:36 +08:00
parent b8e5a42a12
commit 49c062d5a5
@@ -3,7 +3,8 @@ import 'dart:io';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:record/record.dart'; import 'package:record/record.dart';
import '../../../core/l10n/l10n.dart'; import '../../../../core/l10n/l10n.dart';
import '../../../../core/logging/logger.dart';
abstract class VoiceRecorder { abstract class VoiceRecorder {
Future<void> start(); Future<void> start();
@@ -13,6 +14,7 @@ abstract class VoiceRecorder {
class RecordVoiceRecorder implements VoiceRecorder { class RecordVoiceRecorder implements VoiceRecorder {
final AudioRecorder _recorder; final AudioRecorder _recorder;
final Logger _logger = getLogger('features.home.voice_recorder');
String? _currentPath; String? _currentPath;
RecordVoiceRecorder({AudioRecorder? recorder}) RecordVoiceRecorder({AudioRecorder? recorder})
@@ -23,10 +25,16 @@ class RecordVoiceRecorder implements VoiceRecorder {
bool hasPermission; bool hasPermission;
try { try {
hasPermission = await _recorder.hasPermission(); hasPermission = await _recorder.hasPermission();
} on MissingPluginException catch (_) { } on MissingPluginException catch (e, stackTrace) {
_logger.error(
message: 'Voice recorder plugin unavailable',
error: e,
stackTrace: stackTrace,
);
throw StateError(L10n.current.homeRecorderPluginUnavailable); throw StateError(L10n.current.homeRecorderPluginUnavailable);
} }
if (!hasPermission) { if (!hasPermission) {
_logger.warning(message: 'Voice recorder permission denied');
throw StateError(L10n.current.homeRecorderPermissionDenied); throw StateError(L10n.current.homeRecorderPermissionDenied);
} }
@@ -43,7 +51,13 @@ class RecordVoiceRecorder implements VoiceRecorder {
), ),
path: path, path: path,
); );
} on MissingPluginException catch (_) { _logger.info(message: 'Voice recording started', extra: {'path': path});
} on MissingPluginException catch (e, stackTrace) {
_logger.error(
message: 'Failed to start voice recording',
error: e,
stackTrace: stackTrace,
);
throw StateError(L10n.current.homeRecorderPluginUnavailable); throw StateError(L10n.current.homeRecorderPluginUnavailable);
} }
} }
@@ -53,7 +67,16 @@ class RecordVoiceRecorder implements VoiceRecorder {
String? stoppedPath; String? stoppedPath;
try { try {
stoppedPath = await _recorder.stop(); stoppedPath = await _recorder.stop();
} on MissingPluginException catch (_) { _logger.info(
message: 'Voice recording stopped',
extra: {'path': stoppedPath ?? _currentPath},
);
} on MissingPluginException catch (e, stackTrace) {
_logger.error(
message: 'Failed to stop voice recording',
error: e,
stackTrace: stackTrace,
);
throw StateError(L10n.current.homeRecorderPluginUnavailable); throw StateError(L10n.current.homeRecorderPluginUnavailable);
} }
return stoppedPath ?? _currentPath; return stoppedPath ?? _currentPath;