From 88a8fbaf45c83ccff21fb4802110be3d2ea744d1 Mon Sep 17 00:00:00 2001 From: Airyzz <36567925+Airyzz@users.noreply.github.com> Date: Tue, 13 Aug 2024 10:58:57 +0000 Subject: [PATCH] Improve logging (#340) --- commet/lib/debug/log.dart | 33 ++++++++++++++----- commet/lib/ui/atoms/tiny_pill.dart | 6 ++-- .../matrix/session/matrix_session_view.dart | 17 +++------- .../categories/developer/log_page.dart | 6 ++++ commet/lib/utils/text_utils.dart | 18 ++++++++++ 5 files changed, 56 insertions(+), 24 deletions(-) diff --git a/commet/lib/debug/log.dart b/commet/lib/debug/log.dart index 7e55fe43..a7f3b2d5 100644 --- a/commet/lib/debug/log.dart +++ b/commet/lib/debug/log.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:commet/main.dart'; import 'package:commet/utils/notifying_list.dart'; +import 'package:commet/utils/text_utils.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; @@ -11,11 +12,17 @@ class LogEntry { late DateTime _time; LogType type; DateTime get time => _time; - String content; + String rawContent; + int count = 1; - LogEntry(this.type, this.content) { + LogEntry(this.type, this.rawContent) { _time = DateTime.now(); } + + // Log content with sensitive information removed + String get content { + return TextUtils.redactSensitiveInfo(rawContent); + } } class LogEntryException extends LogEntry { @@ -31,6 +38,16 @@ class Log { static String prefix = ""; + static void add(LogEntry entry) { + if (log.isNotEmpty && + log.last.type == entry.type && + log.last.rawContent == entry.rawContent) { + log.last.count += 1; + } else { + log.add(entry); + } + } + static ZoneSpecification spec = ZoneSpecification( print: (self, parent, zone, line) { parent.print(zone, "($prefix) $line"); @@ -40,7 +57,7 @@ class Log { } if (!preferences.isInit || preferences.developerMode == true) { - log.add(LogEntry(LogType.info, line)); + add(LogEntry(LogType.info, line)); } }, errorCallback: (self, parent, zone, error, stackTrace) { @@ -49,7 +66,7 @@ class Log { parent.print(zone, stackTrace?.toString() ?? ""); String? info = stackTrace != null ? getDetailFromStackTrace(stackTrace) : null; - log.add(LogEntryException( + add(LogEntryException( LogType.error, "${error.toString()}${info != null ? " ($info)" : ""}", error, @@ -59,7 +76,7 @@ class Log { handleUncaughtError: (self, parent, zone, error, stackTrace) { parent.print(zone, "HandleUncaughtError"); String? info = getDetailFromStackTrace(stackTrace); - log.add(LogEntryException( + add(LogEntryException( LogType.error, "${error.toString()} ($info)", error, stackTrace)); }, ); @@ -84,10 +101,10 @@ class Log { } static void _print(LogEntry entry) { - log.add(entry); + add(entry); // ignore: avoid_print - print(entry.content); + print(entry.rawContent); } static void i(Object o) { @@ -127,7 +144,7 @@ class Log { static void _onFlutterError(FlutterErrorDetails details) { String? info = details.stack != null ? getDetailFromStackTrace(details.stack!) : null; - log.add(LogEntryException( + add(LogEntryException( LogType.error, "${details.exception.toString()}${info != null ? " ($info)" : ""}", details.exception, diff --git a/commet/lib/ui/atoms/tiny_pill.dart b/commet/lib/ui/atoms/tiny_pill.dart index cff5172a..6150013e 100644 --- a/commet/lib/ui/atoms/tiny_pill.dart +++ b/commet/lib/ui/atoms/tiny_pill.dart @@ -9,16 +9,16 @@ class TinyPill extends StatelessWidget { @override Widget build(BuildContext context) { return Padding( - padding: const EdgeInsets.fromLTRB(0, 0, 8, 0), + padding: const EdgeInsets.fromLTRB(0, 0, 0, 0), child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(5), - color: Theme.of(context).colorScheme.primary), + color: Theme.of(context).colorScheme.primaryContainer), child: Padding( padding: const EdgeInsets.all(2.0), child: tiamat.Text.tiny( text, - color: Theme.of(context).colorScheme.onPrimary, + color: Theme.of(context).colorScheme.onPrimaryContainer, ), ), ), diff --git a/commet/lib/ui/pages/settings/categories/account/security/matrix/session/matrix_session_view.dart b/commet/lib/ui/pages/settings/categories/account/security/matrix/session/matrix_session_view.dart index b193c7dc..79e9076a 100644 --- a/commet/lib/ui/pages/settings/categories/account/security/matrix/session/matrix_session_view.dart +++ b/commet/lib/ui/pages/settings/categories/account/security/matrix/session/matrix_session_view.dart @@ -1,4 +1,5 @@ import 'package:commet/config/layout_config.dart'; +import 'package:commet/ui/atoms/tiny_pill.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:tiamat/tiamat.dart' as tiamat; @@ -59,19 +60,9 @@ class MatrixSessionView extends StatelessWidget { Row( children: [ if (isThisDevice) - Padding( - padding: const EdgeInsets.fromLTRB(0, 0, 8, 0), - child: Container( - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(5), - color: Theme.of(context) - .colorScheme - .primary), - child: const Padding( - padding: EdgeInsets.all(2.0), - child: tiamat.Text.tiny("This Device"), - ), - ), + const Padding( + padding: EdgeInsets.fromLTRB(0, 0, 8, 0), + child: TinyPill("This Device"), ), tiamat.Text.labelLow(deviceId), ], diff --git a/commet/lib/ui/pages/settings/categories/developer/log_page.dart b/commet/lib/ui/pages/settings/categories/developer/log_page.dart index 48a0e78a..dd193023 100644 --- a/commet/lib/ui/pages/settings/categories/developer/log_page.dart +++ b/commet/lib/ui/pages/settings/categories/developer/log_page.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:commet/config/build_config.dart'; import 'package:commet/debug/log.dart'; import 'package:commet/ui/atoms/code_block.dart'; +import 'package:commet/ui/atoms/tiny_pill.dart'; import 'package:commet/ui/navigation/adaptive_dialog.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:flutter/material.dart'; @@ -109,6 +110,11 @@ class _LogPageState extends State { children: [ Row( children: [ + if (entry.count > 1) + Padding( + padding: const EdgeInsets.fromLTRB(0, 0, 0, 0), + child: TinyPill("${entry.count}"), + ), Padding( padding: const EdgeInsets.all(2.0), child: Icon( diff --git a/commet/lib/utils/text_utils.dart b/commet/lib/utils/text_utils.dart index 56a930b7..473bf141 100644 --- a/commet/lib/utils/text_utils.dart +++ b/commet/lib/utils/text_utils.dart @@ -1,5 +1,7 @@ import 'dart:math'; +import 'package:commet/client/matrix/matrix_client.dart'; +import 'package:commet/main.dart'; import 'package:commet/ui/atoms/rich_text/spans/link.dart'; import 'package:flutter/material.dart'; import 'emoji/emoji_matcher.dart'; @@ -178,4 +180,20 @@ class TextUtils { " " + units[digitGroups]; } + + static String redactSensitiveInfo(String text) { + if (clientManager != null) { + for (final client in clientManager!.clients) { + if (client is MatrixClient) { + var token = client.getMatrixClient().accessToken; + + if (token != null) { + text = text.replaceAll(token, "[REDACTED ACCESS TOKEN]"); + } + } + } + } + + return text; + } }