Exception.cpp

Go to the documentation of this file.
00001 /*
00002         This file is part of ALIZE which is an open-source tool for 
00003         speaker recognition.
00004 
00005     ALIZE is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU Lesser General Public License as 
00007     published by the Free Software Foundation, either version 3 of 
00008     the License, or any later version.
00009 
00010     ALIZE 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
00013     GNU 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 ALIZE.
00017     If not, see <http://www.gnu.org/licenses/>.
00018         
00019         ALIZE is a development project initiated by the ELISA consortium
00020         [alize.univ-avignon.fr/] and funded by the French Research 
00021         Ministry in the framework of the TECHNOLANGUE program 
00022         [www.technolangue.net]
00023 
00024         The ALIZE project team wants to highlight the limits of voice
00025         authentication in a forensic context.
00026         The "Person  Authentification by Voice: A Need of Caution" paper 
00027         proposes a good overview of this point (cf. "Person  
00028         Authentification by Voice: A Need of Caution", Bonastre J.F., 
00029         Bimbot F., Boe L.J., Campbell J.P., Douglas D.A., Magrin-
00030         chagnolleau I., Eurospeech 2003, Genova].
00031         The conclusion of the paper of the paper is proposed bellow:
00032         [Currently, it is not possible to completely determine whether the 
00033         similarity between two recordings is due to the speaker or to other 
00034         factors, especially when: (a) the speaker does not cooperate, (b) there 
00035         is no control over recording equipment, (c) recording conditions are not 
00036         known, (d) one does not know whether the voice was disguised and, to a 
00037         lesser extent, (e) the linguistic content of the message is not 
00038         controlled. Caution and judgment must be exercised when applying speaker 
00039         recognition techniques, whether human or automatic, to account for these 
00040         uncontrolled factors. Under more constrained or calibrated situations, 
00041         or as an aid for investigative purposes, judicious application of these 
00042         techniques may be suitable, provided they are not considered as infallible.
00043         At the present time, there is no scientific process that enables one to 
00044         uniquely characterize a person=92s voice or to identify with absolute 
00045         certainty an individual from his or her voice.]
00046         Contact Jean-Francois Bonastre for more information about the licence or
00047         the use of ALIZE
00048 
00049         Copyright (C) 2003-2010
00050         Laboratoire d'informatique d'Avignon [lia.univ-avignon.fr]
00051         ALIZE admin [alize@univ-avignon.fr]
00052         Jean-Francois Bonastre [jean-francois.bonastre@univ-avignon.fr]
00053 */
00054 
00055 #if !defined(ALIZEExceptioncpp)
00056 #define ALIZEExceptioncpp
00057 
00058 #include "Exception.h"
00059 
00060 /* g++-4.3 needs following inlcusions (seems to work without for g++-4.1 and 4.2) */
00061 #include <cstdio>    /* for: stderr fprintf sprintf popen fgets pclose */
00062 
00063 #if defined(__GNUC__)
00064 #include <unistd.h>  /* for: readlink getpid */
00065 #endif
00066 
00067 /* TODO: If you know a way to do it the clean C++ way, feel free ! */
00068 
00069 using namespace alize;
00070 
00071 //-------------------------------------------------------------------------
00072 Exception::Exception(const String& msg, const String& sourceFile, int line)
00073 :Object(), msg(msg), sourceFile(sourceFile), line(line), trace(stackTrace(getClassName())) {}
00074 //-------------------------------------------------------------------------
00075 /* NOTE: we need the name of the derived class in the superclass. But execution sequence is:
00076  * superclass-initializationlist, superclass-constructor, class-initializationlist, class-constructor.
00077  * The only way is to pass the className of the current class in the call to the superclass-constructor.
00078  */
00079 Exception::Exception(const String& msg, const String& sourceFile, int line, String callerName)
00080 :Object(), msg(msg), sourceFile(sourceFile), line(line), trace(stackTrace(callerName)) {}
00081 //-------------------------------------------------------------------------
00082 Exception::Exception(const Exception& e)
00083 :Object(),msg(e.msg),sourceFile(e.sourceFile),line(e.line),trace(e.trace) {}
00084 //-------------------------------------------------------------------------
00085 String Exception::toString() const
00086 {
00087   return trace  //stack trace is output in front, since self-made message will be extended by other FooException classes
00088     + Object::toString()
00089     + "\n  message   = \"" + msg + "\""
00090     + "\n  source file = " + sourceFile
00091     + "\n  line number = " + String::valueOf(line);
00092 }
00093 //-------------------------------------------------------------------------
00094 String Exception::getClassName() const { return "Exception"; }
00095 //-------------------------------------------------------------------------
00122 String Exception::stackTrace( const String callerName) const
00123 {
00124         //fprintf(stdout, "DBG: Exception::stackTrace() called with caller[%s]!\n", callerName.c_str()) ;
00125 
00128         if( callerName == "EOFException") {
00129                 return "" ;
00130         }
00131         if( callerName == "OutOfMemoryException") {
00132                 // don't allocate a String, since we don't have any memory any more... (implies putting msg twice hardcoded)
00133                 fprintf(stderr, "OutOfMemoryException: Exception::stackTrace() won't succeed in tracing the stack...");
00134                 return "OutOfMemoryException: Exception::stackTrace() won't succeed in tracing the stack..." ;
00135         }
00136 
00140         /*if( callerName == "FileNotFoundException") {
00141                 //return "" ;
00142         }*/
00143 
00144         String data ;
00145 
00146 #if defined(linux) || defined(__linux)          ///< fetch required bits (workaround the fact we don't have access to any info (argv[0] i.e.) )
00147         char* mistralProg = new char[2048];
00148         size_t length = readlink("/proc/self/exe", mistralProg, 2048);
00149         mistralProg[length] = '\0';
00150         char* myPid = new char[9]; //usually 32k -> 5chars+nul, max possible is 8chars
00151         sprintf( myPid, "%d", getpid() ) ;
00152 
00153         // TODO: escape mistralProg to avoid nasty things
00154         // with 'bt full', prints also local variables (not very useful if having only complex objects...)
00155         //String cmd = "gdb --batch -ex='bt full' " + String(mistralProg) +" "+ String(myPid) ;
00156 #if defined(THREAD)  //note: no threads in ALIZE!
00157         String cmd = "gdb --batch -ex='info threads' -ex=bt " + String(mistralProg) +" "+ String(myPid) ; //doesn't hurt if there's no threads split off
00158 #else
00159         String cmd = "gdb --batch -ex=bt " + String(mistralProg) +" "+ String(myPid) ;
00160 #endif
00161         data += callerName + "; stack trace by GDB [" + cmd +"]\n" ;
00162 
00163         // run cmd and read its output
00164         size_t MAX_BUFFER = 255 ;
00165         FILE *stream;
00166         char buffer[MAX_BUFFER];
00167         stream = popen( cmd.c_str(), "r");
00168         while( fgets(buffer, MAX_BUFFER, stream) != NULL ) {
00169            data += buffer ;
00170         }
00171         pclose(stream);
00172         
00173         /* //another way to do it (the above is +- a wrapper to this)
00174         pid_t pid;
00175         pid = fork();
00176         if ( pid < 0 ) {
00177                 printf( "fork failed" );
00178                 //exit(1);
00179         } else if ( pid ==0 ) {
00180                 printf ( "I'm the child (%d), my parent is %d\n", getpid(), getppid());
00181                 printf( "executing [gdb --batch -ex=bt %s %s]\n", mistralProg, myPid) ;
00182                 int ret = execlp( "gdb", "gdb", "--batch", "-ex=bt", mistralProg, myPid, (char *) NULL) ;
00183                 printf( "ERROR: exec returned with [%d]\n", ret) ;
00184         } else {
00185                 printf ( "I'm the parent (%d), I too have a parent (%d)\n", getpid(), getppid() );
00186         }
00187         */
00188 #else
00189         data = " *** Exception::stackTrace() uses gdb and GNU/Linux' /proc fs which are not available on others systems - won't trace the stack.\n" ;
00190 #endif
00191 
00192 
00193         //fprintf( stdout, "DBG: trace[%s]", data.c_str());
00194         return data;
00195 }
00196 //-------------------------------------------------------------------------
00197 Exception::~Exception() {}
00198 //-------------------------------------------------------------------------
00199 
00200 
00201 
00202 //-------------------------------------------------------------------------
00203 IndexOutOfBoundsException::IndexOutOfBoundsException(const String& msg,
00204         const String& sourceFile, int line, long index, long limit)
00205 :Exception(msg, sourceFile, line, getClassName()), index(index), limit(limit) {}
00206 //-------------------------------------------------------------------------
00207 IndexOutOfBoundsException::IndexOutOfBoundsException(
00208                 const IndexOutOfBoundsException& e)
00209 :Exception(e.msg, e.sourceFile, e.line, getClassName()),
00210  index(e.index), limit(e.limit) {}
00211 //-------------------------------------------------------------------------
00212 String IndexOutOfBoundsException::toString() const
00213 {
00214   return Exception::toString()
00215     + "\n  index " + String::valueOf(index)
00216     + " >= limit " + String::valueOf(limit);
00217 }
00218 //-------------------------------------------------------------------------
00219 String IndexOutOfBoundsException::getClassName() const
00220 { return "IndexOutOfBoundsException"; }
00221 //-------------------------------------------------------------------------
00222 IndexOutOfBoundsException::~IndexOutOfBoundsException() {}
00223 //-------------------------------------------------------------------------
00224 
00225 
00226 
00227 
00228 //-------------------------------------------------------------------------
00229 IOException::IOException(const String& msg, const String& sourceFile,
00230                      int line, const FileName& f)
00231 :Exception(msg, sourceFile, line, getClassName()), fileName(f) {}
00232 //-------------------------------------------------------------------------
00233 IOException::IOException(const String& msg, const String& sourceFile,
00234                      int line, const FileName& f, const String callerName)
00235 :Exception(msg, sourceFile, line, callerName), fileName(f) {}
00236 //-------------------------------------------------------------------------
00237 IOException::IOException(const IOException& e)
00238 :Exception(e.msg, e.sourceFile, e.line, getClassName()) {}
00239 //-------------------------------------------------------------------------
00240 String IOException::toString() const
00241 { return Exception::toString() + "\n  fileName =  " + fileName; }
00242 //-------------------------------------------------------------------------
00243 String IOException::getClassName() const { return "IOException"; }
00244 //-------------------------------------------------------------------------
00245 IOException::~IOException() {}
00246 //-------------------------------------------------------------------------
00247 
00248 
00249 
00250 
00251 //-------------------------------------------------------------------------
00252 IdAlreadyExistsException::IdAlreadyExistsException(const String& msg,
00253                     const String& sourceFile, int line)
00254 :Exception(msg, sourceFile, line, getClassName()) {}
00255 //-------------------------------------------------------------------------
00256 IdAlreadyExistsException::IdAlreadyExistsException(
00257                   const IdAlreadyExistsException& e)
00258 :Exception(e.msg, e.sourceFile, e.line, getClassName()) {}
00259 //-------------------------------------------------------------------------
00260 String IdAlreadyExistsException::getClassName() const
00261 { return "IdAlreadyExistsException"; }
00262 //-------------------------------------------------------------------------
00263 IdAlreadyExistsException::~IdAlreadyExistsException() {}
00264 //-------------------------------------------------------------------------
00265 
00266 
00267 
00268 
00269 //-------------------------------------------------------------------------
00270 InvalidDataException::InvalidDataException(const String& msg,
00271         const String& sourceFile, int line, const FileName& f)
00272 :IOException(msg, sourceFile, line, f, getClassName()) {}
00273 //-------------------------------------------------------------------------
00274 InvalidDataException::InvalidDataException(const InvalidDataException& e)
00275 :IOException(e.msg, e.sourceFile, e.line, e.fileName, getClassName()) {}
00276 //-------------------------------------------------------------------------
00277 String InvalidDataException::getClassName() const
00278 { return "InvalidDataException"; }
00279 //-------------------------------------------------------------------------
00280 InvalidDataException::~InvalidDataException() {}
00281 //-------------------------------------------------------------------------
00282 
00283 
00284 
00285 
00286 //-------------------------------------------------------------------------
00287 OutOfMemoryException::OutOfMemoryException(const String& msg,
00288                     const String& sourceFile, int line)
00289 :Exception(msg, sourceFile, line, getClassName()) {}
00290 //-------------------------------------------------------------------------
00291 OutOfMemoryException::OutOfMemoryException(const OutOfMemoryException& e)
00292 :Exception(e.msg, e.sourceFile, e.line, getClassName()) {}
00293 //-------------------------------------------------------------------------
00294 String OutOfMemoryException::getClassName() const
00295 { return "OutOfMemoryException"; }
00296 //-------------------------------------------------------------------------
00297 OutOfMemoryException::~OutOfMemoryException() {}
00298 //-------------------------------------------------------------------------
00299 
00300 
00301 
00302 
00303 //-------------------------------------------------------------------------
00304 FileNotFoundException::FileNotFoundException(const String& msg,
00305         const String& sourceFile, int line, const FileName& f)
00306 :IOException(msg, sourceFile, line, f, getClassName()) {}
00307 //-------------------------------------------------------------------------
00308 FileNotFoundException::FileNotFoundException(
00309                   const FileNotFoundException& e)
00310 :IOException(e.msg, e.sourceFile, e.line, e.fileName, getClassName()) {}
00311 //-------------------------------------------------------------------------
00312 String FileNotFoundException::getClassName() const
00313 { return "FileNotFoundException"; }
00314 //-------------------------------------------------------------------------
00315 FileNotFoundException::~FileNotFoundException() {}
00316 //-------------------------------------------------------------------------
00317 
00318 
00319 
00320 
00321 //-------------------------------------------------------------------------
00322 ParamNotFoundInConfigException::ParamNotFoundInConfigException(const String& msg,
00323         const String& sourceFile, int line)
00324 :Exception(msg, sourceFile, line, getClassName()) {}
00325 //-------------------------------------------------------------------------
00326 ParamNotFoundInConfigException::ParamNotFoundInConfigException(
00327                   const ParamNotFoundInConfigException& e)
00328 :Exception(e.msg, e.sourceFile, e.line, getClassName()) {}
00329 //-------------------------------------------------------------------------
00330 String ParamNotFoundInConfigException::getClassName() const
00331 { return "ParamNotFoundInConfigException"; }
00332 //-------------------------------------------------------------------------
00333 ParamNotFoundInConfigException::~ParamNotFoundInConfigException() {}
00334 //-------------------------------------------------------------------------
00335 
00336 
00337 
00338 
00339 //-------------------------------------------------------------------------
00340 ConfigCheckException::ConfigCheckException(const String& msg,
00341         const String& sourceFile, int line)
00342 :Exception(msg, sourceFile, line, getClassName()) {}
00343 //-------------------------------------------------------------------------
00344 ConfigCheckException::ConfigCheckException(
00345                   const ConfigCheckException& e)
00346 :Exception(e.msg, e.sourceFile, e.line, getClassName()) {}
00347 //-------------------------------------------------------------------------
00348 String ConfigCheckException::getClassName() const
00349 { return "ConfigCheckException"; }
00350 //-------------------------------------------------------------------------
00351 ConfigCheckException::~ConfigCheckException() {}
00352 //-------------------------------------------------------------------------
00353 
00354 
00355 
00356 
00357 //-------------------------------------------------------------------------
00358 EOFException::EOFException(const String& msg, const String& sourceFile,
00359                                                int line, const FileName& f)
00360 :IOException(msg, sourceFile, line, f, getClassName()) {}
00361 //-------------------------------------------------------------------------
00362 EOFException::EOFException(const EOFException& e)
00363 :IOException(e.msg, e.sourceFile, e.line, e.fileName, getClassName()) {}
00364 //-------------------------------------------------------------------------
00365 String EOFException::getClassName() const { return "EOFException"; }
00366 //-------------------------------------------------------------------------
00367 EOFException::~EOFException() {}
00368 //-------------------------------------------------------------------------
00369 
00370 /*void* operator new(sizet n)
00371 {
00372   void* p = new (std::nothrow) (n);
00373   return p;
00374 }
00375 */
00376 #endif // !defined(ALIZEExceptioncpp)