-- Pupwatch: verbose log

-- DiskLog.mesa

-- Andrew Birrell October 26, 1983 3:02 pm

DIRECTORY
Ascii    USING[ CR, SP,TAB ],
BasicTime  USING[ Now ],
FS    USING[ Copy, Error, StreamOpen ],
IO    USING[ Close, Put, PutChar, PutRope, STREAM],
LookerDefs  USING[],
Rope    USING[ ROPE ],
ViewerClasses USING[ Viewer ],
ViewerOps  USING[ FindViewer, RestoreViewer ],
ViewerTools USING[ MakeNewTextViewer ];

DiskLog: PROGRAM
IMPORTS FS, IO, BasicTime, ViewerOps, ViewerTools
EXPORTS LookerDefs =

BEGIN

lineLength: CARDINAL ← 0;
lineMaxlength: CARDINAL = 100;
firstChar: BOOLEANTRUE;
firstTime: BOOLEANTRUE;

DiskChar: PUBLIC PROC[c: CHARACTER] =
BEGIN
IF firstChar
THEN BEGIN
firstChar ← FALSE;
IF firstTime
THEN BEGIN
firstTime ← FALSE;
file ← FS.StreamOpen[logName, $create];
END
ELSE BEGIN
FS.Copy[from: logName, to: logName, keep: 1 ! FS.Error => CONTINUE];
file ← FS.StreamOpen[logName, $append];
END;
LogTime[];
END;
IF c = Ascii.CR
THEN { IO.PutChar[file, c]; lineLength ← 0 }
ELSE { IO.PutChar[file, c]; lineLength ← lineLength+1 };
END;

LogTime: PROC =
BEGIN
IO.PutRope[file, "Pupwatch.log written at "];
IO.Put[file, [time[BasicTime.Now[]]] ];
END;

DiskMultiple: PUBLIC PROC[desc: LONG DESCRIPTOR FOR PACKED ARRAY OF CHARACTER] =
BEGIN
FOR i: CARDINAL IN [0..LENGTH[desc])
DO c: CHARACTER = desc[i];
IF c IN [40C..176C]
THEN DiskChar[c]
ELSE DiskChar['?];
ENDLOOP;
END;

DiskPos: PUBLIC PROC[pos: CARDINAL] =
BEGIN
THROUGH [lineLength..pos-1) DO IO.PutChar[file, Ascii.SP] ENDLOOP;
IO.PutChar[file, Ascii.TAB]; -- help alignment for variable width fonts --
lineLength ← MAX[lineLength, lineLength+pos];
END;

logName: Rope.ROPE = "Pupwatch.log";

DiskCommit: PUBLIC PROC =
BEGIN
DiskChar[Ascii.CR]; DiskChar[Ascii.CR];
IO.Close[file];
BEGIN
v: ViewerClasses.Viewer = ViewerOps.FindViewer["Pupwatch.log"].viewer;
IF v = NIL
THEN [] ← ViewerTools.MakeNewTextViewer[
info: [name: logName, file: "Pupwatch.log", iconic: FALSE] ]
ELSE ViewerOps.RestoreViewer[v];
END;
firstChar ← TRUE;
END;

file: IO.STREAM;

END.