Sierra Toolkit Version of the Day
ExceptionReport.cpp
00001 
00010 #include <vector>
00011 #include <iostream>
00012 #include <fstream>
00013 #include <string>
00014 
00015 #include <mpi.h>
00016 
00017 #include <stk_util/diag/StringUtil.hpp>
00018 #include <stk_util/diag/Trace.hpp>
00019 #include <stk_util/parallel/ExceptionReport.hpp>
00020 #include <stk_util/diag/Env.hpp>
00021 
00022 namespace sierra {
00023 
00024 
00025 
00026 // int get_next_message_id(int max_id_messages) {
00027 //   if(max_id_messages == -1) max_id_messages = get_default_max_message_id_displayed();
00028 //   return stk::stk_get_next_message_id(max_id_messages);
00029 // }
00030 
00031 namespace {
00032 
00033 std::ofstream *s_testErrorMessagesFile = NULL;
00034 
00035 bool s_dieOnFirstWarning = false;
00036 bool s_dieOnFirstError = false;
00037 
00038 std::string s_testErrorMessagesPath;
00039 
00040 } // namespace <unnamed>
00041 
00042 void
00043 test_error_messages_to_file_report_handler(const char *   message,  int     type) {
00044 
00045   std::string new_message(message);
00046   std::string::size_type start_pos;
00047   //
00048   // Strip out platform dependent exception related messages.
00049   //
00050   start_pos = new_message.find("exception thrown from");
00051   if(start_pos != std::string::npos) {
00052     int end_pos = new_message.find('\n');
00053     new_message.erase(start_pos, (end_pos - start_pos) + 1);
00054   }
00055   start_pos = new_message.find("error thrown from");
00056   if(start_pos != std::string::npos) {
00057     int end_pos = new_message.find('\n');
00058     new_message.erase(start_pos, (end_pos - start_pos) + 1);
00059   }
00060   start_pos = new_message.find("warning thrown from");
00061   if(start_pos != std::string::npos) {
00062     int end_pos = new_message.find('\n');
00063     new_message.erase(start_pos, (end_pos - start_pos) + 1);
00064   }
00065   start_pos = new_message.find("Exception of type");
00066   if(start_pos != std::string::npos) {
00067     int end_pos = new_message.find('\n');
00068     new_message.erase(start_pos, (end_pos - start_pos) + 1);
00069   }
00070   start_pos = new_message.find("with signature");
00071   if(start_pos != std::string::npos) {
00072     int end_pos = new_message.find('\n', start_pos);
00073     new_message.erase(start_pos, (end_pos - start_pos) + 1);
00074   }
00075 
00076   *s_testErrorMessagesFile << "********************************************************************************" << std::endl
00077           << word_wrap(new_message.c_str(), 80, "**  ")
00078           << "********************************************************************************" << std::endl;
00079   *s_testErrorMessagesFile << "===== ENDING ERROR FILE \"" << s_testErrorMessagesPath << "\" =====" << std::endl;
00080 
00081 
00082 
00083   int msgType =   (type & stk::MSG_TYPE_MASK);
00084 
00085 
00086   bool dieNow = false;
00087 
00088   if((msgType == stk::MSG_WARNING) && s_dieOnFirstWarning) {
00089     dieNow = true;
00090   }
00091 
00092   if((msgType == stk::MSG_DOOMED) && s_dieOnFirstError) {
00093     dieNow = true;
00094   }
00095 
00096   if((msgType == stk::MSG_EXCEPTION) && s_dieOnFirstError) {
00097     dieNow = true;
00098   }
00099 
00100   if(dieNow) {
00101     delete s_testErrorMessagesFile;
00102     MPI_Finalize();
00103     std::exit(0);
00104   }
00105 
00106 
00107   
00108 }
00109 
00110 
00111 void
00112 set_test_error_messages_file(
00113   const std::string &   test_error_messages_path)
00114 {
00115   s_testErrorMessagesPath = test_error_messages_path;
00116 
00117   s_testErrorMessagesFile = new std::ofstream(s_testErrorMessagesPath.c_str(), std::ios::out);
00118   *s_testErrorMessagesFile << "===== STARTING ERROR FILE \"" << s_testErrorMessagesPath << "\" =====" << std::endl;
00119 
00120   stk::set_report_handler(test_error_messages_to_file_report_handler);
00121 }
00122 
00123 
00124 std::ofstream *
00125 get_test_error_messages_file()
00126 {
00127   return s_testErrorMessagesFile;
00128 }
00129 
00130 
00131   void set_test_error_messages_die_on_first_message(std::vector<ErrorDieEnum> errorTypes) {
00132   for(unsigned int ierr=0; ierr< errorTypes.size(); ++ierr) {
00133     if(errorTypes[ierr] == DIE_ON_WARN) {
00134       s_dieOnFirstWarning = true;
00135     }
00136     if(errorTypes[ierr] == DIE_ON_ERROR) {
00137       s_dieOnFirstError = true;
00138     }
00139     if(errorTypes[ierr] == DIE_ON_MESSAGE) {
00140       s_dieOnFirstWarning = true;
00141       s_dieOnFirstError  = true;
00142     }
00143   }
00144 
00145 
00146 }
00147 
00148 
00149 bool get_test_error_messages_die_on_first_warning() {
00150   return s_dieOnFirstWarning;
00151 }
00152 
00153 bool get_test_error_messages_die_on_first_error() {
00154   return s_dieOnFirstError;
00155 }
00156 
00157 } // namespace sierra
00158 
00159 extern "C" {
00160   void SIERRA_FORTRAN(report_error)(int &int_val, const char *message, const int message_length) {
00161     switch (int_val) {
00162       case 1:
00163   sierra::Env::outputP0() << "  " << std::string(message, message + message_length) << std::endl;
00164   break;
00165 
00166       case 2:
00167   sierra::RuntimeWarning() << "In Fmwk, " << std::string(message, message + message_length) << std::endl << WarnTrace;
00168   break;
00169 
00170       case 3:
00171   throw sierra::RuntimeError() << "In Fmwk, " << std::string(message, message + message_length) << std::endl << ErrorTrace;
00172     }
00173   }
00174 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines