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() throw()
55 std::string Exception::asString() const
57 std::ostringstream str;
62 std::string Exception::asUserString() const
64 std::ostringstream str;
66 // call gettext to translate the message. This will
67 // not work if dumpOn() uses composed messages.
68 return _(str.str().c_str());
71 void Exception::remember( const Exception & old_r )
73 if ( &old_r != this ) // no self-remember
75 History newh( old_r._history.begin(), old_r._history.end() );
76 newh.push_front( old_r.asUserString() );
77 _history.swap( newh );
81 void Exception::addHistory( const std::string & msg_r )
83 _history.push_front( msg_r );
86 std::string Exception::historyAsString() const
88 // TranslatorExplanation followed by the list of error messages that lead to this exception
89 std::string history( _("History:") );
90 std::ostringstream ret;
91 dumpRange( ret, historyBegin(), historyEnd(),
92 "", history+"\n - ", "\n - ", "\n", "" );
96 std::ostream & Exception::dumpOn( std::ostream & str ) const
97 { return str << _msg; }
99 std::ostream & Exception::dumpError( std::ostream & str ) const
100 { return dumpOn( str << _where << ": " ); }
102 std::ostream & operator<<( std::ostream & str, const Exception & obj )
103 { return obj.dumpError( str ); }
106 std::string Exception::strErrno( int errno_r )
107 { return str::strerror( errno_r ); }
109 std::string Exception::strErrno( int errno_r, const std::string & msg_r )
111 std::string ret( msg_r );
113 return ret += strErrno( errno_r );
116 void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
117 const char *const prefix_r )
119 INT << where_r << " " << prefix_r << " " << excpt_r << endl;
122 /////////////////////////////////////////////////////////////////
124 ///////////////////////////////////////////////////////////////////