#1 2021-03-12 08:33:15

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,564
Website

TSynLog enter/leave without using interfaces

Task I tries to solve is to expose SynLog.Enter / Leave functionality to the external C library (JS in my case).
In pascal program I have an instance of TSynLog

  logger := TSynLog.Add;

Now I want to expose to a C program 2 function logEnter(pText: PChar) and logLeave() to be used as such:

// inside C library
logEnter('someText')
  ......
logLeave()

so logging of both Pascal code and external library code share the same log recursion depth.

Currently I cant do this - some refactoring of TSynLog required  - I need to have something like TSynLog.manualEnter / TSynLog.manualLeave public methods and modify existed TSynLog.Enter / TSynLog._Release to internally use them.
This allows Pascal code to continue works with ISynLog := logger.Enter and uses auto-leave feature and function what I expose to C library will internally call manualEnter / manualLeave

@ab - Is such refactoring theoretically possible? Can I ask You for it, or I can create a MR it you confirm an idea

Last edited by mpv (2021-03-12 08:34:00)

Offline

#2 2021-03-12 10:57:54

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,375
Website

Re: TSynLog enter/leave without using interfaces

Yes, of course it is possible.
The only trick is that ISynLog _addref/_release actually do some process, so full reuse of the Enter class method is not possible.

But we have to make sure that the Leave methods are called exactly the same time than Enter methods.
It is done automatically thanks to the interface, and should be taken care of from manual code.

I have added it to mORMot 2 - https://github.com/synopse/mORMot2/comm … 7e18b56fd1
You can prepare a backport PR for mORMot 1 if you need.

Offline

#3 2021-03-12 13:32:01

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,564
Website

Re: TSynLog enter/leave without using interfaces

Created a backport for mORMot1
I expose enter/leave functions to JS and verify - works like a charm!
To protect mysef from enter without leave I do not expose enter/leave directly to JS developers but create a wrapper:

function wrapEnterLeave(enterText, methodImpl) {
    return function(ctx) {
      logEnter(enterText)
      try {
        methodImpl(ctx)
      } finally {
        logLeave()
      }
    }
  }

Offline

#4 2021-03-12 15:30:04

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,375
Website

Re: TSynLog enter/leave without using interfaces

The JS wrapper is a very elegant solution.

Offline

Board footer

Powered by FluxBB