This commit is contained in:
2018-06-01 21:39:34 +03:00
parent ee71383973
commit f4396f4550

View File

@@ -1,4 +1,6 @@
unit AG.Logs; unit AG.Logs;
//Copyright by Artem Gavrilov.
//Site:https://teamfnd.ru
interface interface
@@ -18,22 +20,22 @@ uses
type type
TAGLog=class abstract TAGLog=class abstract
strict protected strict protected
FTabText:string; FIndentText:string;
tabs:cardinal; Indents:cardinal;
tabstr:string; IndentStr:string;
constructor Create(); constructor Create();
procedure TabRegen(); procedure UpdateIndentStr();
procedure SetTabText(const s:string);virtual; procedure SetIndentText(const s:string);virtual;
const const
CBaseTab='--------------------------'; CBaseIndent='--------------------------';
public public
class function SizeableWordtoStr(i:word;size:int8):string;static;inline; class function SizeableWordtoStr(i:word;size:int8):string;static;inline;
class function GenerateLogString(const s:string;o:TObject=nil):string;static;inline; class function GenerateLogString(const s:string;o:TObject=nil):string;static;inline;
procedure Tab();virtual; procedure Indent();virtual;
procedure UnTab();virtual; procedure UnIndent();virtual;
procedure Write(const Text:string;o:TObject=nil);overload;virtual;abstract; procedure Write(const Text:string;o:TObject=nil);overload;virtual;abstract;
destructor Destroy();override; destructor Destroy();override;
property TabText:string read FTabText write SetTabText; property IndentText:string read FIndentText write SetIndentText;
end; end;
TAGRamLog=class(TAGLog) TAGRamLog=class(TAGLog)
@@ -101,7 +103,7 @@ type
TAGMultiLog=class(TAGLog) TAGMultiLog=class(TAGLog)
strict protected strict protected
procedure SetTabText(const s:string);override; procedure SetIndentText(const s:string);override;
public public
type type
TLogsList={$IFDEF FPC}specialize TFPGList<TAGLog>{$ELSE}TList<TAGLog>{$ENDIF}; TLogsList={$IFDEF FPC}specialize TFPGList<TAGLog>{$ELSE}TList<TAGLog>{$ENDIF};
@@ -109,28 +111,30 @@ type
Logs:TLogsList; Logs:TLogsList;
constructor Create(Default:TLogsList);overload; constructor Create(Default:TLogsList);overload;
procedure Write(const Text:string;o:TObject=nil);overload;override; procedure Write(const Text:string;o:TObject=nil);overload;override;
procedure Tab();override; procedure Indent();override;
procedure UnTab();override; procedure UnIndent();override;
destructor Destroy();overload;override; destructor Destroy();overload;override;
end; end;
const const
CharSize=SizeOf(char);//{$IFDEF FPC}1{$ELSE}2{$ENDIF};
DefOneTabStr=' '; DefOneTabStr=' ';
Implementation Implementation
const
CharSize=SizeOf(char);//{$IFDEF FPC}1{$ELSE}2{$ENDIF};
constructor TAGLog.Create(); constructor TAGLog.Create();
begin begin
Self.Write(CBaseTab+'Logging init-'+CBaseTab); Self.Write(CBaseIndent+'Logging init-'+CBaseIndent);
TabRegen; UpdateIndentStr;
FTabText:=DefOneTabStr; FIndentText:=DefOneTabStr;
end; end;
class function TAGLog.SizeableWordtoStr(i:word;size:int8):string; class function TAGLog.SizeableWordtoStr(i:word;size:int8):string;
begin begin
Result:=IntToStr(i); Result:=IntToStr(i);
size:=size-Length(Result); dec(size,Length(Result));
case size of case size of
0:Result:=Result; 0:Result:=Result;
1:Result:='0'+Result; 1:Result:='0'+Result;
@@ -159,52 +163,52 @@ Result:='['+SizeableWordtoStr(DayOfTheMonth(D),2)+'.'+SizeableWordtoStr(MonthOfT
+SizeableWordtoStr(MilliSecondOfTheSecond(D),3)+'] '+Result+s+sLineBreak; +SizeableWordtoStr(MilliSecondOfTheSecond(D),3)+'] '+Result+s+sLineBreak;
end; end;
procedure TAGLog.Tab(); procedure TAGLog.Indent();
begin begin
inc(tabs); inc(Indents);
tabstr:=tabstr+' '; IndentStr:=IndentStr+FIndentText;
end; end;
procedure TAGLog.UnTab(); procedure TAGLog.UnIndent();
begin begin
if tabs=0 then if Indents=0 then
Exit; Exit;
dec(tabs); dec(Indents);
Delete(tabstr,1,2); Delete(IndentStr,1,length(FIndentText));
end; end;
procedure TAGLog.TabRegen(); procedure TAGLog.UpdateIndentStr();
var var
i:cardinal; i:cardinal;
begin begin
tabstr:=''; IndentStr:='';
for i:=1 to tabs do for i:=1 to Indents do
tabstr:=tabstr+FTabText; IndentStr:=IndentStr+FIndentText;
end; end;
procedure TAGLog.SetTabText(const s:string); procedure TAGLog.SetIndentText(const s:string);
begin begin
FTabText:=s; FIndentText:=s;
TabRegen; UpdateIndentStr;
end; end;
destructor TAGLog.Destroy(); destructor TAGLog.Destroy();
begin begin
Self.Write(CBaseTab+'Logging ended'+CBaseTab); Self.Write(CBaseIndent+'Logging ended'+CBaseIndent);
inherited; inherited;
end; end;
constructor TAGRamLog.Create(); constructor TAGRamLog.Create();
begin begin
buf:=''; buf:='';
tabs:=0; Indents:=0;
tabstr:=''; IndentStr:='';
inherited Create; inherited Create;
end; end;
procedure TAGRamLog.Write(const Text:string;o:TObject=nil); procedure TAGRamLog.Write(const Text:string;o:TObject=nil);
begin begin
buf:=buf+GenerateLogString(tabstr+Text,o); buf:=buf+GenerateLogString(IndentStr+Text,o);
end; end;
constructor TAGDiskLog.Create(const FileName:String); constructor TAGDiskLog.Create(const FileName:String);
@@ -212,8 +216,8 @@ constructor TAGDiskLog.Create(const FileName:String);
begin begin
Lock:=TCriticalSection.Create; Lock:=TCriticalSection.Create;
WantTerminate:=False; WantTerminate:=False;
tabs:=0; Indents:=0;
tabstr:=''; IndentStr:='';
buf1:=''; buf1:='';
LogHandle:=CreateFileW(Pwidechar(FileName),GENERIC_WRITE,0,nil,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); LogHandle:=CreateFileW(Pwidechar(FileName),GENERIC_WRITE,0,nil,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
SetFilePointer(LogHandle,0,nil,FILE_END); SetFilePointer(LogHandle,0,nil,FILE_END);
@@ -284,7 +288,7 @@ procedure TAGDiskLog.Write(const Text:string;o:TObject=nil);
{$IF defined(MSWINDOWS) and not defined(FPC)} {$IF defined(MSWINDOWS) and not defined(FPC)}
begin begin
Lock.Enter; Lock.Enter;
buf1:=buf1+GenerateLogString(tabstr+text,o); buf1:=buf1+GenerateLogString(IndentStr+text,o);
Lock.Leave; Lock.Leave;
{$ELSE} {$ELSE}
var var
@@ -339,7 +343,7 @@ var
temptext:string; temptext:string;
a,b:cardinal; a,b:cardinal;
begin begin
temptext:=GenerateLogString(tabstr+text,o); temptext:=GenerateLogString(IndentStr+text,o);
p:=addr(temptext[1]); p:=addr(temptext[1]);
a:=length(temptext); a:=length(temptext);
while a<>0 do while a<>0 do
@@ -390,12 +394,12 @@ end;
{TAGMultiLog} {TAGMultiLog}
procedure TAGMultiLog.SetTabText(const s:string); procedure TAGMultiLog.SetIndentText(const s:string);
var var
i:TAGLog; i:TAGLog;
begin begin
for i in Logs do for i in Logs do
i.TabText:=s; i.IndentText:=s;
end; end;
constructor TAGMultiLog.Create(Default:TLogsList); constructor TAGMultiLog.Create(Default:TLogsList);
@@ -415,20 +419,20 @@ for i in Logs do
i.Write(Text,o); i.Write(Text,o);
end; end;
procedure TAGMultiLog.Tab(); procedure TAGMultiLog.Indent();
var var
i:TAGLog; i:TAGLog;
begin begin
for i in Logs do for i in Logs do
i.Tab(); i.Indent();
end; end;
procedure TAGMultiLog.UnTab(); procedure TAGMultiLog.UnIndent();
var var
i:TAGLog; i:TAGLog;
begin begin
for i in Logs do for i in Logs do
i.UnTab(); i.UnIndent();
end; end;
destructor TAGMultiLog.Destroy(); destructor TAGMultiLog.Destroy();