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.
This commit is contained in:
Krille Fear 2021-11-23 10:31:35 +01:00
parent 6f4f69106e
commit 2383cef438
3 changed files with 70 additions and 16 deletions

View File

@ -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());
}
}
}

View File

@ -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');
}
}

View File

@ -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;
}
}
}