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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <pch.h>
00037 #include "Utility/RuntimeError.h"
00038
00039 using namespace std;
00040
00041 namespace Onikiri
00042 {
00043
00044 static bool g_inException = false;
00045 static bool g_suppressWarning = false;
00046
00047 static String DebugWhere( const RuntimeErrorInfo& info ){
00048 return String().format(
00049 "%s(%d):\n Method: %s\n Object: %s \n",
00050 info.file,
00051 info.line,
00052 info.func,
00053 info.name
00054 );
00055 }
00056
00057 static bool g_assertNoThrow = false;
00058
00059 void SetAssertNoThrow( bool noThrow )
00060 {
00061 g_assertNoThrow = noThrow;
00062 }
00063
00064 void SuppressWaning( bool suppress )
00065 {
00066 g_suppressWarning = suppress;
00067 }
00068
00069 bool IsInException()
00070 {
00071 return g_inException;
00072 }
00073
00074
00075
00076 void RuntimeErrorFunction( const RuntimeErrorInfo& info, const char* fmt, ...)
00077 {
00078 String str;
00079
00080 va_list arg;
00081 va_start(arg, fmt);
00082 str.format_arg(fmt, arg);
00083 va_end(arg);
00084
00085 std::runtime_error error("");
00086 if( String(info.file) == "" ){
00087 error = std::runtime_error(
00088 str + "\n\n"
00089 );
00090 }
00091 else{
00092 error = std::runtime_error(
00093 "\n" + DebugWhere( info ) + " Message: " + str + "\n"
00094 );
00095 }
00096
00097 if(g_inException){
00098
00099 return;
00100 }
00101 g_inException = true;
00102 throw error;
00103 }
00104
00105
00106
00107
00108 void RuntimeWarningFunction( const RuntimeErrorInfo& info, const char* fmt, ...)
00109 {
00110 String str;
00111
00112 va_list arg;
00113 va_start(arg, fmt);
00114 str.format_arg(fmt, arg);
00115 va_end(arg);
00116
00117
00118 String msg;
00119 if( String(info.file) == "" ){
00120 msg = str;
00121 }
00122 else{
00123 msg =
00124 DebugWhere( info ) +
00125 "Warning:\n" +
00126 str;
00127 }
00128
00129 if( !g_suppressWarning ){
00130 printf( "%s\n\n", msg.c_str() );
00131 }
00132 }
00133
00134
00135
00136
00137
00138 void AssertFunction( const RuntimeErrorInfo& info, bool assertCond, const char* fmt, ... )
00139 {
00140 if(assertCond)
00141 return;
00142 if(g_inException)
00143 return;
00144 g_inException = true;
00145
00146 if(g_assertNoThrow){
00147 assert(0);
00148 }
00149 else{
00150 String str;
00151
00152 va_list arg;
00153 va_start(arg, fmt);
00154 str.format_arg(fmt, arg);
00155 va_end(arg);
00156
00157 if( String(info.file) == "" ){
00158 throw std::runtime_error(
00159 str + "\n"
00160 );
00161 }
00162 else{
00163 throw std::runtime_error(
00164 DebugWhere( info ) +
00165 "Assertion failed.\n" +
00166 "Condition : " + info.cond + "\n"
00167 + str + "\n"
00168 );
00169 }
00170 }
00171 }
00172
00173 void AssertFunction( const RuntimeErrorInfo& info, bool assertCond )
00174 {
00175 if(assertCond)
00176 return;
00177 if(g_inException)
00178 return;
00179 g_inException = true;
00180
00181 if(g_assertNoThrow){
00182 assert(0);
00183 }
00184 else{
00185 throw std::runtime_error(
00186 DebugWhere( info ) +
00187 "Assertion failed.\n" +
00188 "Condition : " + info.cond + "\n"
00189 );
00190 }
00191 }
00192 }
00193