Recent

Author Topic: How to use customize output in LazLogger?  (Read 1355 times)

artem101

  • Jr. Member
  • **
  • Posts: 70
How to use customize output in LazLogger?
« on: November 02, 2022, 03:49:57 pm »
For example, show time and message type:
Code: [Select]
12:00:00 INFO Reading finished
12:01:20 WARN Device busy
12:05:44 ERROR Decoding failed
12:10:55 DEBUG x=9 y=15

artem101

  • Jr. Member
  • **
  • Posts: 70
Re: How to use customize output in LazLogger?
« Reply #1 on: May 23, 2023, 03:43:50 pm »
As far as I understand, LazLogger doesn't support different types, but what about add current time at the begining of each line?

I found TLazLoggerFile.OnDebugLn, but this code doesn`t work:
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.OnDebugLnEvent(Sender: TObject; S: string;
  2.   var Handled: Boolean);
  3. begin
  4.   S := TimeToStr(Now) + ' ' + S;
  5.   DebugLn(S);
  6.   Handled:=True;
  7. end;  

Code: Pascal  [Select][+][-]
  1. DebugLogger.OnDebugLn:=@OnDebugLnEvent;  
« Last Edit: May 23, 2023, 03:45:59 pm by artem101 »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9213
  • Debugger - SynEdit - and more
    • wiki
Re: How to use customize output in LazLogger?
« Reply #2 on: May 23, 2023, 04:00:59 pm »
If you do "Debugln" in the callback, you create an endless recursive loop.

You would need to temporary unset the event.
Or have a flag, that you are calling from inside the event, and return "handled := false" in that case

Code: Pascal  [Select][+][-]
  1. var InDbglnCb: boolean = false; // threadvar if you use threads
  2. procedure TMainForm.OnDebugLnEvent(Sender: TObject; S: string; var Handled: Boolean);
  3. begin
  4.   Handled := not InDbglnCb;
  5.   if InDbglnCb then
  6.     exit;
  7.   InDbglnCb := True;
  8.   try
  9.     S := TimeToStr(Now) + ' ' + S;
  10.     DebugLn(S);
  11.   finally
  12.     InDbglnCb := False;
  13.   end;
  14. end;  


It probably be a good idea,  to add a callback, that can modify the line/text. Feel free to create a feature request on the bug tracker

artem101

  • Jr. Member
  • **
  • Posts: 70
Re: How to use customize output in LazLogger?
« Reply #3 on: May 23, 2023, 08:38:13 pm »
If you do "Debugln" in the callback, you create an endless recursive loop.

You would need to temporary unset the event.
Thank you. This code works:
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.OnDebugLnEvent(Sender: TObject; S: string;
  2.   var Handled: Boolean);
  3. var
  4.   Callback:  procedure(Sender: TObject; S: string; var Handled: Boolean) of object;
  5. begin
  6.   S := Format('[%s]   %s', [FormatDateTime('hh:nn:ss.zzz', Now()), S]);
  7.   Callback := DebugLogger.OnDebugLn;
  8.   DebugLogger.OnDebugLn := Nil;
  9.   try
  10.     DebugLn(S);
  11.   finally
  12.     DebugLogger.OnDebugLn := Callback;
  13.   end;
  14.   Handled := True;
  15. end;    

It probably be a good idea,  to add a callback, that can modify the line/text. Feel free to create a feature request on the bug tracker
You are right. It will be better to pass S variable by reference to OnDebugLn callback. Perhaps I will make contribution to Lazarus repository.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9213
  • Debugger - SynEdit - and more
    • wiki
Re: How to use customize output in LazLogger?
« Reply #4 on: May 23, 2023, 08:55:05 pm »
You are right. It will be better to pass S variable by reference to OnDebugLn callback. Perhaps I will make contribution to Lazarus repository.

That will be a 2nd callback. So compatibility wont break.  Maybe "OnDebuglnEx".

Also have to think what other params may be useful.

But patches will be welcome.

artem101

  • Jr. Member
  • **
  • Posts: 70
Re: How to use customize output in LazLogger?
« Reply #5 on: May 23, 2023, 09:05:10 pm »
That will be a 2nd callback. So compatibility wont break.

Oh, I missed this.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9213
  • Debugger - SynEdit - and more
    • wiki
Re: How to use customize output in LazLogger?
« Reply #6 on: May 28, 2023, 09:52:29 pm »
I added a new callback in commit 34053f3e3294f003a109913176a8acc446a12f3d https://gitlab.com/freepascal.org/lazarus/lazarus/-/commit/34053f3e3294f003a109913176a8acc446a12f3d  (and the next commit)

Code: Pascal  [Select][+][-]
  1.     property  OnDebugLnEx: TLazLoggerWriteExEvent read FOnDebugLnEx write FOnDebugLnEx;
  2.     property  OnDbgOutEx:  TLazLoggerWriteExEvent read FOnDbgOutEx write FOnDbgOutEx;

Code: Pascal  [Select][+][-]
  1. type
  2.   TLazLoggerWriteExEventInfo = record
  3.     Group: PLazLoggerLogGroup; // if only one group / remember nestlevel count
  4.     DbgOutAtBOL: Boolean;      // Only for DbgOut, True if first segment in new line (that is LogIndent will be added, otherwise it wont)
  5.   end;
  6.   TLazLoggerWriteExEvent = procedure(Sender: TObject; var LogTxt, LogIndent: string; var Handled: Boolean; const AnInfo: TLazLoggerWriteExEventInfo) of object;

- The LogIndent has not yet been added to the LogText.
- You can modify LogIndent and/or LogText  (Or combine them, and empty the other)
- Handled works as before

AnInfo is an record => that means more fields can be added in future, without breaking anything.

About the "Group", it will be present if you do
Code: Pascal  [Select][+][-]
  1. Debuln(LogGroup, 'Foo bar');
  2. Debuln(LogGroup, ['Foo bar', 123]);
or similar.

But not if you do
Code: Pascal  [Select][+][-]
  1. Debuln(LogGroup or OtherLogGroup, 'Foo bar');
Since then more than one group is in play. (May change later....)


Please test, and let me know if you find any issues with it. Or if you can think of any improvement.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to use customize output in LazLogger?
« Reply #7 on: May 29, 2023, 09:27:05 am »
Martin, many thanks for adding this.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9213
  • Debugger - SynEdit - and more
    • wiki
Re: How to use customize output in LazLogger?
« Reply #8 on: May 29, 2023, 11:22:53 am »
I forgot: If anyone using this could find the time, and help updating the wiki.... thanks

artem101

  • Jr. Member
  • **
  • Posts: 70
Re: How to use customize output in LazLogger?
« Reply #9 on: May 30, 2023, 11:38:43 am »
I added a new callback in commit

Good work! Thank you.

 

TinyPortal © 2005-2018