Assert.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #include "Assert.hxx"
00023 #include <iostream>
00024 #include <csignal>
00025 #if defined(__linux__)
00026 #include <execinfo.h>
00027 #endif
00028 
00029 
00030 namespace CLAM {
00031 
00032 void DumpBacktrace(std::ostream & os)
00033 {
00034 #if defined(__linux__)
00035 
00036         void *bt_array[100];    // 100 should be enough ?!?
00037         char **bt_strings;
00038         int num_entries;
00039 
00040         if ((num_entries = backtrace(bt_array, 100)) < 0) {
00041                 os << " Unable to generate a backtrace" << std::endl;
00042                 return;
00043         }
00044 
00045         if ((bt_strings = backtrace_symbols(bt_array, num_entries)) == NULL) {
00046                 os << " Unable to adquire symbols names for the backtrace" << std::endl;
00047                 return;
00048         }
00049 
00050         os << "\n Backtrace:\n" << std::endl;
00051         for (int i = 0; i < num_entries; i++) {
00052                 os << "[" << i << "] " <<  bt_strings[i] << std::endl;
00053         }
00054         free(bt_strings);
00055 #else
00056         os << " Unable to adquire symbols names for the backtrace" << std::endl;
00057 #endif
00058 }
00059 
00060 
00061 // by default, CLAM asserts must breakpoint
00062 // we'll want to disable breakpoints for automatic assertion testing 
00063 // purposes
00064 bool disabledCLAMAssertBreakpoint = false;
00065 
00066 // Assert related
00067 
00068 static void DefaultAssertHandler(const char* message, const char* filename, int lineNumber )
00069 {
00070         std::cerr << "##########################################################" << std::endl;
00071         std::cerr << "################### ASSERTION FAILED #####################" << std::endl;
00072         std::cerr << "##########################################################" << std::endl;
00073         std::cerr << "At file " << filename << " line " << lineNumber << std::endl;
00074         std::cerr << message << std::endl;
00075         DumpBacktrace(std::cerr);
00076 }
00077 
00078 static AssertFailedHandlerType CurrentAssertFailedHandler=DefaultAssertHandler;
00079 
00080 AssertFailedHandlerType SetAssertFailedHandler(AssertFailedHandlerType handler) {
00081         AssertFailedHandlerType oldHandler = CurrentAssertFailedHandler;
00082         CurrentAssertFailedHandler = handler;
00083         return oldHandler;
00084 }
00085 
00086 void ExecuteAssertFailedHandler(const char* message, const char* filename, int lineNumber )
00087 {
00088         CurrentAssertFailedHandler(message,filename,lineNumber);
00089 }
00090 
00091 bool ErrAssertionFailed::breakpointInCLAMAssertEnabled = true;
00092 
00093 ErrAssertionFailed::ErrAssertionFailed(const char* message, const char* filename, int lineNumber)
00094         : Err(message)
00095 {
00096         if (!breakpointInCLAMAssertEnabled) return; 
00097 
00098         CurrentAssertFailedHandler( message, filename, lineNumber );
00099 }
00100 
00101 // Warning related
00102 
00103 static void DefaultWarningHandler(const char* message, const char* filename, int lineNumber )
00104 {
00105         std::cerr << "##########################################################" << std::endl;
00106         std::cerr << "######################## WARNING #########################" << std::endl;
00107         std::cerr << "##########################################################" << std::endl;
00108         std::cerr << "At file " << filename << " line " << lineNumber << std::endl;
00109         std::cerr << message << std::endl;
00110 }
00111 
00112 static WarningHandlerType CurrentWarningHandler=DefaultWarningHandler;
00113 
00114 WarningHandlerType SetWarningHandler(WarningHandlerType handler) {
00115         WarningHandlerType oldHandler = CurrentWarningHandler;
00116         CurrentWarningHandler = handler;
00117         return oldHandler;
00118 }
00119 
00120 void ExecuteWarningHandler(const char* message, const char* filename, int lineNumber )
00121 {
00122         CurrentWarningHandler(message,filename,lineNumber);
00123 }
00124 
00125 #if 0 //  defined(__linux__)
00126 class SystemSignalTrapper
00127 {
00128         int _signal;
00129         sighandler_t _oldHandler;
00130 public:
00131         SystemSignalTrapper(int signal, sighandler_t handler) :
00132                 _signal(signal)
00133         {
00134                 _oldHandler = std::signal(signal, handler);
00135         }
00136         ~SystemSignalTrapper()
00137         {
00138                 std::signal(_signal, _oldHandler);
00139         }
00140 };
00141 void segvSignalHandler(int myInt)
00142 {
00143         std::cerr << std::endl;
00144         std::cerr << "##########################################################" << std::endl;
00145         std::cerr << "#################### BAD MEMORY ACCES ####################" << std::endl;
00146         std::cerr << "##########################################################" << std::endl;
00147         DumpBacktrace(std::cerr);
00148         std::abort();
00149 }
00150 
00151 static SystemSignalTrapper segvSignalTrapper(SIGSEGV,segvSignalHandler);
00152 #endif //defined linux
00153 
00154 
00155 }
00156 
00157 

Generated on Tue Jun 19 20:34:52 2007 for CLAM-Development by  doxygen 1.5.2