/* \file Exceptions.cpp
 * \brief: implementation of the exceptions.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#else
#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
#endif

#include "./exceptions.h"
#include "../io/Print/Print.h"
#include "../io/Comm/IssmComm.h"

ErrorException::ErrorException(const string &what_arg){/*{{{*/

	what_str=what_arg;
	file_name="";
	function_name="";
	file_line=0;
}/*}}}*/
ErrorException::ErrorException(const string& what_file, const string& what_function,int what_line, const string& what_arg){/*{{{*/

	what_str=what_arg;
	file_name=what_file;
	function_name=what_function;
	file_line=what_line;
}/*}}}*/
ErrorException::~ErrorException() throw(){/*{{{*/
}/*}}}*/
const char* ErrorException::what() const throw(){/*{{{*/
	return what_str.c_str();
}/*}}}*/
void ErrorException::Report() const{/*{{{*/

	int my_rank;
	int num_procs;

	/*recover my_rank and num_procs:*/
	my_rank=IssmComm::GetRank();
	num_procs=IssmComm::GetSize();

	if (function_name=="" || file_line==0){ //WINDOWS
		_printf_("Error message: " << what());
	}
	else{
		if(num_procs==1){
			_printf_("\n??? Error in ==> " << file_name.c_str() << ":" << file_line << "\n");
			_printf_(function_name.c_str() << " error message: " << what() << "\n\n");
		}
		else{
			_printf_("\n[" << my_rank<< "] ??? Error using ==> " << file_name.c_str() << ":" << file_line << "\n");
			_printf_(  "[" << my_rank << "] " << function_name.c_str() << " error message: " << what() << "\n\n");
		}
	}
	return;
}/*}}}*/
const char* ErrorException::MatlabReport() const{/*{{{*/

	/*Output*/
	std::ostringstream buffer;

	if (this->file_line==0){ //WINDOWS
		 buffer << " error message: " << (this->what_str).c_str();
	}
	else{
		buffer << "\nError in ==> " << (this->file_name).c_str() << ":" << file_line << "\n";
		buffer << (this->function_name).c_str() << " error message: " << (this->what_str).c_str();
	}

	const string buffer2 = buffer.str();
	return buffer2.c_str();
}/*}}}*/
const char* ErrorException::PythonReport() const{/*{{{*/

	/*Output*/
	std::ostringstream buffer;

	if (this->file_line==0){ //WINDOWS
		buffer << " error message: " << (this->what_str).c_str();
	}
	else{
		buffer << "\nError in ==> " << (this->file_name).c_str() << ":" << file_line << "\n";
		buffer << (this->function_name).c_str() << " error message: " << (this->what_str).c_str();
	}

	const string buffer2 = buffer.str();
	return buffer2.c_str();
}/*}}}*/
