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