Skip to content

Commit

Permalink
Improve logging (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
Airyzz authored Aug 13, 2024
1 parent fc2670b commit 88a8fba
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
33 changes: 25 additions & 8 deletions commet/lib/debug/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 {
Expand All @@ -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");
Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -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));
},
);
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions commet/lib/ui/atoms/tiny_pill.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -109,6 +110,11 @@ class _LogPageState extends State<LogPage> {
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(
Expand Down
18 changes: 18 additions & 0 deletions commet/lib/utils/text_utils.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 88a8fba

Please sign in to comment.