yubioath-flutter/lib/android/logger.dart

66 lines
1.9 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
* Copyright (C) 2022 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-04-13 16:26:32 +03:00
import 'package:flutter/services.dart';
import 'package:logging/logging.dart';
import '../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
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
});
}
}