var test = "testContent";
print("test:$test");
打印结果: I/flutter (12983): test:testContent
打印文件,行数,打印信息
打印结果: I/flutter (13709): test:testContent I/flutter (13709): 文件: main.dart, 行: 92, 打印信息: test:testContent
var test = "testContent";
print("test:$test");
printLog("test:$test", StackTrace.current);
void printLog(Object message, StackTrace current) {
MYCustomTrace programInfo = MYCustomTrace(current);
print("文件: ${programInfo.fileName}, 行: ${programInfo.lineNumber}, 打印信息: $message");
}
class MYCustomTrace {
final StackTrace _trace;
String fileName = "";
int lineNumber = 0;
int columnNumber = 0;
MYCustomTrace(this._trace) {
_parseTrace();
}
void _parseTrace() {
var traceString = this._trace.toString().split("\n")[0];
var indexOfFileName = traceString.indexOf(RegExp(r'[A-Za-z_]+.dart'));
var fileInfo = traceString.substring(indexOfFileName);
var listOfInfos = fileInfo.split(":");
this.fileName = listOfInfos[0];
this.lineNumber = int.parse(listOfInfos[1]);
var columnStr = listOfInfos[2];
columnStr = columnStr.replaceFirst(")", "");
this.columnNumber = int.parse(columnStr);
}
}
相关博客: flutter 打印日志调试小工具 - 文件信息、行数、打印信息
|