mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-04-20 22:13:48 +00:00
formats all dart files using `dart format .` from the root project dir this makes the code style repeatable by new contributors and makes PR review easier
92 lines
2.1 KiB
Dart
92 lines
2.1 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
enum AppDebugLogLevel { info, warning, error }
|
|
|
|
class AppDebugLogEntry {
|
|
final DateTime timestamp;
|
|
final AppDebugLogLevel level;
|
|
final String tag;
|
|
final String message;
|
|
|
|
AppDebugLogEntry({
|
|
required this.timestamp,
|
|
required this.level,
|
|
required this.tag,
|
|
required this.message,
|
|
});
|
|
|
|
String get levelLabel {
|
|
switch (level) {
|
|
case AppDebugLogLevel.info:
|
|
return 'INFO';
|
|
case AppDebugLogLevel.warning:
|
|
return 'WARN';
|
|
case AppDebugLogLevel.error:
|
|
return 'ERROR';
|
|
}
|
|
}
|
|
|
|
String get formattedTime {
|
|
return '${timestamp.hour.toString().padLeft(2, '0')}:'
|
|
'${timestamp.minute.toString().padLeft(2, '0')}:'
|
|
'${timestamp.second.toString().padLeft(2, '0')}.'
|
|
'${timestamp.millisecond.toString().padLeft(3, '0')}';
|
|
}
|
|
}
|
|
|
|
class AppDebugLogService extends ChangeNotifier {
|
|
static const int maxEntries = 1000;
|
|
final List<AppDebugLogEntry> _entries = [];
|
|
bool _enabled = false;
|
|
|
|
List<AppDebugLogEntry> get entries => List.unmodifiable(_entries);
|
|
bool get enabled => _enabled;
|
|
|
|
void setEnabled(bool value) {
|
|
_enabled = value;
|
|
notifyListeners();
|
|
}
|
|
|
|
void log(
|
|
String message, {
|
|
String tag = 'App',
|
|
AppDebugLogLevel level = AppDebugLogLevel.info,
|
|
}) {
|
|
if (!_enabled) return;
|
|
|
|
_entries.add(
|
|
AppDebugLogEntry(
|
|
timestamp: DateTime.now(),
|
|
level: level,
|
|
tag: tag,
|
|
message: message,
|
|
),
|
|
);
|
|
|
|
if (_entries.length > maxEntries) {
|
|
_entries.removeRange(0, _entries.length - maxEntries);
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
// Also print to console for development
|
|
debugPrint('[$tag] $message');
|
|
}
|
|
|
|
void info(String message, {String tag = 'App'}) {
|
|
log(message, tag: tag, level: AppDebugLogLevel.info);
|
|
}
|
|
|
|
void warn(String message, {String tag = 'App'}) {
|
|
log(message, tag: tag, level: AppDebugLogLevel.warning);
|
|
}
|
|
|
|
void error(String message, {String tag = 'App'}) {
|
|
log(message, tag: tag, level: AppDebugLogLevel.error);
|
|
}
|
|
|
|
void clear() {
|
|
_entries.clear();
|
|
notifyListeners();
|
|
}
|
|
}
|