2023-12-26 20:26:54 -05:00
|
|
|
import 'package:bloc/bloc.dart';
|
2024-02-12 10:35:41 -05:00
|
|
|
import 'package:loggy/loggy.dart';
|
2023-09-26 22:32:18 -04:00
|
|
|
import 'loggy.dart';
|
|
|
|
|
2024-02-12 10:35:41 -05:00
|
|
|
const Map<String, LogLevel> _blocChangeLogLevels = {
|
2024-03-07 08:28:05 -05:00
|
|
|
'ConnectionStateCubit': LogLevel.off,
|
2024-04-20 13:43:17 -04:00
|
|
|
'ActiveSingleContactChatBlocMapCubit': LogLevel.off,
|
|
|
|
'ActiveConversationsBlocMapCubit': LogLevel.off,
|
|
|
|
'PersistentQueueCubit<Message>': LogLevel.off,
|
2024-06-03 21:20:00 -04:00
|
|
|
'TableDBArrayProtobufCubit<ReconciledMessage>': LogLevel.off,
|
|
|
|
'DHTLogCubit<Message>': LogLevel.off,
|
2024-04-24 22:43:42 -04:00
|
|
|
'SingleContactMessagesCubit': LogLevel.off,
|
2024-06-06 00:19:07 -04:00
|
|
|
'ChatComponentCubit': LogLevel.off,
|
2024-02-12 10:35:41 -05:00
|
|
|
};
|
2024-06-06 00:19:07 -04:00
|
|
|
|
2024-02-12 10:35:41 -05:00
|
|
|
const Map<String, LogLevel> _blocCreateCloseLogLevels = {};
|
|
|
|
const Map<String, LogLevel> _blocErrorLogLevels = {};
|
|
|
|
|
2023-12-26 20:26:54 -05:00
|
|
|
/// [BlocObserver] for the VeilidChat application that
|
|
|
|
/// observes all state changes.
|
|
|
|
class StateLogger extends BlocObserver {
|
|
|
|
/// {@macro counter_observer}
|
2023-09-26 22:32:18 -04:00
|
|
|
const StateLogger();
|
2023-12-26 20:26:54 -05:00
|
|
|
|
2024-02-12 10:35:41 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 22:32:18 -04:00
|
|
|
@override
|
2023-12-26 20:26:54 -05:00
|
|
|
void onChange(BlocBase<dynamic> bloc, Change<dynamic> change) {
|
|
|
|
super.onChange(bloc, change);
|
2024-02-12 10:35:41 -05:00
|
|
|
_checkLogLevel(_blocChangeLogLevels, LogLevel.debug, bloc, (logLevel) {
|
|
|
|
log.log(logLevel, 'Change: ${bloc.runtimeType} $change');
|
|
|
|
});
|
2023-12-26 20:26:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onCreate(BlocBase<dynamic> bloc) {
|
|
|
|
super.onCreate(bloc);
|
2024-02-12 10:35:41 -05:00
|
|
|
_checkLogLevel(_blocCreateCloseLogLevels, LogLevel.debug, bloc, (logLevel) {
|
|
|
|
log.log(logLevel, 'Create: ${bloc.runtimeType}');
|
|
|
|
});
|
2023-12-26 20:26:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onClose(BlocBase<dynamic> bloc) {
|
|
|
|
super.onClose(bloc);
|
2024-02-12 10:35:41 -05:00
|
|
|
_checkLogLevel(_blocCreateCloseLogLevels, LogLevel.debug, bloc, (logLevel) {
|
|
|
|
log.log(logLevel, 'Close: ${bloc.runtimeType}');
|
|
|
|
});
|
2023-12-26 20:26:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onError(BlocBase<dynamic> bloc, Object error, StackTrace stackTrace) {
|
|
|
|
super.onError(bloc, error, stackTrace);
|
2024-02-12 10:35:41 -05:00
|
|
|
_checkLogLevel(_blocErrorLogLevels, LogLevel.error, bloc, (logLevel) {
|
|
|
|
log.log(logLevel, 'Error: ${bloc.runtimeType} $error\n$stackTrace');
|
|
|
|
});
|
2023-09-26 22:32:18 -04:00
|
|
|
}
|
|
|
|
}
|