使用断言的示例:
procedure AnyProcedure();begin try Assert(1=0); except on E: Exception do Log.Addline('Log occurred is '+E.Message+' : Start');//Log occurred is "c:\progr~..jkdj.pas" at line [29] end; //....some code try Assert(1=0); except on E: Exception do Log.Addline('Log occurred is '+E.Message+' : Step1 done');//Log occurred is "c:\progr~..jkdj.pas" at line [37] end; //....some code try Assert(1=0); except on E: Exception do Log.Addline('Log occurred is '+E.Message+' : Step2 done');//Log occurred is "c:\progr~..jkdj.pas" at line [45] end; //....some code try Assert(1=0); except on E: Exception do Log.Addline('Log occurred is '+E.Message+' : Step3 done');//Log occurred is "c:\progr~..jkdj.pas" at line [53] end; //....some code try Assert(1=0); except on E: Exception do Log.Addline('Log '+E.Message+' : End');//Log occurred is "c:\progr~..jkdj.pas" at line [61] end;end;
这个工作正常,它引发异常并且代码变得太大,所以我不能使用函数 – 请参见下一个示例函数LogMessage-并在另一个地方调用它,因为该行也将是相同的文件name将是LogMessage函数实现的位置:
不工作的例子:
procedure LogMessage(AMessage: String);var Afile,Aline: String;begin try Assert(1=0); //line 29 except on E: Exception do begin Afile:= copy(E.Message,Pos(' (',E.Message)+2,Pos(',line ',E.Message)-Pos(' (',E.Message)-2); Aline:= copy(E.Message,E.Message)+7,Pos(')',E.Message)-Pos(',E.Message)-7); ShowMessage('Log occurred in file "'+Afile+'" at line ['+Aline+'] : '+AMessage); end; end;end;procedure AnyProcedure();begin LogMessage('Start'); //Log occurred in file "c:\progr~....jkashdj.pas" at line [29]//.... LogMessage('step1'); //Log occurred in file "c:\progr~....jkashdj.pas" at line [29]//.... LogMessage('step2'); //Log occurred in file "c:\progr~....jkashdj.pas" at line [29]//.... LogMessage('step3'); //Log occurred in file "c:\progr~....jkashdj.pas" at line [29]//.... LogMessage('end');end
请帮助,并提前感谢.
解决方法 您可以将自己的TAssertErrorProc
过程绑定到 AssertErrorProc
变量.你可能写这样的东西: procedure OnAssert(const Message,filename: string; lineNumber: Integer; ErrorAddr: Pointer);begin ShowMessage(Format('Assert in file "%s" at line %d.',[filename,lineNumber]));end;procedure TForm1.FormCreate(Sender: TObject);begin AssertErrorProc := OnAssert;end;总结
以上是内存溢出为你收集整理的Delphi:如何获取(当前代码行,当前单位,当前函数)而不使用Assertion?全部内容,希望文章能够帮你解决Delphi:如何获取(当前代码行,当前单位,当前函数)而不使用Assertion?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)