Recent

Author Topic: Using LazLogger to Write Log File to Terminal  (Read 1453 times)

c_knowles4834

  • New Member
  • *
  • Posts: 25
Using LazLogger to Write Log File to Terminal
« on: September 04, 2021, 05:26:15 pm »
Hello all,

Recently, I have been working on a simple calculator program and found the need to trace the inputs the user gives the program and corresponding outputs for debugging purposes. I found that Lazarus comes with a unit called "LazLogger" and it seems like it will create a log file that can be displayed in a separate terminal window; however, I am not sure how to configure and implement the necessary changes to my source code to fulfill this function.  I would like the input and output to be displayed in a terminal window like so:

 
Code: Pascal  [Select][+][-]
  1.  Start of Calc Trace
  2. <Date MMDDYYYY= "8/22/2021"/>
  3. <Time HH:MM:SS XM= "15:01:05"/>
  4. 1 btn3_OnClick 3
  5. 2 btnDec_OnClick 3.
  6. 3 btn1_OnClick 3.1
  7. 4 btn4_OnClick 3.14
  8. 5 btn1_OnClick 3.141
  9. 6 btn5_OnClick 3.1415
  10. 7 btn9_OnClick 3.14159

Does anyone know if LazLogger has this capability or will I have to create some new source code separate from the LazLogger unit to fulfill this function?

Thanks!

Below are my system parameters:

macOS: BigSur 11.5.2
Lazarus 2.0.12 x86_64-macosx
fpc-3.2.0.intel-macosx
fpc-src-3.2.0-2



Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7529
  • Debugger - SynEdit - and more
    • wiki
Re: Using LazLogger to Write Log File to Terminal
« Reply #1 on: September 04, 2021, 05:56:59 pm »
https://wiki.lazarus.freepascal.org/LazLogger
See units LazLogger and LazLoggerBase.

Default is to write to the terminal, if a terminal is open. If no terminal is open, then nothing is written, unless a logfile is given.

You need to insert "debugln" into your source.
"btn3_onClick" is just text. It will not be done automatically.
But you can use code templates to pre-fill -> tools > code templates
and define "d" as:
Code: Text  [Select][+][-]
  1. debugln(['$ProcedureName() '|]);


When running from the IDE, you should be able to set a terminal in the global debugger settings (Tools > Options)
« Last Edit: September 04, 2021, 05:59:27 pm by Martin_fr »

c_knowles4834

  • New Member
  • *
  • Posts: 25
Re: Using LazLogger to Write Log File to Terminal
« Reply #2 on: September 04, 2021, 07:50:35 pm »
Thanks for the help Martin!

Could I ask though for an example of how to implement "debugln" into my source code? 

I am quite new to Pascal, Lazarus, and programming at this level to fully understand what I must do in the source code to implement your suggestion.   

I apologize in advance for any inconvinence.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7529
  • Debugger - SynEdit - and more
    • wiki
Re: Using LazLogger to Write Log File to Terminal
« Reply #3 on: September 04, 2021, 08:12:45 pm »
Code: Pascal  [Select][+][-]
  1. uses LazLogger;
  2. procedure Foo;
  3. begin
  4.   debugln('Doing Foo');
  5.   debugln(['Doing Foo', dbgs(SomeInt)]);
  6.   debugln('Forrmat like %s %i', ['Doing Foo', SomeInt]);
  7. end;
  8.  


Make sure to run the app from a console.

replace "uses LazLogger; " with "uses LazLoggerDummy; " and no output will be generated



It is similar to "writeln". But, it does not give an error if stdout is closed. On the otherhand, it is a normal function, so some of the magic of writeln is not avail.


Also you can control it when calling your app

yourapp --debug-log=file
(On Mac you may need to specify inside the bundle / no idea)
« Last Edit: September 04, 2021, 08:15:01 pm by Martin_fr »

c_knowles4834

  • New Member
  • *
  • Posts: 25
Re: Using LazLogger to Write Log File to Terminal
« Reply #4 on: September 05, 2021, 12:15:28 am »
Thanks Martin!

I got it to work flawlessly with your suggestions.  But, I had a question about a message I get when compiling my program:

"vsmcalculatorunit.pas(154,8) Note: Call to subroutine "function GetDebugLogger:TLazLogger;" marked as inline is not inlined"

What does that mean?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7529
  • Debugger - SynEdit - and more
    • wiki
Re: Using LazLogger to Write Log File to Terminal
« Reply #5 on: September 05, 2021, 01:48:03 am »
"marked as inlined ..."

If you have something like this:
Code: Pascal  [Select][+][-]
  1. function foo: integer; inline;

It means the "inline" ask the compiler to see if it could be inlined.
But there can be various reasons why this may not be possible.

Apparently GetDebugLogger was not possible to be inlined. So a note is given about it. Usually not a problem.


 

TinyPortal © 2005-2018