/********************************************************************************/
/*																				*/
/*	NetDebug.c																	*/
/*																				*/
/*	This file defines the log file facility										*/
/*																				*/
/********************************************************************************/
 
 
#include "warnings.h"
#include <windows.h>
#include <malloc.h>
#include <conio.h>
#include <stdio.h>
#include <errno.h>
#include <direct.h>
#include <NET\NetDebug.h>
 
static char gs_a_TabFile[][20]={"L0GSUbi.log",		/* 1 */
"L0Ten.log",		/* 2 */
"L0IPX.log",		/* 3 */
"L0TCP.log",		/* 4 */
"L0UDP.log",		/* 5 */
"NetSer.log",		/* 6 */
"NetL1.log",		/* 7 */
"L0Serial.log",		/* 8 */
"L0Modem.log",		/* 9 */
"L0Link.log",		/* 10 */
"L0SBlocks.log",	/* 11 */
"L0SThread.log",	/* 12 */
"L0SBuffin.log",	/* 13 */
"L0SBuffout.log",	/* 14 */
"Memory.log",		/* 15 */
"L0DPlay.log",		/* 16 */
"User 1.log",		/* 17 */
"User 2.log",		/* 18 */
"User 3.log"		/* 19 */
};
 
/* Number message by file. */
static unsigned long gs_a_ulNbrMes[]={0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0};
 
/* Trace active ? */
static char gs_a_cState[]={0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0};
 
/* Trace active ? */
static FILE *gs_a_ptdstFile[]={NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL
};
/* mutual exclusion */
 
static HANDLE gs_a_hExclusion[]={INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,INVALID_HANDLE_VALUE,
};
 
/* Open a trace file. */
void _NET_CALLING_CONV_ vDebugOpen(unsigned short uwNumFile)
{
	char File[80]="Log\\";
	
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		strcat(File,gs_a_TabFile[uwNumFile]);
		if (gs_a_ptdstFile[uwNumFile])
		{
			fclose(gs_a_ptdstFile[uwNumFile]);
			gs_a_ptdstFile[uwNumFile]=NULL;
		}
		gs_a_cState[uwNumFile]=1;
		gs_a_ptdstFile[uwNumFile]=fopen(File,"a");
		if ((!gs_a_ptdstFile[uwNumFile]) && (errno==ENOENT))
		{
			if(!mkdir("Log")) gs_a_ptdstFile[uwNumFile]=fopen(File,"a");
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Active a trace file. */
void _NET_CALLING_CONV_ vDebugActiv(unsigned short uwNumFile)
{
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_ptdstFile[uwNumFile])
		{
			fclose(gs_a_ptdstFile[uwNumFile]);
			gs_a_ptdstFile[uwNumFile]=NULL;
		}
		gs_a_cState[uwNumFile]=1;
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Close a trace file. */
void _NET_CALLING_CONV_ vDebugClose(unsigned short uwNumFile)
{
	HANDLE hLocalHandle;
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_ptdstFile[uwNumFile])
		{
			fclose(gs_a_ptdstFile[uwNumFile]);
			gs_a_ptdstFile[uwNumFile]=NULL;
		}
		gs_a_cState[uwNumFile]=0;
		hLocalHandle=gs_a_hExclusion[uwNumFile];
		gs_a_hExclusion[uwNumFile]=INVALID_HANDLE_VALUE;
		CloseHandle(hLocalHandle);
	}
}
 
/* Output of debug line on a file. */
static void vDebugOutPut(unsigned short uwNumFile,const char *s)
{
	FILE *f;
	char File[80]="Log\\";
	if (gs_a_ptdstFile[uwNumFile])
	{
		fprintf(gs_a_ptdstFile[uwNumFile],"%ld,%ld>%s\n",gs_a_ulNbrMes[uwNumFile]++,GetTickCount(),s);
	}
	else
	{
		strcat(File,gs_a_TabFile[uwNumFile]);
		f=fopen(File,"a");
		if ((!f) && (errno==ENOENT))
		{
			if(!mkdir("Log")) f=fopen(File,"a");
		}
		if (f)
		{
			fprintf(f,"%ld,%ld>%s\n",gs_a_ulNbrMes[uwNumFile]++,GetTickCount(),s);
			fclose(f);
		}
	}
}
 
/* Ouput a format string in the select file debug. */
void _NET_CALLING_CONV_ vDebugFormat(unsigned short uwNumFile,const char *pFormat,...)
{
	va_list va;
	char Str[200];
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0) {
			va_start(va,pFormat);
			vsprintf(Str,pFormat,va);
			vDebugOutPut(uwNumFile,Str);
			va_end(va);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Ouput a string in the select file debug. */
void _NET_CALLING_CONV_ vDebugS(unsigned short uwNumFile,const char *s)
{
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0)
			vDebugOutPut(uwNumFile,s);
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Ouput a string with a long in the select file debug. */
void _NET_CALLING_CONV_ vDebugSI(unsigned short uwNumFile,const char *s,long l)
{
	char Str[200];
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0) {
			sprintf(Str,"%s : %ld",s,l);
			vDebugOutPut(uwNumFile,Str);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Ouput two string with a long in the select file debug. */
void _NET_CALLING_CONV_ vDebugSISI(unsigned short uwNumFile,const char *s1,long l1,const char *s2,long l2)
{
	char Str[200];
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0) {
			sprintf(Str,"%s : %ld  %s : %ld",s1,l1,s2,l2);
			vDebugOutPut(uwNumFile,Str);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Ouput three string with a long in the select file debug. */
void _NET_CALLING_CONV_ vDebugSISISI(unsigned short uwNumFile,const char *s1,long l1,const char *s2,long l2,const char *s3,long l3)
{
	char Str[200];
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0) {
			sprintf(Str,"%s : %ld  %s : %ld  %s : %ld",s1,l1,s2,l2,s3,l3);
			vDebugOutPut(uwNumFile,Str);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Ouput four string with a long in the select file debug. */
void _NET_CALLING_CONV_ vDebugSISISISI(unsigned short uwNumFile,const char *s1,long l1,const char *s2,long l2,const char *s3,long l3,const char *s4,long l4)
{
	char Str[200];
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0) {
			sprintf(Str,"%s : %ld  %s : %ld  %s : %ld  %s : %ld",s1,l1,s2,l2,s3,l3,s4,l4);
			vDebugOutPut(uwNumFile,Str);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Ouput two string in the select file debug. */
void _NET_CALLING_CONV_ vDebugSS(unsigned short uwNumFile,const char *s1,const char *s2)
{
	char Str[200];
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0) {
			sprintf(Str,"%s%s",s1,s2);
			vDebugOutPut(uwNumFile,Str);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Ouput three string in the select file debug. */
void _NET_CALLING_CONV_ vDebugSSS(unsigned short uwNumFile,const char *s1,const char *s2,const char *s3)
{
	char Str[200];
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0) {
			sprintf(Str,"%s%s%s",s1,s2,s3);
			vDebugOutPut(uwNumFile,Str);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Ouput a string with a long and a other string in the select file debug. */
void _NET_CALLING_CONV_ vDebugSIS(unsigned short uwNumFile,const char *s1,long l1,const char *s2)
{
	char Str[200];
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0) {
			sprintf(Str,"%s : %ld  %s",s1,l1,s2);
			vDebugOutPut(uwNumFile,Str);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Ouput two string with a long in the select file debug. */
void _NET_CALLING_CONV_ vDebugSSI(unsigned short uwNumFile,const char *s1,const char *s2,long l1)
{
	char Str[200];
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0) {
			sprintf(Str,"%s%s : %ld",s1,s2,l1);
			vDebugOutPut(uwNumFile,Str);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* OutPut a string whith dump memory ... */
void _NET_CALLING_CONV_ vDebugSDump(unsigned short uwNumFile,const char *s,void *pAddr, unsigned long ulSize)
{
	unsigned long i;
	char Str[600];
	unsigned long l;
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		if (gs_a_cState[uwNumFile]!=0) {
			
			sprintf(Str,"%s : ",s);
			l=strlen(Str);
			
			for(i=0; i<ulSize && l<1+sizeof(Str); i++) {
				l+=sprintf(Str+l,"%.2x",((unsigned char *)pAddr)[i]);
			}
			vDebugOutPut(uwNumFile,Str);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
 
/* Clear the select file debug. */
void _NET_CALLING_CONV_ vDebugClear(unsigned short uwNumFile)
{
	FILE *f;
	if(gs_a_hExclusion[uwNumFile]==INVALID_HANDLE_VALUE)
		gs_a_hExclusion[uwNumFile]=CreateMutex(NULL, FALSE, NULL);
	
	if(WaitForSingleObject(gs_a_hExclusion[uwNumFile], INFINITE)==WAIT_OBJECT_0) {
		char File[80]="Log\\";
		strcat(File,gs_a_TabFile[uwNumFile]);
		if (gs_a_ptdstFile[uwNumFile])
		{
			fclose(gs_a_ptdstFile[uwNumFile]);
			f=fopen(File,"w");
			if ((!f) && (errno==ENOENT))
			{
				if(!mkdir("Log")) f=fopen(File,"w");
			}
			gs_a_ptdstFile[uwNumFile]=f;
		}
		else
		{
			f=fopen(File,"w");
			if ((!f) && (errno==ENOENT))
			{
				if(!mkdir("Log")) f=fopen(File,"w");
			}
			if (f) fclose(f);
		}
		ReleaseMutex(gs_a_hExclusion[uwNumFile]);
	}
}
