1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Exception.cc
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/String.h"
18 #include "zypp/base/Exception.h"
22 ///////////////////////////////////////////////////////////////////
24 { /////////////////////////////////////////////////////////////////
25 ///////////////////////////////////////////////////////////////////
26 namespace exception_detail
27 { /////////////////////////////////////////////////////////////////
29 std::string CodeLocation::asString() const
31 return str::form( "%s(%s):%u",
37 std::ostream & operator<<( std::ostream & str, const CodeLocation & obj )
38 { return str << obj.asString(); }
40 /////////////////////////////////////////////////////////////////
41 } // namespace exception_detail
42 ///////////////////////////////////////////////////////////////////
44 Exception::Exception()
47 Exception::Exception( const std::string & msg_r )
51 Exception::~Exception() throw()
54 std::string Exception::asString() const
56 std::ostringstream str;
61 std::string Exception::asUserString() const
63 std::ostringstream str;
65 // call gettext to translate the message. This will
66 // not work if dumpOn() uses composed messages.
67 return _(str.str().c_str());
70 void Exception::remember( const Exception & old_r )
72 if ( &old_r != this ) // no self-remember
74 History newh( old_r._history.begin(), old_r._history.end() );
75 newh.push_front( old_r.asUserString() );
76 _history.swap( newh );
80 void Exception::addHistory( const std::string & msg_r )
82 _history.push_front( msg_r );
85 std::string Exception::historyAsString() const
87 // TranslatorExplanation followed by the list of error messages that lead to this exception
88 std::string history( _("History:") );
90 dumpRange( ret, err.historyBegin(), err.historyEnd(),
91 "", history+"\n - ", "\n - ", "\n", "" );
95 std::ostream & Exception::dumpOn( std::ostream & str ) const
96 { return str << _msg; }
98 std::ostream & Exception::dumpError( std::ostream & str ) const
99 { return dumpOn( str << _where << ": " ); }
101 std::ostream & operator<<( std::ostream & str, const Exception & obj )
102 { return obj.dumpError( str ); }
105 std::string Exception::strErrno( int errno_r )
106 { return str::strerror( errno_r ); }
108 std::string Exception::strErrno( int errno_r, const std::string & msg_r )
110 std::string ret( msg_r );
112 return ret += strErrno( errno_r );
115 void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
116 const char *const prefix_r )
118 INT << where_r << " " << prefix_r << " " << excpt_r << endl;
121 /////////////////////////////////////////////////////////////////
123 ///////////////////////////////////////////////////////////////////