Added Exceptions (Exception.h)
[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 exceptinon_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 exceptinon_detail
40   ///////////////////////////////////////////////////////////////////
41
42   Exception::Exception( const CodeLocation & where_r, const std::string & msg_r )
43   : _where( where_r ), _msg( msg_r )
44   {
45     INT << asString() << endl;
46   }
47
48   Exception::~Exception() throw()
49   {}
50
51   std::string Exception::asString() const
52   {
53     std::string ret( _where.asString() );
54     ret += ": ";
55     return ret += _msg;
56   }
57
58   std::string Exception::strErrno( int errno_r )
59   {
60     return str::strerror( errno_r );
61   }
62
63   std::string Exception::strErrno( int errno_r, const std::string & msg_r )
64   {
65     std::string ret( msg_r );
66     ret += ": ";
67     return ret += strErrno( errno_r );
68   }
69
70   void Exception::caught() const
71   {
72     INT << "Caught: " << asString() << endl;
73   }
74
75   std::ostream & operator<<( std::ostream & str, const Exception & obj )
76   { return str << obj.asString(); }
77
78   /////////////////////////////////////////////////////////////////
79 } // namespace zypp
80 ///////////////////////////////////////////////////////////////////