//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * Mayaエラー出力実装
 * @author Junpee
 */

#include "System/stdafx.h"
#include "System/MayaErrorOutput.h"
#include "Core/System/StringMethod.h"

namespace LampForMaya{

//------------------------------------------------------------------------------
// 初期化
void MayaErrorOutput::initialize(){
	// エラーハンドラ変更
	ErrorOutput::setErrorHandler(MayaErrorOutput::errorHandler);
}
//------------------------------------------------------------------------------
// メッセージ出力
void MayaErrorOutput::mayaMessageOutput(const char* message){
	MString messageString(message);
	MGlobal::displayInfo(messageString);
	StdOutputDebugString(messageString.asChar());
}
//------------------------------------------------------------------------------
// メッセージ出力
void MayaErrorOutput::mayaMessageOutput(const String& message){
	MString messageString(message.getBytes());
	MGlobal::displayInfo(messageString);
	StdOutputDebugString(messageString.asChar());
}
//------------------------------------------------------------------------------
// メッセージ出力
void MayaErrorOutput::mayaMessageOutput(const MString& message){
	MGlobal::displayInfo(message);
	StdOutputDebugString(message.asChar());
}
//------------------------------------------------------------------------------
// エラー出力
void MayaErrorOutput::mayaErrorOutput(
	const char* message, const char* fileName, int lineNumber){
	String errorString;
	errorString.format("%s\n%s %d\n", message, fileName, lineNumber);
	ErrorOut(errorString);
}
//------------------------------------------------------------------------------
// エラー出力
void MayaErrorOutput::mayaErrorOutput(
	const String& message, const char* fileName, int lineNumber){
	String errorString;
	errorString.format("%s\n%s %d\n",
		message.getBytes(), fileName, lineNumber);
	ErrorOut(errorString);
}
//------------------------------------------------------------------------------
// エラー出力
void MayaErrorOutput::mayaErrorOutput(
	const MString& message, const char* fileName, int lineNumber){
	String errorString;
	errorString.format("%s\n%s %d\n",
		message.asChar(), fileName, lineNumber);
	ErrorOut(errorString);
}
//------------------------------------------------------------------------------
// エラーチェック
bool MayaErrorOutput::mayaErrorCheck(
	const MStatus& status, const char* fileName, int lineNumber){
	if(!status){
		String errorString;
		errorString.format("%s\n%s %d\n",
			status.errorString().asChar(), fileName, lineNumber);
		ErrorOut(errorString);
		return false;
	}
	return true;
}
//------------------------------------------------------------------------------
// OpenGLエラーチェック
bool MayaErrorOutput::mayaOpenGLErrorCheck(
	const char* fileName, int lineNumber){
	GLenum mayaOpenGLErrorCode = glGetError();
	if(mayaOpenGLErrorCode != GL_NO_ERROR){
		String errorString;
		errorString.format("%s\n%s %d\n",
			(const char*)gluErrorString(mayaOpenGLErrorCode),
			fileName, lineNumber);
		ErrorOut(errorString);
		return false;
	}
	return true;
}
//------------------------------------------------------------------------------
// Maya用エラーハンドラ
void MayaErrorOutput::errorHandler(const char* message){
	MString errorString(message);
	MGlobal::displayError(errorString);
	StdOutputDebugString(errorString.asChar());
#ifdef _DEBUG
	// Maya単体で起動している場合にブレークすると、Mayaが強制終了します。
	_asm{ int 3 }
#endif
}
//------------------------------------------------------------------------------
} // End of namespace LampForMaya
//------------------------------------------------------------------------------
