Console

From BlackBox Framework Wiki
Revision as of 09:22, 11 September 2016 by Josef templ (talk | contribs) (Created page with "For logging within the BlackBox framework, in particular the text subsystem, or for logging in very low-level modules the standard BlackBox logging facilities (Log, StdLog) ca...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

For logging within the BlackBox framework, in particular the text subsystem, or for logging in very low-level modules the standard BlackBox logging facilities (Log, StdLog) cannot be used.

The following module 'Console' can be used for that purpose. It attaches a Windows Console window to the BlackBox process and outputs the log to the Console window. In order to make it easy to detect new output it adds a line number at the left.

MODULE Console;

IMPORT WinApi, SYSTEM;

VAR res, stdOut, lineNr: INTEGER;

PROCEDURE String*(IN s: ARRAY OF CHAR);
	VAR line: ARRAY 10000 OF SHORTCHAR;
BEGIN
	line := SHORT(s);
	res := WinApi.WriteFile(stdOut, SYSTEM.ADR(line), LEN(line$), NIL, NIL);
END String;

PROCEDURE IntToString(x: LONGINT; OUT s: ARRAY OF CHAR); (* copied from Strings *)
	CONST minLongIntRev = "8085774586302733229";
	VAR j, k: INTEGER; ch: CHAR; a: ARRAY 32 OF CHAR;
BEGIN
	IF x # MIN(LONGINT) THEN
		IF x < 0 THEN s[0] := "-"; k := 1; x := -x ELSE k := 0 END;
		j := 0; REPEAT a[j] := CHR(x MOD 10 + ORD("0")); x := x DIV 10; INC(j) UNTIL x = 0
	ELSE
		a := minLongIntRev; s[0] := "-"; k := 1;
		j := 0; WHILE a[j] # 0X DO INC(j) END
	END;
	ASSERT(k + j < LEN(s), 23);
	REPEAT DEC(j); ch := a[j]; s[k] := ch; INC(k) UNTIL j = 0;
	s[k] := 0X
END IntToString;

PROCEDURE Int*(x: INTEGER);
	VAR str: ARRAY 20 OF CHAR; sstr: ARRAY 20 OF SHORTCHAR;
BEGIN
	IntToString(x, str);
	sstr := SHORT(str);
	res := WinApi.WriteFile(stdOut, SYSTEM.ADR(sstr), LEN(sstr$), NIL, NIL);
END Int;

PROCEDURE LogLineNr;
BEGIN INC(lineNr); Int(lineNr); String(": ")
END LogLineNr;

PROCEDURE Ln*();
	VAR crlf: ARRAY 2 OF SHORTCHAR;
BEGIN
	crlf[0] := 0DX; crlf[1] := 0AX;
	res := WinApi.WriteFile(stdOut, SYSTEM.ADR(crlf), 2, NIL, NIL);
	LogLineNr
END Ln;

BEGIN
	res := WinApi.AllocConsole();
	stdOut := WinApi.GetStdHandle(WinApi.STD_OUTPUT_HANDLE);
	LogLineNr
END Console.