xlog function
void
xlog(
- dynamic msg
给外部调用快速定位log所在位置
Implementation
void xlog(dynamic msg) {
if (!kDebugMode) {
debugPrint('$msg');
return;
}
final now = DateTime.now().toString();
final time = now.substring(5, now.length > 23 ? 23 : now.length);
String loc = '';
final root = Directory.current.path.replaceAll('\\', '/');
try {
final lines = StackTrace.current.toString().split('\n');
if (lines.length > 1) {
// 先尝试将 package 路径归一化为 file 绝对路径;如果是 file 路径则保持不变
final line = lines[1].replaceFirst(
RegExp(r'\(package:[^/]+/'),
'(file:///$root/lib/',
);
final m = RegExp(r'\(file:///(.+?)\)').firstMatch(line);
if (m != null) loc = ' ${m.group(1)}';
}
} catch (_) {}
debugPrint('\x1B[90m[$time]\x1B[0m\x1B[94m$loc\x1B[0m $msg');
}