#include "pch.hpp"
#include "eventid.hpp"


DWORD *GosEventLog::pLogBase = &GosEventLog::NullLog[0];
DWORD GosEventLog::LogOffset = 0;
DWORD GosEventLog::LogMod = 0;
DWORD GosEventLog::NullLog[16];
bool GosEventLog::WaitingToStart = false;
bool GosEventLog::LoggingInProgress = false;

char *GosEventLog::m_pLogStart1;
DWORD GosEventLog::m_nLogSize1; 
char *GosEventLog::m_pLogStart2;
DWORD GosEventLog::m_nLogSize2; 


DWORD GosLogFuncScope::Current = 0;

DWORD *pLargeLog = NULL;
DWORD LargeLogMod = 0;

void GosEventLog::LogStart()
{
	if( !pLargeLog )
	{
		LargeLogMod = 0x0ffffe;
		pLargeLog = (DWORD *)malloc( sizeof(DWORD)*(LargeLogMod+2) );
	}
	if( LogMod == 0 )
		WaitingToStart = true;
}

void GosEventLog::LogStartPoint()
{
	if( WaitingToStart && ( LogMod == 0 ) )
	{
		pLargeLog[LargeLogMod] = 0x1234AACC;	// drop an end marker so we can see if we wrapped
		pLogBase = pLargeLog;
		LogOffset = 0;
		LogMod = LargeLogMod;
		WaitingToStart = false;
		LoggingInProgress = true;	// not tested to log events, used to see if we should sleep when not in focus
	}
}

void GosEventLog::LogStop()
{
	LoggingInProgress = false;

	if( pLargeLog )
	{
		if( pLargeLog[LargeLogMod] == 0x1234AACC )
		{
			// log didn't wrap
			m_pLogStart1 = (char *)pLogBase;
			m_nLogSize1 = LogOffset<<2;
			m_pLogStart2 = (char *)pLogBase;
			m_nLogSize2 = 0;
		}
		else
		{
			// log did wrap
			m_pLogStart1 = (char *)(&pLogBase[LogOffset]);
			m_nLogSize1 = (LargeLogMod+2-LogOffset)<<2;
			m_pLogStart2 = (char *)pLogBase;
			m_nLogSize2 = LogOffset<<2;
		}
	}
	LogMod = 0;
	LogOffset = 0;
	pLogBase = NullLog;
}

void GosEventLog::Cleanup()
{
	LogStop();
	if( pLargeLog )
		free( pLargeLog );
}


GosLogRef::GosLogRef( EventType type, char *name, char *filename, int lineno )
{
	m_Count = 0;
	m_id = GosEventIdMgr::AssignId( type, name, filename, lineno );
}

