Recent

Author Topic: Capture form name  (Read 4160 times)

elidorio

  • Sr. Member
  • ****
  • Posts: 295
Capture form name
« on: August 30, 2015, 06:30:58 am »
Hello,

I'm trying to set up a function to register the application error log, there arose a question, how do I capture form name that generated the error.

--
Edson
Lazarus 1.4.4 | FPC 2.6.4 | Windows / Linux Debian

derek.john.evans

  • Guest
Re: Capture form name
« Reply #1 on: August 30, 2015, 07:26:41 am »
application error log? Which one? Most logs use string messages. So, you need to send the name of the form with the message. Best way todo that is to write some helper functions (Im unsure which method of logging you are using):

Code: Pascal  [Select][+][-]
  1. uses LazLogger;
  2.  
  3. procedure FormLog1(const AForm: TForm; const AMessage: String);
  4. begin
  5.   raise Exception.CreateFmt('%s -> %s', [AForm.Name, AMessage]);
  6. end;
  7.  
  8. procedure FormLog2(const AForm: TForm; const AMessage: String);
  9. begin
  10.   if not (TextRec(StdErr).Mode = fmClosed) then WriteLn(StdErr, AForm.Name, ' -> ', AMessage);
  11. end;
  12.  
  13. procedure FormLog3(const AForm: TForm; const AMessage: String);
  14. begin
  15.   DebugLn(AMessage);
  16.   DbgSName(AForm);
  17. end;
  18.  
  19. procedure FormLog4(const AForm: TForm; const AMessage: String);
  20. begin
  21.   DebugLn('%s -> %s', [AForm.Name, AMessage]);
  22. end;    
  23.  
« Last Edit: October 02, 2015, 03:19:39 am by Geepster »

elidorio

  • Sr. Member
  • ****
  • Posts: 295
Re: Capture form name
« Reply #2 on: August 30, 2015, 04:28:40 pm »
Want to get the error that generated for my application, I wanted to get the name of the form and the line that generated the error.
I want to use exception inside a try block.
Lazarus 1.4.4 | FPC 2.6.4 | Windows / Linux Debian

derek.john.evans

  • Guest
Re: Capture form name
« Reply #3 on: August 30, 2015, 05:09:07 pm »
The only way I can think of is something like:
Code: [Select]
procedure RaiseHell(const AForm: TForm; const AMessage, AFile, ALine: String);
begin
  raise Exception.CreateFmt('%s @ %s(%s) File="%s" Line="%s"', [
    AMessage, AForm.ClassName, AForm.Name, AFile, ALine]);
end;

And then call your exception function like:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  RaiseHell(Self, 'Exception Message Here', {$INCLUDE %FILE%}, {$INCLUDE %LINE%});
end;   

There is some interesting stuff in units lnfodwrf & lineinfo. ie: GetLineInfo(). But, I cant get it working, and it relies on debug builds.
http://wiki.freepascal.org/Logging_exceptions#DWARF

derek.john.evans

  • Guest
Re: Capture form name
« Reply #4 on: August 30, 2015, 05:54:38 pm »
Another idea: If you enable MACRO's and ASSERT's you can define macros for each of your exceptions.
Code: [Select]
{$MACRO ON}
{$ASSERTIONS ON}
{$DEFINE RAISE_NOT_FOUND := Assert(False, 'Not Found @ ' + Self.ClassName + '[' + Self.Name + ']')}
{$DEFINE RAISE_OUT_OF_RANGE := Assert(False, 'Out Of Range @ ' + Self.ClassName + '[' + Self.Name + ']')}   
Then just add an exception like this:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  RAISE_NOT_FOUND;
end;   

The cool thing about this is, you can always change your exceptions for release, by changing the macros.

*EDIT*: Scrap that. The line number is the line number of the macro define. Dang
« Last Edit: August 30, 2015, 06:06:45 pm by derek.john.evans »

elidorio

  • Sr. Member
  • ****
  • Posts: 295
Re: Capture form name
« Reply #5 on: August 30, 2015, 06:28:41 pm »
OK, I'll do the test
Lazarus 1.4.4 | FPC 2.6.4 | Windows / Linux Debian

 

TinyPortal © 2005-2018