1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Exception.cc
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/LogTools.h"
17 #include "zypp/base/Gettext.h"
18 #include "zypp/base/String.h"
19 #include "zypp/base/Exception.h"
23 ///////////////////////////////////////////////////////////////////
25 { /////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
27 namespace exception_detail
28 { /////////////////////////////////////////////////////////////////
30 std::string CodeLocation::asString() const
32 return str::form( "%s(%s):%u",
38 std::ostream & operator<<( std::ostream & str, const CodeLocation & obj )
39 { return str << obj.asString(); }
41 /////////////////////////////////////////////////////////////////
42 } // namespace exception_detail
43 ///////////////////////////////////////////////////////////////////
45 Exception::Exception()
48 Exception::Exception( const std::string & msg_r )
52 Exception::Exception( const std::string & msg_r, const Exception & history_r )
54 { remember( history_r ); }
56 Exception::~Exception() throw()
59 std::string Exception::asString() const
61 std::ostringstream str;
66 std::string Exception::asUserString() const
68 std::ostringstream str;
70 // call gettext to translate the message. This will
71 // not work if dumpOn() uses composed messages.
72 return _(str.str().c_str());
75 std::string Exception::asUserHistory() const
78 return asUserString();
80 std::string ret( asUserString() );
82 return historyAsString();
85 ret += historyAsString();
89 void Exception::remember( const Exception & old_r )
91 if ( &old_r != this ) // no self-remember
93 History newh( old_r._history.begin(), old_r._history.end() );
94 newh.push_front( old_r.asUserString() );
95 _history.swap( newh );
99 void Exception::addHistory( const std::string & msg_r )
101 _history.push_front( msg_r );
104 std::string Exception::historyAsString() const
106 // TranslatorExplanation followed by the list of error messages that lead to this exception
107 std::string history( _("History:") );
108 std::ostringstream ret;
109 dumpRange( ret, historyBegin(), historyEnd(),
110 "", history+"\n - ", "\n - ", "\n", "" );
114 std::ostream & Exception::dumpOn( std::ostream & str ) const
115 { return str << _msg; }
117 std::ostream & Exception::dumpError( std::ostream & str ) const
118 { return dumpOn( str << _where << ": " ); }
120 std::ostream & operator<<( std::ostream & str, const Exception & obj )
121 { return obj.dumpError( str ); }
124 std::string Exception::strErrno( int errno_r )
125 { return str::strerror( errno_r ); }
127 std::string Exception::strErrno( int errno_r, const std::string & msg_r )
129 std::string ret( msg_r );
131 return ret += strErrno( errno_r );
134 void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
135 const char *const prefix_r )
137 INT << where_r << " " << prefix_r << " " << excpt_r.asUserHistory() << endl;
140 /////////////////////////////////////////////////////////////////
142 ///////////////////////////////////////////////////////////////////