From 49c062d5a53366797b7309496aa3ed73559b4603 Mon Sep 17 00:00:00 2001 From: qzl Date: Wed, 1 Apr 2026 14:34:36 +0800 Subject: [PATCH] feat(logging): add logging to home voice recorder --- .../features/home/data/voice_recorder.dart | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/apps/lib/features/home/data/voice_recorder.dart b/apps/lib/features/home/data/voice_recorder.dart index 337679e..0f32533 100644 --- a/apps/lib/features/home/data/voice_recorder.dart +++ b/apps/lib/features/home/data/voice_recorder.dart @@ -3,7 +3,8 @@ import 'dart:io'; import 'package:flutter/services.dart'; import 'package:record/record.dart'; -import '../../../core/l10n/l10n.dart'; +import '../../../../core/l10n/l10n.dart'; +import '../../../../core/logging/logger.dart'; abstract class VoiceRecorder { Future start(); @@ -13,6 +14,7 @@ abstract class VoiceRecorder { class RecordVoiceRecorder implements VoiceRecorder { final AudioRecorder _recorder; + final Logger _logger = getLogger('features.home.voice_recorder'); String? _currentPath; RecordVoiceRecorder({AudioRecorder? recorder}) @@ -23,10 +25,16 @@ class RecordVoiceRecorder implements VoiceRecorder { bool hasPermission; try { 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); } if (!hasPermission) { + _logger.warning(message: 'Voice recorder permission denied'); throw StateError(L10n.current.homeRecorderPermissionDenied); } @@ -43,7 +51,13 @@ class RecordVoiceRecorder implements VoiceRecorder { ), 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); } } @@ -53,7 +67,16 @@ class RecordVoiceRecorder implements VoiceRecorder { String? stoppedPath; try { 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); } return stoppedPath ?? _currentPath;