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 {
00044 
00046 static bool warnings_enabled = true;
00047 static bool file_line_info_enabled = true;
00048 static std::ostream *warn = &std::cerr;
00050 
00051 void it_assert_f(std::string ass, std::string msg, std::string file, int line)
00052 {
00053   std::ostringstream error;
00054   if (file_line_info_enabled) {
00055     error << "*** Assertion failed in " << file << " on line " << line
00056     << ":\n" << msg << " (" << ass << ")\n";
00057   }
00058   else {
00059     error << msg << " (" << ass << ")\n";
00060   }
00061   std::cerr << error.str() << std::flush;
00062 #ifdef ITPP_EXCEPTIONS
00063   throw std::runtime_error(error.str());
00064 #else
00065   abort();
00066 #endif
00067 }
00068 
00069 void it_error_f(std::string msg, std::string file, int line)
00070 {
00071   std::ostringstream error;
00072   if (file_line_info_enabled) {
00073     error << "*** Error in " << file << " on line " << line << ":\n"
00074     << msg << "\n";
00075   }
00076   else {
00077     error << msg << "\n";
00078   }
00079   std::cerr << error.str() << std::flush;
00080 #ifdef ITPP_EXCEPTIONS
00081   throw std::runtime_error(error.str());
00082 #else
00083   abort();
00084 #endif
00085 }
00086 
00087 void it_info_f(std::string msg)
00088 {
00089   std::cerr << msg << std::flush;
00090 }
00091 
00092 void it_warning_f(std::string msg, std::string file, int line)
00093 {
00094   if (warnings_enabled) {
00095     if (file_line_info_enabled) {
00096       (*warn) << "*** Warning in " << file << " on line " << line << ":\n"
00097       << msg << std::endl << std::flush;
00098     }
00099     else {
00100       (*warn) << msg << std::endl << std::flush;
00101     }
00102   }
00103 }
00104 
00105 void it_enable_warnings()
00106 {
00107   warnings_enabled = true;
00108 }
00109 
00110 void it_disable_warnings()
00111 {
00112   warnings_enabled = false;
00113 }
00114 
00115 void it_redirect_warnings(std::ostream *warn_stream)
00116 {
00117   warn = warn_stream;
00118 }
00119 
00120 void it_error_msg_style(error_msg_style style)
00121 {
00122   switch (style) {
00123   case Full:
00124     file_line_info_enabled = true;
00125     break;
00126   case Minimum:
00127     file_line_info_enabled = false;
00128     break;
00129   default:
00130     file_line_info_enabled = true;
00131   }
00132 }
00133 
00134 } //namespace itpp
SourceForge Logo

Generated on Sun Jul 26 08:54:25 2009 for IT++ by Doxygen 1.5.9