Make the beautify logging tool

(cherry picked from commit 3322703de81c2c53f45b25743071769f8bdabb55)
This commit is contained in:
dab246
2023-08-28 18:51:49 +07:00
committed by Dat Vu
parent 20c9777819
commit 566575fcb9
3 changed files with 42 additions and 11 deletions
+38 -7
View File
@@ -3,17 +3,38 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
final logHistory = _Dispatcher("");
final logHistory = _Dispatcher('');
void log(String? value) {
if (kDebugMode) {
String v = value ?? "";
logHistory.value = "$v\n${logHistory.value}";
print(v);
void log(String? value, {Level level = Level.info}) {
if (!kDebugMode) return;
String logsStr = value ?? '';
logHistory.value = '$logsStr\n${logHistory.value}';
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('[TeamMail] $logsStr');
}
void logError(String? value) => log("[ERROR] ${value ?? ""}");
void logError(String? value) => log(value, level: Level.error);
// Take from: https://flutter.dev/docs/testing/errors
void initLogger(VoidCallback runApp) {
@@ -31,4 +52,14 @@ void initLogger(VoidCallback runApp) {
class _Dispatcher extends ValueNotifier<String> {
_Dispatcher(String value) : super(value);
}
enum Level {
wtf,
error,
warning,
info,
debug,
verbose,
}