import 'package:bloc/bloc.dart'; import 'package:loggy/loggy.dart'; import 'loggy.dart'; const Map _blocChangeLogLevels = { 'ConnectionStateCubit': LogLevel.off, 'ActiveSingleContactChatBlocMapCubit': LogLevel.off, 'ActiveConversationsBlocMapCubit': LogLevel.off, 'PersistentQueueCubit': LogLevel.off, 'TableDBArrayProtobufCubit': LogLevel.off, 'DHTLogCubit': LogLevel.off, 'SingleContactMessagesCubit': LogLevel.off, 'ChatComponentCubit': LogLevel.off, }; const Map _blocCreateCloseLogLevels = {}; const Map _blocErrorLogLevels = {}; /// [BlocObserver] for the VeilidChat application that /// observes all state changes. class StateLogger extends BlocObserver { /// {@macro counter_observer} const StateLogger(); void _checkLogLevel( Map blocLogLevels, LogLevel defaultLogLevel, BlocBase bloc, void Function(LogLevel) closure) { final logLevel = blocLogLevels[bloc.runtimeType.toString()] ?? defaultLogLevel; if (logLevel != LogLevel.off) { closure(logLevel); } } @override void onChange(BlocBase bloc, Change change) { super.onChange(bloc, change); _checkLogLevel(_blocChangeLogLevels, LogLevel.debug, bloc, (logLevel) { log.log(logLevel, 'Change: ${bloc.runtimeType} $change'); }); } @override void onCreate(BlocBase bloc) { super.onCreate(bloc); _checkLogLevel(_blocCreateCloseLogLevels, LogLevel.debug, bloc, (logLevel) { log.log(logLevel, 'Create: ${bloc.runtimeType}'); }); } @override void onClose(BlocBase bloc) { super.onClose(bloc); _checkLogLevel(_blocCreateCloseLogLevels, LogLevel.debug, bloc, (logLevel) { log.log(logLevel, 'Close: ${bloc.runtimeType}'); }); } @override void onError(BlocBase bloc, Object error, StackTrace stackTrace) { super.onError(bloc, error, stackTrace); _checkLogLevel(_blocErrorLogLevels, LogLevel.error, bloc, (logLevel) { log.log(logLevel, 'Error: ${bloc.runtimeType} $error\n$stackTrace'); }); } }