//*******************************************************************
//
// File: Asura_Debug_Timers.h
//
// Created: 30 May 2007 DLH
//
// Description: 
//
//*******************************************************************

#ifndef __ASURA_DEBUG_TIMERS_H__
#define __ASURA_DEBUG_TIMERS_H__

// other libraries
#include "Core/Asura_Platform_Timers.h" // for Asura_TickCount

// internal dependencies

// forward declarations
class Asura_CPUProfile_ScopedTimer_Global_Close;

//*******************************************************************
// Constants
//*******************************************************************

//*******************************************************************
// Class Definitions
//*******************************************************************

#if defined( ASURA_CPU_PROFILE_SCOPED_TIMER_ENABLE)
class Asura_CPUProfile_ScopedTimer
{
public:
	Asura_CPUProfile_ScopedTimer( const char* pszText);
	~Asura_CPUProfile_ScopedTimer();
	
	static void EnableLogFileReset( const bool bEnable);
	static void EnableXMLLogFile( const bool bEnable);
	static void SetFilename( const char* pszFilename);
	static void SetXMLFilename( const char* pszFilename);

private:
	// DLH - Members
	Asura_CPUProfile_ScopedTimer* m_pxParentTimer;
	
	const char* m_pszText;
	Asura_TickCount m_xTimeStart;
	Asura_TickCount m_xTimeChildren;

#if defined( ASURA_CPU_PROFILE_SCOPED_TIMER_ENABLE_MEMORY_LOG)
	size_t	m_iMemoryStart;
	size_t	m_iMemoryChildrenDelta;
#endif

	// DLH - Static
	static Asura_CPUProfile_ScopedTimer* s_pxPresentTimer;
	static u_int s_uNestedDepth;
	
	static bool s_bDebugOutputEnabled;
	static bool s_bLogFileInitialised;
	static bool s_bLogFileReset;
	static bool s_bLogFileXML;

	static float s_fReportTimeThreshold;
	
	friend class Asura_CPUProfile_ScopedTimer_Global_Close;
};

#define ASURA_CPU_PROFILE_SCOPED_TIMER( X) Asura_CPUProfile_ScopedTimer xTimeScopedCode( X );
#define ASURA_CPU_PROFILE_SCOPED_TIMER_ALLOW_FILE_RESET( X) Asura_CPUProfile_ScopedTimer::EnableLogFileReset( X);
#define ASURA_CPU_PROFILE_SCOPED_TIMER_ALLOW_XML_FILE( X) Asura_CPUProfile_ScopedTimer::EnableXMLLogFile( X);
#define ASURA_CPU_PROFILE_SCOPED_TIMER_LOG_FILENAME( X) Asura_CPUProfile_ScopedTimer::SetFilename( X);
#define ASURA_CPU_PROFILE_SCOPED_TIMER_LOG_XML_FILENAME( X) Asura_CPUProfile_ScopedTimer::SetXMLFilename( X);
#define ASURA_CPU_PROFILE_CALL_TIMER(__function) \
	{ \
		ASURA_CPU_PROFILE_SCOPED_TIMER(#__function) \
		__function; \
	}

#else // !defined( ASURA_CPU_PROFILE_SCOPED_TIMER_ENABLE)

#define ASURA_CPU_PROFILE_SCOPED_TIMER( X)
#define ASURA_CPU_PROFILE_SCOPED_TIMER_ALLOW_FILE_RESET( X)
#define ASURA_CPU_PROFILE_SCOPED_TIMER_ALLOW_XML_FILE( X)
#define ASURA_CPU_PROFILE_SCOPED_TIMER_LOG_FILENAME( X)
#define ASURA_CPU_PROFILE_SCOPED_TIMER_LOG_XML_FILENAME( X)
#define ASURA_CPU_PROFILE_CALL_TIMER(__function) __function

#endif // !defined( ASURA_CPU_PROFILE_SCOPED_TIMER_ENABLE)

//*******************************************************************
//
// (c) 2006 Rebellion
//
// Warning: This computer program is protected by copyright law and 
// international treaties. Unauthorized reproduction or distribution
// of this program, or any portion of it, may result in severe civil and
// criminal penalties, and will be prosecuted to the maximum extent 
// possible under the law.
//
//*******************************************************************

#endif // __ASURA_DEBUG_TIMERS_H__
