veilidchat/lib/tools/state_logger.dart
Christien Rioux 1f99279cd2 ui cleanup
2024-04-07 23:16:06 -04:00

63 lines
1.9 KiB
Dart

import 'package:bloc/bloc.dart';
import 'package:loggy/loggy.dart';
import 'loggy.dart';
const Map<String, LogLevel> _blocChangeLogLevels = {
'ConnectionStateCubit': LogLevel.off,
'ActiveConversationMessagesBlocMapCubit': LogLevel.off,
'DHTShortArrayCubit<Message>': LogLevel.off,
};
const Map<String, LogLevel> _blocCreateCloseLogLevels = {};
const Map<String, LogLevel> _blocErrorLogLevels = {};
/// [BlocObserver] for the VeilidChat application that
/// observes all state changes.
class StateLogger extends BlocObserver {
/// {@macro counter_observer}
const StateLogger();
void _checkLogLevel(
Map<String, LogLevel> blocLogLevels,
LogLevel defaultLogLevel,
BlocBase<dynamic> bloc,
void Function(LogLevel) closure) {
final logLevel =
blocLogLevels[bloc.runtimeType.toString()] ?? defaultLogLevel;
if (logLevel != LogLevel.off) {
closure(logLevel);
}
}
@override
void onChange(BlocBase<dynamic> bloc, Change<dynamic> change) {
super.onChange(bloc, change);
_checkLogLevel(_blocChangeLogLevels, LogLevel.debug, bloc, (logLevel) {
log.log(logLevel, 'Change: ${bloc.runtimeType} $change');
});
}
@override
void onCreate(BlocBase<dynamic> bloc) {
super.onCreate(bloc);
_checkLogLevel(_blocCreateCloseLogLevels, LogLevel.debug, bloc, (logLevel) {
log.log(logLevel, 'Create: ${bloc.runtimeType}');
});
}
@override
void onClose(BlocBase<dynamic> bloc) {
super.onClose(bloc);
_checkLogLevel(_blocCreateCloseLogLevels, LogLevel.debug, bloc, (logLevel) {
log.log(logLevel, 'Close: ${bloc.runtimeType}');
});
}
@override
void onError(BlocBase<dynamic> 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');
});
}
}