-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadProtocolObject.pas
97 lines (83 loc) · 2.49 KB
/
ThreadProtocolObject.pas
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
unit ThreadProtocolObject;
interface
uses
Controls, Sysutils, syncobjs, classes;
const
TimeOut = 10000;
type
TLogLevel = (llNormal, llDebug);
TIdentifySenderFunction = function(Sender, SenderToIdentify: TObject;
var SenderName: string): Boolean;
TThreadProtocol = class
protected
Dir: String;
CanWrite: TEvent;
Protocol: TFileStream;
procedure WriteString(s: string);
function SenderToStr(Sender: TObject): String;
public
LogLevel: TLogLevel;
OnIdentifySender: TIdentifySenderFunction;
constructor Create(ADir: String);
function DateToFilename(Date: TDate): String;
procedure AddEntry(Sender: TObject; Entry: string; Level: TLogLevel = llNormal);
destructor Destroy; override;
end;
implementation
procedure TThreadProtocol.AddEntry(Sender: TObject; Entry: string; Level: TLogLevel = llNormal);
var final: string;
begin
if (Level <= LogLevel) then
begin
final := DateTimeToStr(now) + '|' + SenderToStr(Sender) + '|:|' + Entry + #13 + #10;
WriteString(final);
end;
end;
constructor TThreadProtocol.Create(ADir: String);
begin
inherited Create;
Dir := ADir;
LogLevel := llDebug;
if (not FileExists(DateToFilename(now))) then
begin
Protocol := TFileStream.Create(DateToFilename(Now),fmCreate);
Protocol.Free;
end;
Protocol := TFileStream.Create(DateToFilename(Now),fmOpenWrite or fmShareDenyNone);
Protocol.Seek(0,soFromEnd);
CanWrite := TEvent.Create(nil,False,True,'');
AddEntry(Self,'Start Protocol');
end;
function TThreadProtocol.DateToFilename(Date: TDate): String;
var d,m,y: word;
begin
DecodeDate(now,Y,M,D);
Result := Dir + '_' + IntToStr(Y) + '.' + IntToStr(M) + '.' + IntToStr(D) + '.txt';
end;
destructor TThreadProtocol.Destroy;
begin
AddEntry(Self,'End Protocol');
Protocol.Free;
CanWrite.Free;
inherited Destroy;
end;
function TThreadProtocol.SenderToStr(Sender: TObject): String;
begin
if Sender = Self then
Result := 'ProtocolObject'
else
if not(Assigned(OnIdentifySender) and
OnIdentifySender(Self, Sender, Result)) then
Result := IntToHex(Cardinal(Sender),4)
end;
procedure TThreadProtocol.WriteString(s: string);
begin
if CanWrite.WaitFor(TimeOut) = wrSignaled then
try
Protocol.Write(PChar(s)^,length(s));
finally
CanWrite.SetEvent;
end else raise Exception.Create('TThreadProtocol.AddEntry: ' +
'Timeout beim Warten auf Schreiberlaubnis MSG:' + s);
end;
end.