import 'dart:collection'; import 'package:flutter/foundation.dart'; /// Small in-memory log the client can copy from the Debug screen and send /// back with a bug report. Keeps the newest [_capacity] lines only. class AppLog { AppLog._(); static const int _capacity = 400; static final ListQueue _lines = ListQueue(); static void i(String msg) => _add('I', msg); static void w(String msg) => _add('W', msg); static void e(String msg, [Object? error, StackTrace? stack]) { final b = StringBuffer(msg); if (error != null) b.write(' | $error'); if (stack != null) { // First few frames are enough to locate a crash. final frames = stack.toString().split('\n').take(8).join('\n '); b.write('\n $frames'); } _add('E', b.toString()); } static void _add(String level, String msg) { final t = DateTime.now(); String two(int v) => v.toString().padLeft(2, '0'); final ts = '${two(t.hour)}:${two(t.minute)}:${two(t.second)}.' '${t.millisecond.toString().padLeft(3, '0')}'; final line = '$ts $level $msg'; _lines.addLast(line); while (_lines.length > _capacity) { _lines.removeFirst(); } debugPrint(line); } static String dump() => _lines.join('\n'); static int get length => _lines.length; }