Support changing log level.

This commit is contained in:
Dain Nilsson 2021-11-24 11:36:59 +01:00
parent 064fb038e6
commit e439acdce6
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
5 changed files with 42 additions and 19 deletions

View File

@ -42,20 +42,13 @@ class _Request {
};
}
Level _forPythonName(String name) {
switch (name) {
case 'DEBUG':
return Level.CONFIG;
case 'INFO':
return Level.INFO;
case 'WARNING':
return Level.WARNING;
case 'ERROR':
return Level.SEVERE;
default:
return Level.INFO;
}
}
const _py2level = {
'DEBUG': Level.CONFIG,
'INFO': Level.INFO,
'WARNING': Level.WARNING,
'ERROR': Level.SEVERE,
'CRITICAL': Level.SHOUT,
};
class RpcSession {
final Process _process;
@ -73,7 +66,7 @@ class RpcSession {
.map((event) => jsonDecode(event))
.listen((event) {
Logger('rpc.${event['name']}').log(
_forPythonName(event['level']),
_py2level[event['level']] ?? Level.INFO,
event['message'],
//time: DateTime.fromMillisecondsSinceEpoch(event['time'] * 1000),
);

View File

@ -1,4 +1,5 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'models.dart';
@ -14,6 +15,21 @@ final rpcProvider = Provider<RpcSession>((ref) {
throw UnimplementedError();
});
final logLevelProvider = StateNotifierProvider<LogLevelNotifier, Level>(
(ref) => LogLevelNotifier(ref.watch(rpcProvider), Logger.root.level));
class LogLevelNotifier extends StateNotifier<Level> {
final RpcSession rpc;
LogLevelNotifier(this.rpc, Level state) : super(state);
setLevel(Level level) {
Logger.root.level = level;
rpc.setLogLevel(level);
state = level;
}
}
class RpcNodeSession {
final RpcSession _rpc;
final List<String> devicePath;

View File

@ -60,7 +60,7 @@ void _initLogging(Level level, RpcSession rpc) {
//TODO: Add support for logging to stderr and file
Logger.root.onRecord.listen((record) {
developer.log(
'${record.level} ${record.message}',
'${record.level}: ${record.message}',
error: record.error,
name: record.loggerName,
time: record.time,

View File

@ -63,7 +63,7 @@ packages:
name: build_resolvers
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
version: "2.0.5"
build_runner:
dependency: "direct dev"
description:
@ -421,6 +421,20 @@ packages:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.9"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.9"
shared_preferences_ios:
dependency: transitive
description:
name: shared_preferences_ios
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
shared_preferences_linux:
dependency: transitive
@ -587,7 +601,7 @@ packages:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
version: "2.3.1"
xdg_directories:
dependency: transitive
description:

@ -1 +1 @@
Subproject commit 318b0981d545950a45a8c9b9e17de9332e1f64b5
Subproject commit 87480b6cfffeda8ac7f25c3f60d3cc0bb11acd0a