|
Sierra Toolkit
Version of the Day
|
00001 /*------------------------------------------------------------------------*/ 00002 /* Copyright 2010 Sandia Corporation. */ 00003 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */ 00004 /* license for use of this work by or on behalf of the U.S. Government. */ 00005 /* Export of this program may require a license from the */ 00006 /* United States Government. */ 00007 /*------------------------------------------------------------------------*/ 00008 00009 #include <stk_util/environment/ReportHandler.hpp> 00010 00011 #include <iostream> 00012 #include <stdexcept> 00013 00014 namespace stk_classic { 00015 00016 namespace { 00017 00018 // Global variables used to store the current handlers. We do not want direct 00019 // external access to this state, so we put these in an empty namespace. 00020 ErrorHandler s_assert_handler = &default_assert_handler; 00021 ErrorHandler s_error_handler = &default_error_handler; 00022 ErrorHandler s_invalid_arg_handler = &default_invalid_arg_handler; 00023 REH s_reportHandler = &default_report_handler; 00024 00025 template<class EXCEPTION> 00026 void default_handler_req(const char* expr, 00027 const std::string& location, 00028 std::ostringstream& message) 00029 { 00030 std::string error_msg = ""; 00031 if (message.str() != "") { 00032 error_msg = std::string("Error: ") + message.str() + "\n"; 00033 } 00034 00035 throw EXCEPTION( 00036 std::string("Requirement( ") + expr + " ) FAILED\n" + 00037 "Error occured at: " + source_relative_path(location) + "\n" + 00038 error_msg); 00039 } 00040 00041 template<class EXCEPTION> 00042 void default_handler_exc(const char* expr, 00043 const std::string& location, 00044 std::ostringstream& message) 00045 { 00046 std::string error_msg = ""; 00047 if (message.str() != "") { 00048 error_msg = std::string("Error: ") + message.str() + "\n"; 00049 } 00050 00051 std::string expr_msg = ""; 00052 if (expr != std::string("")) { 00053 expr_msg = std::string("Expr '") + expr + "' eval'd to true, throwing.\n"; 00054 } 00055 00056 throw EXCEPTION( 00057 expr_msg + 00058 "Error occured at: " + source_relative_path(location) + "\n" + 00059 error_msg); 00060 } 00061 00062 } 00063 00064 void 00065 default_report_handler( 00066 const char * message, 00067 int type) 00068 { 00069 std::cout << "Message type " << type << ": " << message << std::endl; 00070 } 00071 00072 void 00073 report( 00074 const char * message, 00075 int type) 00076 { 00077 (*s_reportHandler)(message, type); 00078 } 00079 00080 00081 REH 00082 set_report_handler( 00083 REH reh) 00084 { 00085 /* %TRACE[ON]% */ /* %TRACE% */ 00086 if (!reh) 00087 throw std::runtime_error("Cannot set report handler to NULL"); 00088 00089 REH prev_reh = s_reportHandler; 00090 s_reportHandler = reh; 00091 00092 return prev_reh; 00093 } 00094 00095 00096 std::string 00097 source_relative_path( 00098 const std::string & path) 00099 { 00100 static const char *prefix[] = {"/src/", "/include/", "/Apps_", "/stk_"}; 00101 00102 for (unsigned int i = 0; i < sizeof(prefix)/sizeof(prefix[0]); ++i) { 00103 std::string::size_type j = path.rfind(prefix[i], path.length()); 00104 if (j != std::string::npos) { 00105 j = path.rfind("/", j - 1); 00106 if (j != std::string::npos) 00107 return path.substr(j + 1, path.length()); 00108 else 00109 return path; 00110 } 00111 } 00112 return path; 00113 } 00114 00115 void default_assert_handler(const char* expr, 00116 const std::string& location, 00117 std::ostringstream& message) 00118 { 00119 default_handler_req<std::logic_error>(expr, location, message); 00120 } 00121 00122 void default_error_handler(const char* expr, 00123 const std::string& location, 00124 std::ostringstream& message) 00125 { 00126 default_handler_exc<std::runtime_error>(expr, location, message); 00127 } 00128 00129 void default_invalid_arg_handler(const char* expr, 00130 const std::string& location, 00131 std::ostringstream& message) 00132 { 00133 default_handler_exc<std::invalid_argument>(expr, location, message); 00134 } 00135 00136 ErrorHandler set_assert_handler(ErrorHandler handler) 00137 { 00138 if (!handler) 00139 throw std::runtime_error("Cannot set assert handler to NULL"); 00140 00141 ErrorHandler prev_handler = s_assert_handler; 00142 s_assert_handler = handler; 00143 00144 return prev_handler; 00145 } 00146 00147 ErrorHandler set_error_handler(ErrorHandler handler) 00148 { 00149 if (!handler) 00150 throw std::runtime_error("Cannot set error handler to NULL"); 00151 00152 ErrorHandler prev_handler = s_error_handler; 00153 s_error_handler = handler; 00154 00155 return prev_handler; 00156 } 00157 00158 ErrorHandler set_invalid_arg_handler(ErrorHandler handler) 00159 { 00160 if (!handler) 00161 throw std::runtime_error("Cannot set invalid_arg handler to NULL"); 00162 00163 ErrorHandler prev_handler = s_invalid_arg_handler; 00164 s_invalid_arg_handler = handler; 00165 00166 return prev_handler; 00167 } 00168 00169 void handle_assert(const char* expr, 00170 const std::string& location, 00171 std::ostringstream& message) 00172 { 00173 (*s_assert_handler)(expr, location, message); 00174 } 00175 00176 void handle_error(const char* expr, 00177 const std::string& location, 00178 std::ostringstream& message) 00179 { 00180 (*s_error_handler)(expr, location, message); 00181 } 00182 00183 void handle_invalid_arg(const char* expr, 00184 const std::string& location, 00185 std::ostringstream& message) 00186 { 00187 (*s_invalid_arg_handler)(expr, location, message); 00188 } 00189 00190 } // namespace stk_classic