libzypp
10.5.0
|
00001 /*---------------------------------------------------------------------\ 00002 | ____ _ __ __ ___ | 00003 | |__ / \ / / . \ . \ | 00004 | / / \ V /| _/ _/ | 00005 | / /__ | | | | | | | 00006 | /_____||_| |_| |_| | 00007 | | 00008 \---------------------------------------------------------------------*/ 00012 #include <iostream> 00013 #include <sstream> 00014 00015 #include "zypp/base/Logger.h" 00016 #include "zypp/base/LogTools.h" 00017 #include "zypp/base/Gettext.h" 00018 #include "zypp/base/String.h" 00019 #include "zypp/base/Exception.h" 00020 00021 using std::endl; 00022 00024 namespace zypp 00025 { 00026 00027 namespace exception_detail 00028 { 00029 00030 std::string CodeLocation::asString() const 00031 { 00032 return str::form( "%s(%s):%u", 00033 _file.c_str(), 00034 _func.c_str(), 00035 _line ); 00036 } 00037 00038 std::ostream & operator<<( std::ostream & str, const CodeLocation & obj ) 00039 { return str << obj.asString(); } 00040 00042 } // namespace exception_detail 00044 00045 Exception::Exception() 00046 {} 00047 00048 Exception::Exception( const std::string & msg_r ) 00049 : _msg( msg_r ) 00050 {} 00051 00052 Exception::Exception( const std::string & msg_r, const Exception & history_r ) 00053 : _msg( msg_r ) 00054 { remember( history_r ); } 00055 00056 Exception::~Exception() throw() 00057 {} 00058 00059 std::string Exception::asString() const 00060 { 00061 std::ostringstream str; 00062 dumpOn( str ); 00063 return str.str(); 00064 } 00065 00066 std::string Exception::asUserString() const 00067 { 00068 std::ostringstream str; 00069 dumpOn( str ); 00070 // call gettext to translate the message. This will 00071 // not work if dumpOn() uses composed messages. 00072 return _(str.str().c_str()); 00073 } 00074 00075 std::string Exception::asUserHistory() const 00076 { 00077 if ( historyEmpty() ) 00078 return asUserString(); 00079 00080 std::string ret( asUserString() ); 00081 if ( ret.empty() ) 00082 return historyAsString(); 00083 00084 ret += '\n'; 00085 ret += historyAsString(); 00086 return ret; 00087 } 00088 00089 void Exception::remember( const Exception & old_r ) 00090 { 00091 if ( &old_r != this ) // no self-remember 00092 { 00093 History newh( old_r._history.begin(), old_r._history.end() ); 00094 newh.push_front( old_r.asUserString() ); 00095 _history.swap( newh ); 00096 } 00097 } 00098 00099 void Exception::addHistory( const std::string & msg_r ) 00100 { 00101 _history.push_front( msg_r ); 00102 } 00103 00104 std::string Exception::historyAsString() const 00105 { 00106 // TranslatorExplanation followed by the list of error messages that lead to this exception 00107 std::string history( _("History:") ); 00108 std::ostringstream ret; 00109 dumpRange( ret, historyBegin(), historyEnd(), 00110 "", history+"\n - ", "\n - ", "\n", "" ); 00111 return ret.str(); 00112 } 00113 00114 std::ostream & Exception::dumpOn( std::ostream & str ) const 00115 { return str << _msg; } 00116 00117 std::ostream & Exception::dumpError( std::ostream & str ) const 00118 { return dumpOn( str << _where << ": " ); } 00119 00120 std::ostream & operator<<( std::ostream & str, const Exception & obj ) 00121 { return obj.dumpError( str ); } 00122 00123 00124 std::string Exception::strErrno( int errno_r ) 00125 { return str::strerror( errno_r ); } 00126 00127 std::string Exception::strErrno( int errno_r, const std::string & msg_r ) 00128 { 00129 std::string ret( msg_r ); 00130 ret += ": "; 00131 return ret += strErrno( errno_r ); 00132 } 00133 00134 void Exception::log( const Exception & excpt_r, const CodeLocation & where_r, 00135 const char *const prefix_r ) 00136 { 00137 INT << where_r << " " << prefix_r << " " << excpt_r.asUserHistory() << endl; 00138 } 00139 00141 } // namespace zypp