[fpc-pascal] Question: Is there a global callback, when a thread gets terminated?
Martin Frb
lazarus at mfriebe.de
Tue Feb 12 20:15:46 CET 2019
On 12/02/2019 19:46, Marc Santhoff wrote:
> On Tue, 2019-02-12 at 17:36 +0100, Sven Barth via fpc-pascal wrote:
>
>> Not every LazLogger user knows that their code is run in a thread. And not
>> every thread user knows that the code they are using is using LazLogger in
>> turn.
> Ok.
>
> But if used that way, would LazLogger need to notice at all?
>
> As I understand it the use case will be to discriminate and not mix up the log
> entries from different threads. If the user does not want this at all, why
> bother inside the logger code?
>
> I can imagine race conditions or misplaced log lines. If used in debugging
> code this is mostly irrelevant, imho...
Yes it is not critical, and may be made optional (global switch in main
thread / or different unit in uses (once in main unit))
The current idea I am pursuing is this.
LazLogger already has
DebuglnEnter
DebuglnExit
which add/remove indent.
So your log can have lines that are indented.
procedure DoFoo;
begin
DebuglnEnter('start foo');
CallBar;
CallSome;
DebuglnExit('end foo');
end;
The called code can contain debugln, and all of them will be nicely
indented.
Of course that is limited in threading. Should the code in other threads
also log indented? Worse, if the other thread goes through a
DebuglnExit, when the last DebuglnEnter was not in that other thread,
then the indent becomes entirely messed up.
Maybe I will also combine this with prefixing the thread ID. But for
that I do not need extra objects.
And yes, at first it would like that the indent needs no extra object
either. just have a global
threadvar LazLoggerIndent: integer;
But that fails, if there are (in the main thread, or in any one thread)
more than one logger. (Yes I do have that, eg in testcase where the test
logs independent of the tested code)
Once there is more than one instance of logging (in the main thread),
then which of them gets to use the global threadvar?
Well on 2nd thought it could be done. (Discussing an issue has a
tendency to bring up new ideas)
the functional coded "debugln" uses the main instance. So that could
have a flag, to use the global var.
2ndary loggers must anyway be called via there object, and therefore do
not have the benefit of automatic wrappers.
So yes maybe I will go that way. It is a bit against OO paradigm. But it
should yield the same result.
Thanks for all the feedback.
Out of interest I still keep the mem-manager question....
More information about the fpc-pascal
mailing list