Subject: Directing output also to printer 2. ***** Q: I want to have a printed documentation of my students' Turbo Pascal program exercises. How is all input and output directed also to the printer? A1: Use a screen capturing program to put everything that comes onto the screen into a file, and print the file. See FAQPROGS.TXT in ftp://garbo.uwasa.fi/pc/ts/tsfaqn43.zip (or whatever version number is the current) for more about these programs. Available by anonymous FTP or mail server from garbo.uwasa.fi. A2: See the code in TSPAS.NWS (item: Redirecting writes to the printer) in the ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip (or whatever is the current version number) Turbo Pascal units package (70 = 40, 50, 55, 60, or 70 depending on your TP version). Alternatively use USECON and USEPRN routines in the TSUNTG unit of the same package. +------------------------------------------+ ! To get these and other packages given as ! ! /dir/subdir/name ! ! see the instructions in PD2ANS.TXT ! +------------------------------------------+ A3: But the really elegant solution to the problem of getting a logfile (or a printed list) of a Turbo Pascal run is to rewrite the write(ln) and read(ln) device driver functions. In itself writing such driver redirections is very advanced Turbo Pascal programming, but when the programming has once been done, the system is extremely easy to use as many times as you like. It goes like this. The driver redirections are programmed into a unit (say, tpulog or tpuprn). All that is needed after that is to include the following uses statement into the program (the target program) which has to be logged: uses TPULOG; ( or ) uses TPUPRN; This is all there is to it. Just adding one simple line to the target program. (If you call any other units, "uses tpulog" must come AFTER the system units (e.g. Dos), but BEFORE any which you may define yourself!) The reason that I have named two units here instead of just one in the above example is that the preferred log for the target program may be a logfile or the printer. The better solution of these two is to use the logfile option, and then print it. The reason is simple. If the target program itself prints something, your printout will look confused. The logging also has obvious limitations. It works for standard input and output (read(ln) and write(ln)) only. 1) It does not support graphics, in other words it is for the textmode. 2) It does not support direct (Crt) screen writes. 3) And, naturally it only shows the input and output that comes to the screen. Not any other input or output, such as from or to a file. 4) Furthermore, you are not allowed to reassign input or output. Statements like assign (output, '') will result in a crash, because the rewritten output device redirections are invalidated by such statements. 5) The device on the default drive must not be write protected, since else the logfile cannot be written to it. 6) It does not work for Turbo Pascal 4.0. Despite these restrictions, the method is perfectly suited for logging students' Turbo Pascal escapades. It is advisable first to test and run your target program without "tpulog", so that if you get any strange errors you'll know whether they are caused by the logging. Where to get such a unit. The code can be found in Michael Tischer (1990), Turbo Pascal Internals, Abacus, Section 4.2. Next a few of my own tips on this unit Tischer calls Prot. 1) The code is in incorrect order. The code that is listed on pages 142 - 145 goes between pages 139 and 140. 2) You can change the logfile name (const prot_name) to lpt1 for a printed list of the target program run. In that case it is advisable to include a test for the online status of the printer within Tischer's unit. 3) I see no reason why the two lines in Tischer's interface section couldn't be transferred to the implementation section. Why have any global definitions? But all in all, it works like magic! A4: From: abcscnuk@csunb.csun.edu (Naoto Kimura (ACM)) Subject: Re: Printing a log of students' exercises revisited To: ts@uwasa.fi Date: Fri, 2 Nov 90 20:52:03 pdt [Reproduced with Naoto's kind permission] By the way, several months ago, I had submitted a file (nktools.zip) file on SimTel that contains sources to a unit (LOGGER), which allows logging of I/O going through the standard input and output files, while still being able to use the program interactively. I believe that I also submitted a copy to your site. It was something I put together for use by students here at California State University at Northridge. The source works equally well in all presently available versions of Turbo Pascal. The only requirements are that * you place it as one of the last entries in the USES clause. If there is anything that redirects the standard input and output file variables, you should put that unit before my unit in the USES clause, so that it can see the I/O stream. * Don't use the GotoXY and similar screen display control procedures in the Crt unit and expect it to come out the same way you had it on the display. Since all my unit does is just capture the I/O stream to send it through the normal channels and also to the log file, all screen control information is not sent to the log file. * All I/O you want logged should go through the standard input and output file variables. * Don't close the standard input and output file variables, because it will cause problems. Basically, as far as I have checked, it just causes the logging to stop at that point. --------------------------------------------------------------------