#include ".\logwriter.h"

Logwriter::Logwriter(void)
{
	FILE *fp;
	fopen_s(&fp, "error.log","w");
	fclose(fp);
}

Logwriter::~Logwriter(void)
{
}

void Logwriter::Log(char*str,...)
{
	char	buf[30];
	FILE	*fp;
	int		i;

	va_list vl;

    va_start(vl,str);

	fopen_s(&fp, "error.log","a");

	if(!fp)
		return;

    _strdate_s(buf);
    fprintf(fp,"%s ",buf );
	_strtime_s(buf);
    fprintf(fp,"%s ",buf );

	for(i=0;str[i]!='\0';i++)
    {
		if(str[i]=='%')
        {
			i++;
			switch(str[i])
			{
				case 'd':fprintf(fp,"%d",va_arg(vl,int));break;
				case 'D':fprintf(fp,"%d",va_arg(vl,short));break;
				case 'c':fprintf(fp,"%c",va_arg(vl,char));break;
				case 'f':fprintf(fp,"%f",va_arg(vl,double));break;
				case 's':fprintf(fp,"%s",va_arg(vl,char*));break;
				case 'S':fprintf(fp,"%S",va_arg(vl,wchar_t*));break;
				//case 'r':switch(va_arg(vl,HRESULT))
				//		 {
				//			case S_OK:fprintf(fp,"%s","OK");break;
				//			case DDERR_SURFACELOST:fprintf(fp,"%s","DDERR_SURFACELOST");break; 
				//			case DDERR_WASSTILLDRAWING:fprintf(fp,"%s","DDERR_WASSTILLDRAWING");break;
				//			case DDERR_INVALIDOBJECT:fprintf(fp,"%s","DDERR_INVALIDOBJECT");break;
				//			case DDERR_INVALIDPARAMS:fprintf(fp,"%s","DDERR_INVALIDPARAMS");break;
				//			case DDERR_NOTFOUND:fprintf(fp,"%s","DDERR_NOTFOUND");break;
				//			case DDERR_NOTINITIALIZED:fprintf(fp,"%s","DDERR_NOTINITIALIZED");break;
				//			case DDERR_OUTOFMEMORY:fprintf(fp,"%s","DDERR_OUTOFMEMORY");break;
				//			case DDERR_GENERIC:fprintf(fp,"%s","DDERR_GENERIC");break;
				//			case DDERR_INVALIDMODE:fprintf(fp,"%s","DDERR_INVALIDMODE");break;
				//			case DDERR_LOCKEDSURFACES:fprintf(fp,"%s","DDERR_LOCKEDSURFACES");break;
				//			case DDERR_NOEXCLUSIVEMODE:fprintf(fp,"%s","DDERR_NOEXCLUSIVEMODE");break;
				//			case DDERR_SURFACEBUSY:fprintf(fp,"%s","DDERR_SURFACEBUSY");break;
				//			case DDERR_UNSUPPORTED:fprintf(fp,"%s","DDERR_UNSUPPORTED");break;
				//			case DDERR_UNSUPPORTEDMODE:fprintf(fp,"%s","DDERR_UNSUPPORTEDMODE");break;
				//			case D3DERR_INVALIDCALL:fprintf(fp,"%s","D3DERR_INVALIDCALL");break;  
				//			case D3DERR_NOTAVAILABLE:fprintf(fp,"%s","D3DERR_NOTAVAILABLE");break;  
				//			case D3DERR_OUTOFVIDEOMEMORY:fprintf(fp,"%s","D3DERR_OUTOFVIDEOMEMORY");break;	
				//			case D3DERR_DEVICELOST:fprintf(fp,"%s","D3DERR_DEVICELOST");break;  
				//			case D3DERR_DEVICENOTRESET:fprintf(fp,"%s","D3DERR_DEVICENOTRESET");break;
				//			/*case DPNERR_HOSTREJECTEDCONNECTION :fprintf(fp,"%s","DPNERR_HOSTREJECTEDCONNECTION");break;
				//			case DPNERR_INVALIDAPPLICATION:fprintf(fp,"%s","DPNERR_INVALIDAPPLICATION");break; 
				//			case DPNERR_INVALIDDEVICEADDRESS:fprintf(fp,"%s","DPNERR_INVALIDDEVICEADDRESS");break; 
				//			case DPNERR_INVALIDFLAGS:fprintf(fp,"%s","DPNERR_INVALIDFLAGS");break; 
				//			case DPNERR_INVALIDHOSTADDRESS:fprintf(fp,"%s","DPNERR_INVALIDHOSTADDRESS");break; 
				//			case DPNERR_INVALIDINSTANCE:fprintf(fp,"%s","DPNERR_INVALIDINSTANCE");break; 
				//			case DPNERR_INVALIDINTERFACE:fprintf(fp,"%s","DPNERR_INVALIDINTERFACE");break; 
				//			case DPNERR_INVALIDPASSWORD:fprintf(fp,"%s","DPNERR_INVALIDPASSWORD");break; 
				//			case DPNERR_NOCONNECTION:fprintf(fp,"%s","DPNERR_NOCONNECTION");break; 
				//			case DPNERR_NOTHOST:fprintf(fp,"%s","DPNERR_NOTHOST");break; 
				//			case DPNERR_SESSIONFULL:fprintf(fp,"%s","DPNERR_SESSIONFULL");break; 
				//			case DPNERR_ALREADYCONNECTED:fprintf(fp,"%s","DPNERR_ALREADYCONNECTED");break; 
				//			case DPNERR_ABORTED:fprintf(fp,"%s","DPNERR_ALREADYCONNECTED");break;  
				//			case DPNERR_ADDRESSING:fprintf(fp,"%s","DPNERR_ADDRESSING");break;  
				//			case DPNERR_ALREADYCLOSING:fprintf(fp,"%s","DPNERR_ALREADYCLOSING");break;  
				//			case DPNERR_ALREADYDISCONNECTING:fprintf(fp,"%s","DPNERR_ALREADYDISCONNECTING");break;  
				//			case DPNERR_ALREADYINITIALIZED :fprintf(fp,"%s","DPNERR_ALREADYINITIALIZED");break; 
				//			case DPNERR_BUFFERTOOSMALL :fprintf(fp,"%s","DPNERR_BUFFERTOOSMALL");break; 
				//			case DPNERR_CANNOTCANCEL :fprintf(fp,"%s","DPNERR_CANNOTCANCEL");break; 
				//			case DPNERR_CANTCREATEGROUP :fprintf(fp,"%s","DPNERR_CANTCREATEGROUP");break; 
				//			case DPNERR_CANTCREATEPLAYER :fprintf(fp,"%s","DPNERR_CANTCREATEPLAYER");break; 
				//			case DPNERR_CANTLAUNCHAPPLICATION :fprintf(fp,"%s","DPNERR_CANTLAUNCHAPPLICATION");break; 
				//			case DPNERR_CONNECTING :fprintf(fp,"%s","DPNERR_CONNECTING");break; 
				//			case DPNERR_CONNECTIONLOST :fprintf(fp,"%s","DPNERR_CONNECTIONLOST");break; 
				//			case DPNERR_DATATOOLARGE :fprintf(fp,"%s","DPNERR_DATATOOLARGE");break; 
				//			case DPNERR_DOESNOTEXIST :fprintf(fp,"%s","DPNERR_DOESNOTEXIST");break; 
				//			case DPNERR_ENUMQUERYTOOLARGE :fprintf(fp,"%s","DPNERR_ENUMQUERYTOOLARGE");break; 
				//			case DPNERR_ENUMRESPONSETOOLARGE :fprintf(fp,"%s","DPNERR_ENUMRESPONSETOOLARGE");break; 
				//			case DPNERR_EXCEPTION :fprintf(fp,"%s","DPNERR_EXCEPTION");break; 
				//			case DPNERR_GROUPNOTEMPTY :fprintf(fp,"%s","DPNERR_GROUPNOTEMPTY");break; 
				//			case DPNERR_HOSTTERMINATEDSESSION :fprintf(fp,"%s","DPNERR_HOSTTERMINATEDSESSION");break; 
				//			case DPNERR_INCOMPLETEADDRESS :fprintf(fp,"%s","DPNERR_INCOMPLETEADDRESS");break; 
				//			case DPNERR_INVALIDADDRESSFORMAT :fprintf(fp,"%s","DPNERR_INVALIDADDRESSFORMAT");break; 
				//			case DPNERR_INVALIDCOMMAND :fprintf(fp,"%s","DPNERR_INVALIDCOMMAND");break; 
				//			case DPNERR_INVALIDGROUP :fprintf(fp,"%s","DPNERR_INVALIDGROUP");break; 
				//			case DPNERR_INVALIDHANDLE :fprintf(fp,"%s","DPNERR_INVALIDHANDLE");break; 
				//			case DPNERR_INVALIDOBJECT :fprintf(fp,"%s","DPNERR_INVALIDOBJECT");break; 
				//			case DPNERR_INVALIDPLAYER :fprintf(fp,"%s","DPNERR_INVALIDPLAYER");break; 
				//			case DPNERR_INVALIDPOINTER :fprintf(fp,"%s","DPNERR_INVALIDPOINTER");break; 
				//			case DPNERR_INVALIDPRIORITY :fprintf(fp,"%s","DPNERR_INVALIDPRIORITY");break; 
				//			case DPNERR_INVALIDXString :fprintf(fp,"%s","DPNERR_INVALIDXString");break; 
				//			case DPNERR_INVALIDURL :fprintf(fp,"%s","DPNERR_INVALIDURL");break; 
				//			case DPNERR_INVALIDVERSION :fprintf(fp,"%s","DPNERR_INVALIDVERSION");break; 
				//			case DPNERR_NOCAPS :fprintf(fp,"%s","DPNERR_NOCAPS");break; 
				//			case DPNERR_NOHOSTPLAYER :fprintf(fp,"%s","DPNERR_NOHOSTPLAYER");break; 
				//			case DPNERR_NOINTERFACE :fprintf(fp,"%s","DPNERR_NOINTERFACE");break; 
				//			case DPNERR_NORESPONSE :fprintf(fp,"%s","DPNERR_NORESPONSE");break; 
				//			case DPNERR_NOTALLOWED :fprintf(fp,"%s","DPNERR_NOTALLOWED");break; 
				//			case DPNERR_PENDING :fprintf(fp,"%s","DPNERR_PENDING");break; 
				//			case DPNERR_PLAYERLOST :fprintf(fp,"%s","DPNERR_PLAYERLOST");break; 
				//			case DPNERR_PLAYERNOTREACHABLE :fprintf(fp,"%s","DPNERR_PLAYERNOTREACHABLE");break; 
				//			case DPNERR_TIMEDOUT :fprintf(fp,"%s","DPNERR_TIMEDOUT");break; 
				//			case DPNERR_UNINITIALIZED :fprintf(fp,"%s","DPNERR_UNINITIALIZED");break; 
				//			case DPNERR_USERCANCEL :fprintf(fp,"%s","DPNERR_USERCANCEL");break; 
				//			case D3DXERR_INVALIDDATA:fprintf(fp,"%s","D3DXERR_INVALIDDATA");break; */
				//			//case DIERR_INPUTLOST:fprintf(fp,"%s","DIERR_INPUTLOST");break;
				//			//case DIERR_NOTACQUIRED:fprintf(fp,"%s","DIERR_NOTACQUIRED");break;
				//			//case DIERR_NOTINITIALIZED:fprintf(fp,"%s","DIERR_NOTINITIALIZED");break;
				//			default: fprintf(fp,"UNKNOWN RESULT: %d",va_arg(vl,HRESULT));break;
				//		 }break;
				default:fprintf(fp,"%c",str[i]);break;
			}
		}
		else
			fprintf(fp,"%c",str[i]);
    }
	fputs("\n",fp);
	fclose(fp);
	va_end(vl);
}