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/Gettext.h" 00017 #include "zypp/base/String.h" 00018 #include "zypp/base/Exception.h" 00019 00020 using std::endl; 00021 00023 namespace zypp 00024 { 00025 00026 namespace exception_detail 00027 { 00028 00029 std::string CodeLocation::asString() const 00030 { 00031 return str::form( "%s(%s):%u", 00032 _file.c_str(), 00033 _func.c_str(), 00034 _line ); 00035 } 00036 00037 std::ostream & operator<<( std::ostream & str, const CodeLocation & obj ) 00038 { return str << obj.asString(); } 00039 00041 } // namespace exception_detail 00043 00044 Exception::Exception() 00045 {} 00046 00047 Exception::Exception( const std::string & msg_r ) 00048 : _msg( msg_r ) 00049 {} 00050 00051 Exception::~Exception() throw() 00052 {} 00053 00054 std::string Exception::asString() const 00055 { 00056 std::ostringstream str; 00057 dumpOn( str ); 00058 return str.str(); 00059 } 00060 00061 std::string Exception::asUserString() const 00062 { 00063 std::ostringstream str; 00064 dumpOn( str ); 00065 // call gettext to translate the message. This will 00066 // not work if dumpOn() uses composed messages. 00067 return _(str.str().c_str()); 00068 } 00069 00070 00071 std::ostream & Exception::dumpOn( std::ostream & str ) const 00072 { return str << _msg; } 00073 00074 std::ostream & Exception::dumpError( std::ostream & str ) const 00075 { return dumpOn( str << _where << ": " ); } 00076 00077 std::ostream & operator<<( std::ostream & str, const Exception & obj ) 00078 { return obj.dumpError( str ); } 00079 00080 00081 std::string Exception::strErrno( int errno_r ) 00082 { return str::strerror( errno_r ); } 00083 00084 std::string Exception::strErrno( int errno_r, const std::string & msg_r ) 00085 { 00086 std::string ret( msg_r ); 00087 ret += ": "; 00088 return ret += strErrno( errno_r ); 00089 } 00090 00091 void Exception::log( const Exception & excpt_r, const CodeLocation & where_r, 00092 const char *const prefix_r ) 00093 { 00094 INT << where_r << " " << prefix_r << " " << excpt_r << endl; 00095 } 00096 00098 } // namespace zypp