pas besoin d'HoroDate
penser à protéger les BeginUpdate/EndUpdate des assertions et exception avec des try finally
Null besoin dans un log de supprimer N lignes si T > MAX, on ajoute 1 on supprime 1 si le log est pleins.
Utiliser des dates au format anglais, c'est mieux pour le tris naturel :
format anglais et tris naturel :
2013-01-01
2013-01-02
2013-02-01
2013-02-02
format français et tris naturel :
01-01-2013
01-02-2013
02-01-2013
02-02-2013
1 2 3 4 5 6 7 8 9 10 11 12
|
S := formatDateTime('yyyy-mm-dd hh:nn:ss.zzz', now)+ ' > ' + S;
Memo1.lines.beginupdate;
try
if Memo1.lines.count > 400 then
Memo1.lines.delete(0); // supprime 1, pas besoin d'en supprimer 200 d'un coups.
Memo1.lines.add(S); // ajoute 1
Memo1.SelStart := Memo1.Perform(EM_LINEINDEX, MaxInt, 0);
finally
Memo1.lines.endupdate;
Memo1.Perform(EM_SCROLLCARET, 0, 0);
end; |
Version ListBox, beaucoup plus rapide :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
Procedure TForm13.Log(S:string);
Begin
with ListBoxLog do
begin
Items.beginupdate;
try
if Items.count > 400 then
Items.delete(0);
ItemIndex := Items.add(formatDateTime('yyyy-mm-dd hh:nn:ss.zzz', now)+ ' > ' + S);
finally
Items.endupdate;
end;
end;
End; |
Version TStrings indépendante :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function Log(aLogStrings: TStrings; aLogMsg:string; const aMax: integer= 400): integer;
Begin
with aLogStrings do
begin
beginUpdate;
try
if Count > aMax then
Delete(0);
result := add(formatDateTime('yyyy-mm-dd hh:nn:ss.zzz', now)+ ' > ' + aLogMsg);
finally
endUpdate;
end;
end;
End; |
version unité :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
unit Log;
interface
uses
Windows, SysUtils, Classes;
type
TAppLogOption = (
aloAddLast, // add push at last line
aloAddFirst, // add push at first line
aloAutoSave,
aloClearOnOpen
);
TAppLogOptions = set of TAppLogOption;
const
AppLogDefaultSizeLimit = 2*1024*1024; // 2Mo
type
TAppLog = class
private
fLog : TStringList;
fFileName : TFileName;
fSizeLimit : int64;
fOptions : TAppLogOptions;
fTick : LongWord;
fOnChange : TNotifyEvent;
protected
procedure Load;
procedure Save;
procedure Clean;
procedure Change;
public
property OnChange : TNotifyEvent read fOnChange write fOnChange;
property Options : TAppLogOptions read fOptions write fOptions;
function push(aMsg: string): integer;
procedure AssignTo(aStrings: TStrings);
constructor Create(const aFileName: TFileName; const aSizeLimit: int64=AppLogDefaultSizeLimit; const aOptions: TAppLogOptions = [aloAddLast, aloAutoSave]); reintroduce; virtual;
destructor Destroy; override;
end;
var
AppLog : TAppLog;
implementation
{ TAppLog }
procedure TAppLog.AssignTo(aStrings: TStrings);
begin
aStrings.Assign(fLog);
end;
procedure TAppLog.Change;
begin
if assigned(fOnChange) then
fOnChange(Self);
end;
procedure TAppLog.Clean;
var C: int64;
begin
C := length(fLog.Text);
while Length(fLog.Text) > fSizeLimit do
if aloAddLast in fOptions then
fLog.Delete(0)
else
fLog.Delete(fLog.Count-1);
if length(fLog.Text) <> C then
Change;
end;
constructor TAppLog.Create(const aFileName: TFileName; const aSizeLimit: int64; const aOptions: TAppLogOptions);
begin
inherited Create;
fFileName := aFileName;
fSizeLimit:= aSizeLimit;
fOptions := aOptions;
fLog := TStringList.Create;
Load;
Clean;
fTick := GetTickCount;
end;
destructor TAppLog.Destroy;
begin
fOnChange := nil;
Clean;
Save;
fLog.Free;
inherited;
end;
procedure TAppLog.Load;
begin
if fileExists(fFileName) then
fLog.LoadFromFile(fFileName);
if aloClearOnOpen in fOptions then
fLog.Clear;
end;
function TAppLog.push(aMsg: string): integer;
begin
result := 0;
if (aloAddLast in fOptions) or (fLog.Count = 0) then
result := fLog.Add(aMsg)
else
fLog.Insert(0,aMsg);
Change;
if (getTickCount-fTick) >= 600000 then // clean all 10 minutes
begin
Clean;
fTick := GetTickCount;
end;
end;
procedure TAppLog.Save;
begin
if aloAutoSave in fOptions then
fLog.SaveToFile(fFileName);
end;
initialization
AppLog := TAppLog.Create(ChangeFileExt(ParamStr(0),'.log'));
finalization
AppLog.Free;
end. |
Partager