//	DebugHeaders.h - Bits and pieces that are very useful for debugging.
//  ----------------------------------------------------------------------------
//	This file is part of 'NiallsAVLib', base code for any kind of audiovisual
//	apps.
//	Copyright (C) 2012  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#ifndef DEBUGHEADERS_H_
#define DEBUGHEADERS_H_

#include "UTF8File.h"

#include <sstream>

#if defined(WIN32) && defined(DEBUG)
#define NiallNew new (_NORMAL_BLOCK, __FILE__, __LINE__)
#else
#define NiallNew new
#endif

#ifdef DEBUG_STATEMENTS
///	Singleton used to write DebugStatements to a log file.
/*
	TODO: Make sure we don't get stuck in an infinite loop if logFile cannot be
	opened.
 */
class DebugLogFile
{
  public:
	///	Returns the sole instance of the class (not thread-safe, btw).
	static DebugLogFile& getInstance();

	///	Returns the temporary string buffer.
	std::wstringstream& getBuffer() {return tempBuffer;};
	///	Writes the temporary stringbuffer to the log file.
	void writeBuffer();

	///	Writes a wstring to the log file.
	//friend DebugLogFile& operator<<(DebugLogFile& out, const std::wstring& text);
  private:
	///	Constructor.
	DebugLogFile();
	///	Destructor.
	~DebugLogFile();

	///	The log file stream.
	UTF8File logFile;
	///	Temp string buffer.
	std::wstringstream tempBuffer;
};

#define DebugStatement(val) {DebugLogFile::getInstance().getBuffer() << val << std::endl << L"\t\t\t\t\t\t\t\t\t\t(" << __FILE__ << L"  line:" << __LINE__ << L")" << std::endl << std::endl; DebugLogFile::getInstance().writeBuffer();};
#else
#define DebugStatement(val) {};
#endif

#endif
