/* \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 "../shared.h"
#include "../../include/include.h"

ErrorException::ErrorException(const string &what_arg){

	what_str=what_arg;
	file_name="";
	function_name="";
	file_line=0;
}

ErrorException::ErrorException(string what_file,string what_function,int what_line,string what_arg){

	what_str=what_arg;
	file_name=what_file;
	function_name=what_function;
	file_line=what_line;
}

ErrorException::~ErrorException() throw(){
	extern int num_procs;
	/*We want the report only for matlab modules, otherwise we get twice the report
	 * We assume that if num_procs==1, it is a module (FIXME)*/
	if(num_procs==1) this->Report();
}

const char* ErrorException::what() const throw(){
	return what_str.c_str();
}

void ErrorException::Report(){
	extern int my_rank;
	extern int num_procs;

	if (function_name=="" || file_line==0){ //WINDOWS
		printf("%s%s","Error message: ",what());
	}
	else{
		if(num_procs==1){
			printf("\n??? Error using ==> %s:%i\n",file_name.c_str(),file_line);
			printf("%s error message: %s\n\n",function_name.c_str(),what());
		}
		else{
			printf("\n[%i] ??? Error using ==> %s:%i\n",my_rank,file_name.c_str(),file_line);
			printf("[%i] %s error message: %s\n\n",my_rank,function_name.c_str(),what());
		}
	}
	return;
}
