comment the code
[platform/upstream/libzypp.git] / zypp / base / Exception.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Exception.cc
10  *
11 */
12 #include <iostream>
13 #include <sstream>
14
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/String.h"
18 #include "zypp/base/Exception.h"
19
20 using std::endl;
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25   ///////////////////////////////////////////////////////////////////
26   namespace exception_detail
27   { /////////////////////////////////////////////////////////////////
28
29     std::string CodeLocation::asString() const
30     {
31       return str::form( "%s(%s):%u",
32                         _file.c_str(),
33                         _func.c_str(),
34                         _line );
35     }
36
37     std::ostream & operator<<( std::ostream & str, const CodeLocation & obj )
38     { return str << obj.asString(); }
39
40     /////////////////////////////////////////////////////////////////
41   } // namespace exception_detail
42   ///////////////////////////////////////////////////////////////////
43
44   Exception::Exception()
45   {}
46
47   Exception::Exception( const std::string & msg_r )
48   : _msg( msg_r )
49   {}
50
51   Exception::~Exception() throw()
52   {}
53
54   std::string Exception::asString() const
55   {
56     std::ostringstream str;
57     dumpOn( str );
58     return str.str();
59   }
60
61   std::string Exception::asUserString() const
62   {
63     std::ostringstream str;
64     dumpOn( str );
65     // call gettext to translate the message. This will
66     // not work if dumpOn() uses composed messages.
67     return _(str.str().c_str());
68   }
69
70
71   std::ostream & Exception::dumpOn( std::ostream & str ) const
72   { return str << _msg; }
73
74   std::ostream & Exception::dumpError( std::ostream & str ) const
75   { return dumpOn( str << _where << ": " ); }
76
77   std::ostream & operator<<( std::ostream & str, const Exception & obj )
78   { return obj.dumpError( str ); }
79
80
81   std::string Exception::strErrno( int errno_r )
82   { return str::strerror( errno_r ); }
83
84   std::string Exception::strErrno( int errno_r, const std::string & msg_r )
85   {
86     std::string ret( msg_r );
87     ret += ": ";
88     return ret += strErrno( errno_r );
89   }
90
91   void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
92                        const char *const prefix_r )
93   {
94     INT << where_r << " " << prefix_r << " " << excpt_r << endl;
95   }
96
97   /////////////////////////////////////////////////////////////////
98 } // namespace zypp
99 ///////////////////////////////////////////////////////////////////