From 2383cef43837c3da4a038cd83125c78109b2c53c Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Tue, 23 Nov 2021 10:31:35 +0100 Subject: [PATCH] feat: Colored logs on native and web This uses the developer.logs method to print logs on native and console.logs on web. Therefore we have native colored logs everywhere without the use of a package. It highlights errors and warnings on web much better and supports collapsing stacktraces on web. --- lib/src/utils/logs.dart | 18 ++------------- lib/src/utils/print_logs_native.dart | 34 ++++++++++++++++++++++++++++ lib/src/utils/print_logs_web.dart | 34 ++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 lib/src/utils/print_logs_native.dart create mode 100644 lib/src/utils/print_logs_web.dart diff --git a/lib/src/utils/logs.dart b/lib/src/utils/logs.dart index 37852945..f9ae4394 100644 --- a/lib/src/utils/logs.dart +++ b/lib/src/utils/logs.dart @@ -21,6 +21,8 @@ * SOFTWARE. */ +import 'print_logs_native.dart' if (dart.library.html) 'print_logs_web.dart'; + enum Level { wtf, error, @@ -124,20 +126,4 @@ class LogEvent { this.stackTrace, this.level = Level.debug, }); - - void printOut() { - var logsStr = - '# [${level.toString().split('.').last.toUpperCase()}] $title'; - if (exception != null) { - logsStr += ' - ' + exception.toString(); - } - // ignore: avoid_print - print(logsStr); - if (stackTrace != null) { - // ignore: avoid_print - print('## Stacktrace:'); - // ignore: avoid_print - print(stackTrace.toString()); - } - } } diff --git a/lib/src/utils/print_logs_native.dart b/lib/src/utils/print_logs_native.dart new file mode 100644 index 00000000..b6790e4e --- /dev/null +++ b/lib/src/utils/print_logs_native.dart @@ -0,0 +1,34 @@ +import 'package:matrix_api_lite/matrix_api_lite.dart'; + +extension PrintLogs on LogEvent { + void printOut() { + var logsStr = title; + if (exception != null) { + logsStr += ' - ' + exception.toString(); + } + if (stackTrace != null) { + logsStr += '\n${stackTrace.toString()}'; + } + switch (level) { + case Level.wtf: + logsStr = '\x1B[31m!!!CRITICAL!!! $logsStr\x1B[0m'; + break; + case Level.error: + logsStr = '\x1B[31m$logsStr\x1B[0m'; + break; + case Level.warning: + logsStr = '\x1B[33m$logsStr\x1B[0m'; + break; + case Level.info: + logsStr = '\x1B[32m$logsStr\x1B[0m'; + break; + case Level.debug: + logsStr = '\x1B[34m$logsStr\x1B[0m'; + break; + case Level.verbose: + break; + } + // ignore: avoid_print + print('[Matrix] $logsStr'); + } +} diff --git a/lib/src/utils/print_logs_web.dart b/lib/src/utils/print_logs_web.dart new file mode 100644 index 00000000..ea67b71d --- /dev/null +++ b/lib/src/utils/print_logs_web.dart @@ -0,0 +1,34 @@ +import 'package:matrix_api_lite/matrix_api_lite.dart'; +import 'dart:html'; + +extension PrintLogs on LogEvent { + void printOut() { + var logsStr = '[Matrix] $title'; + if (exception != null) { + logsStr += ' - ' + exception.toString(); + } + if (stackTrace != null) { + logsStr += '\n${stackTrace.toString()}'; + } + switch (level) { + case Level.wtf: + window.console.error('!!!CRITICAL!!! $logsStr'); + break; + case Level.error: + window.console.error(logsStr); + break; + case Level.warning: + window.console.warn(logsStr); + break; + case Level.info: + window.console.info(logsStr); + break; + case Level.debug: + window.console.debug(logsStr); + break; + case Level.verbose: + window.console.log(logsStr); + break; + } + } +}