Exception.cc

Go to the documentation of this file.
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/LogTools.h"
00017 #include "zypp/base/Gettext.h"
00018 #include "zypp/base/String.h"
00019 #include "zypp/base/Exception.h"
00020 
00021 using std::endl;
00022 
00024 namespace zypp
00025 { 
00026 
00027   namespace exception_detail
00028   { 
00029 
00030     std::string CodeLocation::asString() const
00031     {
00032       return str::form( "%s(%s):%u",
00033                         _file.c_str(),
00034                         _func.c_str(),
00035                         _line );
00036     }
00037 
00038     std::ostream & operator<<( std::ostream & str, const CodeLocation & obj )
00039     { return str << obj.asString(); }
00040 
00042   } // namespace exception_detail
00044 
00045   Exception::Exception()
00046   {}
00047 
00048   Exception::Exception( const std::string & msg_r )
00049   : _msg( msg_r )
00050   {}
00051 
00052   Exception::~Exception() throw()
00053   {}
00054 
00055   std::string Exception::asString() const
00056   {
00057     std::ostringstream str;
00058     dumpOn( str );
00059     return str.str();
00060   }
00061 
00062   std::string Exception::asUserString() const
00063   {
00064     std::ostringstream str;
00065     dumpOn( str );
00066     // call gettext to translate the message. This will
00067     // not work if dumpOn() uses composed messages.
00068     return _(str.str().c_str());
00069   }
00070 
00071   void Exception::remember( const Exception & old_r )
00072   {
00073     if ( &old_r != this ) // no self-remember
00074     {
00075       History newh( old_r._history.begin(), old_r._history.end() );
00076       newh.push_front( old_r.asUserString() );
00077       _history.swap( newh );
00078     }
00079   }
00080 
00081   void Exception::addHistory( const std::string & msg_r )
00082   {
00083     _history.push_front( msg_r );
00084   }
00085 
00086   std::string Exception::historyAsString() const
00087   {
00088     // TranslatorExplanation followed by the list of error messages that lead to this exception
00089     std::string history( _("History:") );
00090     std::ostringstream ret;
00091     dumpRange( ret, historyBegin(), historyEnd(),
00092                "", history+"\n - ", "\n - ", "\n", "" );
00093     return ret.str();
00094   }
00095 
00096   std::ostream & Exception::dumpOn( std::ostream & str ) const
00097   { return str << _msg; }
00098 
00099   std::ostream & Exception::dumpError( std::ostream & str ) const
00100   { return dumpOn( str << _where << ": " ); }
00101 
00102   std::ostream & operator<<( std::ostream & str, const Exception & obj )
00103   { return obj.dumpError( str ); }
00104 
00105 
00106   std::string Exception::strErrno( int errno_r )
00107   { return str::strerror( errno_r ); }
00108 
00109   std::string Exception::strErrno( int errno_r, const std::string & msg_r )
00110   {
00111     std::string ret( msg_r );
00112     ret += ": ";
00113     return ret += strErrno( errno_r );
00114   }
00115 
00116   void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
00117                        const char *const prefix_r )
00118   {
00119     INT << where_r << " " << prefix_r << " " << excpt_r << endl;
00120   }
00121 
00123 } // namespace zypp

Generated on Tue Sep 25 19:22:58 2007 for libzypp by  doxygen 1.5.3