Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

ErrorOutput.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * エラー出力実装
00022  * @author Junpee
00023  */
00024 
00025 #include <cstdlib>// exitに必要
00026 #include "LampBasic.h"
00027 #include "Core/System/StringMethod.h"
00028 #include "Core/Debug/ErrorOutput.h"
00029 #include "Core/Debug/Logger.h"
00030 #include "Core/Debug/DebugStringTemporary.h"
00031 #include "Input/System/LampInput.h"
00032 #include "Graphics/System/LampGraphics.h"
00033 
00034 namespace Lamp{
00035 
00036 // エラーハンドラ
00037 ErrorOutput::ErrorHandler ErrorOutput::handler_ =
00038     ErrorOutput::defaultErrorHandler;
00039 
00040 // ロガー
00041 Logger* ErrorOutput::logger_ = NULL;
00042 
00043 //------------------------------------------------------------------------------
00044 // 初期化
00045 void ErrorOutput::initialize(const char* fileName){
00046     if(fileName == NULL){ return; }
00047     logger_ = new Logger(fileName);
00048 }
00049 //------------------------------------------------------------------------------
00050 // 後始末
00051 void ErrorOutput::finalize(){
00052     SafeDelete(logger_);
00053 }
00054 //------------------------------------------------------------------------------
00055 // デバッグ時
00056 #ifdef _DEBUG
00057 //------------------------------------------------------------------------------
00058 // エラー出力
00059 int ErrorOutput::print(const char* format, ...){
00060     va_list args;
00061     va_start(args, format);
00062     int size = StdVsnprintf(DebugStringTemporary::buffer_,
00063         DebugStringTemporary::bufferSize_ - 1, format, args);
00064     if(size < 0){
00065         ErrorOut(String("エラー出力のフォーマットに失敗しました。"));
00066         return size;
00067     }
00068     DebugStringTemporary::buffer_[size] = '\0';
00069     va_end(args);
00070     // 最高優先度でロガーへ出力
00071     if(logger_ != NULL){
00072         logger_->output(
00073             Logger::fewLevel, String(DebugStringTemporary::buffer_));
00074         logger_->flush();
00075     }
00076     // 入力のロギングを行っていれば終了しておく
00077     if(LampInput::isLogging()){ LampInput::endLogging(); }
00078     // エラーハンドラの呼び出し
00079     (*handler_)(DebugStringTemporary::buffer_);
00080     return size;
00081 }
00082 //------------------------------------------------------------------------------
00083 // エラー出力
00084 int ErrorOutput::print(const String& string){
00085     // 最高優先度でロガーへ出力
00086     if(logger_ != NULL){
00087         logger_->output(Logger::fewLevel, string);
00088         logger_->flush();
00089     }
00090     // 入力のロギングを行っていれば終了しておく
00091     if(LampInput::isLogging()){ LampInput::endLogging(); }
00092     // エラーハンドラの呼び出し
00093     (*handler_)(string.getBytes());
00094     return string.getSize();
00095 }
00096 //------------------------------------------------------------------------------
00097 // デフォルトエラーハンドラ
00098 void ErrorOutput::defaultErrorHandler(const char* message){
00099     StdOutputDebugString("致命的なエラーが発生したので強制終了します\n");
00100     StdOutputDebugString(message);
00101     StdOutputDebugString("\n");
00102     StdPrintf("致命的なエラーが発生したので強制終了します\n%s\n", message);
00103     // デバッガで中断する。
00104     _asm{ int 3 }
00105     // メッセージ出力
00106     MessageBox(LampGraphics::getWindowHandle(), message,
00107         "致命的なエラーが発生したので強制終了します",
00108         (MB_OK | MB_ICONERROR | MB_APPLMODAL));
00109     // システムを強制終了
00110     std::exit(EXIT_FAILURE);
00111 }
00112 //------------------------------------------------------------------------------
00113 // 非デバッグ時
00114 #else // ! _DEBUG
00115 //------------------------------------------------------------------------------
00116 // エラー出力
00117 int ErrorOutput::print(const char* format, ...){
00118     va_list args;
00119     va_start(args, format);
00120     int size = StdVsnprintf(DebugStringTemporary::buffer_,
00121         DebugStringTemporary::bufferSize_ - 1, format, args);
00122     if(size < 0){
00123         ErrorOut("エラー出力のフォーマットに失敗しました。");
00124         return size;
00125     }
00126     DebugStringTemporary::buffer_[size] = '\0';
00127     va_end(args);
00128     // 最高優先度でロガーへ出力
00129     if(logger_ != NULL){
00130         logger_->output(Logger::fewLevel,
00131             String(DebugStringTemporary::buffer_));
00132         logger_->flush();
00133     }
00134     // 入力のロギングを行っていれば終了しておく
00135     if(LampInput::isLogging()){ LampInput::endLogging(); }
00136     // エラーハンドラの呼び出し
00137     (*handler_)(DebugStringTemporary::buffer_);
00138     return size;
00139 }
00140 //------------------------------------------------------------------------------
00141 // エラー出力
00142 int ErrorOutput::print(const String& string){
00143     // 最高優先度でロガーへ出力
00144     if(logger_ != NULL){
00145         logger_->output(Logger::fewLevel, string);
00146         logger_->flush();
00147     }
00148     // 入力のロギングを行っていれば終了しておく
00149     if(LampInput::isLogging()){ LampInput::endLogging(); }
00150     // エラーハンドラの呼び出し
00151     (*handler_)(string.getBytes());
00152     return string.getSize();
00153 }
00154 //------------------------------------------------------------------------------
00155 // デフォルトエラーハンドラ
00156 void ErrorOutput::defaultErrorHandler(const char* message){
00157     StdPrintf("致命的なエラーが発生したので強制終了します\n%s\n", message);
00158     // メッセージ出力
00159     MessageBox(LampGraphics::getWindowHandle(), message,
00160         "致命的なエラーが発生したので強制終了します",
00161         (MB_OK | MB_ICONERROR | MB_APPLMODAL));
00162     // システムを強制終了
00163     std::exit(EXIT_FAILURE);
00164 }
00165 //------------------------------------------------------------------------------
00166 #endif// End of _DEBUG
00167 } // End of namespace Lamp
00168 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:30 2005 for Lamp by doxygen 1.3.2