00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <cstdlib>
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 }
00168