/*======================================================================================

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkDebugManager.cxx
	Version:		$Revision: 1761 $
	Date:			$Date: 2011-09-23 17:25:41 +0200 (Fr, 23 Sep 2011) $

	Copyright (c) 2010-2011
	Pattern Recognition Lab, 
    University Erlangen-Nuremberg, Germany
    All rights reserved.

	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 RITKCopyright.txt for more information.

	http://www5.informatik.uni-erlangen.de/research/software/ritk

========================================================================================*/

#include "ritkDebugManager.h"

#include <QMetaType>
#include <QString>
#include <sstream>
#include <iostream>

namespace ritk
{

//----------------------------------------------------------------------------
DebugManager::DebugManager() 
{
	/// Enable debugging by default
	m_DebugMode = Enabled;
	/// std::cout as default
	m_OutputStream = new QTextStream(stdout);

	// Internal communication
	qRegisterMetaType<QString>("QString");
	connect(this, SIGNAL(LogMessageSignal(QString)), this, SLOT(LogMessageSlot(QString)));
}


//----------------------------------------------------------------------------
DebugManager::~DebugManager()
{
}


//----------------------------------------------------------------------------
void
DebugManager::LogMessageSlot(QString Message)
{
	QMutexLocker locker(&m_Mutex);
	std::cout << Message.toStdString() << std::endl;
	if ( m_OutputStream )
	{
		*m_OutputStream << Message;
		m_OutputStream->flush();
	}
}




#if 0
//----------------------------------------------------------------------------
void
DebugManager::SetDebugMode(DebugManager::DebugModes Mode)
{
	m_DebugMode = Mode;
}


//----------------------------------------------------------------------------
DebugManager::DebugModes
DebugManager::GetDebugMode()
{
	return m_DebugMode;
}


//----------------------------------------------------------------------------
void 
DebugManager::SetOutputStream(std::ostream *stream)
{
	m_OutputStream = stream;
}


//----------------------------------------------------------------------------
std::ostream*
DebugManager::GetOutputStream()
{
	return m_OutputStream;
}
#endif


} // end namespace ritk


QTextStream& operator<<(QTextStream& out, std::string &s)
{
	out << s.c_str();
	return out;
}

QTextStream& operator<<(QTextStream& out, std::exception &e)
{
	std::stringstream ss;
	ss << e.what();
	out << ss.str().c_str();
	return out;
}
