- Adapt to virtual ostream operator<< provided by ReferenceCounted.
[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/String.h"
17 #include "zypp/base/Exception.h"
18
19 using std::endl;
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24   ///////////////////////////////////////////////////////////////////
25   namespace exception_detail
26   { /////////////////////////////////////////////////////////////////
27
28     std::string CodeLocation::asString() const
29     {
30       return str::form( "%s(%s):%u",
31                         _file.c_str(),
32                         _func.c_str(),
33                         _line );
34     }
35
36     std::ostream & operator<<( std::ostream & str, const CodeLocation & obj )
37     { return str << obj.asString(); }
38
39     /////////////////////////////////////////////////////////////////
40   } // namespace exception_detail
41   ///////////////////////////////////////////////////////////////////
42
43   Exception::Exception()
44   {}
45
46   Exception::Exception( const std::string & msg_r )
47   : _msg( msg_r )
48   {}
49
50   Exception::~Exception() throw()
51   {}
52
53   std::string Exception::asString() const
54   {
55     std::ostringstream str;
56     dumpOn( str );
57     return str.str();
58   }
59
60   std::ostream & Exception::dumpOn( std::ostream & str ) const
61   {
62     return str << _msg;
63   }
64
65   std::ostream & Exception::dumpError( std::ostream & str ) const
66   {
67     return dumpOn( str << _where << ": " );
68   }
69
70   std::ostream & operator<<( std::ostream & str, const Exception & obj )
71   { return obj.dumpError( str ); }
72
73   std::string Exception::strErrno( int errno_r )
74   {
75     return str::strerror( errno_r );
76   }
77
78   std::string Exception::strErrno( int errno_r, const std::string & msg_r )
79   {
80     std::string ret( msg_r );
81     ret += ": ";
82     return ret += strErrno( errno_r );
83   }
84
85   void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
86                        const char *const prefix_r )
87   {
88     INT << where_r << " " << prefix_r << " " << excpt_r << endl;
89   }
90
91   /////////////////////////////////////////////////////////////////
92 } // namespace zypp
93 ///////////////////////////////////////////////////////////////////