00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "proc/control/handling-pattern.hpp"
00026 #include "proc/control/handling-patterns.hpp"
00027
00028 #include "include/logging.h"
00029 #include "lib/util.hpp"
00030
00031 #include <boost/format.hpp>
00032
00033
00034 using boost::str;
00035 using boost::format;
00036 using namespace lumiera;
00037 using util::isnil;
00038 using util::cStr;
00039
00040
00041 namespace control {
00042
00044 HandlingPattern const&
00045 HandlingPattern::get (ID id)
00046 {
00047 return getPatternInstance(id);
00048 }
00049
00050
00054 ExecResult
00055 HandlingPattern::invoke (CommandImpl& command, Symbol name) const
00056 {
00057 TRACE (proc_dbg, "invoking %s...", name.c());
00058 static format err_pre ("Error state detected, %s *NOT* invoked.");
00059 static format err_post ("Error state after %s invocation.");
00060 static format err_fatal ("Execution of %s raised unknown error.");
00061 try
00062 {
00063 Symbol errID_pre = lumiera_error();
00064 if (errID_pre)
00065 return ExecResult (error::Logic (str (err_pre % command), errID_pre));
00066
00067
00068 perform (command);
00069
00070 Symbol errID = lumiera_error();
00071 if (errID)
00072 return ExecResult (error::State (str (err_post % command),errID));
00073 else
00074 return ExecResult();
00075 }
00076
00077
00078 catch (lumiera::Error& problem)
00079 {
00080 Symbol errID = lumiera_error();
00081 WARN (command, "Invocation of %s failed: %s", name.c(), problem.what());
00082 TRACE (proc_dbg, "Error flag was: %s", errID.c());
00083 return ExecResult (problem);
00084 }
00085 catch (std::exception& library_problem)
00086 {
00087 Symbol errID = lumiera_error();
00088 WARN (command, "Invocation of %s failed: %s", name.c(), library_problem.what());
00089 TRACE (proc_dbg, "Error flag was: %s", errID.c());
00090 return ExecResult (error::External (library_problem));
00091 }
00092 catch (...)
00093 {
00094 Symbol errID = lumiera_error();
00095 ERROR (command, "Invocation of %s failed with unknown exception; error flag is: %s", name.c(), errID.c());
00096 throw error::Fatal (str (err_fatal % command), errID);
00097 }
00098 }
00099
00100
00101
00102
00103
00104
00107 ExecResult::ExecResult (lumiera::Error const& problem)
00108 : log_(problem.what())
00109 { }
00110
00111
00112 bool
00113 ExecResult::isValid() const
00114 {
00115 return isnil(log_);
00116 }
00117
00118
00119 void
00120 ExecResult::maybeThrow() const
00121 {
00122 if (!isnil (log_))
00123 throw error::Logic ("Command execution failed: "+log_);
00124 }
00125
00126
00127 }