//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//	Debug.c
//	Luiz Marques Afonso 	Setembro/95
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>
#include <share.h>
#include <sys\stat.h>

#include "laycab.h"

#if defined( DEBUG )
WORD
	Psp;
static int
	FileHandle = -1;
static char
	*NomeArquivo = "c:\\laycab.txt";
static DWORD
	TickCountInicial = 0L;
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
void
	RegistraNoLog( char *msg )
{
	WORD
		oldPsp;
	char
		strAux[ 31 ];

	//Quando se tratar de uma callback, tenho de setar o PSP corrente, para poder utilizar o
	//handle correto do arquivo de log.

	//pega o psp
	_AX = 0x5100;
	asm int 0x21;
	oldPsp = _BX;

	//seta o psp
	_AX = 0x5000;
	_BX = Psp;
	asm int 0x21;

	if( FileHandle == -1 )
	{
		FileHandle = open( NomeArquivo, O_WRONLY | O_TEXT | O_CREAT | O_TRUNC, S_IWRITE );
		if( FileHandle == -1 )
			return;
		TickCountInicial = GetTickCount();
	}

	wsprintf( strAux, "%lu : ", GetTickCount() - TickCountInicial );
	write( FileHandle, strAux, strlen(strAux) );
	write( FileHandle, msg, strlen( msg ) );
	write( FileHandle, "\n", 1 );

	if( ! strcmp( msg, LOG_FIM_EXECUCAO ) )
		close( FileHandle );

	//seta o psp velho
	_AX = 0x5000;
	_BX = oldPsp;
	asm int 0x21;
}
#endif

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
