-- Pupwatch: writing log file

-- [Indigo]<Grapevine>PupWatch>DiskLog.mesa

-- Andrew Birrell  23-Nov-81 14:42:03

DIRECTORY
Ascii		USING[ CR, SP, TAB ],
LookerDefs	USING[],
StreamDefs	USING[ Append, DiskHandle, NewByteStream, WriteAppend ],
Time		USING[ AppendCurrent ];

DiskLog: PROGRAM
IMPORTS StreamDefs, Time
EXPORTS LookerDefs =

BEGIN

lineLength: CARDINAL ← 0;
lineMaxlength: CARDINAL = 100;
firstChar: BOOLEAN ← TRUE;

DiskChar: PUBLIC PROC[c: CHARACTER] =
  BEGIN
  IF firstChar
  THEN BEGIN
       firstChar ← FALSE;
       LogTime[];
       END;
  IF c = Ascii.CR
  THEN { file.put[file,c]; lineLength ← 0 }
  ELSE IF lineLength < lineMaxlength
       THEN { file.put[file,c]; lineLength ← lineLength+1 };
  END;

LogTime: PROC =
  BEGIN
  head: STRING = "Log written at "L;
  s: STRING = [30];
  Time.AppendCurrent[s];
  DiskMultiple[DESCRIPTOR[@(head.text), head.length]];
  DiskMultiple[DESCRIPTOR[@(s.text), s.length]]; DiskChar[Ascii.CR];
  END;

DiskMultiple: PUBLIC PROC[desc: DESCRIPTOR FOR PACKED ARRAY OF CHARACTER] =
  BEGIN
  FOR i: CARDINAL IN [0..MIN[LENGTH[desc],lineMaxlength-lineLength])
  DO c: CHARACTER ← desc[i];
     IF c IN [40C..176C]
     THEN DiskChar[c]
     ELSE BEGIN
          DiskChar['<];
          IF c > 77C
          THEN DiskChar['0+LOOPHOLE[c,CARDINAL]/100B];
          IF c > 7C
          THEN DiskChar['0+(LOOPHOLE[c,CARDINAL] MOD 100B)/10B];
          DiskChar['0+LOOPHOLE[c,CARDINAL] MOD 10B];
          DiskChar['>];
          END;
  ENDLOOP;
  END;

DiskPos: PUBLIC PROC[pos: CARDINAL] =
  BEGIN
  IF lineLength = 0
  THEN BEGIN
       THROUGH [0..pos/8) DO file.put[file,Ascii.TAB] ENDLOOP;
       THROUGH [0..pos MOD 8) DO file.put[file,Ascii.SP] ENDLOOP;
       END
  ELSE THROUGH [lineLength..pos) DO file.put[file,Ascii.SP] ENDLOOP;
  lineLength ← MAX[lineLength, pos];
  END;

DiskCommit: PUBLIC PROC =
  BEGIN
  DiskChar[Ascii.CR]; DiskChar[Ascii.CR];
  file.destroy[file];
  file ← StreamDefs.NewByteStream[
		"PupWatch.log"L, StreamDefs.Append];
  firstChar ← TRUE;
  END;

file: StreamDefs.DiskHandle ← StreamDefs.NewByteStream[
		"PupWatch.log"L, StreamDefs.WriteAppend];

END.