MadExcept settings for a Windows Service app

delphi package - automated exception handling
Post Reply
kbriggs
Posts: 4
Joined: Sat Apr 09, 2022 2:03 am

MadExcept settings for a Windows Service app

Post by kbriggs »

I've tinkered around with MadExcept in the past but finally got around to purchasing a developers license so I can integrate it into my commercial app. That app is web-socket base game server written as service app using Indy HTTP/TCP components. So there is no Windows interface. Currently I trap exceptions with code like this:

Code: Select all

class procedure WSServerEvents.OnExecute(AContext: TIdContext);
begin
  Sleep(10);
  try
    // do stuff here
  except
    on E: Exception do
    begin
      if E is EIdException then raise else
      begin
        LogData.AddError('WSServerExecute error: ' + E.Message);
        raise;
      end;
    end;
  end;
end;
When that catches an exception I get an entry in my own log file. But the E.Message just spits out a raw memory address. I can subtract 00401000 from it and look up the line in the .map file. But most of the time it tells me very little. So I of course want MadExcept to create dump file with a stack trace. So far, I'm unable to even get it to create a bugreport file when I intentionally insert a bug to fire my except clause. So a little guidance would be appreciated.
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: MadExcept settings for a Windows Service app

Post by madshi »

Can you please explain what you want to happen exactly, ideally? E.g. do you want to keep your current code, with the try..except blocks, but call madExcept in there to create a bug report for you silently? Or do you want to get rid of those try..except blocks and have madExcept automatically catch exceptions for you? Do you want to just store bug reports to your internal log file? Or do you want madExcept to send an email to you or something like that? And since we're talking about a service, I assume you want no GUI to be visible for any crashes?
kbriggs
Posts: 4
Joined: Sat Apr 09, 2022 2:03 am

Re: MadExcept settings for a Windows Service app

Post by kbriggs »

For now, I'd prefer to keep the exception as-is (unless that is more complicated than the alternative) and have MadExcept silently create a bugreport.txt file in the program folder immediately after my LogData entry. My log entry will alert the end user to look for that file.

This OnExecute routine is called repeatedly by an Indy thread when a TCP connection is made. If the exception occurred in Indy code then it gets handled by Indy itself (hence the "if E is EIdException then raise" line). For all other exceptions, I want to keep my LogData line but also have MadExcept dump the stack trace for it. The manual "raise" I make tells Indy to abort this connection.
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: MadExcept settings for a Windows Service app

Post by madshi »

Ok, there are 2 options:

1) Either you can add a call to "madExcept.HandleException()", to instruct madExcept to handle this exception. Doing so with default settings will open the madExcept exception box, though, so you should modify the settings to hide the box. You should probably do that, in any case. You can do that by switching the bottom right combobox on the following settings tab to "display's show anything":

http://help.madshi.net/madExceptSettings3.htm

Plus, uncheck "show please wait box" here:

http://help.madshi.net/madExceptSettings2.htm

Finally, if you choose this option, you should probably configure the path where the bug report is stored. You can do that here:

http://help.madshi.net/madExceptSettings7.htm

Or you can do it at runtime by doing something like "madExcept.MESettings().BugReportFile := 'c:\somePath\bugReport.txt';".

2) Alternatively, you could call "madExcept.CreateBugReport(etNormal)". This is a function exported by madExcept which will return a full bug report to you as an AnsiString. You could then store it to a file yourself, if you like.
kbriggs
Posts: 4
Joined: Sat Apr 09, 2022 2:03 am

Re: MadExcept settings for a Windows Service app

Post by kbriggs »

madshi wrote: Sat Apr 09, 2022 5:29 pm 2) Alternatively, you could call "madExcept.CreateBugReport(etNormal)". This is a function exported by madExcept which will return a full bug report to you as an AnsiString. You could then store it to a file yourself, if you like.
Thank you very much. Option 2 is perfect. I ran a test and got it working. Do I have any options other than a full bug report? I'm mainly only interested in that first thread trace. I can of course parse that out myself but the full report will likely be huge because there are usually dozens if not hundreds of other threads running.
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: MadExcept settings for a Windows Service app

Post by madshi »

Inside of a try..except block you can use GetCrashStackTrace:

http://help.madshi.net/madExceptUnit.ht ... StackTrace

All parameters at defaults should work just fine.
kbriggs
Posts: 4
Joined: Sat Apr 09, 2022 2:03 am

Re: MadExcept settings for a Windows Service app

Post by kbriggs »

madshi wrote: Sat Apr 09, 2022 8:20 pm Inside of a try..except block you can use GetCrashStackTrace:
I was about to reply that I just found that in the docs. Worked perfectly! :D
Post Reply