How to direct DumpExceptionBackTrace output to a file? lazarus / 8-Other ---------------------------------------------------------------------- jl · 2009-07-13 04:45 I've been exploring with DumpExceptionBackTrace to help my debugging, and trying to direct the output from DumpExceptionBackTrace output to a file? I'm able to direct DumpExceptionBackTrace output to a console in Linux from my daemon, but not able to do so to a file on both windows and linux.  Googled and looked at all articles, tried the following, but it doesn't work. Code: [Select] procedure TDaemon1.writelog(logtype, message: string); var   f: TextFile;   sLogFile :string; begin try   sLogFile:=sAppPath+'daemon.log';   AssignFile(f, sLogFile);   if FileExists(sLogFile) then Append(f) else ReWrite(f);   WriteLn(f, formatdatetime('yyyy_mm_dd_hh_nn_ss',now)+#9+message);   DumpExceptionBackTrace;    CloseFile(f);  except     on E: Exception do     begin      WriteLn(formatdatetime('yyyy_mm_dd_hh_nn_ss',now)+#9+message);      DumpExceptionBackTrace;     end;   end; end;    I also found the procedure for DumpExceptionBackTrace within \lazarus\fpc\2.2.5\source\rtl\inc\system.inc Code: [Select]{$ifdef FPC_HAS_FEATURE_EXCEPTIONS} procedure DumpExceptionBackTrace(var f:text); var   FrameNumber,   FrameCount   : longint;   Frames       : PPointer; begin   if RaiseList=nil then     exit;   WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));   FrameCount:=RaiseList^.Framecount;   Frames:=RaiseList^.Frames;   for FrameNumber := 0 to FrameCount-1 do     WriteLn(f,BackTraceStrFunc(Frames[FrameNumber])); end; {$endif FPC_HAS_FEATURE_EXCEPTIONS} Here's more information: http://www.freepascal.org/docs- html/rtl/system/dumpexceptionbacktrace.html Grateful for any pointers. ---------------------------------------------------------------------- Vincent Snijders · 2009-07-13 08:30 Are you sure you have an open console on windows? Otherwise Code: [Select]WriteLn(formatdatetime('yyyy_mm_dd_hh_nn_ss',now)+#9+message); will cause an exception in the exception handler. ---------------------------------------------------------------------- jl · 2009-07-13 13:35 Quote from: Vincent Snijders on July 13, 2009, 10:30:33 amAre you sure you have an open console on windows? Otherwise Code: [Select]WriteLn(formatdatetime('yyyy_mm_dd_hh_nn_ss',now)+#9+message); will cause an exception in the exception handler. no, as it's a service, but i don't see any exception.  however, i tried debugln also, and the service hangs when i use that.  thks. ---------------------------------------------------------------------- Vincent Snijders · 2009-07-13 13:41 I am sorry, your response is not clear. Does writeln work or not in a service? ---------------------------------------------------------------------- jl · 2009-07-13 15:30 The following works, i'm able to write the content of message to sLogfile (pls refer to the whole procedure i posted previously).  It's not possible for a Win32 service to WriteLn to a console, but it possible to write to a file: Code: [Select]  WriteLn(f, formatdatetime('yyyy_mm_dd_hh_nn_ss',now)+#9+message);   DumpExceptionBackTrace;   What i'm not able to do is to write the output from DumpExceptionBackTrace to a file. As for the WriteLn within the exception - take note, there's no 'f' parameter for this WriteLn - it has no 'visible' effect in a Win32 service.  I only use it for Linux daemon - the same code is used for both binaries. Code: [Select] except     on E: Exception do     begin      WriteLn(formatdatetime('yyyy_mm_dd_hh_nn_ss',now)+#9+message);      DumpExceptionBackTrace;     end;   end; ---------------------------------------------------------------------- Vincent Snijders · 2009-07-13 16:21 Well, on win32 gui applications, a writeln causes an exception, I just wanted to be sure. ---------------------------------------------------------------------- jl · 2009-07-14 09:58 Quote from: Vincent Snijders on July 13, 2009, 06:21:49 pmWell, on win32 gui applications, a writeln causes an exception, I just wanted to be sure. Yes, writeln does not generate an exception in a win32 service. ---------------------------------------------------------------------- Vincent Snijders · 2009-07-14 11:31 Did you read: http://wiki.lazarus.freepascal.org/Lazarus_Faq#How_can_I _see_debug_output.3F ---------------------------------------------------------------------- jl · 2009-07-14 16:24 Quote from: Vincent Snijders on July 14, 2009, 01:31:21 pmDid you read: http://wiki.lazarus.freepascal.org/Lazarus_Faq#How_can_I_see_deb ug_output.3F Yes, i also tried DebugLn, it outputs to console. ---------------------------------------------------------------------- Vincent Snijders · 2009-07-14 18:45 Oh, then you have an usual system configuration that ignores command line parameters and environment variables. I am sorry, then your only options is to change the lclproc code and hardcode the file name, instead of reading it from the command line parameters or the environment. ---------------------------------------------------------------------- jl · 2009-07-14 18:51 Thanks for replying, I can write text string to file using WriteLn on both Linux and Win32, i.e. WriteLn ( var FileHandle  : TextFile; text: String) ; The problem which I'm facing is that I can't direct the output of DumpExceptionBackTrace to the FileHandle. ---------------------------------------------------------------------- Paul Ishenin · 2009-07-15 23:07 I wrote previously: Code: [Select]DumpExceptionBackTrace(var f:text) It has f: file argument. Have you tried DumpExceptionBackTrace(f);? ---------------------------------------------------------------------- jl · 2009-07-16 05:26 Quote from: Paul Ishenin on July 16, 2009, 01:07:14 amI wrote previously: Code: [Select]DumpExceptionBackTrace(var f:text) It has f: file argument. Have you tried DumpExceptionBackTrace(f);? I tried that, it generates an exception: Code: [Select]daemonunit1.pas(51,25) Error: Wrong number of parameters specified for call to "DumpExceptionBackTrace" ---------------------------------------------------------------------- Vincent Snijders · 2009-07-16 06:50 Check if you use DumpExceptionBackTrace from the correct unit. ---------------------------------------------------------------------- jl · 2009-07-17 13:22 Quote from: Vincent Snijders on July 16, 2009, 08:50:43 amCheck if you use DumpExceptionBackTrace from the correct unit. Thanks Vincent, i removed LCLProc and it worked.  I put that in them after reading some article, I guess it's the wrong DumpExceptionBackTrace.