-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathULOG.PAS
155 lines (131 loc) · 2.99 KB
/
ULOG.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
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
150
151
152
153
154
155
{
ULog Unit
2022 LRT
}
unit
ULog;
interface
uses
consts, utils, uexc, uclasses, types, locale, uobject, ustream,
uansiw, umsgs;
type
ELevel = (
ELevelStandard,
ELevelWarning,
ELevelError
);
PLog = ^TLog;
TLog = object (TObject)
public
constructor initWithStream(stream: PStream);
destructor done; virtual;
procedure receiveMessage(msg: PObjectMessage); virtual;
procedure setAsExceptionHandler;
procedure reset;
procedure out(str: string);
procedure warn(str: string);
procedure fail(str: string);
procedure setAnsiSupport(value: boolean);
procedure setAsDefault;
function getClassName: string; virtual;
function getClassId: word; virtual;
private
_stream: PStream;
_ansi: PAnsiWriter;
_count: longint;
procedure entry(level: ELevel; var str: string);
end;
var
Log: PLog;
implementation
const
C_LEVEL_COLORS: array [ELevel] of EAnsiColor = (
fgWhite,
fgYellow,
fgRed
);
{ TLog public }
constructor TLog.initWithStream(stream: PStream);
begin
inherited init;
_stream := stream;
_stream^.retain;
_ansi := new(PAnsiWriter, initWithStream(_stream));
_ansi^.writeln('');
_count := 0;
end;
destructor TLog.done;
begin
_ansi^.release;
_stream^.release;
inherited done;
end;
procedure TLog.receiveMessage(msg: PObjectMessage);
var
ex: PException;
error: string;
begin
if msg^.opcode = C_MSG_EXCEPTION then
begin
ex := PException(msg^.sender);
error := ex^.getText;
entry(ELevelError, error);
end else
inherited receiveMessage(msg);
end;
procedure TLog.setAsExceptionHandler;
begin
setExceptionHandler(@self);
end;
procedure TLog.reset;
begin
_ansi^.setColor(none);
_ansi^.clear;
_ansi^.gotoxy(1, 1);
end;
procedure TLog.out(str: string);
begin
entry(ELevelStandard, str);
end;
procedure TLog.warn(str: string);
begin
entry(ELevelWarning, str);
end;
procedure TLog.fail(str: string);
begin
entry(ELevelError, str);
end;
procedure TLog.setAnsiSupport(value: boolean);
begin
_ansi^.setEnabled(value);
end;
procedure TLog.setAsDefault;
begin
if Log <> nil then Log^.release;
Log := @self;
retain;
end;
function TLog.getClassName: string;
begin
getClassName := 'TLog';
end;
function TLog.getClassId: word;
begin
getClassId := C_CLASS_ID_Log;
end;
{ TLog private }
procedure TLog.entry(level: ELevel; var str: string);
begin
with _ansi^ do
begin
_ansi^.setColor(C_LEVEL_COLORS[ELevelStandard]);
writestr('[' + longtostr(_count) + '] ');
_ansi^.setColor(C_LEVEL_COLORS[level]);
writeln(str);
inc(_count);
end;
end;
{ Other }
begin
Log := nil;
end.