[1] | 1 | //----------------------------------------------------------- |
---|
| 2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
| 3 | // arnault@lal.in2p3.fr |
---|
| 4 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
| 5 | //----------------------------------------------------------- |
---|
| 6 | |
---|
| 7 | #include "cmt_error.h" |
---|
| 8 | #include "cmt_vector.h" |
---|
| 9 | |
---|
| 10 | class Error |
---|
| 11 | { |
---|
| 12 | public: |
---|
| 13 | static Error& instance (); |
---|
| 14 | |
---|
| 15 | Error () |
---|
| 16 | { |
---|
| 17 | error_names.add() = "ok"; |
---|
| 18 | error_names.add() = "Warning"; |
---|
| 19 | error_names.add() = "symbol not found"; |
---|
| 20 | error_names.add() = "pattern not found"; |
---|
| 21 | error_names.add() = "syntax error"; |
---|
| 22 | error_names.add() = "command_not_implemented"; |
---|
| 23 | error_names.add() = "package_not_found"; |
---|
| 24 | error_names.add() = "path_not_found"; |
---|
| 25 | error_names.add() = "version_conflict"; |
---|
| 26 | error_names.add() = "file_access_error"; |
---|
| 27 | error_names.add() = "execution_error"; |
---|
| 28 | error_names.add() = "cannot_lock"; |
---|
| 29 | error_names.add() = "cannot_write_lock"; |
---|
| 30 | error_names.add() = "cannot_run_lock_command"; |
---|
| 31 | error_names.add() = "cannot_unlock"; |
---|
| 32 | error_names.add() = "cannot_run_unlock_command"; |
---|
| 33 | error_names.add() = "cannot_remove_lock"; |
---|
| 34 | error_names.add() = "conflicting_lock"; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | ~Error () |
---|
| 38 | { |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | void clear () |
---|
| 42 | { |
---|
| 43 | m_code = CmtError::ok; |
---|
| 44 | m_text = ""; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | void set (CmtError::code code, const cmt_string& text) |
---|
| 48 | { |
---|
| 49 | m_code = code; |
---|
| 50 | m_text = text; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | CmtError::code get_code () const |
---|
| 54 | { |
---|
| 55 | return (m_code); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | const cmt_string& get_text () const |
---|
| 59 | { |
---|
| 60 | return (m_text); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | const cmt_string& get_name (CmtError::code error) const |
---|
| 64 | { |
---|
| 65 | const cmt_string& s = error_names[error]; |
---|
| 66 | |
---|
| 67 | return (s); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | private: |
---|
| 71 | CmtError::code m_code; |
---|
| 72 | cmt_string m_text; |
---|
| 73 | cmt_vector<cmt_string> error_names; |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | //--------------------------------------------------------------- |
---|
| 77 | Error& Error::instance () |
---|
| 78 | { |
---|
| 79 | static Error e; |
---|
| 80 | |
---|
| 81 | return (e); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | //--------------------------------------------------------------- |
---|
| 85 | void CmtError::clear () |
---|
| 86 | { |
---|
| 87 | Error& e = Error::instance (); |
---|
| 88 | |
---|
| 89 | e.clear (); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | //--------------------------------------------------------------- |
---|
| 93 | bool CmtError::has_pending_error () |
---|
| 94 | { |
---|
| 95 | Error& e = Error::instance (); |
---|
| 96 | |
---|
| 97 | if (e.get_code () == ok) return (false); |
---|
| 98 | else return (true); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | //--------------------------------------------------------------- |
---|
| 102 | CmtError::code CmtError::get_last_error_code () |
---|
| 103 | { |
---|
| 104 | Error& e = Error::instance (); |
---|
| 105 | |
---|
| 106 | return (e.get_code ()); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | //--------------------------------------------------------------- |
---|
| 110 | const cmt_string& CmtError::get_error_name (code error) |
---|
| 111 | { |
---|
| 112 | Error& e = Error::instance (); |
---|
| 113 | |
---|
| 114 | return (e.get_name (error)); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | //--------------------------------------------------------------- |
---|
| 118 | void CmtError::set (code error, const cmt_string& text) |
---|
| 119 | { |
---|
| 120 | Error& e = Error::instance (); |
---|
| 121 | |
---|
| 122 | e.set (error, text); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | //--------------------------------------------------------------- |
---|
| 126 | cmt_string CmtError::get_last_error () |
---|
| 127 | { |
---|
| 128 | Error& e = Error::instance (); |
---|
| 129 | |
---|
| 130 | cmt_string result; |
---|
| 131 | |
---|
| 132 | result = get_error_name (e.get_code ()); |
---|
| 133 | result += " : "; |
---|
| 134 | result += e.get_text (); |
---|
| 135 | |
---|
| 136 | return (result); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | //--------------------------------------------------------------- |
---|
| 140 | void CmtError::print () |
---|
| 141 | { |
---|
| 142 | Error& e = Error::instance (); |
---|
| 143 | |
---|
| 144 | cerr << "CMT> Error: " << get_error_name (e.get_code ()) << " : " << e.get_text () << endl; |
---|
| 145 | } |
---|
| 146 | |
---|