IT++ Logo

itassert.cpp

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

Generated on Sat Apr 19 10:42:04 2008 for IT++ by Doxygen 1.5.5