Use "standard" log level names.

This commit is contained in:
Dain Nilsson 2022-05-03 11:24:25 +02:00
parent 51a76f686d
commit 488b1189ad
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
16 changed files with 97 additions and 70 deletions

View File

@ -66,7 +66,7 @@ class LoggingPanel extends ConsumerWidget {
DropdownButton<Level>(
value: ref.watch(logLevelProvider),
isDense: true,
items: [Level.WARNING, Level.INFO, Level.CONFIG, Level.FINE]
items: Levels.LEVELS
.map((e) => DropdownMenuItem(
value: e,
child: Text(e.name.toUpperCase()),
@ -74,7 +74,7 @@ class LoggingPanel extends ConsumerWidget {
.toList(),
onChanged: (level) {
ref.read(logLevelProvider.notifier).setLogLevel(level!);
_log.config('Log level set to $level');
_log.debug('Log level set to $level');
showMessage(context, 'Log level set to $level');
},
),

View File

@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/android/oath/command_providers.dart';
import 'package:yubico_authenticator/app/logging.dart';
import '../app/models.dart';
import '../core/models.dart';
@ -22,7 +23,7 @@ class _YubikeyProvider extends StateNotifier<YubiKeyData?> {
void setFromString(String input) {
try {
if (input.isEmpty) {
_log.config('Yubikey was detached.');
_log.debug('Yubikey was detached.');
state = null;
// reset other providers when YubiKey is removed
@ -46,7 +47,7 @@ class _YubikeyProvider extends StateNotifier<YubiKeyData?> {
deviceInfo);
state = YubiKeyData(deviceNode, name, deviceInfo);
} on Exception catch (e) {
_log.config('Invalid data for yubikey: $input. $e');
_log.debug('Invalid data for yubikey: $input. $e');
state = null;
}
}

View File

@ -7,6 +7,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/android/api/impl.dart';
import 'package:yubico_authenticator/app/logging.dart';
import 'package:yubico_authenticator/app/models.dart';
import 'package:yubico_authenticator/app/state.dart';
import 'package:yubico_authenticator/core/models.dart';
@ -45,7 +46,7 @@ class _AndroidOathStateNotifier extends OathStateNotifier {
.copyWith(locked: false, remembered: false, hasKey: false));
_ref.refresh(androidStateProvider);
} catch (e) {
_log.config('Calling reset failed with exception: $e');
_log.debug('Calling reset failed with exception: $e');
}
}
@ -59,7 +60,7 @@ class _AndroidOathStateNotifier extends OathStateNotifier {
final remembered = unlockResponse.isRemembered == true;
if (unlocked) {
_log.config('applet unlocked');
_log.debug('applet unlocked');
setData(state.value!.copyWith(
locked: false,
remembered: remembered,
@ -67,7 +68,7 @@ class _AndroidOathStateNotifier extends OathStateNotifier {
}
return Pair(unlocked, remembered);
} on PlatformException catch (e) {
_log.config('Calling unlock failed with exception: $e');
_log.debug('Calling unlock failed with exception: $e');
return Pair(false, false);
}
}
@ -79,7 +80,7 @@ class _AndroidOathStateNotifier extends OathStateNotifier {
setData(state.value!.copyWith(hasKey: true));
return true;
} on PlatformException catch (e) {
_log.config('Calling set password failed with exception: $e');
_log.debug('Calling set password failed with exception: $e');
return false;
}
}
@ -91,7 +92,7 @@ class _AndroidOathStateNotifier extends OathStateNotifier {
setData(state.value!.copyWith(hasKey: false, locked: false));
return true;
} on PlatformException catch (e) {
_log.config('Calling unset password failed with exception: $e');
_log.debug('Calling unset password failed with exception: $e');
return false;
}
}
@ -102,7 +103,7 @@ class _AndroidOathStateNotifier extends OathStateNotifier {
await _api.forgetPassword();
setData(state.value!.copyWith(remembered: false));
} on PlatformException catch (e) {
_log.config('Calling forgetPassword failed with exception: $e');
_log.debug('Calling forgetPassword failed with exception: $e');
}
}
}
@ -162,7 +163,7 @@ class _AndroidCredentialListNotifier extends OathCredentialListNotifier {
var resultJson = await _api.calculate(credential.id);
var result = jsonDecode(resultJson);
code = OathCode.fromJson(result);
_log.config('Calculate', jsonEncode(code));
_log.debug('Calculate', jsonEncode(code));
if (update && mounted) {
final creds = state!.toList();
final i = creds.indexWhere((e) => e.credential.id == credential.id);
@ -219,7 +220,7 @@ class _AndroidCredentialListNotifier extends OathCredentialListNotifier {
return renamedCredential;
} on PlatformException catch (e) {
_log.config('Failed to execute renameOathCredential: ${e.message}');
_log.debug('Failed to execute renameOathCredential: ${e.message}');
}
return credential;
@ -234,13 +235,13 @@ class _AndroidCredentialListNotifier extends OathCredentialListNotifier {
state = state!.toList()..removeWhere((e) => e.credential == credential);
}
} catch (e) {
_log.config('Call to delete credential failed: $e');
_log.debug('Call to delete credential failed: $e');
}
}
refresh() async {
if (_currentDevice == null) return;
_log.config('refreshing credentials...');
_log.debug('refreshing credentials...');
final pairs = [];
@ -269,7 +270,7 @@ class _AndroidCredentialListNotifier extends OathCredentialListNotifier {
_scheduleRefresh();
}
} catch (e) {
_log.config('Failure refreshing codes: $e');
_log.debug('Failure refreshing codes: $e');
}
}

View File

@ -2,15 +2,38 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
class Levels {
/// Key for tracing information ([value] = 500).
static const Level TRAFFIC = Level('TRAFFIC', 500);
/// Key for static configuration messages ([value] = 700).
static const Level DEBUG = Level('DEBUG', 700);
/// Key for informational messages ([value] = 800).
static const Level INFO = Level.INFO;
/// Key for potential problems ([value] = 900).
static const Level WARNING = Level.WARNING;
/// Key for serious failures ([value] = 1000).
static const Level ERROR = Level('ERROR', 1000);
static const List<Level> LEVELS = [
TRAFFIC,
DEBUG,
INFO,
WARNING,
ERROR,
];
}
extension LoggerExt on Logger {
void critical(Object? message, [Object? error, StackTrace? stackTrace]) =>
shout(message, error, stackTrace);
void error(Object? message, [Object? error, StackTrace? stackTrace]) =>
severe(message, error, stackTrace);
log(Levels.ERROR, message, error, stackTrace);
void debug(Object? message, [Object? error, StackTrace? stackTrace]) =>
config(message, error, stackTrace);
log(Levels.DEBUG, message, error, stackTrace);
void traffic(Object? message, [Object? error, StackTrace? stackTrace]) =>
fine(message, error, stackTrace);
log(Levels.TRAFFIC, message, error, stackTrace);
}
final logLevelProvider =

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:yubico_authenticator/app/logging.dart';
import '../core/state.dart';
import 'models.dart';
@ -28,7 +29,7 @@ class ThemeModeNotifier extends StateNotifier<ThemeMode> {
ThemeModeNotifier(this._prefs) : super(_fromName(_prefs.getString(_key)));
void setThemeMode(ThemeMode mode) {
_log.config('Set theme to $mode');
_log.debug('Set theme to $mode');
state = mode;
_prefs.setString(_key, mode.name);
}

View File

@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/app/logging.dart';
import '../app/models.dart';
import '../app/state.dart';
@ -36,7 +37,7 @@ class UsbDeviceNotifier extends StateNotifier<List<UsbYubiKeyNode>> {
UsbDeviceNotifier(this._rpc) : super([]);
void refresh() {
_log.config('Refreshing all USB devics');
_log.debug('Refreshing all USB devics');
_usbState = -1;
_pollDevices();
}
@ -106,7 +107,7 @@ class UsbDeviceNotifier extends StateNotifier<List<UsbYubiKeyNode>> {
}
}
} on RpcError catch (e) {
_log.severe('Error polling USB', jsonEncode(e));
_log.error('Error polling USB', jsonEncode(e));
}
if (mounted) {
@ -163,7 +164,7 @@ class NfcDeviceNotifier extends StateNotifier<List<NfcReaderNode>> {
.toList();
}
} on RpcError catch (e) {
_log.severe('Error polling NFC', jsonEncode(e));
_log.error('Error polling NFC', jsonEncode(e));
}
if (mounted) {
@ -249,7 +250,7 @@ class CurrentDeviceDataNotifier extends StateNotifier<YubiKeyData?> {
_pollTimer?.cancel();
final node = _deviceNode!;
try {
_log.config('Polling for USB device changes...');
_log.debug('Polling for USB device changes...');
var result = await _rpc.command('get', node.path.segments);
if (mounted) {
if (result['data']['present']) {
@ -260,7 +261,7 @@ class CurrentDeviceDataNotifier extends StateNotifier<YubiKeyData?> {
}
}
} on RpcError catch (e) {
_log.severe('Error polling NFC', jsonEncode(e));
_log.error('Error polling NFC', jsonEncode(e));
}
if (mounted) {
_pollTimer = Timer(

View File

@ -4,6 +4,7 @@ import 'dart:io';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/app/logging.dart';
import '../../app/models.dart';
import '../../fido/models.dart';
@ -66,7 +67,7 @@ class _DesktopFidoStateNotifier extends FidoStateNotifier {
Future<void> refresh() => updateState(() async {
final result = await _session.command('get');
_log.config('application status', jsonEncode(result));
_log.debug('application status', jsonEncode(result));
return FidoState.fromJson(result['data']);
});

View File

@ -6,6 +6,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart';
import 'package:yubico_authenticator/app/logging.dart';
import '../app/app.dart';
import '../app/views/main_page.dart';
@ -119,12 +120,12 @@ void _initLogging(List<String> argv) {
if (logLevelIndex != -1) {
try {
final levelName = argv[logLevelIndex + 1];
Level level = Level.LEVELS
Level level = Levels.LEVELS
.firstWhere((level) => level.name == levelName.toUpperCase());
Logger.root.level = level;
_log.info('Log level initialized from command line argument');
} catch (error) {
_log.severe('Failed to set log level', error);
_log.error('Failed to set log level', error);
}
}

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/app/logging.dart';
import 'package:yubico_authenticator/management/models.dart';
import '../../app/models.dart';
@ -56,7 +57,7 @@ class _DesktopManagementStateNotifier extends ManagementStateNotifier {
try {
await _session.command('get', target: path);
_subpath = path;
_log.config('Using transport $iface for management');
_log.debug('Using transport $iface for management');
return info;
} catch (e) {
_log.warning('Failed connecting to management via $iface');

View File

@ -4,6 +4,7 @@ import 'dart:math';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/app/logging.dart';
import 'package:yubico_authenticator/app/views/user_interaction.dart';
import '../../app/models.dart';
@ -72,7 +73,7 @@ class _DesktopOathStateNotifier extends OathStateNotifier {
refresh() => updateState(() async {
final result = await _session.command('get');
_log.config('application status', jsonEncode(result));
_log.debug('application status', jsonEncode(result));
var oathState = OathState.fromJson(result['data']);
final key = _ref.read(_oathLockKeyProvider(_session.devicePath));
if (oathState.locked && key != null) {
@ -107,7 +108,7 @@ class _DesktopOathStateNotifier extends OathStateNotifier {
final bool valid = validate['valid'];
final bool remembered = validate['remembered'];
if (valid) {
_log.config('applet unlocked');
_log.debug('applet unlocked');
_ref.read(_oathLockKeyProvider(_session.devicePath).notifier).setKey(key);
setData(state.value!.copyWith(
locked: false,
@ -148,7 +149,7 @@ class _DesktopOathStateNotifier extends OathStateNotifier {
await _session.command('set_key', params: {'key': key});
_ref.read(_oathLockKeyProvider(_session.devicePath).notifier).setKey(key);
}
_log.config('OATH key set');
_log.debug('OATH key set');
if (!oathState.hasKey) {
setData(oathState.copyWith(hasKey: true));
@ -231,7 +232,7 @@ class _DesktopCredentialListNotifier extends OathCredentialListNotifier {
@override
void dispose() {
_log.config('OATH notifier discarded');
_log.debug('OATH notifier discarded');
_timer?.cancel();
super.dispose();
}
@ -269,7 +270,7 @@ class _DesktopCredentialListNotifier extends OathCredentialListNotifier {
target: ['accounts', credential.id], signal: signaler);
code = OathCode.fromJson(result);
}
_log.config('Calculate', jsonEncode(code));
_log.debug('Calculate', jsonEncode(code));
if (update && mounted) {
final creds = state!.toList();
final i = creds.indexWhere((e) => e.credential.id == credential.id);
@ -333,9 +334,9 @@ class _DesktopCredentialListNotifier extends OathCredentialListNotifier {
refresh() async {
if (_locked) return;
_log.config('refreshing credentials...');
_log.debug('refreshing credentials...');
var result = await _session.command('calculate_all', target: ['accounts']);
_log.config('Entries', jsonEncode(result));
_log.debug('Entries', jsonEncode(result));
final pairs = [];
for (var e in result['entries']) {
@ -368,7 +369,7 @@ class _DesktopCredentialListNotifier extends OathCredentialListNotifier {
_timer?.cancel();
if (_locked) return;
if (state == null) {
_log.config('No OATH state, refresh immediately');
_log.debug('No OATH state, refresh immediately');
refresh();
} else if (mounted) {
final expirations = (state ?? [])
@ -379,16 +380,16 @@ class _DesktopCredentialListNotifier extends OathCredentialListNotifier {
.whereType<OathCode>()
.map((e) => e.validTo);
if (expirations.isEmpty) {
_log.config('No expirations, no refresh');
_log.debug('No expirations, no refresh');
_timer = null;
} else {
final earliest = expirations.reduce(min) * 1000;
final now = DateTime.now().millisecondsSinceEpoch;
if (earliest < now) {
_log.config('Already expired, refresh immediately');
_log.debug('Already expired, refresh immediately');
refresh();
} else {
_log.config('Schedule refresh in ${earliest - now}ms');
_log.debug('Schedule refresh in ${earliest - now}ms');
_timer = Timer(Duration(milliseconds: earliest - now), refresh);
}
}

View File

@ -64,7 +64,7 @@ class _RpcConnection {
try {
return RpcResponse.fromJson(jsonDecode(event));
} catch (e) {
_log.severe('Response was not valid JSON', event);
_log.error('Response was not valid JSON', event);
return RpcResponse.error('invalid-response', e.toString(), {});
}
}));
@ -105,7 +105,7 @@ class RpcSession {
Future<void> initialize() async {
final process = await Process.start(executable, []);
_log.config('RPC process started');
_log.debug('RPC process started');
process.stderr
.transform(const Utf8Decoder())
.transform(const LineSplitter())
@ -133,7 +133,7 @@ class RpcSession {
// Bind to random port
final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
final port = server.port;
_log.config('Listening for RPC connection on $port');
_log.debug('Listening for RPC connection on $port');
// Launch the elevated process
final process =
@ -153,12 +153,12 @@ class RpcSession {
_log.warning('Failed to elevate RPC process', error);
return false;
}
_log.config('Elevated RPC process started');
_log.debug('Elevated RPC process started');
// Accept only a single connection
final client = await server.first;
await server.close();
_log.config('Client connected: $client');
_log.debug('Client connected: $client');
// Stop the old subprocess.
try {
@ -174,7 +174,7 @@ class RpcSession {
// The nonce needs to be received first.
if (!authenticated) {
if (nonce == line) {
_log.config('Client authenticated with correct nonce');
_log.debug('Client authenticated with correct nonce');
authenticated = true;
completer.complete();
return '';
@ -213,22 +213,13 @@ class RpcSession {
return request.completer.future;
}
setLogLevel(Level level) {
String pyLevel;
if (level.value <= Level.FINE.value) {
pyLevel = 'traffic';
} else if (level.value <= Level.CONFIG.value) {
pyLevel = 'debug';
} else if (level.value <= Level.INFO.value) {
pyLevel = 'info';
} else if (level.value <= Level.WARNING.value) {
pyLevel = 'warning';
} else if (level.value <= Level.SEVERE.value) {
pyLevel = 'error';
} else {
pyLevel = 'critical';
}
command('logging', [], params: {'level': pyLevel});
void setLogLevel(Level level) async {
final name = Levels.LEVELS
.firstWhere((e) => level.value <= e.value, orElse: () => Level.OFF)
.name
.toLowerCase();
await command('logging', [], params: {'level': name});
}
void _send(Map data) {

View File

@ -5,6 +5,7 @@ import 'package:logging/logging.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart';
import 'package:yubico_authenticator/app/logging.dart';
import '../app/state.dart';
import '../core/state.dart';
@ -74,7 +75,7 @@ class _WindowStateNotifier extends StateNotifier<WindowState>
@override
set state(WindowState value) {
_log.config('Window state changed: $value');
_log.debug('Window state changed: $value');
super.state = value;
}
@ -104,7 +105,7 @@ class _WindowStateNotifier extends StateNotifier<WindowState>
_idleTimer?.cancel();
break;
default:
_log.fine('Window event ignored: $eventName');
_log.traffic('Window event ignored: $eventName');
}
}
}

View File

@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/app/logging.dart';
import '../../app/message.dart';
import '../../widgets/responsive_dialog.dart';
@ -83,12 +84,12 @@ class _AddFingerprintDialogState extends ConsumerState<AddFingerprintDialog>
// This needs a short delay to ensure the field is enabled first
Timer(const Duration(milliseconds: 100), _nameFocus.requestFocus);
}, error: (code) {
_log.config('Fingerprint capture error (code: $code)');
_log.debug('Fingerprint capture error (code: $code)');
_color = _animateColor(Colors.redAccent);
});
});
}, onError: (error, stacktrace) {
_log.severe('Error adding fingerprint', error, stacktrace);
_log.error('Error adding fingerprint', error, stacktrace);
Navigator.of(context).pop();
showMessage(context, 'Error adding fingerprint');
});

View File

@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/app/logging.dart';
import '../../app/message.dart';
import '../../core/models.dart';
@ -93,7 +94,7 @@ class _ResetDialogState extends ConsumerState<ResetDialog> {
Navigator.of(context).pop();
showMessage(context, 'FIDO application reset');
}, onError: (e) {
_log.severe('Error performing FIDO reset', e);
_log.error('Error performing FIDO reset', e);
Navigator.of(context).pop();
showMessage(context, 'Error performing reset');
});

View File

@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/app/logging.dart';
import '../../app/message.dart';
import '../../app/models.dart';
@ -383,7 +384,7 @@ class _OathAddAccountPageState extends ConsumerState<OathAddAccountPage> {
Navigator.of(context).pop();
showMessage(context, 'Account added');
} catch (e) {
_log.severe('Failed to add account', e);
_log.error('Failed to add account', e);
showMessage(context, 'Failed adding account');
}
} else {

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/app/logging.dart';
import 'app/state.dart';
import 'widgets/responsive_dialog.dart';
@ -30,7 +31,7 @@ class SettingsPage extends ConsumerWidget {
.toList(),
onChanged: (mode) {
ref.read(themeModeProvider.notifier).setThemeMode(mode!);
_log.config('Set theme mode to $mode');
_log.debug('Set theme mode to $mode');
},
),
],