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   std::string Exception::asUserHistory() const
00072   {
00073     if ( historyEmpty() )
00074       return asUserString();
00075 
00076     std::string ret( asUserString() );
00077     if ( ret.empty() )
00078       return historyAsString();
00079 
00080     ret += '\n';
00081     ret += historyAsString();
00082     return ret;
00083   }
00084 
00085   void Exception::remember( const Exception & old_r )
00086   {
00087     if ( &old_r != this ) // no self-remember
00088     {
00089       History newh( old_r._history.begin(), old_r._history.end() );
00090       newh.push_front( old_r.asUserString() );
00091       _history.swap( newh );
00092     }
00093   }
00094 
00095   void Exception::addHistory( const std::string & msg_r )
00096   {
00097     _history.push_front( msg_r );
00098   }
00099 
00100   std::string Exception::historyAsString() const
00101   {
00102     // TranslatorExplanation followed by the list of error messages that lead to this exception
00103     std::string history( _("History:") );
00104     std::ostringstream ret;
00105     dumpRange( ret, historyBegin(), historyEnd(),
00106                "", history+"\n - ", "\n - ", "\n", "" );
00107     return ret.str();
00108   }
00109 
00110   std::ostream & Exception::dumpOn( std::ostream & str ) const
00111   { return str << _msg; }
00112 
00113   std::ostream & Exception::dumpError( std::ostream & str ) const
00114   { return dumpOn( str << _where << ": " ); }
00115 
00116   std::ostream & operator<<( std::ostream & str, const Exception & obj )
00117   { return obj.dumpError( str ); }
00118 
00119 
00120   std::string Exception::strErrno( int errno_r )
00121   { return str::strerror( errno_r ); }
00122 
00123   std::string Exception::strErrno( int errno_r, const std::string & msg_r )
00124   {
00125     std::string ret( msg_r );
00126     ret += ": ";
00127     return ret += strErrno( errno_r );
00128   }
00129 
00130   void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
00131                        const char *const prefix_r )
00132   {
00133     INT << where_r << " " << prefix_r << " " << excpt_r << endl;
00134   }
00135 
00137 } // namespace zypp

doxygen