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-05-03 12:31:43 +03:00
|
|
|
// ignore_for_file: constant_identifier_names
|
|
|
|
|
2022-04-29 14:13:42 +03:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-03-25 14:23:14 +03:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
2022-09-08 13:17:44 +03:00
|
|
|
import '../android/state.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
import '../core/state.dart';
|
2022-09-08 13:17:44 +03:00
|
|
|
|
2022-05-30 17:04:50 +03:00
|
|
|
String _pad(int value, int zeroes) => value.toString().padLeft(zeroes, '0');
|
|
|
|
|
|
|
|
extension DateTimeFormat on DateTime {
|
|
|
|
String get logFormat =>
|
|
|
|
'${_pad(hour, 2)}:${_pad(minute, 2)}:${_pad(second, 2)}.${_pad(millisecond, 3)}';
|
|
|
|
}
|
|
|
|
|
2022-05-03 12:24:25 +03:00
|
|
|
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,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:23:14 +03:00
|
|
|
extension LoggerExt on Logger {
|
|
|
|
void error(Object? message, [Object? error, StackTrace? stackTrace]) =>
|
2022-05-03 12:24:25 +03:00
|
|
|
log(Levels.ERROR, message, error, stackTrace);
|
2022-03-25 14:23:14 +03:00
|
|
|
void debug(Object? message, [Object? error, StackTrace? stackTrace]) =>
|
2022-05-03 12:24:25 +03:00
|
|
|
log(Levels.DEBUG, message, error, stackTrace);
|
2022-03-25 14:23:14 +03:00
|
|
|
void traffic(Object? message, [Object? error, StackTrace? stackTrace]) =>
|
2022-05-03 12:24:25 +03:00
|
|
|
log(Levels.TRAFFIC, message, error, stackTrace);
|
2022-03-25 14:23:14 +03:00
|
|
|
}
|
|
|
|
|
2022-04-29 14:13:42 +03:00
|
|
|
final logLevelProvider =
|
|
|
|
StateNotifierProvider<LogLevelNotifier, Level>((ref) => LogLevelNotifier());
|
2022-03-25 14:23:14 +03:00
|
|
|
|
2022-04-29 14:13:42 +03:00
|
|
|
class LogLevelNotifier extends StateNotifier<Level> {
|
|
|
|
final List<String> _buffer = [];
|
|
|
|
LogLevelNotifier() : super(Logger.root.level) {
|
|
|
|
Logger.root.onRecord.listen((record) {
|
2022-05-30 17:04:50 +03:00
|
|
|
_buffer.add(
|
|
|
|
'${record.time.logFormat} [${record.loggerName}] ${record.level}: ${record.message}');
|
2022-04-29 14:13:42 +03:00
|
|
|
if (record.error != null) {
|
|
|
|
_buffer.add('${record.error}');
|
|
|
|
}
|
|
|
|
while (_buffer.length > 1000) {
|
|
|
|
_buffer.removeAt(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-03-25 14:23:14 +03:00
|
|
|
|
2022-04-29 14:13:42 +03:00
|
|
|
void setLogLevel(Level level) {
|
|
|
|
state = level;
|
|
|
|
Logger.root.level = level;
|
2022-03-25 14:23:14 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 16:47:35 +03:00
|
|
|
Future<List<String>> getLogs() async {
|
2022-03-25 14:23:14 +03:00
|
|
|
return List.unmodifiable(_buffer);
|
|
|
|
}
|
|
|
|
}
|
2022-04-29 14:13:42 +03:00
|
|
|
|
|
|
|
class LogWarningOverlay extends StatelessWidget {
|
|
|
|
final Widget child;
|
|
|
|
|
2022-05-12 10:56:55 +03:00
|
|
|
const LogWarningOverlay({super.key, required this.child});
|
2022-04-29 14:13:42 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Stack(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
children: [
|
|
|
|
child,
|
|
|
|
Consumer(builder: (context, ref, _) {
|
2022-09-08 13:17:44 +03:00
|
|
|
final sensitiveLogs = ref.watch(logLevelProvider
|
|
|
|
.select((level) => level.value <= Level.CONFIG.value));
|
|
|
|
final allowScreenshots =
|
|
|
|
isAndroid ? ref.watch(androidAllowScreenshotsProvider) : false;
|
|
|
|
|
|
|
|
if (!(sensitiveLogs || allowScreenshots)) {
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
final String message;
|
|
|
|
if (sensitiveLogs && allowScreenshots) {
|
|
|
|
message =
|
|
|
|
'Potentially sensitive data is being logged, and other apps can potentially record the screen';
|
|
|
|
} else if (sensitiveLogs) {
|
|
|
|
message = 'Potentially sensitive data is being logged';
|
|
|
|
} else if (allowScreenshots) {
|
|
|
|
message = 'Other apps can potentially record the screen';
|
|
|
|
} else {
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
|
2023-05-22 11:29:11 +03:00
|
|
|
var mediaQueryData = MediaQueryData.fromView(View.of(context));
|
2022-09-09 12:24:33 +03:00
|
|
|
var bottomPadding = mediaQueryData.systemGestureInsets.bottom;
|
2022-09-08 19:34:47 +03:00
|
|
|
return Padding(
|
2022-09-09 17:41:14 +03:00
|
|
|
padding: EdgeInsets.fromLTRB(5, 0, 5, bottomPadding),
|
2022-09-08 19:34:47 +03:00
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.bottomCenter,
|
|
|
|
child: IgnorePointer(
|
|
|
|
child: Text(
|
|
|
|
'WARNING: $message!',
|
|
|
|
textDirection: TextDirection.ltr,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: const TextStyle(
|
2022-09-09 11:45:34 +03:00
|
|
|
color: Colors.red,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
height: 1.5,
|
|
|
|
fontSize: 16),
|
2022-04-29 14:13:42 +03:00
|
|
|
),
|
|
|
|
),
|
2022-09-08 13:17:44 +03:00
|
|
|
),
|
|
|
|
);
|
2022-04-29 14:13:42 +03:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|