1 MODULE ConsLog;
3 (*
4 A. V. Shiryaev, 2012.10
6 Log.Hook implementation
7 based on StdLog
8 *)
10 IMPORT
11 Log, Views, Dialog,
12 TextModels, TextMappers,
13 Console;
15 TYPE
16 LogHook = POINTER TO RECORD (Log.Hook) END;
18 VAR
19 logAlerts: BOOLEAN;
20 subOut: TextMappers.Formatter;
22 buf: TextModels.Model;
23 textR: TextModels.Reader;
25 (* Sub support *)
27 PROCEDURE* Guard (o: ANYPTR): BOOLEAN;
28 BEGIN
29 RETURN o # NIL
30 END Guard;
32 PROCEDURE* ClearBuf;
33 VAR subBuf: TextModels.Model;
34 BEGIN
35 subBuf := subOut.rider.Base(); subBuf.Delete(0, subBuf.Length())
36 END ClearBuf;
38 PROCEDURE* FlushBuf;
39 VAR c: CHAR;
40 BEGIN
41 IF buf.Length() > 0 THEN
42 textR.SetPos(0);
43 textR.ReadChar(c);
44 WHILE ~textR.eot DO
45 IF c = 0DX THEN
46 Console.WriteLn
47 ELSE
48 Console.WriteChar(c)
49 END;
50 textR.ReadChar(c)
51 END;
52 buf.Delete(0, buf.Length())
53 END
54 END FlushBuf;
56 PROCEDURE* SubFlush;
57 BEGIN
58 IF Log.synch THEN
59 FlushBuf;
60 (* IF Log.force THEN Views.RestoreDomain(text.Domain()) END *)
61 END;
62 END SubFlush;
64 PROCEDURE (log: LogHook) Guard* (o: ANYPTR): BOOLEAN;
65 BEGIN RETURN Guard(o)
66 END Guard;
68 PROCEDURE (log: LogHook) ClearBuf*;
69 BEGIN ClearBuf
70 END ClearBuf;
72 PROCEDURE (log: LogHook) FlushBuf*;
73 BEGIN FlushBuf
74 END FlushBuf;
76 PROCEDURE (log: LogHook) Beep*;
77 BEGIN Dialog.Beep
78 END Beep;
80 PROCEDURE (log: LogHook) Char* (ch: CHAR);
81 BEGIN
82 subOut.WriteChar(ch); SubFlush
83 END Char;
85 PROCEDURE (log: LogHook) Int* (n: INTEGER);
86 BEGIN
87 subOut.WriteChar(" "); subOut.WriteInt(n); SubFlush
88 END Int;
90 PROCEDURE (log: LogHook) Real* (x: REAL);
91 BEGIN
92 subOut.WriteChar(" "); subOut.WriteReal(x); SubFlush
93 END Real;
95 PROCEDURE (log: LogHook) String* (IN str: ARRAY OF CHAR);
96 BEGIN
97 subOut.WriteString(str); SubFlush
98 END String;
100 PROCEDURE (log: LogHook) Bool* (x: BOOLEAN);
101 BEGIN
102 subOut.WriteChar(" "); subOut.WriteBool(x); SubFlush
103 END Bool;
105 PROCEDURE (log: LogHook) Set* (x: SET);
106 BEGIN
107 subOut.WriteChar(" "); subOut.WriteSet(x); SubFlush
108 END Set;
110 PROCEDURE (log: LogHook) IntForm* (x: INTEGER; base, minWidth: INTEGER; fillCh: CHAR; showBase: BOOLEAN);
111 BEGIN
112 subOut.WriteIntForm(x, base, minWidth, fillCh, showBase); SubFlush
113 END IntForm;
115 PROCEDURE (log: LogHook) RealForm* (x: REAL; precision, minW, expW: INTEGER; fillCh: CHAR);
116 BEGIN
117 subOut.WriteRealForm(x, precision, minW, expW, fillCh); SubFlush
118 END RealForm;
120 PROCEDURE (log: LogHook) Tab*;
121 BEGIN
122 subOut.WriteTab; SubFlush
123 END Tab;
125 PROCEDURE (log: LogHook) Ln*;
126 BEGIN
127 subOut.WriteLn; SubFlush;
128 (* IF Log.synch THEN Views.RestoreDomain(text.Domain()) END *)
129 END Ln;
131 PROCEDURE (log: LogHook) Para*;
132 BEGIN
133 subOut.WritePara; SubFlush;
134 (* IF Log.synch THEN Views.RestoreDomain(text.Domain()) END *)
135 END Para;
137 PROCEDURE (log: LogHook) View* (v: ANYPTR);
138 BEGIN
139 IF (v # NIL) & (v IS Views.View) THEN
140 subOut.WriteView(v(Views.View)); SubFlush
141 END
142 END View;
144 PROCEDURE (log: LogHook) ViewForm* (v: ANYPTR; w, h: INTEGER);
145 BEGIN
146 ASSERT(v # NIL, 20);
147 IF (v # NIL) & (v IS Views.View) THEN
148 subOut.WriteViewForm(v(Views.View), w, h); SubFlush
149 END
150 END ViewForm;
152 PROCEDURE (log: LogHook) ParamMsg* (IN s, p0, p1, p2: ARRAY OF CHAR);
153 VAR msg: ARRAY 256 OF CHAR; i: INTEGER; ch: CHAR;
154 BEGIN
155 IF logAlerts THEN
156 (* IF Log.synch THEN Open END; *)
157 Dialog.MapParamString(s, p0, p1, p2, msg);
158 i := 0; ch := msg[0];
159 WHILE ch # 0X DO
160 IF ch = TextModels.line THEN subOut.WriteLn
161 ELSIF ch = TextModels.para THEN subOut.WritePara
162 ELSIF ch = TextModels.tab THEN subOut.WriteTab
163 ELSIF ch >= " " THEN subOut.WriteChar(ch)
164 END;
165 INC(i); ch := msg[i];
166 END;
167 subOut.WriteLn; SubFlush
168 (* ELSE
169 HostDialog.ShowParamMsg(s, p0, p1, p2) *)
170 END
171 END ParamMsg;
174 PROCEDURE AttachSubLog;
175 VAR h: LogHook;
176 BEGIN
177 subOut.ConnectTo(TextModels.dir.New());
178 buf := subOut.rider.Base();
179 textR := buf.NewReader(NIL);
180 NEW(h);
181 Log.SetHook(h);
182 END AttachSubLog;
184 PROCEDURE DetachSubLog;
185 BEGIN
186 Log.SetHook(NIL)
187 END DetachSubLog;
189 BEGIN
190 AttachSubLog
191 CLOSE
192 DetachSubLog;
193 END ConsLog.