Recent

Author Topic: [Solved] Program crash output to file (runtime errors)  (Read 4989 times)

Paul_

  • Full Member
  • ***
  • Posts: 143
[Solved] Program crash output to file (runtime errors)
« on: April 13, 2018, 09:42:22 pm »
I know that is possible save heaptrc/memleaks log via SetHeapTraceOutput('trace.log'); but is also possible save crash log as file?
Instead of output to the command line/error messages (-WG) ?

Quote
Command line output:
An unhandled exception occurred at $0042FDD4:
EInvalidOp: Invalid floating point operation
  $0042FDD4 line 400 of uSFML_Func.pas
  $0042A448 line 315 of uSFML_Chrono.pas
  $0040452D line 773 of app.lpr
  ..

« Last Edit: April 14, 2018, 04:47:48 pm by Paul_ »

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Program crash output to file
« Reply #1 on: April 14, 2018, 01:01:19 am »
With windows I can "Copy" from a console window and then paste it in an editor like NotePad and then do what ever with it.

 or are you looking for a configurable feature?

The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Program crash output to file
« Reply #2 on: April 14, 2018, 10:40:17 am »
You can redirect compiler error output to file  -Fe<filename>  This a compiler option.
« Last Edit: April 14, 2018, 10:45:47 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Program crash output to file
« Reply #3 on: April 14, 2018, 10:57:30 am »
Demo:
This will generate error:
Code: Pascal  [Select][+][-]
  1. program erroutput;
  2. var
  3.   a:integer = 100;
  4. begin
  5.   writeln(a div 0);
  6. end.
Compile as:
Code: Bash  [Select][+][-]
  1.   fpc -Feerror.log erroutpu.pas
Output from compiler:
Code: Bash  [Select][+][-]
  1. pi@raspberrypi:~ $ fpc -Feerror.log erroutput.pas
  2. Error: /usr/local/bin/ppcarm returned an error exitcode

Now read error.log:
Code: Bash  [Select][+][-]
  1. pi@raspberrypi:~ $ cat error.log
  2. Free Pascal Compiler version 3.1.1-r38734 [2018/04/12] for arm
  3. Copyright (c) 1993-2018 by Florian Klaempfl and others
  4. Target OS: Linux for ARMHF
  5. Compiling erroutput.pas
  6. erroutput.pas(6,13) Error: Division by zero
  7. erroutput.pas(8) Fatal: There were 1 errors compiling module, stopping
  8. Fatal: Compilation aborted

Under Windows use type instead of cat.



« Last Edit: April 14, 2018, 11:00:30 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Paul_

  • Full Member
  • ***
  • Posts: 143
Re: Program crash output to file
« Reply #4 on: April 14, 2018, 11:49:06 am »
With windows I can "Copy" from a console window and then paste it in an editor like NotePad and then do what ever with it.

 or are you looking for a configurable feature?

This is for the users, if there is an error => they can send me a file. Yes, thats possible if command line isn't closed but I will distribute *.exe which runs without output to the command line, without *.bat and so.

You can redirect compiler error output to file  -Fe<filename>  This a compiler option.

I need it for runtime errors not for compilers error:

Code: Pascal  [Select][+][-]
  1. program erroutput;
  2. var
  3.   a: integer = 100;
  4.   b: integer = 0;
  5. begin
  6.  
  7.   writeln(a div b);
  8.  
  9. end.
« Last Edit: April 14, 2018, 11:56:04 am by Paul_ »

ASerge

  • Hero Member
  • *****
  • Posts: 2246
Re: Program crash output to file
« Reply #5 on: April 14, 2018, 12:53:32 pm »
Example:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses Classes, SysUtils, Forms, Controls, StdCtrls;
  7.  
  8. type
  9.   TForm1 = class(TForm)
  10.     Button1: TButton;
  11.     Memo1: TMemo;
  12.     procedure Button1Click(Sender: TObject);
  13.     procedure FormCreate(Sender: TObject);
  14.   private
  15.     procedure AppException(Sender: TObject; E: Exception);
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. uses StreamIO;
  24.  
  25. {$R *.lfm}
  26.  
  27. function ExceptionStack: string;
  28. var
  29.   Text: TStringStream;
  30.   F: TextFile;
  31. begin
  32.   Text := TStringStream.Create('');
  33.   try
  34.     AssignStream(F, Text);
  35.     Rewrite(F);
  36.     DumpExceptionBackTrace(F);
  37.     Result := Text.DataString;
  38.   finally
  39.     Text.Free;
  40.   end;
  41. end;
  42.  
  43. procedure TForm1.AppException(Sender: TObject; E: Exception);
  44. begin
  45.   Memo1.Text := E.Message + sLineBreak + ExceptionStack;
  46. end;
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. var
  50.   A: Integer = 100;
  51.   B: Integer = 0;
  52. begin
  53.   A := A div B;
  54. end;
  55.  
  56. procedure TForm1.FormCreate(Sender: TObject);
  57. begin
  58.   Application.OnException := @AppException;
  59. end;
  60.  
  61. end.
For an application with debug information, memo contains:
Code: [Select]
Division by zero
  $000000010002D274 line 53 of unit1.pp
  $0000000100122B12 line 2864 of include/control.inc
  $000000010013EA1A line 55 of include/buttoncontrol.inc
  $000000010013F1A3 line 169 of include/buttons.inc
  $000000010013E8E6 line 21 of include/buttoncontrol.inc
  $000000010000E035
  $00000001001219E3 line 2252 of include/control.inc
  $000000010011587D line 5397 of include/wincontrol.inc
  $000000010017DA2D line 112 of lclmessageglue.pas
  $00000001000F7899 line 2513 of win32/win32callback.inc
  $00000001000F805C line 2671 of win32/win32callback.inc
  $0000000100186B1E line 386 of win32/win32wsforms.pp
  $0000000076F19BBD
  $0000000076F16A5C
  $0000000076F16B61
  $000007FEFB590C73
  $000007FEFB5948B2

Paul_

  • Full Member
  • ***
  • Posts: 143
Re: Program crash output to file (runtime errors)
« Reply #6 on: April 14, 2018, 04:47:28 pm »
@ASerge thanks, it's working now.

Exceptions must be captured in the program (http://wiki.freepascal.org/Logging_exceptions), maybe with compiler command it would be simpler.
« Last Edit: April 14, 2018, 06:05:11 pm by Paul_ »

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Program crash output to file (runtime errors)
« Reply #7 on: April 14, 2018, 06:45:04 pm »
Exceptions must be captured in the program (http://wiki.freepascal.org/Logging_exceptions), maybe with compiler command it would be simpler.
No, it is either/or. If ASerge's solution works you *must* include some code in your program. That is not something that should be done from a compiler command line.
- My solution is for compilation errors
- ASerge's solution is for runtime errors.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Paul_

  • Full Member
  • ***
  • Posts: 143
Re: [Solved] Program crash output to file (runtime errors)
« Reply #8 on: April 14, 2018, 07:50:49 pm »
Ok, to make it more clear :)

If there is global function which have error/exception output to command line, it can also have output to file without including extra code, just with some compiler command.

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: [Solved] Program crash output to file (runtime errors)
« Reply #9 on: April 14, 2018, 09:25:23 pm »
Ok, to make it more clear :)

If there is global function which have error/exception output to command line, it can also have output to file without including extra code, just with some compiler command.
Yes, there is such a function in system that you can hook into. You still need some of the code from ASerge to obtain the frames if you use sysutils/structured exception handling.
https://www.freepascal.org/docs-html/rtl/system/writeerrorstostderr.html
Subsequently make sure you redirect sderr to file.

There are more options (ExitProc and the likes) but that is the easiest.
And it should not be done with a compiler command, you should write (very little) code for it.

example:
Code: Pascal  [Select][+][-]
  1. program erroutput;
  2. {$mode delphi}{$H+}
  3. var
  4.   d:integer = 0;
  5. begin
  6.   Assign(stderr,'errorlog.txt');
  7.   Rewrite(stderr);
  8.   writeln(100 div d);
  9. end.
Code: Bash  [Select][+][-]
  1. ~ $ cat errorlog.txt
  2. Runtime error 200 at $00010124
  3.   $00010124

Is that what you mean? Any errors will be redirected to errorlog.txt in this case (well, almost any....there are obvious exceptions, like errors accessing errorlog.txt...or out of resources or memory)
Under Windows you *must* set writeerrorstostderr to true. Under Linux this is not necessary.
« Last Edit: April 14, 2018, 09:57:59 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Paul_

  • Full Member
  • ***
  • Posts: 143
Re: [Solved] Program crash output to file (runtime errors)
« Reply #10 on: April 15, 2018, 12:58:29 am »
Yes, probably thats all what I need for now, my code lack some better error/warning/reporting structures, I'm using try .. except only for files handling..

Thanks Thaddy.

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: [Solved] Program crash output to file (runtime errors)
« Reply #11 on: April 15, 2018, 08:12:43 am »
Also have a look at the example here:
https://www.freepascal.org/docs-html-3.0.0/rtl/baseunix/fpdup2.html

I don't know if there is a windows equivalent.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018