@@ -646,8 +646,10 @@ TSynLogFamily = class;
646
646
// / available TSynLogThreadInfo.Flags definition
647
647
// - tiExceptionIgnore store TSynLogFamily.ExceptionIgnoreCurrentThread
648
648
// property (used only if NOEXCEPTIONINTERCEPT conditional is undefined)
649
+ // - tiTemporaryDisable store TSynLogFamily.DisableCurrentThread property
649
650
TSynLogThreadInfoFlag = (
650
- tiExceptionIgnore);
651
+ tiExceptionIgnore,
652
+ tiTemporaryDisable);
651
653
// / TSynLogThreadInfo.Flags property set type definition
652
654
TSynLogThreadInfoFlags = set of TSynLogThreadInfoFlag;
653
655
@@ -796,6 +798,11 @@ TSynLogFamily = class
796
798
// to this flag
797
799
property ExceptionIgnoreCurrentThread: boolean
798
800
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;
799
806
// / you can let exceptions be ignored from a callback
800
807
// - if set and returns true, the given exception won't be logged
801
808
// - execution of this event handler is protected via the logs global lock
@@ -1104,7 +1111,7 @@ TSynLog = class(TObject, ISynLog)
1104
1111
procedure RaiseDoEnter ;
1105
1112
procedure LockAndPrepareEnter (nfo: PSynLogThreadInfo;
1106
1113
microsecs: PInt64); // no profit inlining
1107
- procedure LockAndDisableExceptions ; // no profit inlining
1114
+ function LockAndDisableExceptions : boolean ; // no profit inlining
1108
1115
procedure LogEnter (nfo: PSynLogThreadInfo; inst: TObject; txt: PUtf8Char
1109
1116
{ $ifdef ISDELPHI} ; addr: PtrUInt = 0 { $endif} );
1110
1117
procedure LogEnterFmt (nfo: PSynLogThreadInfo; inst: TObject;
@@ -4783,25 +4790,31 @@ procedure SetThreadInfoAndThreadName(log: TSynLog; nfo: PSynLogThreadInfo);
4783
4790
log.AddLogThreadName;
4784
4791
end ;
4785
4792
4786
- procedure TSynLog.LockAndDisableExceptions ;
4793
+ function TSynLog.LockAndDisableExceptions : boolean ;
4787
4794
var
4788
4795
nfo: PSynLogThreadInfo;
4789
4796
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
4805
4818
end ;
4806
4819
4807
4820
function TSynLog.QueryInterface (
@@ -4964,13 +4977,17 @@ function TSynLog.DoEnter: PSynLogThreadInfo;
4964
4977
(fFamily.fPerThreadLog = ptNoThreadProcess) then // don't mess with recursion
4965
4978
exit;
4966
4979
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
4974
4991
end ;
4975
4992
4976
4993
procedure TSynLog.LockAndPrepareEnter (nfo: PSynLogThreadInfo; microsecs: PInt64);
@@ -5508,7 +5525,7 @@ procedure TSynLog.Log(Level: TSynLogLevel);
5508
5525
lasterror := 0 ;
5509
5526
if Level = sllLastError then
5510
5527
lasterror := GetLastError;
5511
- LockAndDisableExceptions;
5528
+ if LockAndDisableExceptions then
5512
5529
try
5513
5530
LogHeader(Level, nil );
5514
5531
if lasterror <> 0 then
@@ -5543,7 +5560,7 @@ procedure TSynLog.LogText(Level: TSynLogLevel; Text: PUtf8Char; Instance: TObjec
5543
5560
(Text = nil ) or
5544
5561
not (Level in fFamily.fLevel) then
5545
5562
exit;
5546
- LockAndDisableExceptions;
5563
+ if LockAndDisableExceptions then
5547
5564
{ $ifdef HASFASTTRYFINALLY}
5548
5565
try
5549
5566
{ $else}
@@ -5940,7 +5957,7 @@ procedure TSynLog.LogInternalFmt(Level: TSynLogLevel; Format: PUtf8Char;
5940
5957
lasterror := 0 ;
5941
5958
if Level = sllLastError then
5942
5959
lasterror := GetLastError;
5943
- LockAndDisableExceptions;
5960
+ if LockAndDisableExceptions then
5944
5961
try
5945
5962
LogHeader(Level, Instance);
5946
5963
fWriter.AddFmt(Format, Values, ValuesCount, twOnSameLine,
@@ -5964,7 +5981,7 @@ procedure TSynLog.LogInternalText(Level: TSynLogLevel; Text: PUtf8Char;
5964
5981
lasterror := 0 ;
5965
5982
if Level = sllLastError then
5966
5983
lasterror := GetLastError;
5967
- LockAndDisableExceptions;
5984
+ if LockAndDisableExceptions then
5968
5985
try
5969
5986
LogHeader(Level, Instance);
5970
5987
if Text = nil then
@@ -6005,7 +6022,7 @@ procedure TSynLog.LogInternalText(Level: TSynLogLevel; Text: PUtf8Char;
6005
6022
procedure TSynLog.LogInternalRtti (Level: TSynLogLevel; const aName: RawUtf8;
6006
6023
aTypeInfo: PRttiInfo; const aValue; Instance: TObject);
6007
6024
begin
6008
- LockAndDisableExceptions;
6025
+ if LockAndDisableExceptions then
6009
6026
try
6010
6027
LogHeader(Level, Instance);
6011
6028
fWriter.AddOnSameLine(pointer(aName));
@@ -6447,7 +6464,7 @@ procedure SynLogException(const Ctxt: TSynLogExceptionContext);
6447
6464
if log = nil then
6448
6465
exit;
6449
6466
thrdnam := CurrentThreadNameShort;
6450
- log.LockAndDisableExceptions;
6467
+ log.LockAndDisableExceptions; // ignore result = tiTemporaryDisable flag
6451
6468
try
6452
6469
try
6453
6470
// ensure we need to log this
0 commit comments