fluffychat/lib/widgets/log_view.dart

94 lines
2.4 KiB
Dart
Raw Normal View History

2020-12-22 14:23:23 +01:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:matrix/matrix.dart';
2022-09-10 12:11:11 +02:00
import 'm2_popup_menu_button.dart';
2020-12-22 14:23:23 +01:00
class LogViewer extends StatefulWidget {
2022-01-28 18:21:20 +01:00
const LogViewer({Key? key}) : super(key: key);
2021-10-14 18:09:30 +02:00
2020-12-22 14:23:23 +01:00
@override
2022-08-14 16:59:21 +02:00
LogViewerState createState() => LogViewerState();
2020-12-22 14:23:23 +01:00
}
2022-08-14 16:59:21 +02:00
class LogViewerState extends State<LogViewer> {
2020-12-22 14:23:23 +01:00
Level logLevel = Level.debug;
double fontSize = 14;
@override
Widget build(BuildContext context) {
final outputEvents = Logs()
.outputEvents
2021-05-23 13:11:55 +02:00
.where((e) => e.level.index <= logLevel.index)
2020-12-22 14:23:23 +01:00
.toList();
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: Text(logLevel.toString()),
2021-10-14 18:09:30 +02:00
leading: const BackButton(),
2020-12-22 14:23:23 +01:00
actions: [
IconButton(
2021-10-14 18:09:30 +02:00
icon: const Icon(Icons.zoom_in_outlined),
2020-12-22 14:23:23 +01:00
onPressed: () => setState(() => fontSize++),
),
IconButton(
2021-10-14 18:09:30 +02:00
icon: const Icon(Icons.zoom_out_outlined),
2020-12-22 14:23:23 +01:00
onPressed: () => setState(() => fontSize--),
),
2022-09-10 12:11:11 +02:00
M2PopupMenuButton<Level>(
2020-12-22 14:23:23 +01:00
itemBuilder: (context) => Level.values
.map((level) => PopupMenuItem(
value: level,
2021-03-04 12:28:06 +01:00
child: Text(level.toString()),
2020-12-22 14:23:23 +01:00
))
.toList(),
onSelected: (Level level) => setState(() => logLevel = level),
),
],
),
body: ListView.builder(
itemCount: outputEvents.length,
itemBuilder: (context, i) => SingleChildScrollView(
scrollDirection: Axis.horizontal,
2021-05-23 13:11:55 +02:00
child: SelectableText(
outputEvents[i].toDisplayString(),
style: TextStyle(
color: outputEvents[i].color,
),
),
2020-12-22 14:23:23 +01:00
),
),
);
}
}
2021-05-20 13:59:55 +02:00
extension on LogEvent {
2021-05-23 13:11:55 +02:00
Color get color {
switch (level) {
case Level.wtf:
return Colors.purple;
case Level.error:
return Colors.red;
case Level.warning:
return Colors.orange;
case Level.info:
return Colors.green;
case Level.debug:
return Colors.white;
case Level.verbose:
default:
return Colors.grey;
}
}
2021-05-20 13:59:55 +02:00
String toDisplayString() {
2021-05-23 13:11:55 +02:00
var str = '# [${level.toString().split('.').last.toUpperCase()}] $title';
2021-05-20 13:59:55 +02:00
if (exception != null) {
str += ' - ${exception.toString()}';
2020-12-22 14:23:23 +01:00
}
2021-05-20 13:59:55 +02:00
if (stackTrace != null) {
str += '\n${stackTrace.toString()}';
2020-12-22 14:23:23 +01:00
}
2021-05-20 13:59:55 +02:00
return str;
2020-12-22 14:23:23 +01:00
}
}