yubioath-flutter/lib/android/logger.dart

54 lines
1.5 KiB
Dart
Raw Normal View History

2022-04-13 16:26:32 +03:00
import 'package:flutter/services.dart';
2022-05-11 16:47:35 +03:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2022-04-13 16:26:32 +03:00
import 'package:logging/logging.dart';
2022-05-11 16:47:35 +03:00
import 'package:yubico_authenticator/app/logging.dart';
2022-04-13 16:26:32 +03:00
2022-05-09 09:32:44 +03:00
final _log = Logger('android.logger');
2022-05-11 16:47:35 +03:00
final androidLogProvider =
StateNotifierProvider<LogLevelNotifier, Level>((ref) => AndroidLogger());
class AndroidLogger extends LogLevelNotifier {
2022-04-13 16:26:32 +03:00
final MethodChannel _channel = const MethodChannel('android.log.redirect');
2022-05-11 16:47:35 +03:00
AndroidLogger() : super() {
2022-05-09 09:32:44 +03:00
Logger.root.onRecord.listen((record) {
if (record.level >= Logger.root.level) {
log(record);
}
2022-05-09 09:32:44 +03:00
});
_log.info('Logging initialized, outputting to Android/logcat');
}
2022-05-11 16:47:35 +03:00
@override
2022-05-09 16:36:51 +03:00
void setLogLevel(Level level) {
2022-05-11 16:47:35 +03:00
super.setLogLevel(level);
2022-05-09 16:36:51 +03:00
_channel.invokeMethod('setLevel', {
'level': level.name,
2022-05-09 09:32:44 +03:00
});
}
2022-05-11 16:47:35 +03:00
@override
Future<List<String>> getLogs() async {
_log.debug('Getting logs...');
var buffer = await _channel.invokeMethod('getLogs', {});
return List.unmodifiable(buffer);
}
2022-05-09 16:36:51 +03:00
void log(LogRecord record) {
final error = record.error == null
? null
: record.error is Exception
? record.error.toString()
: record.error is String
? record.error
: 'Invalid error type: ${record.error.runtimeType.toString()}';
2022-05-09 16:36:51 +03:00
_channel.invokeMethod('log', {
2022-05-09 09:32:44 +03:00
'loggerName': record.loggerName,
'level': record.level.name,
2022-05-09 09:32:44 +03:00
'message': record.message,
'error': error
2022-04-13 16:26:32 +03:00
});
}
}