src/Utility/RuntimeError.cpp

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 //
00033 // Debug utility
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             //printf( "%s\n",  error.what() );
00099             return;
00100         }
00101         g_inException = true;
00102         throw error;
00103     }
00104 
00105     //
00106     // Runtime warning
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     // Assert
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 

Onikiri2に対してTue Jun 18 14:34:27 2013に生成されました。  doxygen 1.4.7