Recent

Author Topic: How to use the Event Log?  (Read 8043 times)

WooBean

  • Sr. Member
  • ****
  • Posts: 277
How to use the Event Log?
« on: May 31, 2023, 06:10:09 pm »
Lazarus IDE configuration contains an option Event Log as a part of IDE Options/Debugger.
I can not find any clue what is it for and how to use it.
Any pointer?

See attached file from Lazarus 2.2.4 (Win 64).
« Last Edit: May 31, 2023, 06:11:54 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10557
  • Debugger - SynEdit - and more
    • wiki
Re: How to use the Event Log?
« Reply #1 on: May 31, 2023, 06:36:59 pm »
Menu: View > Debug Windows > Event Log

https://wiki.lazarus.freepascal.org/IDE_Window:_Event_Log (wiki is outdated)

WooBean

  • Sr. Member
  • ****
  • Posts: 277
Re: How to use the Event Log?
« Reply #2 on: May 31, 2023, 07:08:23 pm »
Menu: View > Debug Windows > Event Log

https://wiki.lazarus.freepascal.org/IDE_Window:_Event_Log (wiki is outdated)

Thanks for a clue.
I tried to follow it by changing debugger from default FpDebug (what do not promise a lucky way) to GDB (just to follow a wiki guidance) but ... ups.
I have got the below:

 
« Last Edit: May 31, 2023, 08:07:04 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10557
  • Debugger - SynEdit - and more
    • wiki
Re: How to use the Event Log?
« Reply #3 on: May 31, 2023, 09:55:07 pm »
I tried to follow it by changing debugger from default FpDebug (what do not promise a lucky way) to GDB (just to follow a wiki guidance) but ... ups.

Which page on the wiki? That page is outdated.

The current recommendation for debugger:

On Windows and Linux: FpDebug
On Mac: LLDB with FpDebug

Use GDB (or gdb server), only if you
- Do Remote debugging (to another machine / using gdbserver)
- Do not have an Intel/AMD (compatible) cpu. (Except on Mac, where M2 is supported by the above recommendation)
  (And except for cross AVR, for which there is an FpDebug variant)
- Have a 32bit IDE but want to cross debug a 64bit application (better get the 64bit IDE)
- On Mac, and have this issue: https://forum.lazarus.freepascal.org/index.php/topic,61261.0.html
- Very few other edge cases.



As for the error: That is (99% likely) a bug in GDB. Nothing we can do (hence we wrote FpDebug, because at least with that: We can do).

In case you want to experiment further with GDB: Make sure you did not use "Dwarf 3" => This increases the chances of crashing GDB.
"Dwarf 2 with sets" is better.
Then try different GDB versions, sometimes older are better. (For Windows we have a few for download, on our sourceforge page).

WooBean

  • Sr. Member
  • ****
  • Posts: 277
Re: How to use the Event Log?
« Reply #4 on: June 01, 2023, 09:13:35 am »
Martin, thanks for trying to help.
Which page on the wiki? -Just to explain a wiki jumping, they were as follows -
1: https://wiki.lazarus.freepascal.org/IDE_Window:_Event_Log (last edited 2 February 2021, at 05:30), then as it starts from "Important" link to
2: https://wiki.lazarus.freepascal.org/Debugger_Setup (last edited 13 May 2022, at 10:07) and then  (from "See also") to
3: https://wiki.lazarus.freepascal.org/IDE_Window:_DebuggerGeneralOptionsFrame (edited 11 October 2019, at 19:10) and
4: https://wiki.lazarus.freepascal.org/IDE_Window:_DebuggerClassOptionsFrame (edited 5 February 2021, at 18:21) which points back to [2:].

TODO for me - a lot of jumping inside IDE and documentation (including  the wiki) ahead.

PS. I'm trying to deeply and effectively debug  Lazarus (2.2.4) project compiled with default options for Debugging IDE:
-gw3  //Generate DWARFv3 debug information
-gl   //Use line info unit (show more info with backtraces)
-gh   //Use heaptrace unit (for memory leak/corruption debugging)
-gt   //Trash local variables (to detect uninitialized uses; multiple 't' changes
-Co   //Check overflow of integer operations
-Cr   //Range checking
-Ci   //IO-checking
-Sa   //Turn on assertions
« Last Edit: June 01, 2023, 09:26:11 am by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

piola

  • Full Member
  • ***
  • Posts: 147
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: How to use the Event Log?
« Reply #5 on: April 15, 2024, 10:15:10 pm »
The event log seems to be a quite useful feature. I recently stumbled upon it by chance. However there are two "but"s which are a bit irritating:

1. As written on the wiki page, OutputDebugString requires the Windows unit and is platform-specific. I'm not a big fan of including the Windows unit and try to avoid it whenever possible.

2. TCustomApplication.Log would be a really great place to act as a default for logging. But I don't know how to handle it ("if it has been implemented") so that log messages appear in the Lazarus event log.

So my request is to provide a minimal application which shows how to connect TCustomApplication.Log to the Lazarus event log.

Thank you!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10557
  • Debugger - SynEdit - and more
    • wiki
Re: How to use the Event Log?
« Reply #6 on: April 15, 2024, 10:34:56 pm »
Well "logging" (application sent messages) is only one aspect of the event log. (and windows specific)
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses windows;
  3. begin
  4.   OutputDebugStringA('abc');
  5. end.
  6.  

You may need to configure the event log to show the message  / right click and look at the options.




You can also use "breakpoint actions"
See bottom of: https://wiki.freepascal.org/IDE_Window:_Breakpoints#Breakpoint_properties

Mind that this is rather slow, if you have a lot of messages...
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. procedure MyLog(a: Ansistring);
  3. begin
  4.   a := a;
  5. end;
  6.  
  7. begin
  8.   MyLog('abc');
  9.   MyLog('def');
  10. end.

With a breakpoint on line 4, that
- does not break
- Evaluate the expression "a"

Again, ensure you have the right options enabled for the event log.




In the above case, you can also use debug history (take snapshot). It will snapshot all your watches.  But you must select each entry to read any of the recorded data. So while you get more data, it is less convenient to read.
https://wiki.freepascal.org/IDE_Window:_Debug_History

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10557
  • Debugger - SynEdit - and more
    • wiki
Re: How to use the Event Log?
« Reply #7 on: April 15, 2024, 10:38:41 pm »
IIRC there is somewhere an independent "debug server" (not using the event log). It has its own server to show the messages, and a unit with methods to sent the messages from the debugger app. But you may as well log to a file and use "tail". (though debug server, wouldn't have "flush" delays).

However, I have this only from hear-say, and can't provide any further details....

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10557
  • Debugger - SynEdit - and more
    • wiki
Re: How to use the Event Log?
« Reply #8 on: April 15, 2024, 10:45:42 pm »
If you are looking for such a logging facility, because you have data (variable/properties) that either due to their type or accessor are hard to get in the watches window...

That is a matter of finding out once, how to get the date. E.g.: Typecasts, or finding the field(s) if a property can't be called.

FpDebug and DWARF-3
TStringlist can be shown with
Code: Text  [Select][+][-]
  1. (l.FList)^[0..l.FCount-1].FString

"Debug Inspector" can be used to build such an expression.

https://wiki.freepascal.org/FpDebug-Watches-Intrinsic-Functions#Array_slice_MyArray.5Bn..m.5D_with_mapping


Further more you may try (should work, but haven't tested on all platforms)

Code: Text  [Select][+][-]
  1. l.Get(1)
  2. l.GetTextStr()

You need to enable function calling on each watch, and globally in the Tool>Options>Debugger
« Last Edit: April 15, 2024, 10:54:23 pm by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10557
  • Debugger - SynEdit - and more
    • wiki
Re: How to use the Event Log?
« Reply #9 on: April 15, 2024, 10:46:52 pm »
For logging to a file, I usually use lazlogger (search wiki).

It has DebuglnEnter/DebuglnExit to log with indent.

But it does not log to the debugger. Only to files or console

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10557
  • Debugger - SynEdit - and more
    • wiki
Re: How to use the Event Log?
« Reply #10 on: April 15, 2024, 10:58:34 pm »
So my request is to provide a minimal application which shows how to connect TCustomApplication.Log to the Lazarus event log.

Sorry, left that for last.

I haven't worked with TCustomApplication.Log.

But looking at it, you have to override
Code: Pascal  [Select][+][-]
  1. Procedure DoLog(EventType : TEventType; const Msg : String);  virtual;

And call "OutputDebugStringA".

No idea how to do that on non Windows.


Code: Pascal  [Select][+][-]
  1.   TMyApplication = class(TCustomApplication)
  2.   protected
  3.     procedure DoRun; override;
  4.     procedure DoLog(EventType: TEventType; const Msg: String); override;
  5.   public
  6.     constructor Create(TheOwner: TComponent); override;
  7.     destructor Destroy; override;
  8.     procedure WriteHelp; virtual;
  9.   end;
  10.  
Code: Pascal  [Select][+][-]
  1. procedure TMyApplication.DoLog(EventType: TEventType; const Msg: String);
  2. begin
  3.   OutputDebugStringA(PChar(Msg));
  4. end;
  5.  

and add "uses Windows"


Depending on your needs you can convert the string to widestring first, and then use OutputDebugStringW(PWideChar(WideMsg));
« Last Edit: April 15, 2024, 11:03:06 pm by Martin_fr »

piola

  • Full Member
  • ***
  • Posts: 147
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: How to use the Event Log?
« Reply #11 on: April 15, 2024, 11:07:22 pm »
This is great, thank you so much for the ideas and instructions.

What I would like to have is:

Code: [Select]
Application.Log (etDebug, 'whatever message');
to appear in the event log.

This doesn't work (well, at least for me). The wiki page mentioned afore says about TCustomApplication.Log: "if it's implemented", and obviously it ain't.

When I create a console application, there is a custom class created which is derived from TApplication. And the Application instance is explicitly created:

Code: [Select]
var
  Application: TMyApplication;
begin
  Application := TMyApplication.Create(nil);
  Application.Title := 'My Application';
  Application.Run;
  Application.Free;
end.

So I know how I could implement TMyApplication here.

But I have a GUI application for which I don't know how the Application instance is created and how I can provide a custom class:

Code: [Select]
begin
  RequireDerivedFormResource := True;
  Application.Scaled := True;
  Application.Initialize;
  Application.CreateForm(TRRMainWindow, MainWindow);
  Application.Run;
end.

So the magic hides the details from me. An event handler TCustomApplication.OnLog would be handy, but there doesn't seem to be one.

So my question is, how do I achieve something like this in a GUI app:

Code: [Select]
type TMyApplication = class(TApplication)
  procedure Log (EventType: TEventType; const Msg: string); override;
end;

procedure TMyApplication.Log (EventType: TEventType; const Msg: string);
begin
  {$ifdef mswindows}
  OutputDebugString (Msg);
  {$endif}
end;

begin
  Application := TMyApplication.Create (nil); // <----- this is the interesting question :)
  ...
end.


Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10557
  • Debugger - SynEdit - and more
    • wiki
Re: How to use the Event Log?
« Reply #12 on: April 15, 2024, 11:31:29 pm »
But I have a GUI application for which I don't know how the Application instance is created and how I can provide a custom class:
Ah, that is a good question...
I don't know...

Quote
So the magic hides the details from me. An event handler TCustomApplication.OnLog would be handy, but there doesn't seem to be one.

Indeed, IMHO there should be.

I would recommend to add a bug / feature request on the bug tracker.

IMHO, it classifies as bug, since you can't override the class (not in a safe way).

piola

  • Full Member
  • ***
  • Posts: 147
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: How to use the Event Log?
« Reply #13 on: April 16, 2024, 12:18:19 am »
Ok, thank you 👍

So my question wasn't just as stupid as I thought at first :D

n7800

  • Full Member
  • ***
  • Posts: 175
Re: How to use the Event Log?
« Reply #14 on: April 20, 2024, 12:47:25 am »
But I have a GUI application for which I don't know how the Application instance is created

If an object is not created by this unit, then it is created in one of the used units. There are only three units connected: Interfaces, Forms and unit1. And since Application is declared in the Forms unit (just press [Alt+Up]), you can see its creation in its initialization section:

lcl/forms.pp

how I can provide a custom class:

I've seen this way to "create" your own application class:

ide/lazbuild.lpr

However, I'm not sure if this is a good way, especially for a graphical application. Perhaps some kind of compilation directive -dOwnAppClass would be useful for the Forms unit so that the user can create the class manually?

 

TinyPortal © 2005-2018