/*
 * ghost.cpp
 *
 */

#include "ghost.h"

#include <stdarg.h>

#include <windows.h>

class GhostImplementation
{
public:
	HANDLE hStep;

	GhostImplementation()
	{
		hStep=::CreateEventA(0, FALSE, FALSE, 0);
	}

	static GhostImplementation& instance()
	{
		static GhostImplementation theInstance;
		return theInstance;
	}
};


void GhostLogger::print(Level level, const char* fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	vPrint(level, fmt, args);
	va_end(args);
}

void GhostLogger::vPrint(Level /*level*/, const char* /*fmt*/, va_list /*args*/)
{
}

GhostLogger::~GhostLogger()
{
}

static GhostLogger ghostNullLogger;

static GhostLogger* ghostLogger=&ghostNullLogger;

GhostLogger& Ghost::logger()
{
	return *ghostLogger;
}

void Ghost::setLoggerInterface(GhostLogger* logger)
{
	ghostLogger=logger ? logger : &ghostNullLogger;
}


void Ghost::playbackStepWait(int timeout_ms)
{
	if (timeout_ms==0)
		return;

	if (timeout_ms>0)
	{
		::Sleep(timeout_ms);
	}

	if (timeout_ms<0)
	{
		WaitForSingleObject(GhostImplementation::instance().hStep, INFINITE);
	}

}


void Ghost::playbackStep()
{
	PulseEvent(GhostImplementation::instance().hStep);
}


