Skip to content

Commit af2c5be

Browse files
author
Arnaud Bouchez
committed
core: new TSynLogFamily.DisableCurrentThread property
- allow to temporarly avoid logging in the current thread - after setting true to this property, should eventually be reset to false - won't affect exceptions logging, as one would expect for safety reasons
1 parent 52ff787 commit af2c5be

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

src/core/mormot.core.log.pas

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,10 @@ TSynLogFamily = class;
646646
/// available TSynLogThreadInfo.Flags definition
647647
// - tiExceptionIgnore store TSynLogFamily.ExceptionIgnoreCurrentThread
648648
// property (used only if NOEXCEPTIONINTERCEPT conditional is undefined)
649+
// - tiTemporaryDisable store TSynLogFamily.DisableCurrentThread property
649650
TSynLogThreadInfoFlag = (
650-
tiExceptionIgnore);
651+
tiExceptionIgnore,
652+
tiTemporaryDisable);
651653
/// TSynLogThreadInfo.Flags property set type definition
652654
TSynLogThreadInfoFlags = set of TSynLogThreadInfoFlag;
653655

@@ -796,6 +798,11 @@ TSynLogFamily = class
796798
// to this flag
797799
property ExceptionIgnoreCurrentThread: boolean
798800
index tiExceptionIgnore read GetCurrentThreadFlag write SetCurrentThreadFlag;
801+
/// allow to temporarly avoid logging in the current thread
802+
// - after setting true to this property, should eventually be reset to false
803+
// - won't affect exceptions logging, as one would expect for safety
804+
property DisableCurrentThread: boolean
805+
index tiTemporaryDisable read GetCurrentThreadFlag write SetCurrentThreadFlag;
799806
/// you can let exceptions be ignored from a callback
800807
// - if set and returns true, the given exception won't be logged
801808
// - execution of this event handler is protected via the logs global lock
@@ -1104,7 +1111,7 @@ TSynLog = class(TObject, ISynLog)
11041111
procedure RaiseDoEnter;
11051112
procedure LockAndPrepareEnter(nfo: PSynLogThreadInfo;
11061113
microsecs: PInt64); // no profit inlining
1107-
procedure LockAndDisableExceptions; // no profit inlining
1114+
function LockAndDisableExceptions: boolean; // no profit inlining
11081115
procedure LogEnter(nfo: PSynLogThreadInfo; inst: TObject; txt: PUtf8Char
11091116
{$ifdef ISDELPHI} ; addr: PtrUInt = 0 {$endif});
11101117
procedure LogEnterFmt(nfo: PSynLogThreadInfo; inst: TObject;
@@ -4783,25 +4790,31 @@ procedure SetThreadInfoAndThreadName(log: TSynLog; nfo: PSynLogThreadInfo);
47834790
log.AddLogThreadName;
47844791
end;
47854792

4786-
procedure TSynLog.LockAndDisableExceptions;
4793+
function TSynLog.LockAndDisableExceptions: boolean;
47874794
var
47884795
nfo: PSynLogThreadInfo;
47894796
begin
4790-
nfo := @PerThreadInfo; // access the threadvar - inlined GetThreadInfo
4791-
if PInteger(nfo)^ = 0 then // first access
4792-
InitThreadNumber(nfo);
4793-
if not (logInitDone in fFlags) then
4794-
LogFileInit(nfo); // run once, to set start time and write headers
4795-
FillInfo(nfo, nil); // syscall outside of GlobalThreadLock
4796-
GlobalThreadLock.Lock;
4797-
SetThreadInfoAndThreadName(self, nfo);
4798-
{$ifndef NOEXCEPTIONINTERCEPT}
4799-
// any exception within logging process will be ignored from now on
4800-
fThreadInfoBackup := nfo^.Flags;
4801-
// caller should always eventually perform in its finally ... end block:
4802-
// fThreadInfo^.Flags := fThreadInfoBackup;
4803-
include(nfo^.Flags, tiExceptionIgnore);
4804-
{$endif NOEXCEPTIONINTERCEPT}
4797+
nfo := @PerThreadInfo; // access the threadvar
4798+
if not (tiTemporaryDisable in nfo^.Flags) then
4799+
begin
4800+
if PInteger(nfo)^ = 0 then
4801+
InitThreadNumber(nfo); // first access - inlined GetThreadInfo
4802+
if not (logInitDone in fFlags) then
4803+
LogFileInit(nfo); // run once, to set start time and write headers
4804+
FillInfo(nfo, nil); // syscall outside of GlobalThreadLock
4805+
GlobalThreadLock.Lock;
4806+
SetThreadInfoAndThreadName(self, nfo);
4807+
{$ifndef NOEXCEPTIONINTERCEPT}
4808+
// any exception within logging process will be ignored from now on
4809+
fThreadInfoBackup := nfo^.Flags;
4810+
// caller should always eventually perform in its finally ... end block:
4811+
// fThreadInfo^.Flags := fThreadInfoBackup;
4812+
include(nfo^.Flags, tiExceptionIgnore);
4813+
{$endif NOEXCEPTIONINTERCEPT}
4814+
result := true; // normal process, with eventual fThreadInfoBackup + UnLock
4815+
end
4816+
else
4817+
result := false; // TSynLogFamily.DisableCurrentThread=true for this thread
48054818
end;
48064819

48074820
function TSynLog.QueryInterface(
@@ -4964,13 +4977,17 @@ function TSynLog.DoEnter: PSynLogThreadInfo;
49644977
(fFamily.fPerThreadLog = ptNoThreadProcess) then // don't mess with recursion
49654978
exit;
49664979
result := GetThreadInfo; // may call InitThreadNumber() if first access
4967-
ndx := result^.RecursionCount;
4968-
inc(ndx);
4969-
if ndx = 0 then
4970-
RaiseDoEnter;
4971-
result^.RecursionCount := ndx;
4972-
if ndx > high(result^.Recursion) then
4973-
result := nil; // nothing logged above MAX_SYNLOGRECURSION
4980+
if not (tiTemporaryDisable in result^.Flags) then
4981+
begin
4982+
ndx := result^.RecursionCount;
4983+
inc(ndx);
4984+
if ndx = 0 then
4985+
RaiseDoEnter;
4986+
result^.RecursionCount := ndx;
4987+
if ndx <= high(result^.Recursion) then
4988+
exit; // fine
4989+
end;
4990+
result := nil; // logging disabled, or above MAX_SYNLOGRECURSION
49744991
end;
49754992

49764993
procedure TSynLog.LockAndPrepareEnter(nfo: PSynLogThreadInfo; microsecs: PInt64);
@@ -5508,7 +5525,7 @@ procedure TSynLog.Log(Level: TSynLogLevel);
55085525
lasterror := 0;
55095526
if Level = sllLastError then
55105527
lasterror := GetLastError;
5511-
LockAndDisableExceptions;
5528+
if LockAndDisableExceptions then
55125529
try
55135530
LogHeader(Level, nil);
55145531
if lasterror <> 0 then
@@ -5543,7 +5560,7 @@ procedure TSynLog.LogText(Level: TSynLogLevel; Text: PUtf8Char; Instance: TObjec
55435560
(Text = nil) or
55445561
not (Level in fFamily.fLevel) then
55455562
exit;
5546-
LockAndDisableExceptions;
5563+
if LockAndDisableExceptions then
55475564
{$ifdef HASFASTTRYFINALLY}
55485565
try
55495566
{$else}
@@ -5940,7 +5957,7 @@ procedure TSynLog.LogInternalFmt(Level: TSynLogLevel; Format: PUtf8Char;
59405957
lasterror := 0;
59415958
if Level = sllLastError then
59425959
lasterror := GetLastError;
5943-
LockAndDisableExceptions;
5960+
if LockAndDisableExceptions then
59445961
try
59455962
LogHeader(Level, Instance);
59465963
fWriter.AddFmt(Format, Values, ValuesCount, twOnSameLine,
@@ -5964,7 +5981,7 @@ procedure TSynLog.LogInternalText(Level: TSynLogLevel; Text: PUtf8Char;
59645981
lasterror := 0;
59655982
if Level = sllLastError then
59665983
lasterror := GetLastError;
5967-
LockAndDisableExceptions;
5984+
if LockAndDisableExceptions then
59685985
try
59695986
LogHeader(Level, Instance);
59705987
if Text = nil then
@@ -6005,7 +6022,7 @@ procedure TSynLog.LogInternalText(Level: TSynLogLevel; Text: PUtf8Char;
60056022
procedure TSynLog.LogInternalRtti(Level: TSynLogLevel; const aName: RawUtf8;
60066023
aTypeInfo: PRttiInfo; const aValue; Instance: TObject);
60076024
begin
6008-
LockAndDisableExceptions;
6025+
if LockAndDisableExceptions then
60096026
try
60106027
LogHeader(Level, Instance);
60116028
fWriter.AddOnSameLine(pointer(aName));
@@ -6447,7 +6464,7 @@ procedure SynLogException(const Ctxt: TSynLogExceptionContext);
64476464
if log = nil then
64486465
exit;
64496466
thrdnam := CurrentThreadNameShort;
6450-
log.LockAndDisableExceptions;
6467+
log.LockAndDisableExceptions; // ignore result = tiTemporaryDisable flag
64516468
try
64526469
try
64536470
// ensure we need to log this

src/mormot.commit.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
'2.3.12776'
1+
'2.3.12777'

0 commit comments

Comments
 (0)