Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to suppress the error and generate error log

Status
Not open for further replies.

ameen

Programmer
Jun 24, 2001
32
IN
Can any one give some suggestion, how to suppress the error and generate the error log in a application developped using Delphi 5.

Regards




Ameen
 
Use try .... Except blocks and and put code to write to the error log in the except part of the block.

But if you are talking at development time you nedd to configure your debugger options in:

Tools/Debugger Options

This is where you set if delphi stops on exception, as well as event log settings.you can use:

View/Debug Window/Event log to watch the event log

X-)

Billy H

bhogar@acxiom.co.uk
 
Thank you Billy for your response.

Can you give some sample coding, how to create the log file and have it with the application to append the error messages.

Advance thanks.



Regards


Ameen

 
In this example the file 'C:\tempLog.log' must already exist.
The Procedure divides the the contents of 2 text boxes and places the result in a third text box.

The Except part of the code catches any errors, specificly: convertion, devide by zero, overflow and other math, But also caters for any other error that may occur.

I've given each type of error an integer value which is paseed to the error handling procedure, which depending upon this value builds a string and writes it to the. file.

This Procedure can be called from any other procedure and expanded upon to meet your needs.

** If you are not sure of the ErrorCodes these are displayed when you program terminates with an error while debugging.

X-)


Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
   try
      Edit3.Text := floatToStr(StrToFloat(Edit1.Text)/StrTofloat(Edit2.Text))
   except
      on EZeroDivide   do HandleError(0);
      on EConvertError do HandleError(2);
      on EOverflow     do HandleError(3);
      on EMathError    do HandleError(4);
   else
      HandleError(999);
   end
end;

procedure TForm1.HandleError(ErrorCode : Integer);
var
  myLog : TextFile;
  errorLine : String;
begin
  errorLine := dateToStr(date)+'...';
  case ErrorCode of
     0: errorLine := errorLine +'Divide by zero ';
     2: errorLine := errorLine +'Conver to float ';
     3: errorLine := errorLine +'Mathmatical Overflow';
     4: errorLine := errorLine +'General Math';
  else
     errorLine := errorLine +'Unspecified Error';
  end;
  AssignFile(myLog, 'C:\tempLog.log');
  Append(myLog);
  writeln(myLog,errorLine);
  flush(myLog);
  closeFile(myLog);
end;
Billy H

bhogar@acxiom.co.uk
 
If you want to log ALL errors for your whole application you can use the OnException event for the TApplication object. Any exception that gets propogated to the application point can then be logged to an error file. TealWren
 
Hi all,

I was just working on my own piece of code for the problem
that ameen discribes, and I think I have succeeded, but how
can I test this piece of code properly? Exeptionslog is a
TMemo component by the way.

Code:
procedure TMainForm.AppExeption(Sender: TObject; E: Exception);
var FileName: String;
begin
FileName := ExtractFilePath(Application.ExeName) + '\Error.log';

ExeptionsLog.Clear;
if FileExists(FileName) then
ExeptionsLog.Lines.LoadFromFile(filename);

ExeptionsLog.Lines.Add(E.Message);
ExeptionsLog.Lines.SaveToFile(filename);
end;

Thanks allot,

BobbaFet
Everyone has a right to my opinion.
E-mail me at cwcon@programmer.net
 
Sorry, mistake in the code:
This is the corrected code:

procedure TMainForm.AppExeption(Sender: TObject; E: Exception);
var FileName: String;
begin
FileName := ExtractFilePath(Application.ExeName) + 'Error.log';

ExeptionsLog.Clear;
if FileExists(FileName) then
ExeptionsLog.Lines.LoadFromFile(filename);

ExeptionsLog.Lines.Add(E.Message);
ExeptionsLog.Lines.SaveToFile(filename);
end;
Everyone has a right to my opinion.
E-mail me at cwcon@programmer.net
 
There is just one thing with this code I just realised,
if error.log's filesize is greater than 32kB then a looping
error could occur since it will fail and the AppExeption
code will try to handle and initiate it again ...

Any idea's on how to solve that problem??? Everyone has a right to my opinion.
E-mail me at cwcon@programmer.net
 
How about this?

procedure TMainForm.AppExeption(Sender: TObject; E: Exception);
var FileName: String;
logfile: textfile;
begin
FileName := ExtractFilePath(Application.ExeName) + 'Error.log';
AssignFile(logFile, filename);
append(logfile);
writeln(logfile(e.message));
closefile(logfile);
end;
TealWren
 
oops, hurrying too fast - that should be
writeln(logfile, e.message); TealWren
 
Hi TealWren,

With the code that you suggested, does the filesize matter?
because with mine it does, it has a 32kB limit ???

BobbaFet Everyone has a right to my opinion.
E-mail me at cwcon@programmer.net
 
Nope, your 32kb limit comes from loading it into a memo. And actually, I don't think you have that limit if you use Win2k or WinNT. Anyway, that's neither here nor there.

The method I used is opening the file and appending to the end of it, without loading the entire file into memory at all so, in this case, size really doesn't matter. :)

Something I use in my own programs though, is keeping log files by date - for example MyProgram20000822.log would hold all errors for the 22nd of August. That makes it really easy to zap the old ones, or to find what happened on a certain day.

TealWren
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top