Blender V2.61 - r43446

Exception.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of VideoTexture library
00004 
00005 Copyright (c) 2006 The Zdeno Ash Miklas
00006 
00007 This program is free software; you can redistribute it and/or modify it under
00008 the terms of the GNU Lesser General Public License as published by the Free Software
00009 Foundation; either version 2 of the License, or (at your option) any later
00010 version.
00011 
00012 This program is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License along with
00017 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00018 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00019 http://www.gnu.org/copyleft/lesser.txt.
00020 -----------------------------------------------------------------------------
00021 */
00022 
00027 #include <sstream>
00028 #include <fstream>
00029 
00030 #include <PyObjectPlus.h>
00031 
00032 #include "Exception.h"
00033 
00034 
00035 // exception identificators
00036 ExceptionID ErrGeneral, ErrNotFound;
00037 
00038 // exception descriptions
00039 ExpDesc errGenerDesc (ErrGeneral, "General Error");
00040 ExpDesc errNFoundDesc (ErrNotFound, "Error description not found");
00041 
00042 
00043 
00044 // implementation of ExpDesc
00045 
00046 // constructor
00047 ExpDesc::ExpDesc (ExceptionID & exp, const char * desc, RESULT hres)
00048 : m_expID(exp), m_hRslt(hres), m_description(desc)
00049 {
00050 }
00051 
00052 // destructor
00053 ExpDesc::~ExpDesc (void) {}
00054 
00055 // list of descriptions
00056 std::vector<ExpDesc*> ExpDesc::m_expDescs;
00057 
00058 
00059 // class Exception
00060 
00061 
00062 // last exception description
00063 std::string Exception::m_lastError;
00064 
00065 // log file name
00066 const char * Exception::m_logFile = NULL;
00067 
00068 
00069 // basic constructor
00070 Exception::Exception ()
00071 {
00072     // default values
00073     m_expID = &ErrNotFound;
00074     m_hRslt = S_OK;
00075     m_line = 0;
00076 }
00077 
00078 
00079 // destructor
00080 Exception::~Exception () throw() { }
00081 
00082 
00083 // copy constructor
00084 Exception::Exception (const Exception & xpt)
00085 { copy (xpt); }
00086 
00087 
00088 // assignment operator
00089 Exception & Exception::operator= (const Exception & xpt)
00090 { copy (xpt); return *this; }
00091 
00092 
00093 // get exception description
00094 const char * Exception::what()
00095 {
00096     // set exception description
00097     setXptDesc();
00098     // return c string
00099     return m_desc.c_str();
00100 }
00101 
00102 
00103 // debug version - with file and line of exception
00104 Exception::Exception (ExceptionID & expID, RESULT rslt, const char * fil, int lin)
00105 : m_expID (&expID), m_hRslt (rslt)
00106 {
00107     // set file and line
00108     if (fil[0] != '\0' || lin > 0)
00109         setFileLine (fil, lin);
00110 }
00111 
00112 
00113 // set file and line
00114 void Exception::setFileLine (const char * fil, int lin)
00115 {
00116     if (fil != NULL) m_fileName = fil;
00117     m_line = lin;
00118 }
00119 
00120 
00121 // report exception
00122 void Exception::report(void)
00123 {
00124     // set exception description
00125     setXptDesc();
00126     // set python error
00127     PyErr_SetString(PyExc_RuntimeError, what());
00128     // if log file is set
00129     if (m_logFile != NULL)
00130     {
00131         // write description to log
00132         std::ofstream logf (m_logFile, std::ios_base::app);
00133         logf << m_fileName << ':' << m_line << ':' << m_desc << std::endl;
00134         logf.flush();
00135         logf.close();
00136     }
00137 }
00138 
00139 
00140 // set exception description
00141 void Exception::setXptDesc (void)
00142 {
00143     // if description is not set
00144     if (m_desc.size() == 0)
00145     {
00146         // start of search                           -1
00147         // found description "NotFound"               0
00148         // found description without matching result  1
00149         // found description with matching result     2
00150         int best = -1;
00151         // find exception description
00152         for (std::vector<ExpDesc*>::iterator it = ExpDesc::m_expDescs.begin(); it != ExpDesc::m_expDescs.end(); ++it)
00153         {
00154             // use "NotFound", if there is not better
00155             if (best < 0 && (*it)->isExp(&ErrNotFound) > 0)
00156             {
00157                 (*it)->loadDesc(m_desc);
00158                 best = 0;
00159             }
00160             // match exception
00161             int nBest = (*it)->isExp(m_expID, m_hRslt);
00162             // if exception is matching better
00163             if (nBest > 0 && best < nBest)
00164             {
00165                 // set description
00166                 (*it)->loadDesc(m_desc);
00167                 best = nBest;
00168                 // if matching exactly, finish search
00169                 if (best == 2) break;
00170             }
00171         }
00172         // add result code
00173         // length of result code
00174         const size_t rsltSize = 11;
00175         // delimit description
00176         //const char delimRslt[] = ": ";
00177         // set text of description
00178         char rsltTxt[rsltSize];
00179         std::ostringstream os;
00180         os << std::hex << m_hRslt << ": " << '\0';
00181         // copy result to description
00182         m_desc.insert(0, rsltTxt);
00183         // copy exception description to last exception string
00184         m_lastError = m_desc;
00185     }
00186 }
00187 
00188 
00189 // copy exception data
00190 void Exception::copy (const Exception & xpt)
00191 {
00192     // standard data
00193     m_expID = xpt.m_expID;
00194     m_hRslt = xpt.m_hRslt;
00195     m_desc = xpt.m_desc;
00196 
00197     // debug data
00198     m_fileName = xpt.m_fileName;
00199     m_line = xpt.m_line;
00200 }
00201 
00202 void registerAllExceptions(void)
00203 {
00204     errGenerDesc.registerDesc();
00205     errNFoundDesc.registerDesc();
00206     MaterialNotAvailDesc.registerDesc();
00207     ImageSizesNotMatchDesc.registerDesc();
00208     ImageHasExportsDesc.registerDesc();
00209     InvalidColorChannelDesc.registerDesc();
00210     SceneInvalidDesc.registerDesc();
00211     CameraInvalidDesc.registerDesc();
00212     ObserverInvalidDesc.registerDesc();
00213     MirrorInvalidDesc.registerDesc();
00214     MirrorSizeInvalidDesc.registerDesc();
00215     MirrorNormalInvalidDesc.registerDesc();
00216     MirrorHorizontalDesc.registerDesc();
00217     MirrorTooSmallDesc.registerDesc();
00218     SourceVideoEmptyDesc.registerDesc();
00219     SourceVideoCreationDesc.registerDesc();
00220 }