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( std::string && msg_r )
53 : _msg( std::move(msg_r) )
56 Exception::Exception( const std::string & msg_r, const Exception & history_r )
58 { remember( history_r ); }
60 Exception::Exception( std::string && msg_r, const Exception & history_r )
61 : _msg( std::move(msg_r) )
62 { remember( history_r ); }
64 Exception::Exception( const std::string & msg_r, Exception && history_r )
66 { remember( std::move(history_r) ); }
68 Exception::Exception( std::string && msg_r, Exception && history_r )
69 : _msg( std::move(msg_r) )
70 { remember( std::move(history_r) ); }
72 Exception::~Exception() throw()
75 std::string Exception::asString() const
77 std::ostringstream str;
82 std::string Exception::asUserString() const
84 std::ostringstream str;
86 // call gettext to translate the message. This will
87 // not work if dumpOn() uses composed messages.
88 return _(str.str().c_str());
91 std::string Exception::asUserHistory() const
94 return asUserString();
96 std::string ret( asUserString() );
98 return historyAsString();
101 ret += historyAsString();
105 void Exception::remember( const Exception & old_r )
107 if ( &old_r != this ) // no self-remember
109 History newh( old_r._history.begin(), old_r._history.end() );
110 newh.push_front( old_r.asUserString() );
111 _history.swap( newh );
115 void Exception::remember( Exception && old_r )
117 if ( &old_r != this ) // no self-remember
119 History & newh( old_r._history ); // stealing it
120 newh.push_front( old_r.asUserString() );
121 _history.swap( newh );
125 void Exception::addHistory( const std::string & msg_r )
126 { _history.push_front( msg_r ); }
128 void Exception::addHistory( std::string && msg_r )
129 { _history.push_front( std::move(msg_r) ); }
131 std::string Exception::historyAsString() const
133 // TranslatorExplanation followed by the list of error messages that lead to this exception
134 std::string history( _("History:") );
135 std::ostringstream ret;
136 dumpRange( ret, historyBegin(), historyEnd(),
137 "", history+"\n - ", "\n - ", "\n", "" );
141 std::ostream & Exception::dumpOn( std::ostream & str ) const
142 { return str << _msg; }
144 std::ostream & Exception::dumpError( std::ostream & str ) const
145 { return dumpOn( str << _where << ": " ); }
147 std::ostream & operator<<( std::ostream & str, const Exception & obj )
148 { return obj.dumpError( str ); }
151 std::string Exception::strErrno( int errno_r )
152 { return str::strerror( errno_r ); }
154 std::string Exception::strErrno( int errno_r, const std::string & msg_r )
155 { return strErrno( errno_r, std::string(msg_r) ); }
157 std::string Exception::strErrno( int errno_r, std::string && msg_r )
160 return msg_r += strErrno( errno_r );
163 void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
164 const char *const prefix_r )
166 INT << where_r << " " << prefix_r << " " << excpt_r.asUserHistory() << endl;
169 void Exception::log( const char * typename_r, const CodeLocation & where_r,
170 const char *const prefix_r )
172 INT << where_r << " " << prefix_r << " exception of type " << typename_r << endl;
174 /////////////////////////////////////////////////////////////////
176 ///////////////////////////////////////////////////////////////////