创建log.dart文件,里面代码如下:
void myLog(Object message, StackTrace current) {
CustomTrace programInfo = CustomTrace(current);
print("所在文件: ${programInfo.fileName}, 所在行: ${programInfo.lineNumber}, 打印信息: $message");
}
class CustomTrace {
final StackTrace _trace;
String fileName;
int lineNumber;
int columnNumber;
CustomTrace(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);
}
}
使用方法:引入这个log.dart文件,要打印的地方写如下代码
myLog('你要打印信息',StackTrace.current);
案例: