Browse Source

Simplified tracing commands by echoing the complete contents of the argument reader and restoring its original position before actually activating the command. This does neither require an additional writer nor daisy-chaining streams.

git-svn-id: https://svn.inf.ethz.ch/svn/lecturers/a2/trunk@8553 8c9fc860-2736-0410-a75d-ab315db34111
negelef 6 years ago
parent
commit
e97e02dfa3
1 changed files with 10 additions and 59 deletions
  1. 10 59
      source/Commands.Mod

+ 10 - 59
source/Commands.Mod

@@ -24,7 +24,6 @@ CONST
 	Started = 0; Loaded = 1; Finished = 2;
 	Started = 0; Loaded = 1; Finished = 2;
 
 
 TYPE
 TYPE
-
 	Context* = OBJECT
 	Context* = OBJECT
 	VAR
 	VAR
 		in-, arg- : Streams.Reader;
 		in-, arg- : Streams.Reader;
@@ -44,52 +43,6 @@ TYPE
 
 
 	END Context;
 	END Context;
 
 
-
-	(*see StreamUtilities.Mod: reader that can daisychained with another reader that extracts a copy of the data flow to a monitor stream*)
-	ReaderMonitor* = OBJECT(Streams.Reader)
-		VAR in: Streams.Reader; tracer: Streams.Writer; pos0: Streams.Position; tracedPos: LONGINT;
-
-		PROCEDURE &Init(in: Streams.Reader; tracer: Streams.Writer);
-		BEGIN
-			SELF.tracer := tracer;
-			InitReader(Receiver, 1024);
-			SELF.in := in;
-			pos0 := in.Pos();
-			tracedPos := pos0;
-		END Init;
-
-		PROCEDURE Receiver(VAR buf: ARRAY OF CHAR; ofs, size, min: LONGINT; VAR len: LONGINT; VAR res: WORD);
-		VAR inReader: Streams.Reader;nextPos: LONGINT;
-		BEGIN
-			inReader := in;
-			ASSERT((size > 0) & (min <= size) & (min >= 0));
-			in.Bytes(buf, ofs, size, len);
-			(* filter out 0X characters *)
-			nextPos := tracedPos;
-			WHILE (nextPos < ofs + len) & (buf[nextPos] # 0X) DO
-				INC(nextPos);
-			END;
-			IF nextPos > tracedPos THEN
-				tracer.Bytes(buf,tracedPos,nextPos-tracedPos);
-			END;
-			tracedPos := nextPos;
-			res:=in.res
-		END Receiver;
-
-		PROCEDURE CanSetPos*(): BOOLEAN;
-		BEGIN RETURN in.CanSetPos()
-		END CanSetPos;
-
-		PROCEDURE SetPos*(pos: Streams.Position);
-		BEGIN Reset; pos0 := pos; in.SetPos(pos)
-		END SetPos;
-
-		PROCEDURE Pos*(): Streams.Position;
-		BEGIN RETURN Pos^()+pos0;
-		END Pos;
-
-	END ReaderMonitor;
-
 	(* Procedure types that can be called be runner thread *)
 	(* Procedure types that can be called be runner thread *)
 	CommandProc = PROCEDURE;
 	CommandProc = PROCEDURE;
 	CommandContextProc = PROCEDURE(context : Context);
 	CommandContextProc = PROCEDURE(context : Context);
@@ -101,8 +54,6 @@ TYPE
 		moduleName, commandName : Modules.Name;
 		moduleName, commandName : Modules.Name;
 		context : Context;
 		context : Context;
 
 
-		tracer: Streams.Writer; r: ReaderMonitor;
-
 		proc : CommandProc;
 		proc : CommandProc;
 		commandProc : CommandContextProc;
 		commandProc : CommandContextProc;
 
 
@@ -113,15 +64,20 @@ TYPE
 		exception : BOOLEAN;
 		exception : BOOLEAN;
 
 
 		PROCEDURE &Init*(CONST moduleName, commandName : Modules.Name; context : Context);
 		PROCEDURE &Init*(CONST moduleName, commandName : Modules.Name; context : Context);
+		VAR origin: Streams.Position; char: CHAR;
 		BEGIN
 		BEGIN
 			SELF.moduleName := moduleName; SELF.commandName := commandName;
 			SELF.moduleName := moduleName; SELF.commandName := commandName;
 
 
 			IF (context = NIL) THEN NEW(context, NIL, NIL, NIL, NIL, NIL); END;
 			IF (context = NIL) THEN NEW(context, NIL, NIL, NIL, NIL, NIL); END;
 			IF trace THEN
 			IF trace THEN
-				Streams.OpenWriter(tracer, Trace.Send);
-				NEW(r , context.arg, tracer); context.arg:=r;
-				tracer.String("Commands.Activate ");
-				tracer.String(moduleName); tracer.String(Delimiter); tracer.String(commandName); tracer.Char(" ");
+				Trace.String("Commands.Activate ");
+				Trace.String(moduleName); Trace.String(Delimiter); Trace.String(commandName);
+				IF context.arg.CanSetPos () THEN
+					origin := context.arg.Pos ();
+					LOOP context.arg.Char (char); IF context.arg.res # Streams.Ok THEN EXIT END; Trace.Char (char); END;
+					context.arg.SetPos (origin);
+				END;
+				Trace.Char ("~"); Trace.Ln;
 			END;
 			END;
 			SELF.context := context;
 			SELF.context := context;
 			res := CommandError; COPY("Error starting command", msg);
 			res := CommandError; COPY("Error starting command", msg);
@@ -171,11 +127,6 @@ TYPE
 		ELSE
 		ELSE
 			res := CommandTrapped; COPY("Exception during command execution", msg);
 			res := CommandTrapped; COPY("Exception during command execution", msg);
 		END;
 		END;
-		IF trace THEN
-			tracer.String(" ~"); 
-			tracer.Ln; 
-			tracer.Update
-		END;
 		BEGIN {EXCLUSIVE} state := Finished; END;
 		BEGIN {EXCLUSIVE} state := Finished; END;
 	END Runner;
 	END Runner;
 
 
@@ -273,7 +224,7 @@ PROCEDURE Call*(cmds : ARRAY OF CHAR; flags : SET; VAR res : WORD; VAR msg : ARR
 VAR  outer, context : Context; arg : Streams.StringReader; i, j, k : LONGINT; mode : ARRAY 5 OF CHAR;
 VAR  outer, context : Context; arg : Streams.StringReader; i, j, k : LONGINT; mode : ARRAY 5 OF CHAR;
 par : POINTER TO ARRAY OF CHAR;
 par : POINTER TO ARRAY OF CHAR;
 BEGIN
 BEGIN
-	IF trace THEN 	Trace.String("Commands.Call "); Trace.String(cmds); Trace.String("~ "); Trace.Ln END;
+	IF trace THEN Trace.String("Commands.Call "); Trace.String(cmds); Trace.String("~"); Trace.Ln END;
 	NEW(par,LEN(cmds));
 	NEW(par,LEN(cmds));
 	i := 0; WHILE (i # 4) & (i # LEN(cmds)) DO mode[i] := cmds[i]; INC(i); END;
 	i := 0; WHILE (i # 4) & (i # LEN(cmds)) DO mode[i] := cmds[i]; INC(i); END;
 	mode[i] := 0X;	(* copy at most first 4 characters *)
 	mode[i] := 0X;	(* copy at most first 4 characters *)