	title	LOG - logging console to file
	name	('LOG')
	maclib	base80
	maclib	reldef

; This package implements console redirection to a log file

; Copyright (c)	Werner Cirsovius
;		Hohe Weide 44
;		D-20253 Hamburg
;		(+49)40/4223247

;		Version 1.2, September 1993

; The package contains:

; redir		Routine checks if redirection is selected
;		It expects an address pointing to a command
;		line starting with length of line at REDIR-2
;		The redirection file is defined by the '>'
;		character followed by file name
; LOGfile	Creates a log file
; Fconout	Prints character in accu on console and
;		puts to file
; Fconchd	Prints character in reg E B-times on console
;		and puts to file
; Fstring	Prints string pointed to by reg DE console and
;		puts to file
; Fcrlf		Prints CR and LF to console and puts to file
; FOS		Close log file and execute warm start

	ext	parse,wrfcb,wrbuf,creatd,dskput,close,closef
	ext	conout,string,conchd,crlf,@crlf

	entry	redir,LOGfile,Fconout,Fconchd,Fstring
	entry	Fcrlf,FOS

;
; REDIR-2 : Text pointer, starting with length
;
	dw	dma
;
; Check redirection selected
;
redir:
	ld	hl,(redir-2)
	ld	a,(hl)		; Test parameter here
	or	a
	ret	z		; .. no
	ld	c,a		; Set count
	ld	b,0
	add	hl,bc		; Point to end
	ld	a,'>'
	cpdr			; .. find it
	ret	nz		; .. none
	push	hl
	ld	de,(redir-2)
	or	a
	sbc	hl,de
	ld	c,l		; Get fixed length
	pop	hl
	inc	hl
	push	hl
	push	de
	inc	hl
	ld	(red.PB),hl	; Save string pointer
	ld	de,red.PB
	call	parse		; .. convert file
	pop	hl
	pop	de
	ret	c		; .. error
	push	hl
	ld	hl,LOG.FCB+1
	ld	a,(hl)
	cp	' '		; Check empty name
	pop	hl
	ret	z
	xor	a
	ld	(de),a		; Close line
	ld	(hl),c		; Set new length
	ld	a,TRUE
	ld	(LOG),a		; Set flag
	ret
;
; Create log file if selected
;
LOGfile:
	ld	a,(LOG)		; Test selected
	or	a
	ret	z
	ld	de,LOG.FCB
	ld	(wrfcb),de	; Select output
	ld	hl,LOG.dma
	ld	(wrbuf),hl
	call	creatd		; Create file
	ret	nc
dis.dir:
	ld	a,FALSE		; Cannot do, so disable
	ld	(LOG),a
	ret
;
; Output character
; ENTRY	: Accu holds character
;
Fconout:
	push	bc
	ld	c,a
	ld	a,(LOG)		; Test selected
	or	a
	ld	a,c
	pop	bc
	jp	z,conout	; .. to console
.dskput:
	call	dskput		; .. to disk
	ret	nc		; .. check error
	push	de
	ld	de,LOG.FCB
	call	close		; Close file if so
	ld	de,$LOGwarn
	call	string		; Tell error
	pop	de
	jr	dis.dir		; .. disable logging
;
; Output constant characters
; ENTRY	: Reg E holds character
;	  Reg B holds length
;
Fconchd:
	ld	a,(LOG)		; Test selected
	or	a
	jp	z,conchd
F.conchd:
	ld	a,e
	call	Fconout		; <B>-times to disk
	djnz	F.conchd
	ret
;
; Output string
; ENTRY	: Reg DE points to string closed by $
;
Fstring:
	ld	a,(LOG)		; Test selected
	or	a
	jp	z,string
F.string:
	ld	a,(de)
	cp	eot		; Test end
	ret	z
	call	Fconout		; .. put to disk
	inc	de
	jr	F.string
;
; Close line
;
Fcrlf:
	ld	a,(LOG)		; Test selected
	or	a
	jp	z,crlf		; .. to console
	push	de
	ld	de,@crlf
	call	F.string	; .. to disk
	pop	de
	ret
;
; Exit program
;
FOS:
	ld	a,(LOG)		; Test selected
	or	a
	call	nz,closef	; .. close .LOG file if so
	jp	OS

	dseg

red.PB:
	dw	$-$
	dw	LOG.FCB
$LOGwarn:
	db	bel,cr,lf
	db	'WARNING: LOG file write error',cr,lf,eot
LOG:
	db	FALSE

LOG.FCB:
	ds	fcblen
LOG.dma:
	ds	reclng

	end

