Mega Code Archive

 
Categories / Delphi / Examples
 

Automatic bug report sending from your application

Title: Automatic bug report sending from your application Question: Many software companies develop of big projects which are still supported and advanced after the customers accept them. Both sides are interested in robust and reliability of their program. That is why, the developers perform some actions to test and find out possible errors and failures in good time. Answer: Automatic bug report sending from your application All examples in this article were developed by using the components from the Clever Internet Suite library (download it here). Many software companies develop of big projects which are still supported and advanced after the customers accept them. Both sides are interested in robust and reliability of their program. That is why, the developers perform some actions to test and find out possible errors and failures in good time. Although, there is still a probability of the critical situation appearance which is quite hard to reproduce. The situation is complicated by the fact that the end-users of the product may not know the details of its implementation. Therefore, their description of the problem can be not enough to find out and fix the reasons of the failure. The automatic collection of the required information and automatic sending it to your special web resource by the programitself can help you solve this problem. Most developers use this technique now, including Microsoft with their new XP series and many others as well. Let us look at the simple Delphi program which automatically collects and sends bug reports. In the first approximation all exceptions can be split on two kinds: the first allows the program to continue running and the fatal errors after which it is impossible to go on. Distinguishing these two types, we can send the report into your support server in two different ways. This difference appears from the desire to satisfy the user necessity to go on working as soon as possible. That is why it would be nice to combine the report sending process with further user work wherever it is possible. Such feature is given to us while the continuable exceptions occur. To do that, we need the method which would allow us to work asynchronously with a remote resource. One of the easiest and cheapest ways is using the TclUploader component from the Clever Internet Suite library. The own exception handler is the easiest way of catching errors. When using VCL, you can write your own one and connect it to the OnException event of the Application object. At that, the old handler should be stored if it exists. TMyAppForm = class(TForm) ... private FPrevHandleException: TExceptionEvent; end; ... procedure TMyAppForm.FormCreate(Sender: TObject); begin FPrevHandleException := Application.OnException; Application.OnException := DoHandleException; end; procedure TMyAppForm.FormDestroy(Sender: TObject); begin Application.OnException := FPrevHandleException; end; Next, let us create a new TclUploader component and setup it on the web resource specified by you to receive bug reports. The implementation of the DoHandleException doesn't have any difficulties and consists of filling the file being sent with the detailed information about the error and sending. When the continuable exception occurs the component is setup for asynchronous work, and it returns the control immediately. In the rest of cases the halt command, which terminates the program abnormally, is performed after the synchronous upload has done. procedure TMyAppForm.DoHandleException(Sender: TObject; E: Exception); var IsContinuable: Boolean; Msg: TStrings; begin IsContinuable := not (E is EAccessViolation); Msg := TStringList.Create(); try if IsContinuable then begin Msg.Add('The program raised continuable exception and still can continue operating.'); end else begin Msg.Add('The program raised non-continuable exception and will be closed.'); end; Msg.Add(E.Message); Msg.Add(' ... some detailed program information ... '); Msg.Add('Would you like to send the detailed error info to the program vendor? '); if MessageDlg(Msg.Text, mtInformation, [mbYes, mbNo], 0) = mrYes then begin Msg.SaveToFile(clUploader.LocalFile); clUploader.Start(IsContinuable); end; finally Msg.Free(); end; if not IsContinuable then begin Halt; end; end; To display the status of uploading, it is enough to assign a hander to OnDataItemProceed event of the TclUploader component. procedure TMyAppForm.clUploaderDataItemProceed(Sender: TObject; ResourceInfo: TclResourceInfo; BytesProceed: Integer; CurrentData: PChar; CurrentDataSize: Integer); begin Caption := 'MyAppForm. Bug Report Send - ' + IntToStr(BytesProceed) + ' of ' + IntToStr(ResourceInfo.Size) + ' bytes proceed.'; end; To test the automatic bug report sender, we can use the simple emulation of the exceptional situations. The usual "raise Exception" can be used as the continuable exception. procedure TMyAppForm.btnContinuableClick(Sender: TObject); begin raise Exception.Create('Some erroneous situation'); end; To reproduce another case, we approach to the real situation and try to access to invalid memory location by using the "Nil" pointer. procedure TMyAppForm.btnNonContinuableClick(Sender: TObject); begin //Access Violation exception emulator asm xor EAX, EAX; //here we zero CPU register mov EAX, [EAX]; //here we try to access to memory with zero address end; end; In conclusion, we want to note that if it is impossible to open FTP resource for users because of security, there is an ability to perform uploading by using HTTP or even HTTPS protocols. At that, the component automatically will setup itself to use the desired resource type while changing its URL property. When using HTTP protocol it should be noted that you must make an additional setup of your web server to grant the users rights to write into the specified resource. The full version of this example can be downloaded here.