delphimvcframework/LoggerPro.GlobalLogger.pas
Daniele Teti 55500acdf9 Squashed 'lib/loggerpro/' content from commit d1e0db6e
git-subtree-dir: lib/loggerpro
git-subtree-split: d1e0db6e93724e5fd825f0a8c02151d60450f98f
2023-02-27 12:26:25 +01:00

78 lines
1.7 KiB
ObjectPascal

unit LoggerPro.GlobalLogger;
{<@abstract(Contains the global logger as a thread safe singleton)
Use the global logger for fast&dirty logging, but consider to use your own
instance of @link(ILogWriter) (created using @link(BuildLogWriter)) for all your serious logging needs.
@author(Daniele Teti - d.teti@bittime.it)
}
interface
uses
LoggerPro;
{ @abstract(The global logger. Just uses @link(Logger.GlobalLogger) and you can start to log using @code(Log) function.)
The global logger is configured with a @link(TLoggerProFileAppender) using default settings.
}
function Log: ILogWriter;
{ @abstract(Use only inside DLL because dll unloading is not a safe place to shutdown threads, so call this before unload DLL)
Use this also in ISAPI dll. Check the @code(loggerproisapisample.dll) sample
}
procedure ReleaseGlobalLogger;
implementation
uses
LoggerPro.FileAppender;
var
_Logger: ILogWriter;
_Lock: TObject = nil;
_ShuttedDown: boolean = false;
function Log: ILogWriter;
begin
if _Logger = nil then
begin
if not _ShuttedDown then
begin
TMonitor.Enter(_Lock);
try
if _Logger = nil then // double check
begin
_Logger := BuildLogWriter([TLoggerProFileAppender.Create]);
end;
finally
TMonitor.Exit(_Lock);
end;
end;
end;
Result := _Logger;
end;
procedure ReleaseGlobalLogger;
begin
if _Logger <> nil then
begin
TMonitor.Enter(_Lock);
try
if _Logger <> nil then // double check
begin
_Logger := nil;
_ShuttedDown := True;
end;
finally
TMonitor.Exit(_Lock);
end;
end;
end;
initialization
_Lock := TObject.Create;
finalization
_Lock.Free;
end.