IT++ Logo Newcom Logo

itassert.cpp

Go to the documentation of this file.
00001 
00033 #ifndef _MSC_VER
00034 #  include <itpp/config.h>
00035 #else
00036 #  include <itpp/config_msvc.h>
00037 #endif
00038 
00039 #include <itpp/base/itassert.h>
00040 #include <iostream>
00041 #include <sstream>
00042 #include <string>
00043 #include <stdexcept>
00044 
00045 
00046 namespace itpp {
00047 
00048 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00049 
00050   static bool warnings_enabled = true;
00051   static std::ostream *warn = &std::cerr;
00052 #ifdef ITPP_EXCEPTIONS
00053   static bool it_using_exceptions = true;
00054 #else
00055   static bool it_using_exceptions = false;
00056 #endif
00057 
00058 #endif //DOXYGEN_SHOULD_SKIP_THIS
00059 
00060   void it_assert_f(std::string ass, std::string msg, std::string file, int line)
00061   {
00062     std::ostringstream line_str;
00063     line_str << line << std::flush;
00064 
00065     std::string error = "*** Assertion failed in "; 
00066     error += file;
00067     error += " on line ";
00068     error += line_str.str();
00069     error += ":\n";
00070     error += msg;
00071     error += " (";
00072     error += ass;
00073     error += ")";
00074     std::cerr << error << std::endl << std::flush;
00075 #ifdef ITPP_EXCEPTIONS
00076     if (it_using_exceptions)
00077       throw std::runtime_error(error);
00078     else
00079 #endif
00080       abort();
00081   }
00082 
00083   void it_error_f(std::string msg, std::string file, int line)
00084   {
00085     std::ostringstream line_str;
00086     line_str << line << std::flush;
00087 
00088     std::string error = "*** Error in ";
00089     error += file;
00090     error += " on line ";
00091     error += line_str.str();
00092     error += ":";
00093     error += msg;
00094     std::cerr << error << std::endl << std::flush;
00095 #ifdef ITPP_EXCEPTIONS
00096     if (it_using_exceptions)
00097       throw std::runtime_error(error);
00098     else
00099 #endif
00100       abort();
00101   }
00102 
00103   void it_warning_f(std::string msg, std::string file, int line)
00104   {
00105     if (warnings_enabled)
00106       (*warn) << "*** Warning in " << file << " on line " << line << ":" 
00107               << std::endl << msg << std::endl << std::flush;
00108   }
00109 
00110   void it_enable_exceptions(bool on)
00111   {
00112     it_using_exceptions = on;
00113   }
00114 
00115   void it_enable_warnings()
00116   {
00117     warnings_enabled = true;
00118   }
00119 
00120   void it_disable_warnings()
00121   {
00122     warnings_enabled = false;
00123   }
00124 
00125   void it_redirect_warnings(std::ostream *warn_stream)
00126   {
00127     warn = warn_stream;
00128   }
00129 
00130 } //namespace itpp
SourceForge Logo

Generated on Thu Apr 19 14:19:51 2007 for IT++ by Doxygen 1.4.6