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-06 18:45:34 +03:00
|
|
|
|
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-06 18:45:34 +03:00
|
|
|
}
|
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', {
|
2022-05-09 16:31:13 +03:00
|
|
|
'level': level.name,
|
2022-05-09 09:32:44 +03:00
|
|
|
});
|
|
|
|
}
|
2022-05-06 18:45:34 +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) {
|
2022-06-10 13:23:52 +03:00
|
|
|
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,
|
2022-05-09 16:31:13 +03:00
|
|
|
'level': record.level.name,
|
2022-05-09 09:32:44 +03:00
|
|
|
'message': record.message,
|
2022-06-10 13:23:52 +03:00
|
|
|
'error': error
|
2022-04-13 16:26:32 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|