- Add Exception::asUserHistory: Returns a single (multiline) string composed
[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/LogTools.h"
17 #include "zypp/base/Gettext.h"
18 #include "zypp/base/String.h"
19 #include "zypp/base/Exception.h"
20
21 using std::endl;
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26   ///////////////////////////////////////////////////////////////////
27   namespace exception_detail
28   { /////////////////////////////////////////////////////////////////
29
30     std::string CodeLocation::asString() const
31     {
32       return str::form( "%s(%s):%u",
33                         _file.c_str(),
34                         _func.c_str(),
35                         _line );
36     }
37
38     std::ostream & operator<<( std::ostream & str, const CodeLocation & obj )
39     { return str << obj.asString(); }
40
41     /////////////////////////////////////////////////////////////////
42   } // namespace exception_detail
43   ///////////////////////////////////////////////////////////////////
44
45   Exception::Exception()
46   {}
47
48   Exception::Exception( const std::string & msg_r )
49   : _msg( msg_r )
50   {}
51
52   Exception::~Exception() throw()
53   {}
54
55   std::string Exception::asString() const
56   {
57     std::ostringstream str;
58     dumpOn( str );
59     return str.str();
60   }
61
62   std::string Exception::asUserString() const
63   {
64     std::ostringstream str;
65     dumpOn( str );
66     // call gettext to translate the message. This will
67     // not work if dumpOn() uses composed messages.
68     return _(str.str().c_str());
69   }
70
71   std::string Exception::asUserHistory() const
72   {
73     if ( historyEmpty() )
74       return asUserString();
75
76     std::string ret( asUserString() );
77     if ( ret.empty() )
78       return historyAsString();
79
80     ret += '\n';
81     ret += historyAsString();
82     return ret;
83   }
84
85   void Exception::remember( const Exception & old_r )
86   {
87     if ( &old_r != this ) // no self-remember
88     {
89       History newh( old_r._history.begin(), old_r._history.end() );
90       newh.push_front( old_r.asUserString() );
91       _history.swap( newh );
92     }
93   }
94
95   void Exception::addHistory( const std::string & msg_r )
96   {
97     _history.push_front( msg_r );
98   }
99
100   std::string Exception::historyAsString() const
101   {
102     // TranslatorExplanation followed by the list of error messages that lead to this exception
103     std::string history( _("History:") );
104     std::ostringstream ret;
105     dumpRange( ret, historyBegin(), historyEnd(),
106                "", history+"\n - ", "\n - ", "\n", "" );
107     return ret.str();
108   }
109
110   std::ostream & Exception::dumpOn( std::ostream & str ) const
111   { return str << _msg; }
112
113   std::ostream & Exception::dumpError( std::ostream & str ) const
114   { return dumpOn( str << _where << ": " ); }
115
116   std::ostream & operator<<( std::ostream & str, const Exception & obj )
117   { return obj.dumpError( str ); }
118
119
120   std::string Exception::strErrno( int errno_r )
121   { return str::strerror( errno_r ); }
122
123   std::string Exception::strErrno( int errno_r, const std::string & msg_r )
124   {
125     std::string ret( msg_r );
126     ret += ": ";
127     return ret += strErrno( errno_r );
128   }
129
130   void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
131                        const char *const prefix_r )
132   {
133     INT << where_r << " " << prefix_r << " " << excpt_r << endl;
134   }
135
136   /////////////////////////////////////////////////////////////////
137 } // namespace zypp
138 ///////////////////////////////////////////////////////////////////